Skip to content

Commit

Permalink
Support Polymer 2.x shared styles
Browse files Browse the repository at this point in the history
Add support for `:host, html` selectors in custom styles
  • Loading branch information
dfreedm committed Jul 19, 2017
1 parent 8ce0260 commit 1d83b74
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/lib/style-transformer.html
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,12 @@

normalizeRootSelector: function(rule) {
rule.selector = rule.selector.replace(ROOT, 'html');
// handle 2.x rules like `:host, html {}`
var parts = rule.selector.split(COMPLEX_SELECTOR_SEP);
parts = parts.filter(function(part) {
return part.trim() !== HOST;
});
rule.selector = parts.join(COMPLEX_SELECTOR_SEP);
},

_transformDocumentSelector: function(selector) {
Expand Down
25 changes: 25 additions & 0 deletions test/unit/custom-style.html
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@
--html-foo: 10px dotted green;
}
</style>
<style is="custom-style">
:host, html {
--polymer-2-root: 10px solid rgb(123, 123, 123);
}
</style>
</head>
<body>
<div class="italic">italic</div>
Expand All @@ -162,6 +167,8 @@

<parent-variable-with-var></parent-variable-with-var>

<polymer-2-root></polymer-2-root>

<br><br>
<div id="after"></div>

Expand Down Expand Up @@ -314,6 +321,16 @@
</template>
</dom-module>

<dom-module id="polymer-2-root">
<template>
<style>
:host {
border: var(--polymer-2-root);
}
</style>
</template>
</dom-module>

<script>
HTMLImports.whenReady(function() {
Polymer({
Expand All @@ -338,6 +355,9 @@
Polymer({
is: 'x-top-selectors'
});
Polymer({
is: 'polymer-2-root'
})
})
</script>

Expand Down Expand Up @@ -386,6 +406,11 @@
assertComputed(xFoo.$.bar3.$.baz, '5px');
});

test('polymer 2 shared styles applied', function() {
var polymer2 = document.querySelector('polymer-2-root');
assertComputed(polymer2, '10px');
})

test('custom properties registered as defaults', function() {
var propsToCheck = ['--italic'];
if (Polymer.Settings.useNativeCSSProperties || stylesBuilt) {
Expand Down

0 comments on commit 1d83b74

Please sign in to comment.