Skip to content

Commit

Permalink
Correctly clone dynami CSSStyleSheets (Fix #1370)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasvh committed Jan 7, 2018
1 parent 8788a9f commit 4c14894
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,15 @@ export class DocumentCloner {
return tempIframe;
}

if (node instanceof HTMLStyleElement && node.sheet && node.sheet.cssRules) {
const css = [].slice
.call(node.sheet.cssRules, 0)
.reduce((css, rule) => css + rule.cssText, '');
const style = node.cloneNode(false);
style.textContent = css;
return style;
}

return node.cloneNode(false);
}

Expand Down
37 changes: 37 additions & 0 deletions tests/reftests/dynamicstyle.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!DOCTYPE html>
<html>
<head>
<title>Dynamic style</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="../test.js"></script>
<style id="style">
@font-face {
font-family: 'Roboto';
font-style: normal;
font-weight: 400;
src: local('Roboto'), local('Roboto-Regular'), url(https://fonts.gstatic.com/s/roboto/v18/CWB0XYA8bzo0kSThX0UTuA.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2212, U+2215;
}
body { font-family: "Roboto", serif }

div {
padding: 20px;
}
.div1 {
background: darkgreen;
color: white;
}
</style>
<script>
document.querySelector('#style').sheet.insertRule(
'.div2 { background: darkgreen; color:white; }',
document.querySelector('#style').sheet.cssRules.length
);
</script>
</head>
<body>
<div class="div1">Static styles</div>
<div class="div2">Dynamic styles</div>
</body>
</html>

0 comments on commit 4c14894

Please sign in to comment.