Skip to content

Commit 155ab8a

Browse files
committed
Copy <custom-style> styles to main document
Work around deprecation of styles in HTML Imports https://crbug.com/523952 Fixes #4679
1 parent b19e180 commit 155ab8a

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

lib/elements/custom-style.html

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@
2525
* - Document styles defined in a `<custom-style>` are shimmed to ensure they
2626
* do not leak into local DOM when running on browsers without native
2727
* Shadow DOM.
28-
* - Custom properties can be defined in a `<custom-style>`. Use the `html` selector
28+
* - Custom properties can be defined in a `<custom-style>`. Use the `html` selector
2929
* to define custom properties that apply to all custom elements.
30-
* - Custom mixins can be defined in a `<custom-style>`, if you import the optional
30+
* - Custom mixins can be defined in a `<custom-style>`, if you import the optional
3131
* [apply shim](https://github.com/webcomponents/shadycss#about-applyshim)
3232
* (`shadycss/apply-shim.html`).
3333
*
3434
* To use:
35-
*
35+
*
3636
* - Import `custom-style.html`.
3737
* - Place a `<custom-style>` element in the main document, wrapping an inline `<style>` tag that
3838
* contains the CSS rules you want to shim.
39-
*
39+
*
4040
* For example:
4141
*
4242
* ```
@@ -91,6 +91,25 @@
9191
style.removeAttribute(attr);
9292
style.textContent = Polymer.StyleGather.cssFromModules(include) + style.textContent;
9393
}
94+
/*
95+
HTML Imports styling the main document are deprecated in Chrome
96+
https://crbug.com/523952
97+
98+
If this element is not in the main document, then it must be in an HTML Import document.
99+
In that case, copy the style into the main document and disable the current style.
100+
`__appliedElement` is used by the HTML Imports polyfill for this use case, and already understood by
101+
ShadyCSS's Custom Style Interface.
102+
103+
The ordering of `<custom-style>` should stay the same as when loaded by HTML Imports, but there may be odd
104+
cases of ordering w.r.t the main document styles.
105+
*/
106+
if (this.ownerDocument !== window.document) {
107+
let appliedStyle = style.cloneNode(true);
108+
style.__appliedElement = appliedStyle;
109+
// disable style in the HTML Import document
110+
style.setAttribute('media', 'none');
111+
window.document.head.appendChild(appliedStyle);
112+
}
94113
return this._style;
95114
}
96115
}

0 commit comments

Comments
 (0)