-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #1477. This was a regression in 780b5dc since we no longer parse CSS inside mutation events, where errors just go to window.onerror. Instead, bad CSS now fires a jsdomError. Also cleaned up and consolidated duplicate code in the HTMLLinkElement and HTMLStyleElement implementations.
- Loading branch information
Showing
5 changed files
with
69 additions
and
127 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
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,60 @@ | ||
"use strict"; | ||
const cssom = require("cssom"); | ||
const normalizeEncoding = require("../helpers/encoding").normalizeEncoding; | ||
const resourceLoader = require("../../browser/resource-loader"); | ||
const resolveHref = require("../../utils").resolveHref; | ||
|
||
exports.fetchStylesheet = (elementImpl, url, sheet) => { | ||
let defaultEncoding = elementImpl._ownerDocument._encoding; | ||
if (elementImpl.localName === "link" && elementImpl.hasAttribute("charset")) { | ||
defaultEncoding = normalizeEncoding(elementImpl.getAttribute("charset")); | ||
} | ||
|
||
resourceLoader.load(elementImpl, url, { defaultEncoding }, data => { | ||
// TODO: MIME type checking? | ||
exports.evaluateStylesheet(elementImpl, data, sheet, url); | ||
}); | ||
}; | ||
|
||
exports.evaluateStylesheet = (elementImpl, data, sheet, baseURL) => { | ||
let newStyleSheet; | ||
try { | ||
newStyleSheet = cssom.parse(data); | ||
} catch (e) { | ||
if (elementImpl._ownerDocument._defaultView) { | ||
const error = new Error("Could not parse CSS stylesheet"); | ||
error.detail = data; | ||
elementImpl._ownerDocument._defaultView._virtualConsole.emit("jsdomError", error); | ||
} | ||
|
||
elementImpl._ownerDocument.styleSheets.push(sheet); | ||
return; | ||
} | ||
|
||
const spliceArgs = newStyleSheet.cssRules; | ||
spliceArgs.unshift(0, sheet.cssRules.length); | ||
Array.prototype.splice.apply(sheet.cssRules, spliceArgs); | ||
|
||
scanForImportRules(elementImpl, sheet.cssRules, baseURL); | ||
|
||
elementImpl._ownerDocument.styleSheets.push(sheet); | ||
}; | ||
|
||
function scanForImportRules(elementImpl, cssRules, baseURL) { | ||
if (!cssRules) { | ||
return; | ||
} | ||
|
||
for (let i = 0; i < cssRules.length; ++i) { | ||
if (cssRules[i].cssRules) { | ||
// @media rule: keep searching inside it. | ||
scanForImportRules(elementImpl, cssRules[i].cssRules, baseURL); | ||
} else if (cssRules[i].href) { | ||
// @import rule: fetch the resource and evaluate it. | ||
// See http://dev.w3.org/csswg/cssom/#css-import-rule | ||
// If loading of the style sheet fails its cssRules list is simply | ||
// empty. I.e. an @import rule always has an associated style sheet. | ||
exports.fetchStylesheet(elementImpl, resolveHref(baseURL, cssRules[i].href), elementImpl.sheet); | ||
} | ||
} | ||
} |
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