Skip to content

Commit

Permalink
@extends: add support for nested style rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Orvell committed May 14, 2015
1 parent 6095478 commit 92ebf2f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/lib/style-extends.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
transform: function(cssText) {
return Polymer.StyleUtil.toCssText(cssText, function(rule) {
var map = this._mapRule(rule);
if (map) {
if (rule.parent) {
var m;
while (m = this.rx.EXTEND.exec(rule.cssText)) {
var extend = m[1];
var extendor = map[extend];
var extendor = this._findExtendor(extend, rule);

This comment has been minimized.

Copy link
@nazar-pc

nazar-pc Nov 5, 2015

Contributor

@sorvell, looks like since this commit Polymer.StyleExtends._mapRule() method and var map = this._mapRule(rule); few lines above are obsolete, they are not used at all, same in master code.

if (extendor) {
this._extendRule(rule, extendor);
}
Expand All @@ -48,7 +48,15 @@
}
},

_findExtendor: function(extend, rule) {
return rule.parent && rule.parent.map && rule.parent.map[extend] ||
this._findExtendor(extend, rule.parent);
},

_extendRule: function(target, source) {
if (target.parent !== source.parent) {
this._cloneAndRuleToParent(source, target.parent);
}
target.extends = target.extends || (target.extends = []);
target.extends.push(source);
source.selector = source.selector + ',\n' + target.selector;
Expand All @@ -59,6 +67,15 @@
}
},

_cloneAndRuleToParent: function(rule, parent) {
rule = Object.create(rule);
rule.parent = parent;
if (rule.extends) {
rule.extends = rule.extends.slice();
}
parent.rules.push(rule);
},

rx: {
EXTEND: /@extends\(([^)]*)\)\s*?;/gim
}
Expand Down
27 changes: 27 additions & 0 deletions test/unit/styling-extends.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,36 @@
@extends(#bar);
}

.not {
border: 20px solid orange;
}

@media (max-width: 20000px) {
.baz {
padding: 15px;
}

@media (max-width: 20000px) {
#media {
@extends(#foo);
@extends(.baz);
}
}
}

@media (max-width: 1px) {
#media {
@extends(.not);
}
}
</style>

<template>
<div id="container">
<div id="foo">Foo</div>
<div id="bar">Bar</div>
<div id="zot">Zot</div>
<div id="media">Media</div>
</div>
</template>
<script>
Expand Down Expand Up @@ -76,6 +98,11 @@
assertComputed(e.$.zot, '10px', 'padding-top');
});

test('nested styles extended (e.g. @media)', function() {
assertComputed(e.$.media, '10px');
assertComputed(e.$.media, '15px', 'padding-top');
});

});

</script>
Expand Down

0 comments on commit 92ebf2f

Please sign in to comment.