From ae37f83db23c78f8345d333ba50d8c9a02302c76 Mon Sep 17 00:00:00 2001 From: Daniel Freedman Date: Wed, 11 Oct 2017 14:43:37 -0700 Subject: [PATCH] Add :dir support to Polymer 1 Polymer 1.x fix for #4860 `:dir` becomes `:host-context([dir])` --- src/lib/style-transformer.html | 15 +++ test/runner.html | 6 +- test/unit/dir.html | 171 +++++++++++++++++++++++++++++++++ 3 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 test/unit/dir.html diff --git a/src/lib/style-transformer.html b/src/lib/style-transformer.html index 06ff23750d..90733c897d 100644 --- a/src/lib/style-transformer.html +++ b/src/lib/style-transformer.html @@ -37,6 +37,10 @@ * :host-context(...): scopeName..., ... scopeName + * ...:dir(ltr|rtl) -> [dir="ltr|rtl"] ..., ...[dir="ltr|rtl"] + + * :host(:dir[rtl]) -> scopeName:dir(rtl) -> [dir="rtl"] scopeName, scopeName[dir="rtl"] + */ var api = { @@ -105,6 +109,11 @@ cb = function(rule) { rule.selector = self._slottedToContent(rule.selector); rule.selector = rule.selector.replace(ROOT, ':host > *'); + rule.selector = rule.selector.split(',').map(function(s) { + s = s.replace(HOST_DIR, HOST_DIR_REPLACE); + s = s.replace(DIR_PAREN, SHADOW_DIR_REPLACE); + return s; + }).join(','); if (callback) { callback(rule); } @@ -238,6 +247,7 @@ selector = selector.replace(SCOPE_JUMP, ' '); stop = true; } + selector = selector.replace(DIR_PAREN, DIR_REPLACE); return {value: selector, combinator: combinator, stop: stop, hostContext: hostContext}; }, @@ -340,6 +350,11 @@ var SELECTOR_NO_MATCH = 'should_not_match'; var SLOTTED_PAREN = /(?:::slotted)(?:\(((?:\([^)(]*\)|[^)(]*)+?)\))/g; var HOST_OR_HOST_GT_STAR = /:host(?:\s*>\s*\*)?/; + var DIR_PAREN = /(.*):dir\((ltr|rtl)\)/g; + var DIR_REPLACE = '[dir="$2"] $1, $1[dir="$2"]'; + var SHADOW_DIR_REPLACE = ':host-context([dir="$2"]) $1'; + var HOST_DIR = /:host\(:dir\((rtl|ltr)\)\)/g; + var HOST_DIR_REPLACE = ':host-context([dir="$1"])'; // exports return api; diff --git a/test/runner.html b/test/runner.html index b84f6bb2da..3d0b095ea2 100644 --- a/test/runner.html +++ b/test/runner.html @@ -94,7 +94,11 @@ 'unit/script-after-import-in-head.html', 'unit/globals.html', 'unit/lazy-register.html', - 'unit/element-disable-upgrade.html' + 'unit/element-disable-upgrade.html', + 'unit/dir.html', + 'unit/dir.html?dom=shadow', + 'unit/dir.html?lazyRegister=true&useNativeCSSProperties=true', + 'unit/dir.html?lazyRegister=true&useNativeCSSProperties=true&dom=shadow' ]; if (window.customElements || document.registerElement) { diff --git a/test/unit/dir.html b/test/unit/dir.html new file mode 100644 index 0000000000..bb205b7267 --- /dev/null +++ b/test/unit/dir.html @@ -0,0 +1,171 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file