|
1 | | -import traversalCSSRuleList from "./traversalCSSRuleList"; |
2 | | -import convTextToRules from "./convTextToRules"; |
3 | | -import {cssHelper} from "./cssHelper"; |
4 | | -import convUrlToAbs from "./convUrlToAbs"; |
| 1 | +import traversalCSSRuleList from './traversalCSSRuleList' |
| 2 | +import convTextToRules from './convTextToRules' |
| 3 | +import { cssHelper } from './cssHelper' |
| 4 | +import convUrlToAbs from './convUrlToAbs' |
5 | 5 |
|
6 | | -type cssNodeObj = Awaited<ReturnType<typeof convTextToRules>>; |
| 6 | +type cssNodeObj = Awaited<ReturnType<typeof convTextToRules>> |
7 | 7 |
|
8 | 8 | function generateRulesAll( |
9 | 9 | doc: Document, |
10 | | - externalCssCache: { [index: cssNodeObj["href"]]: cssNodeObj } |
| 10 | + externalCssCache: { [index: cssNodeObj['href']]: cssNodeObj } |
11 | 11 | ) { |
12 | | - var x: number; |
| 12 | + var x: number |
13 | 13 |
|
14 | 14 | var objCss = { |
15 | 15 | normRule: [], |
16 | 16 | fontFace: [], |
17 | 17 | keyFram: [], |
18 | | - }; |
| 18 | + } |
19 | 19 |
|
20 | | - var promises = []; |
| 20 | + var promises = [] |
21 | 21 |
|
22 | 22 | return new Promise(function (resolve, reject) { |
23 | 23 | // loop every styleSheets |
24 | 24 | for (x = 0; x < doc.styleSheets.length; x++) { |
25 | | - const styleSheet = doc.styleSheets[x]; |
| 25 | + const styleSheet = doc.styleSheets[x] |
26 | 26 | promises.push( |
27 | 27 | new Promise(function (res) { |
28 | | - var cssNodeArr : cssNodeObj; |
| 28 | + var cssNodeArr: cssNodeObj |
29 | 29 | if (styleSheet.href !== null) { |
30 | 30 | // can be link tag |
31 | | - cssNodeArr = externalCssCache[styleSheet.href]; |
32 | | - cssNodeArr.media = doc.styleSheets[x].media; |
33 | | - traversalCSSRuleList(doc, externalCssCache, cssNodeArr).then(res); |
34 | | - } else if(styleSheet.ownerNode instanceof Element) { |
| 31 | + cssNodeArr = externalCssCache[styleSheet.href] |
| 32 | + cssNodeArr.media = doc.styleSheets[x].media |
| 33 | + traversalCSSRuleList(doc, externalCssCache, cssNodeArr).then(res) |
| 34 | + } else if (styleSheet.ownerNode instanceof Element) { |
35 | 35 | // style tag |
36 | | - let html: string = styleSheet.ownerNode.innerHTML; |
37 | | - if (html === "") { |
| 36 | + let html: string = styleSheet.ownerNode.innerHTML |
| 37 | + if (html === '') { |
38 | 38 | // style may be in style-tag's cssRules but not show in innerHTML |
39 | 39 | for ( |
40 | 40 | let index = 0; |
41 | 41 | index < doc.styleSheets[x].cssRules.length; |
42 | 42 | index++ |
43 | 43 | ) { |
44 | | - const rule = doc.styleSheets[x].cssRules[index]; |
45 | | - html += rule.cssText; |
| 44 | + const rule = doc.styleSheets[x].cssRules[index] |
| 45 | + html += rule.cssText |
46 | 46 | } |
47 | 47 | } |
48 | 48 | // convert urls in style tag to abs |
49 | 49 | html = html.replace( |
50 | 50 | /url\((['"]?)(.*?)\1\)/g, |
51 | | - function (_a, _p1, p2) { |
52 | | - return "url(\"" + convUrlToAbs(doc.location.href, p2) + "\")"; |
| 51 | + function (_a, p1, p2) { |
| 52 | + return ( |
| 53 | + 'url(' + p1 + convUrlToAbs(doc.location.href, p2) + p1 + ')' |
| 54 | + ) |
53 | 55 | } |
54 | | - ); |
| 56 | + ) |
55 | 57 | // the next operation is asynchronous |
56 | 58 | // store the current x value |
57 | | - let _x = x; |
58 | | - convTextToRules(html, doc.location.href).then(cssNodeObj=>{ |
59 | | - cssNodeObj.media = doc.styleSheets[_x].media; |
60 | | - traversalCSSRuleList(doc, externalCssCache, cssNodeObj).then(res); |
61 | | - }); |
62 | | - }else{ |
| 59 | + let _x = x |
| 60 | + convTextToRules(html, doc.location.href).then((cssNodeObj) => { |
| 61 | + cssNodeObj.media = doc.styleSheets[_x].media |
| 62 | + traversalCSSRuleList(doc, externalCssCache, cssNodeObj).then(res) |
| 63 | + }) |
| 64 | + } else { |
63 | 65 | // console.log('ProcessingInstruction', styleSheet.ownerNode); |
64 | 66 | res({}) |
65 | 67 | } |
66 | 68 | }) |
67 | | - ); |
| 69 | + ) |
68 | 70 | } |
69 | 71 |
|
70 | 72 | Promise.all(promises) |
71 | 73 | .then(function (result) { |
72 | 74 | result.forEach(function (ele) { |
73 | | - cssHelper.mergeobjCss(objCss, ele); |
74 | | - }); |
75 | | - resolve(objCss); |
| 75 | + cssHelper.mergeobjCss(objCss, ele) |
| 76 | + }) |
| 77 | + resolve(objCss) |
76 | 78 | }) |
77 | 79 | .catch(function (err) { |
78 | | - reject(err); |
79 | | - }); |
80 | | - }); |
| 80 | + reject(err) |
| 81 | + }) |
| 82 | + }) |
81 | 83 | } |
82 | | -export default generateRulesAll; |
| 84 | +export default generateRulesAll |
0 commit comments