|
| 1 | +/** |
| 2 | + * Returns a then(able) queue of CSSStyleSheet(s) |
| 3 | + * @param {Object} ownerDocument document object to be inspected for stylesheets |
| 4 | + * @param {number} timeout on network request for stylesheet that need to be externally fetched |
| 5 | + * @param {Function} convertTextToStylesheetFn a utility function to generate a style sheet from text |
| 6 | + * @return {Object} queue |
| 7 | + * @private |
| 8 | + */ |
| 9 | +function loadCssom({ root, shadowId }, timeout, convertTextToStylesheetFn) { |
| 10 | + /** |
| 11 | + * Make an axios get request to fetch a given resource and resolve |
| 12 | + * @method getExternalStylesheet |
| 13 | + * @private |
| 14 | + * @param {Object} param an object with properties to configure the external XHR |
| 15 | + * @property {Object} param.resolve resolve callback on queue |
| 16 | + * @property {Object} param.reject reject callback on queue |
| 17 | + * @property {String} param.url string representing the url of the resource to load |
| 18 | + * @property {Number} param.timeout timeout to about network call |
| 19 | + */ |
| 20 | + function getExternalStylesheet({ resolve, reject, url }) { |
| 21 | + axe.imports |
| 22 | + .axios({ |
| 23 | + method: 'get', |
| 24 | + url, |
| 25 | + timeout |
| 26 | + }) |
| 27 | + .then(({ data }) => { |
| 28 | + const sheet = convertTextToStylesheetFn({ |
| 29 | + data, |
| 30 | + isExternal: true, |
| 31 | + shadowId |
| 32 | + }); |
| 33 | + resolve(sheet); |
| 34 | + }) |
| 35 | + .catch(reject); |
| 36 | + } |
| 37 | + |
| 38 | + const q = axe.utils.queue(); |
| 39 | + |
| 40 | + // iterate to decipher multi-level nested sheets if any (this is essential to retrieve styles from shadowDOM) |
| 41 | + Array.from(root.styleSheets).forEach(sheet => { |
| 42 | + // ignore disabled sheets |
| 43 | + if (sheet.disabled) { |
| 44 | + return; |
| 45 | + } |
| 46 | + // attempt to retrieve cssRules, or for external sheets make a XMLHttpRequest |
| 47 | + try { |
| 48 | + // accessing .cssRules throws for external (cross-domain) sheets, which is handled in the catch |
| 49 | + const cssRules = sheet.cssRules; |
| 50 | + // read all css rules in the sheet |
| 51 | + const rules = Array.from(cssRules); |
| 52 | + |
| 53 | + // filter rules that are included by way of @import or nested link |
| 54 | + const importRules = rules.filter(r => r.href); |
| 55 | + |
| 56 | + // if no import or nested link rules, with in these cssRules |
| 57 | + // return current sheet |
| 58 | + if (!importRules.length) { |
| 59 | + q.defer(resolve => |
| 60 | + resolve({ |
| 61 | + sheet, |
| 62 | + isExternal: false, |
| 63 | + shadowId |
| 64 | + }) |
| 65 | + ); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + // if any import rules exists, fetch via `href` which eventually constructs a sheet with results from resource |
| 70 | + importRules.forEach(rule => { |
| 71 | + q.defer((resolve, reject) => { |
| 72 | + getExternalStylesheet({ resolve, reject, url: rule.href }); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + // in the same sheet - get inline rules in <style> tag or in a CSSStyleSheet excluding @import or nested link |
| 77 | + const inlineRules = rules.filter(rule => !rule.href); |
| 78 | + |
| 79 | + // concat all cssText into a string for inline rules |
| 80 | + const inlineRulesCssText = inlineRules |
| 81 | + .reduce((out, rule) => { |
| 82 | + out.push(rule.cssText); |
| 83 | + return out; |
| 84 | + }, []) |
| 85 | + .join(); |
| 86 | + // create and return a sheet with inline rules |
| 87 | + q.defer(resolve => |
| 88 | + resolve( |
| 89 | + convertTextToStylesheetFn({ |
| 90 | + data: inlineRulesCssText, |
| 91 | + shadowId, |
| 92 | + isExternal: false |
| 93 | + }) |
| 94 | + ) |
| 95 | + ); |
| 96 | + } catch (e) { |
| 97 | + // if no href, do not attempt to make an XHR, but this is preventive check |
| 98 | + // NOTE: as further enhancements to resolve nested @imports are done, a decision to throw an Error if necessary here will be made. |
| 99 | + if (!sheet.href) { |
| 100 | + return; |
| 101 | + } |
| 102 | + // external sheet -> make an xhr and q the response |
| 103 | + q.defer((resolve, reject) => { |
| 104 | + getExternalStylesheet({ resolve, reject, url: sheet.href }); |
| 105 | + }); |
| 106 | + } |
| 107 | + }, []); |
| 108 | + // return |
| 109 | + return q; |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Returns an array of objects with root(document) |
| 114 | + * @param {Object} treeRoot the DOM tree to be inspected |
| 115 | + * @return {Array<Object>} array of objects, which each object containing a root (document) and an optional shadowId |
| 116 | + * @private |
| 117 | + */ |
| 118 | +function getAllRootsInTree(tree) { |
| 119 | + let ids = []; |
| 120 | + const documents = axe.utils |
| 121 | + .querySelectorAllFilter(tree, '*', node => { |
| 122 | + if (ids.includes(node.shadowId)) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + ids.push(node.shadowId); |
| 126 | + return true; |
| 127 | + }) |
| 128 | + .map(node => { |
| 129 | + return { |
| 130 | + shadowId: node.shadowId, |
| 131 | + root: axe.utils.getRootNode(node.actualNode) |
| 132 | + }; |
| 133 | + }); |
| 134 | + return documents; |
| 135 | +} |
| 136 | + |
| 137 | +/** |
| 138 | + * @method preloadCssom |
| 139 | + * @memberof axe.utils |
| 140 | + * @instance |
| 141 | + * @param {Object} object argument which is a composite object, with attributes timeout, treeRoot(optional), resolve & reject |
| 142 | + * @property {Number} timeout timeout for any network calls made |
| 143 | + * @property {Object} treeRoot the DOM tree to be inspected |
| 144 | + * @return {Object} a queue with results of cssom assets |
| 145 | + */ |
| 146 | +axe.utils.preloadCssom = function preloadCssom({ |
| 147 | + timeout, |
| 148 | + treeRoot = axe._tree[0] |
| 149 | +}) { |
| 150 | + const roots = axe.utils.uniqueArray(getAllRootsInTree(treeRoot), []); |
| 151 | + const q = axe.utils.queue(); |
| 152 | + |
| 153 | + if (!roots.length) { |
| 154 | + return q; |
| 155 | + } |
| 156 | + |
| 157 | + const dynamicDoc = document.implementation.createHTMLDocument(); |
| 158 | + |
| 159 | + /** |
| 160 | + * Convert text content to CSSStyleSheet |
| 161 | + * @method convertTextToStylesheet |
| 162 | + * @private |
| 163 | + * @param {Object} param an object with properties to construct stylesheet |
| 164 | + * @property {String} param.data text content of the stylesheet |
| 165 | + * @property {Boolean} param.isExternal flag to notify if the resource was fetched from the network |
| 166 | + * @property {Object} param.doc implementation document to create style elements |
| 167 | + * @property {String} param.shadowId (Optional) shadowId if shadowDOM |
| 168 | + */ |
| 169 | + function convertTextToStylesheet({ data, isExternal, shadowId }) { |
| 170 | + const style = dynamicDoc.createElement('style'); |
| 171 | + style.type = 'text/css'; |
| 172 | + style.appendChild(dynamicDoc.createTextNode(data)); |
| 173 | + dynamicDoc.head.appendChild(style); |
| 174 | + return { |
| 175 | + sheet: style.sheet, |
| 176 | + isExternal, |
| 177 | + shadowId |
| 178 | + }; |
| 179 | + } |
| 180 | + |
| 181 | + q.defer((resolve, reject) => { |
| 182 | + // as there can be multiple documents (root document, shadow document fragments, and frame documents) |
| 183 | + // reduce these into a queue |
| 184 | + roots |
| 185 | + .reduce((out, root) => { |
| 186 | + out.defer((resolve, reject) => { |
| 187 | + loadCssom(root, timeout, convertTextToStylesheet) |
| 188 | + .then(resolve) |
| 189 | + .catch(reject); |
| 190 | + }); |
| 191 | + return out; |
| 192 | + }, axe.utils.queue()) |
| 193 | + // await loading all such documents assets, and concat results into one object |
| 194 | + .then(assets => { |
| 195 | + resolve( |
| 196 | + assets.reduce((out, cssomSheets) => { |
| 197 | + return out.concat(cssomSheets); |
| 198 | + }, []) |
| 199 | + ); |
| 200 | + }) |
| 201 | + .catch(reject); |
| 202 | + }); |
| 203 | + |
| 204 | + return q; |
| 205 | +}; |
0 commit comments