Skip to content

Commit

Permalink
automatically scope styles on elements added/removed from localDom.
Browse files Browse the repository at this point in the history
  • Loading branch information
sorvell committed Feb 28, 2015
1 parent 5618821 commit 92b8dc7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/features/mini/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,13 @@
node = node || this;
var list = node.querySelectorAll(selector);
return list;
}
},

// system override point
_elementAdd: function() {},

// system override point
_elementRemove: function() {}

});

Expand Down Expand Up @@ -440,6 +446,7 @@
},

appendChild: function(node, container) {
this.host._elementAdd(node);
container = container || this.node;
if (this.host._useContent) {
saveLightChildrenIfNeeded(container);
Expand Down Expand Up @@ -475,6 +482,7 @@
insertBefore: function(node, ref_node, container) {
container = container || this.node;
if (ref_node) {
this.host._elementAdd(node);
if (this.host._useContent) {
saveLightChildrenIfNeeded(container);
var children = this.children(container);
Expand Down Expand Up @@ -520,6 +528,7 @@
This method also performs dom composition.
*/
removeChild: function(node, container) {
this.host._elementRemove(node);
container = container || this.node;
if (this.host._useContent) {
var children = this.children(container);
Expand Down
14 changes: 14 additions & 0 deletions src/features/standard/styling.html
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,20 @@
transformer.host(this, this.is);
}
stampTemplate.call(this);
},

// add scoping class whenever an element is added to localDOM
_elementAdd: function(node) {
if (this._encapsulateStyle && !node.__styleScoped) {
transformer.dom(node, this.is);
}
},

// remove scoping class whenever an element is removed from localDOM
_elementRemove: function(node) {
if (this._encapsulateStyle) {
transformer.dom(node, '');
}
}

});
Expand Down
8 changes: 6 additions & 2 deletions src/lib/style-transformer.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@
// Given a node and scope name, add a scoping class to each node
// in the tree. This facilitates transforming css into scoped rules.
function transformDom(node, scope) {
_transformDom(node, scope + SCOPE_SUFFIX);
_transformDom(node, scope ? scope + SCOPE_SUFFIX : '');
}

function _transformDom(node, selector) {
if (node.classList) {
node.classList.add(selector);
node.className = node.className.replace(SCOPING_CLASS, '');
if (selector) {
node.classList.add(selector);
}
}
// NOTE: it'd be better to use *Element* but Safari does not support
// this api for document fragments.
Expand Down Expand Up @@ -141,6 +144,7 @@
var SCOPE_JUMP = /\:\:content|\:\:shadow|\/deep\//;
var CLASS_PREFIX = '.';
var PSEUDO_PREFIX = ':';
var SCOPING_CLASS = /(?:^|\s)([\S]*?-x)(?:$|\s)/;

// exports
return {
Expand Down
1 change: 1 addition & 0 deletions src/lib/template/x-repeat.html
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@
var beforeNode = beforeRow ? beforeRow._children[0] : this;
var parentNode = this.lightDom.elementParent();
if (this._domTarget) {
row.root.__styleScoped = true;
this._domTarget.insertBefore(row.root, beforeNode, parentNode);
} else {
parentNode.insertBefore(row.root, beforeNode);
Expand Down
9 changes: 9 additions & 0 deletions test/unit/scoped-styling.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@
test('/deep/ selectors', function() {
assertComputed(styled.$.child.$.deep, '8px');
});

test('dynamically added elements', function() {
var d = document.createElement('div');
d.classList.add('scoped');
styled.localDom.appendChild(d);
assertComputed(d, '4px');
styled.localDom.removeChild(d);
assert.equal(d.className, 'scoped');
});

});

Expand Down

0 comments on commit 92b8dc7

Please sign in to comment.