Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes style scoping when elements are stamped inside repeats. #1439

Merged
merged 2 commits into from
Apr 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/lib/template/templatizer.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

// boilerplate code
archetype._notifyPath = this._notifyPathImpl;
archetype._scopeElementClass = this._scopeElementClassImpl;
// boilerplate code
var _constructor = this._constructorImpl;
var ctor = function TemplateInstance(model, host) {
Expand Down Expand Up @@ -208,6 +209,13 @@
this._tryReady();
},

_scopeElementClassImpl: function(node, value) {
var host = this._rootDataHost;
if (host) {
return host._scopeElementClass(node, value);
}
},

stamp: function(model) {
model = model || {};
if (this._parentProps) {
Expand Down Expand Up @@ -236,7 +244,7 @@
// },

// stamp: function(model) {
// return new this.ctor(model, this.host);
// return new this.ctor(model, this.dataHost);
// }


Expand Down
37 changes: 3 additions & 34 deletions src/mini/ready.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
(function() {

var baseAttachedCallback = Polymer.Base.attachedCallback;
var baseDetachedCallback = Polymer.Base.detachedCallback;

Polymer.Base._addFeature({

Expand All @@ -42,23 +41,6 @@
ready: function() {
},

/**
Returns the host of the local dom in which this element exists.
This is a shorthand for Polymer.dom(this).getOwnerRoot().host
*/
get host() {
return this._host || (this._host = this._queryHost());
},

set host(value) {
this._host = value;
},

_queryHost: function(node) {
var ownerRoot = Polymer.dom(this).getOwnerRoot();
return ownerRoot && ownerRoot.host;
},

// NOTE: The concept of 'host' is overloaded. There are two different
// notions:
// 1. an element hosts the elements in its local dom root.
Expand All @@ -73,16 +55,13 @@
// 2. establish this element as the current hosting element (allows
// any elements we stamp to easily set host to us).
_pushHost: function(host) {
// this.host reflects the parent element whose "local DOM"
// this element is contained within. This may change if the element
// is appended to a different parent.
this.host = host = host ||
// NOTE: The `dataHost` of an element never changes.
this.dataHost = host = host ||
Polymer.Base.hostStack[Polymer.Base.hostStack.length-1];
// this.dataHost reflects the parent element who manages
// any bindings for the element. Only elements originally
// stamped from Polymer templates have a dataHost, and this
// never changes
this.dataHost = host;
if (host && host._clients) {
host._clients.push(this);
}
Expand Down Expand Up @@ -138,8 +117,8 @@
// if (!Polymer.Settings.useNativeCustomElements) {
// CustomElements.takeRecords();
// }
//
this._clientsReadied = true;
this._clients = null;
},

// mark readied and call `ready`
Expand All @@ -151,10 +130,6 @@
this._attachedPending = false;
this.attachedCallback();
}
// reset _host as it needs to be established by local dom after data
// configuration
this.host = null;
this._clients = null;
},

// for system overriding
Expand All @@ -168,12 +143,6 @@
} else {
this._attachedPending = true;
}
},

detachedCallback: function() {
// uncache host so it will be calculated again.
this.host = null;
baseDetachedCallback.call(this);
}

});
Expand Down
10 changes: 10 additions & 0 deletions src/mini/shady.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@
this.shadyRoot.host = this;
},

/**
* Return the element whose local dom within which this element
* is contained. This is a shorthand for
* `Polymer.dom(this).getOwnerRoot().host`.
*/
get domHost() {
var root = Polymer.dom(this).getOwnerRoot();
return root && root.host;
},

/**
* Force this element to distribute its children to its local dom.
* A user should call `distributeContent` if distribution has been
Expand Down
26 changes: 13 additions & 13 deletions src/standard/x-styling.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,17 @@
_computeStylePropertiesFromHost: function() {
// TODO(sorvell): experimental feature, global defaults!
var props = {}, styles = [Polymer.StyleDefaults.defaultSheet];
if (this.host) {
var host = this.domHost;
if (host) {
// enable finding styles in hosts without `enableStyleCustomProperties`
if (!this.host._styleProperties) {
this.host._styleProperties = this.host._computeStyleProperties();
if (!host._styleProperties) {
host._styleProperties = host._computeStyleProperties();
}
props = Object.create(this.host._styleProperties);
styles = this.host._styles;
props = Object.create(host._styleProperties);
styles = host._styles;
}
this.simpleMixin(props,
this._customPropertiesFromStyles(styles, this.host));
this._customPropertiesFromStyles(styles, host));
return props;

},
Expand Down Expand Up @@ -271,11 +272,9 @@
serializeValueToAttribute: function(value, attribute, node) {
if (attribute === 'class') {
// host needed to scope styling.
var host = node.host;
if (!host) {
var root = Polymer.dom(node).getOwnerRoot();
host = root && root.host;
}
var host = node === this ?
Polymer.dom(this).getOwnerRoot() || this.dataHost :
this;
if (host) {
value = host._scopeElementClass(node, value);
}
Expand All @@ -289,8 +288,9 @@
},

updateHostStyles: function() {
if (this.host) {
this.host.updateStyles();
var host = Polymer.dom(this).getOwnerRoot() || this.dataHost;
if (host) {
host.updateStyles();
} else {
this._updateRootStyles(document);
}
Expand Down
14 changes: 13 additions & 1 deletion test/unit/attributes-elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
'aria-role': 'button',
title: 'awesome',
'attr-object': {foo: 'bar', nested: {'meaning': 42}, arr: [0, 'foo', true]},
'attr-stupid': false
'attr-stupid': false,
class: 'foo bar baz'
},
properties: {
object: {
Expand Down Expand Up @@ -108,4 +109,15 @@
}
}
});
</script>

<dom-module id="x-compose">
<template>
<x-basic id="basic"></x-basic>
</template>
</dom-module>
<script>
Polymer({
is: 'x-compose'
});
</script>
23 changes: 23 additions & 0 deletions test/unit/attributes.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

<x-basic id="basicDefault"></x-basic>

<x-compose id="compose"></x-compose>

<x-basic id="basicConfigured"
object='{"foo": "bar", "nested": {"meaning": 42}, "arr": [0, "foo", true]}'
array='[0, "foo", true, {"foo": "bar"}]'
Expand Down Expand Up @@ -249,6 +251,27 @@
assert.equal(basicDefault.getAttribute('attr-object'),
JSON.stringify(configuredObject));
assert.equal(basicDefault.hasAttribute('attr-stupid'), false);
assert(basicDefault.classList.contains('foo'));
assert(basicDefault.classList.contains('bar'));
assert(basicDefault.classList.contains('baz'));
});

test('hostAttributes set correctly in composed element', function() {
assert.strictEqual(compose.$.basic.getAttribute('attr1'), 'this is attr 1');
assert.strictEqual(compose.$.basic.getAttribute('attr2'), '42');
assert.strictEqual(compose.$.basic.getAttribute('aria-role'), 'button');
assert.strictEqual(compose.$.basic.getAttribute('title'), 'awesome');
assert.strictEqual(compose.$.basic.title, 'awesome');
assert.equal(compose.$.basic.getAttribute('attr-object'),
JSON.stringify(configuredObject));
assert.equal(compose.$.basic.hasAttribute('attr-stupid'), false);
assert(compose.$.basic.classList.contains('foo'));
assert(compose.$.basic.classList.contains('bar'));
assert(compose.$.basic.classList.contains('baz'));
if (!Polymer.Settings.useNativeShadow) {
assert(compose.$.basic.classList.contains('style-scope'));
assert(compose.$.basic.classList.contains('x-compose'));
}
});

});
Expand Down
Loading