Skip to content

Commit

Permalink
Merge pull request #5595 from Polymer/legacy-undefined-noBatch-adopte…
Browse files Browse the repository at this point in the history
…dStyleSheets

Adds basic support for `adoptedStyleSheets`
  • Loading branch information
Steve Orvell authored Oct 11, 2019
2 parents 6c3bb48 + dbd9140 commit 3506c52
Show file tree
Hide file tree
Showing 4 changed files with 1,074 additions and 4 deletions.
30 changes: 29 additions & 1 deletion lib/mixins/element-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
import '../utils/boot.js';

import { rootPath, strictTemplatePolicy, allowTemplateFromDomModule, legacyOptimizations, legacyWarnings, syncInitialRender} from '../utils/settings.js';
import { rootPath, strictTemplatePolicy, allowTemplateFromDomModule, legacyOptimizations, legacyWarnings, syncInitialRender, supportsAdoptingStyleSheets} from '../utils/settings.js';
import { dedupingMixin } from '../utils/mixin.js';
import { stylesFromTemplate, stylesFromModuleImports } from '../utils/style-gather.js';
import { pathFromUrl, resolveCss, resolveUrl } from '../utils/resolve-url.js';
Expand Down Expand Up @@ -294,6 +294,29 @@ export const ElementMixin = dedupingMixin(base => {
if (window.ShadyCSS) {
window.ShadyCSS.prepareTemplate(template, is);
}
// Support for `adoptedStylesheets` relies on using native Shadow DOM
// and built CSS. Built CSS is required because runtime transformation of
// `@apply` is not supported. This is because ShadyCSS relies on being able
// to update a `style` element in the element template and this is
// removed when using `adoptedStyleSheets`.
// Note, it would be more efficient to allow style includes to become
// separate stylesheets; however, because of `@apply` these are
// potentially not shareable and sharing the ones that could be shared
// would require some coordination. To keep it simple, all the includes
// and styles are collapsed into a single shareable stylesheet.
if (builtCSS && supportsAdoptingStyleSheets) {
// Remove styles in template and make a shareable stylesheet
const styles = template.content.querySelectorAll('style');
if (styles) {
let css = '';
Array.from(styles).forEach(s => {
css += s.textContent;
s.parentNode.removeChild(s);
});
klass._styleSheet = new CSSStyleSheet();
klass._styleSheet.replaceSync(css);
}
}
}

/**
Expand Down Expand Up @@ -705,6 +728,11 @@ export const ElementMixin = dedupingMixin(base => {
if (!n.shadowRoot) {
n.attachShadow({mode: 'open', shadyUpgradeFragment: dom});
n.shadowRoot.appendChild(dom);
// When `adoptedStyleSheets` is supported a stylesheet is made
// available on the element constructor.
if (this.constructor._styleSheet) {
n.shadowRoot.adoptedStyleSheets = [this.constructor._styleSheet];
}
}
if (syncInitialRender && window.ShadyDOM) {
window.ShadyDOM.flushInitial(n.shadowRoot);
Expand Down
20 changes: 17 additions & 3 deletions lib/utils/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,26 @@ Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
import './boot.js';

import { pathFromUrl } from './resolve-url.js';
export const useShadow = !(window.ShadyDOM);
export const useShadow = !(window.ShadyDOM) || !(window.ShadyDOM.inUse);
export const useNativeCSSProperties = Boolean(!window.ShadyCSS || window.ShadyCSS.nativeCss);
export const useNativeCustomElements = !(window.customElements.polyfillWrapFlushCallback);

export const supportsAdoptingStyleSheets = useShadow &&
('adoptedStyleSheets' in Document.prototype) &&
('replaceSync' in CSSStyleSheet.prototype) &&
// Since spec may change, feature detect exact API we need
(() => {
try {
const sheet = new CSSStyleSheet();
sheet.replaceSync('');
const host = document.createElement('div');
host.attachShadow({mode: 'open'});
host.shadowRoot.adoptedStyleSheets = [sheet];
return (host.shadowRoot.adoptedStyleSheets[0] === sheet);
} catch(e) {
return false;
}
})();

/**
* Globally settable property that is automatically assigned to
Expand Down
1 change: 1 addition & 0 deletions test/runner.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
'unit/styling-cross-scope-apply.html',
'unit/styling-cross-scope-unknown-host.html',
'unit/styling-only-with-template.html',
'unit/styling-build-adopted-stylesheets.html',
'unit/custom-style.html',
'unit/custom-style-late.html',
'unit/custom-style-async.html',
Expand Down
Loading

0 comments on commit 3506c52

Please sign in to comment.