-
Notifications
You must be signed in to change notification settings - Fork 793
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
10 changed files
with
439 additions
and
138 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Function which converts given text to `CSSStyleSheet` | ||
* - used in `CSSOM` computation. | ||
* - factory (closure) function, initialized with `document.implementation.createHTMLDocument()`, which uses DOM API for creating `style` elements. | ||
* | ||
* @method axe.utils.getStyleSheetFactory | ||
* @memberof axe.utils | ||
* @param {Object} dynamicDoc `document.implementation.createHTMLDocument() | ||
* @param {Object} options an object with properties to construct stylesheet | ||
* @property {String} options.data text content of the stylesheet | ||
* @property {Boolean} options.isCrossOrigin flag to notify if the resource was fetched from the network | ||
* @property {String} options.shadowId (Optional) shadowId if shadowDOM | ||
* @property {Object} options.root implementation document to create style elements | ||
* @property {String} options.priority a number indicating the loaded priority of CSS, to denote specificity of styles contained in the sheet. | ||
* @returns {Function} | ||
*/ | ||
axe.utils.getStyleSheetFactory = function getStyleSheetFactory(dynamicDoc) { | ||
if (!dynamicDoc) { | ||
throw new Error( | ||
'axe.utils.getStyleSheetFactory should be invoked with an argument' | ||
); | ||
} | ||
|
||
return options => { | ||
const { | ||
data, | ||
isCrossOrigin = false, | ||
shadowId, | ||
root, | ||
priority, | ||
isLink = false | ||
} = options; | ||
const style = dynamicDoc.createElement('style'); | ||
if (isLink) { | ||
// as creating a stylesheet as link will need to be awaited | ||
// till `onload`, it is wise to convert link href to @import statement | ||
const text = dynamicDoc.createTextNode(`@import "${data.href}"`); | ||
style.appendChild(text); | ||
} else { | ||
style.appendChild(dynamicDoc.createTextNode(data)); | ||
} | ||
dynamicDoc.head.appendChild(style); | ||
return { | ||
sheet: style.sheet, | ||
isCrossOrigin, | ||
shadowId, | ||
root, | ||
priority | ||
}; | ||
}; | ||
}; |
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
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
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
describe('axe.utils.getStyleSheetFactory', function() { | ||
'use strict'; | ||
|
||
var dynamicDoc = document.implementation.createHTMLDocument( | ||
'Dynamic document for testing axe.utils.getStyleSheetFactory' | ||
); | ||
|
||
it('throws if there is no argument of dynamicDocument', function() { | ||
assert.throws(function() { | ||
axe.utils.getStyleSheetFactory(); | ||
}); | ||
}); | ||
|
||
it('returns a function when passed argument of dynamicDocument', function() { | ||
const actual = axe.utils.getStyleSheetFactory(dynamicDoc); | ||
assert.isFunction(actual); | ||
}); | ||
|
||
it('returns a CSSOM stylesheet, when invoked with data (text)', function() { | ||
const stylesheetFactory = axe.utils.getStyleSheetFactory(dynamicDoc); | ||
const actual = stylesheetFactory({ | ||
data: `.someStyle{background-color:red;}`, | ||
root: document, | ||
priority: [1, 0] | ||
}); | ||
|
||
assert.isDefined(actual); | ||
assert.hasAllKeys(actual, [ | ||
'sheet', | ||
'isCrossOrigin', | ||
'shadowId', | ||
'root', | ||
'priority' | ||
]); | ||
assert.deepEqual(actual.priority, [1, 0]); | ||
axe.testUtils.assertStylesheet( | ||
actual.sheet, | ||
'.someStyle', | ||
'.someStyle{background-color:red;}' | ||
); | ||
}); | ||
}); |
Oops, something went wrong.