-
Notifications
You must be signed in to change notification settings - Fork 779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: CSSOM generation for shadowRoot in Safari #1113
Changes from 3 commits
4929809
d29ff17
e14fd3e
aa73cd3
d33527a
63e1b53
018af6e
f79d038
fce6eef
74ee4a4
8987a4a
b60839b
ae5e59b
e945b7f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,12 +10,12 @@ function loadCssom({ root, shadowId }, timeout, convertTextToStylesheetFn) { | |
/** | ||
* Make an axios get request to fetch a given resource and resolve | ||
* @method getExternalStylesheet | ||
* @private | ||
* @param {Object} param an object with properties to configure the external XHR | ||
* @property {Object} param.resolve resolve callback on queue | ||
* @property {Object} param.reject reject callback on queue | ||
* @property {String} param.url string representing the url of the resource to load | ||
* @property {Number} param.timeout timeout to about network call | ||
* @private | ||
*/ | ||
function getExternalStylesheet({ resolve, reject, url }) { | ||
axe.imports | ||
|
@@ -38,11 +38,38 @@ function loadCssom({ root, shadowId }, timeout, convertTextToStylesheetFn) { | |
|
||
const q = axe.utils.queue(); | ||
|
||
// handle .styleSheets non existent on certain shadowDOM root | ||
const rootStyleSheets = root.styleSheets | ||
? Array.from(root.styleSheets) | ||
: null; | ||
if (!rootStyleSheets) { | ||
let styleSheets = null; | ||
|
||
if (!root.styleSheets) { | ||
/** | ||
* In Safari: (Issue: https://github.com/dequelabs/axe-core/issues/1082) | ||
* As per the issue, shadowRoot does not return their styleShets, | ||
* So the content of any `style` tag is parsed to construct shadowDOM cssom. | ||
* | ||
* Steps: | ||
* - root(document).styleSheets does not return on `shadowContent/ shadowRoot`. | ||
* - Hence lookup all `<style>` tag(s) with in the shadowRoot and convert to styleSheet dynamically | ||
* - Pass these converted styleSheets to parse and construct cssom of shadowRoot(s) | ||
*/ | ||
styleSheets = Array.from(root.children) | ||
.filter(node => { | ||
return node.nodeName.toUpperCase() === 'STYLE'; | ||
}) | ||
.map(node => { | ||
const sheet = convertTextToStylesheetFn({ | ||
data: node.textContent | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does this solution work with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Works, just fine as shown here: https://github.com/dequelabs/axe-core/pull/1113/files#diff-b195c7e1a05c62e60347f579d8f7dddeR339 It was the |
||
// no need to pass shadowId, root and isExternal | ||
// as only interested in converting given data to sheet | ||
}); | ||
return sheet.sheet; | ||
}); | ||
} else { | ||
// retrieve stylesheets as an [] | ||
styleSheets = Array.from(root.styleSheets); | ||
} | ||
|
||
// if no root styleSheets then return | ||
if (!styleSheets || !styleSheets.length) { | ||
return q; | ||
} | ||
|
||
|
@@ -51,7 +78,7 @@ function loadCssom({ root, shadowId }, timeout, convertTextToStylesheetFn) { | |
let sheetHrefs = []; | ||
|
||
// filter out sheets, that should not be accounted for... | ||
const sheets = rootStyleSheets.filter(sheet => { | ||
const sheets = styleSheets.filter(sheet => { | ||
// FILTER > sheets with the same href (if exists) | ||
let sheetAlreadyExists = false; | ||
if (sheet.href) { | ||
|
@@ -135,6 +162,7 @@ function loadCssom({ root, shadowId }, timeout, convertTextToStylesheetFn) { | |
}); | ||
} | ||
}, []); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This empty array is passed into |
||
|
||
// return | ||
return q; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -237,7 +237,7 @@ describe('preload cssom integration test', function() { | |
.then(function(results) { | ||
var sheets = results[0]; | ||
// verify count | ||
assert.isAtLeast(sheets.length, 4); | ||
assert.lengthOf(sheets, 7); | ||
// verify that the last non external sheet with shadowId has green selector | ||
var nonExternalsheetsWithShadowId = sheets | ||
.filter(function(s) { | ||
|
@@ -246,20 +246,60 @@ describe('preload cssom integration test', function() { | |
.filter(function(s) { | ||
return s.shadowId; | ||
}); | ||
assertStylesheet( | ||
nonExternalsheetsWithShadowId[ | ||
nonExternalsheetsWithShadowId.length - 1 | ||
].sheet, | ||
'.green', | ||
'.green{background-color:green;}' | ||
); | ||
|
||
// Issue - https://github.com/dequelabs/axe-core/issues/1082 | ||
if ( | ||
nonExternalsheetsWithShadowId && | ||
nonExternalsheetsWithShadowId.length | ||
) { | ||
assertStylesheet( | ||
nonExternalsheetsWithShadowId[ | ||
nonExternalsheetsWithShadowId.length - 1 | ||
].sheet, | ||
'.green', | ||
'.green{background-color:green;}' | ||
); | ||
} | ||
done(); | ||
}) | ||
.catch(done); | ||
} | ||
); | ||
|
||
(shadowSupported ? it : xit)( | ||
'should return styles from shadow dom', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure to test |
||
function(done) { | ||
var fixture = document.getElementById('shadow-fixture2'); | ||
var shadow = fixture.attachShadow({ mode: 'open' }); | ||
shadow.innerHTML = | ||
'<style>@import "https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.css"; @import "preload-cssom-shadow-blue.css"; .green { background-color: green; }</style>' + | ||
'<div class="initialism">Some text</div>' + | ||
'<style>.notGreen { background-color: orange; }</style>' + | ||
'<div class="green">green</div>' + | ||
'<div class="red">red</div>' + | ||
'<div class="duplicateGreen">red</div>' + | ||
'<h1>Heading</h1>'; | ||
getPreload(fixture) | ||
.then(function(results) { | ||
var sheets = results[0]; | ||
// verify count | ||
assert.lengthOf(sheets, 8); | ||
// verify that the last non external sheet with shadowId has green selector | ||
var nonExternalsheetsWithShadowId = sheets | ||
.filter(function(s) { | ||
return !s.isExternal; | ||
}) | ||
.filter(function(s) { | ||
return s.shadowId; | ||
}); | ||
assertStylesheet( | ||
nonExternalsheetsWithShadowId[ | ||
nonExternalsheetsWithShadowId.length - 2 | ||
].sheet, | ||
'.green', | ||
'.green{background-color:green;}' | ||
); | ||
assertStylesheet( | ||
nonExternalsheetsWithShadowId[ | ||
nonExternalsheetsWithShadowId.length - 1 | ||
].sheet, | ||
'.notGreen', | ||
'.notGreen{background-color:orange;}' | ||
); | ||
done(); | ||
}) | ||
.catch(done); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please report this as a bug to Apple.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reported, but do not have an issue/ bug reported link from them yet.