Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
Add build support for :dir() in Polymer 2+
Browse files Browse the repository at this point in the history
  • Loading branch information
dfreedm committed Nov 6, 2018
1 parent 249b5ad commit 6ccee9a
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased
- Fix `shady-unscoped` style imports and add tests
- Add build support for `:dir()` for Polymer 2 Shadow DOM

## [0.6.0] - 2018-09-17
* Add support for Polymer v1 `var(--foo, --bar)` => `var(--foo,var(--bar))` transform
Expand Down
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const {traverse} = require('polymer-analyzer/lib/javascript/estraverse-shim.js')

const {dirShadowTransform, slottedToContent, shadyReplaceContent, fixBadVars} = require('./lib/polymer-1-transforms.js');

const {polymer2DirShadowTransform} = require('./lib/polymer-2-dir-transform.js');

const {createScannerMap, createParserMap} = require('./lib/slim-analyzer-options.js');

const {IdentifierIsDocumentVariable} = require('./lib/closure-html-template-scanner.js');
Expand Down Expand Up @@ -259,9 +261,13 @@ function slottedTransform(ast) {
});
}

function dirTransform(ast) {
function dirTransform(ast, polymerVersion) {
StyleUtil.forEachRule(ast, (rule) => {
rule.selector = dirShadowTransform(rule.selector);
if (polymerVersion === 1) {
rule.selector = dirShadowTransform(rule.selector);
} else {
rule.selector = polymer2DirShadowTransform(rule.selector);
}
});
}

Expand Down Expand Up @@ -636,9 +642,9 @@ async function polymerCssBuild(paths, options = {}) {
}
applyShim(ast);
if (nativeShadow) {
dirTransform(ast, polymerVersion);
if (polymerVersion === 1) {
slottedTransform(ast);
dirTransform(ast);
}
} else {
shadyShim(ast, s, analysis);
Expand Down
30 changes: 30 additions & 0 deletions lib/polymer-2-dir-transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
@license
Copyright (c) 2018 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
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
*/

// copied from polymer DirMixin
const HOST_DIR = /:host\(:dir\((ltr|rtl)\)\)/g;
const HOST_DIR_REPLACMENT = '$&, :host([dir="$1"])';

const EL_DIR = /(?<!:host\()([\s\w-#.[\]*]*):dir\((ltr|rtl)\)/g;
const EL_DIR_REPLACMENT = '$&, :host([dir="$2"]) $1';

const DIR_CHECK = /:dir\((?:ltr|rtl)\)/;

function polymer2DirShadowTransform(selector) {
if (!DIR_CHECK.test(selector)) {
return selector;
}
selector = selector.replace(HOST_DIR, HOST_DIR_REPLACMENT);
return selector.replace(EL_DIR, EL_DIR_REPLACMENT);
}

module.exports = {
polymer2DirShadowTransform
}
32 changes: 32 additions & 0 deletions tests/app/dir-element.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!--
@license
Copyright (c) 2018 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
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
-->
<dom-module id="dir-element">
<template>
<style>
:host {
color: rgb(255, 0, 0);
}
div {
height: 200px;
background-color: rgb(0, 0, 255);
}
:host(:dir(rtl)) {
color: rgb(0, 0, 123);
}
div:dir(rtl) {
background-color: rgb(123, 0, 0);
}
</style>
<div>Dir!</div>
</template>
<script>
Polymer({is: 'dir-element'});
</script>
</dom-module>
22 changes: 22 additions & 0 deletions tests/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<link rel="import" href="cr-bug.html">
<link rel="import" href="polymer-1-vars.html">
<link rel="import" href="shady-unscoped.html">
<link rel="import" href="dir-element.html">

<custom-style>
<style is="custom-style" include="shared-style">
Expand All @@ -31,6 +32,7 @@
<x-app></x-app>
<x-component></x-component>
<cr-bug-owner></cr-bug-owner>
<dir-element></dir-element>

<dom-module id="inline-element">
<template>
Expand Down Expand Up @@ -130,6 +132,26 @@
assert(!(template.hasAttribute('css-build') ^ shouldHaveMarkedTemplate), `dom-module template should ${shouldHaveMarkedTemplate ? 'not ' : ''}have css-build attribute`);
});

suite('dir', function() {
setup(function() {
document.documentElement.setAttribute('dir', 'ltr');
});
teardown(function() {
document.documentElement.removeAttribute('dir');
});
test('dir transform', async function() {
const el = Polymer.dom(document).querySelector('dir-element');
const root = el.shadyRoot || el.shadowRoot;
const div = Polymer.dom(root).querySelector('div');
assertComputed(el, 'rgb(255, 0, 0)', 'color');
assertComputed(div, 'rgb(0, 0, 255)', 'background-color');
document.documentElement.setAttribute('dir', 'rtl');
await new Promise(resolve => requestAnimationFrame(resolve));
assertComputed(el, 'rgb(0, 0, 123)', 'color');
assertComputed(div, 'rgb(123, 0, 0)', 'background-color');
});
});

test('shady-unscoped', function() {
const el = Polymer.dom(document).querySelector('shady-unscoped');
assert(el.isConnected, 'shady-unscoped element must be in document to apply styling');
Expand Down
2 changes: 1 addition & 1 deletion tests/prepare-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ build(){

cp -rv tests/app/* "tests/${outputDir}/"

bin/polymer-css-build --polymer-version=${version} ${shady} ${noInline} --file tests/${outputDir}/index.html tests/${outputDir}/x-app.html tests/${outputDir}/x-app-dom-module.html tests/${outputDir}/x-app-definition.js tests/${outputDir}/x-component.html tests/${outputDir}/shared/shared-style.html tests/${outputDir}/x-nested-apply.html tests/${outputDir}/cr-bug.html tests/${outputDir}/class-element.js tests/${outputDir}/polymer-1-vars.html tests/${outputDir}/shady-unscoped.html
bin/polymer-css-build --polymer-version=${version} ${shady} ${noInline} --file tests/${outputDir}/index.html tests/${outputDir}/x-app.html tests/${outputDir}/x-app-dom-module.html tests/${outputDir}/x-app-definition.js tests/${outputDir}/x-component.html tests/${outputDir}/shared/shared-style.html tests/${outputDir}/x-nested-apply.html tests/${outputDir}/cr-bug.html tests/${outputDir}/class-element.js tests/${outputDir}/polymer-1-vars.html tests/${outputDir}/shady-unscoped.html tests/${outputDir}/dir-element.html

(cd tests/${outputDir}; npx bower install polymer#${version} web-component-tester)
}
Expand Down

0 comments on commit 6ccee9a

Please sign in to comment.