-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
41 additions
and
39 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,84 @@ | ||
import traversalCSSRuleList from "./traversalCSSRuleList"; | ||
import convTextToRules from "./convTextToRules"; | ||
import {cssHelper} from "./cssHelper"; | ||
import convUrlToAbs from "./convUrlToAbs"; | ||
import traversalCSSRuleList from './traversalCSSRuleList' | ||
import convTextToRules from './convTextToRules' | ||
import { cssHelper } from './cssHelper' | ||
import convUrlToAbs from './convUrlToAbs' | ||
|
||
type cssNodeObj = Awaited<ReturnType<typeof convTextToRules>>; | ||
type cssNodeObj = Awaited<ReturnType<typeof convTextToRules>> | ||
|
||
function generateRulesAll( | ||
doc: Document, | ||
externalCssCache: { [index: cssNodeObj["href"]]: cssNodeObj } | ||
externalCssCache: { [index: cssNodeObj['href']]: cssNodeObj } | ||
) { | ||
var x: number; | ||
var x: number | ||
|
||
var objCss = { | ||
normRule: [], | ||
fontFace: [], | ||
keyFram: [], | ||
}; | ||
} | ||
|
||
var promises = []; | ||
var promises = [] | ||
|
||
return new Promise(function (resolve, reject) { | ||
// loop every styleSheets | ||
for (x = 0; x < doc.styleSheets.length; x++) { | ||
const styleSheet = doc.styleSheets[x]; | ||
const styleSheet = doc.styleSheets[x] | ||
promises.push( | ||
new Promise(function (res) { | ||
var cssNodeArr : cssNodeObj; | ||
var cssNodeArr: cssNodeObj | ||
if (styleSheet.href !== null) { | ||
// can be link tag | ||
cssNodeArr = externalCssCache[styleSheet.href]; | ||
cssNodeArr.media = doc.styleSheets[x].media; | ||
traversalCSSRuleList(doc, externalCssCache, cssNodeArr).then(res); | ||
} else if(styleSheet.ownerNode instanceof Element) { | ||
cssNodeArr = externalCssCache[styleSheet.href] | ||
cssNodeArr.media = doc.styleSheets[x].media | ||
traversalCSSRuleList(doc, externalCssCache, cssNodeArr).then(res) | ||
} else if (styleSheet.ownerNode instanceof Element) { | ||
// style tag | ||
let html: string = styleSheet.ownerNode.innerHTML; | ||
if (html === "") { | ||
let html: string = styleSheet.ownerNode.innerHTML | ||
if (html === '') { | ||
// style may be in style-tag's cssRules but not show in innerHTML | ||
for ( | ||
let index = 0; | ||
index < doc.styleSheets[x].cssRules.length; | ||
index++ | ||
) { | ||
const rule = doc.styleSheets[x].cssRules[index]; | ||
html += rule.cssText; | ||
const rule = doc.styleSheets[x].cssRules[index] | ||
html += rule.cssText | ||
} | ||
} | ||
// convert urls in style tag to abs | ||
html = html.replace( | ||
/url\((['"]?)(.*?)\1\)/g, | ||
function (_a, _p1, p2) { | ||
return "url(\"" + convUrlToAbs(doc.location.href, p2) + "\")"; | ||
function (_a, p1, p2) { | ||
return ( | ||
'url(' + p1 + convUrlToAbs(doc.location.href, p2) + p1 + ')' | ||
) | ||
} | ||
); | ||
) | ||
// the next operation is asynchronous | ||
// store the current x value | ||
let _x = x; | ||
convTextToRules(html, doc.location.href).then(cssNodeObj=>{ | ||
cssNodeObj.media = doc.styleSheets[_x].media; | ||
traversalCSSRuleList(doc, externalCssCache, cssNodeObj).then(res); | ||
}); | ||
}else{ | ||
let _x = x | ||
convTextToRules(html, doc.location.href).then((cssNodeObj) => { | ||
cssNodeObj.media = doc.styleSheets[_x].media | ||
traversalCSSRuleList(doc, externalCssCache, cssNodeObj).then(res) | ||
}) | ||
} else { | ||
// console.log('ProcessingInstruction', styleSheet.ownerNode); | ||
res({}) | ||
} | ||
}) | ||
); | ||
) | ||
} | ||
|
||
Promise.all(promises) | ||
.then(function (result) { | ||
result.forEach(function (ele) { | ||
cssHelper.mergeobjCss(objCss, ele); | ||
}); | ||
resolve(objCss); | ||
cssHelper.mergeobjCss(objCss, ele) | ||
}) | ||
resolve(objCss) | ||
}) | ||
.catch(function (err) { | ||
reject(err); | ||
}); | ||
}); | ||
reject(err) | ||
}) | ||
}) | ||
} | ||
export default generateRulesAll; | ||
export default generateRulesAll |