From fea64b9b6a75bb39b7644fdfdd56e1224f43bf3d Mon Sep 17 00:00:00 2001 From: Daniel Freedman Date: Fri, 23 Sep 2016 16:50:11 -0700 Subject: [PATCH] Translate `:root` to `:host > *` for element styles Support `:host > *` as root rules for CSS Custom Properties Fixes #3991 --- src/lib/style-properties.html | 10 +++---- src/lib/style-transformer.html | 2 ++ test/unit/styling-cross-scope-var.html | 37 ++++++++++++++++++++++++-- test/unit/styling-scoped-elements.html | 23 ++++++++++++++++ test/unit/styling-scoped.html | 9 +++++++ 5 files changed, 73 insertions(+), 8 deletions(-) diff --git a/src/lib/style-properties.html b/src/lib/style-properties.html index 161b9d1c0b..20b2077be2 100644 --- a/src/lib/style-properties.html +++ b/src/lib/style-properties.html @@ -285,8 +285,10 @@ return {properties: props, key: o}; }, + _rootSelector: /(?:^:root)|(?::host\s*>\s*\*)/, + _checkRoot: function(hostScope, selector) { - return selector.indexOf(':root') === 0 || + return Boolean(selector.match(this._rootSelector)) || (hostScope === 'html' && selector.indexOf('html') === 0); }, @@ -302,7 +304,7 @@ 'html'; var parsedSelector = rule.parsedSelector; var isRoot = this._checkRoot(hostScope, parsedSelector); - var isHost = parsedSelector.indexOf(':host') === 0; + var isHost = !isRoot && parsedSelector.indexOf(':host') === 0; // build info is either in scope (when scope is an element) or in the style // when scope is the default scope; note: this allows default scope to have // mixed mode built and unbuilt styles. @@ -313,10 +315,6 @@ // :host -> x-foo for elements, but sub-rules have .x-foo in them isHost = !isRoot && parsedSelector.indexOf(hostScope) === 0; } - if (cssBuild === 'shadow') { - isRoot = parsedSelector === ':host > *' || this._checkRoot(hostScope, parsedSelector); - isHost = isHost && !isRoot; - } if (!isRoot && !isHost) { return; } diff --git a/src/lib/style-transformer.html b/src/lib/style-transformer.html index 4acdb16b53..81871a4834 100644 --- a/src/lib/style-transformer.html +++ b/src/lib/style-transformer.html @@ -104,6 +104,7 @@ var self = this; cb = function(rule) { rule.selector = self._slottedToContent(rule.selector); + rule.selector = rule.selector.replace(ROOT, ':host > *'); if (callback) { callback(rule); } @@ -189,6 +190,7 @@ var self = this; selector = selector.trim(); selector = this._slottedToContent(selector); + selector = selector.replace(ROOT, ':host > *'); selector = selector.replace(CONTENT_START, HOST + ' $1'); selector = selector.replace(SIMPLE_SELECTOR_SEP, function(m, c, s) { if (!stop) { diff --git a/test/unit/styling-cross-scope-var.html b/test/unit/styling-cross-scope-var.html index dad5b75176..b37482cede 100644 --- a/test/unit/styling-cross-scope-var.html +++ b/test/unit/styling-cross-scope-var.html @@ -831,6 +831,32 @@ + + + + + diff --git a/test/unit/styling-scoped-elements.html b/test/unit/styling-scoped-elements.html index 3f385d8577..dcebbcfa65 100644 --- a/test/unit/styling-scoped-elements.html +++ b/test/unit/styling-scoped-elements.html @@ -630,3 +630,26 @@ Polymer({is: 'x-slotted'}); + + + + + diff --git a/test/unit/styling-scoped.html b/test/unit/styling-scoped.html index 44d1660574..afc67a5bf3 100644 --- a/test/unit/styling-scoped.html +++ b/test/unit/styling-scoped.html @@ -329,6 +329,15 @@ assertComputed(e.$.bar3, '6px'); }); + test('ShadowRoot-wide selectors', function() { + var e = document.createElement('root-styles'); + document.body.appendChild(e); + // :root + assertComputed(e.$.child, 'rgb(123, 123, 123)', 'color'); + // :host > * + assertComputed(e.$.child, '2px'); + }); + suite('scoped-styling-shady-only', function() { suiteSetup(function() {