Skip to content

Commit

Permalink
use ShadyDOM whenever localDom is required; update shadow feature to …
Browse files Browse the repository at this point in the history
…match api changes; add shadow feature tests
  • Loading branch information
sorvell committed Jan 16, 2015
1 parent 7d19f13 commit a68e2fc
Show file tree
Hide file tree
Showing 6 changed files with 354 additions and 322 deletions.
54 changes: 24 additions & 30 deletions src/features/mini/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,34 @@
Base.addFeature({

_prepContent: function() {
// Use this system iff projection is needed, otherwise localDom
// === composed DOM.
// TODO(sorvell): we should use ShadyDOM whenever there's a template
// not just when there's an a priori content. We need to measure the
// perf impact of this.
// This impacts https://github.com/Polymer/polymer/issues/1078.
this._useContent = this._useContent ||
Boolean(this._template &&
this._template.content.querySelector('content'));
// Use this system iff localDom is needed.
this._useContent = this._useContent || Boolean(this._template);
},

// called as part of content initialization, prior to template stamping
_poolContent: function() {
if (this._useContent) {
// capture lightChildren to help reify dom scoping
saveLightChildrenIfNeeded(this);
this.lightDom = new DomRoot(this, this);
}
},

// called as part of content initialization, after template stamping
_setupRoot: function() {
if (this._useContent) {
// ensure shadyRoot exists
this.shadyRoot = this.root;
this.shadyRoot.host = this;
this.localDom = new DomRoot(this.shadyRoot, this);
this.root = this;
this._createLocalRoot();
} else {
var hasLocalDom = (this._template);
var defRoot = new DomRoot(this, this);
var nullRoot = new DomRoot(nullFragment, this);
nullRoot.emptyRoot = true;
this.localDom = hasLocalDom ? defRoot : nullRoot;
this.lightDom = !hasLocalDom ? defRoot : nullRoot;
this.localDom = new Base.DomRoot(nullFragment, this);
this.localDom.emptyRoot = true;
}
this.lightDom = new Base.DomRoot(this, this);
},

_createLocalRoot: function() {
this.shadyRoot = this.root;
this.shadyRoot.host = this;
this.localDom = new Base.DomRoot(this.shadyRoot, this);
this.root = this;
},

distributeContent: function() {
Expand Down Expand Up @@ -361,17 +353,17 @@
p.oMatchesSelector || p.webkitMatchesSelector;


var DomRoot = function(node, host) {
Base.DomRoot = function(node, host) {
//saveLightChildrenIfNeeded(node);
this.node = node;
this.host = host;
this.host = host || Base;
};

// TODO(sorvell): the api here does not cover the case of inserting an
// element into an insertion point parent. We need api of this type to
// handle that case appendChildTo(parent, node)
// where node goes into parent.lightChildren iff that exists.
DomRoot.prototype = {
Base.DomRoot.prototype = {

domRoot: true,

Expand Down Expand Up @@ -485,20 +477,20 @@
}
},

querySelector: function(selector) {
return this.querySelectorAll(selector)[0];
querySelector: function(selector, node) {
return this.querySelectorAll(selector, node)[0];
},

querySelectorAll: function(selector) {
querySelectorAll: function(selector, node) {
var self = this;
return this._query(function(n) {
return self.host.elementMatches(selector, n);
});
}, node);
},

_query: function(matcher) {
_query: function(matcher, node) {
var list = [];
this._queryElements(this.children(), matcher, list);
this._queryElements(this.children(node), matcher, list);
return list;
},

Expand All @@ -521,6 +513,8 @@

var nullFragment = document.createDocumentFragment();

Polymer.dom = new Base.DomRoot(document.body);

});

</script>
81 changes: 42 additions & 39 deletions src/features/more/shadow.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,22 @@
*/

Base.addFeature({
_prepContent: function() {
this._useContent = this._useContent || Boolean(this._template);
},

_poolContent: function() {
this.lightDom = new DomRoot(this);
},
// no-op's when ShadowDOM is in use
_poolContent: function() {},

// this should happen only 1x.
distributeContent: function() {
if (this.shadowRoot) {
return;
}
// compose "clients"
var c$ = this._getDistributionClients();
for (var i=0, l= c$.length, c; (i<l) && (c=c$[i]); i++) {
c.distributeContent();
}
// compose self
if (this._useContent) {
this._createShadowRoot();
} else if (this.root !== this) {
this.appendChild(this.root);
this.root = this;
}
},
_prepareContent: function() {},

_composeTree: function() {},

distributeContent: function() {},

_createShadowRoot: function() {
var root = this.createShadowRoot();
this.localDom = new DomRoot(root);
root.appendChild(this.root);
this.root = root;
// create a shadowRoot!
_createLocalRoot: function() {
this.createShadowRoot();
this.localDom = new Base.DomRoot(this.shadowRoot);
this.shadowRoot.appendChild(this.root);
this.root = this.shadowRoot;
},

querySelectorAllComposed: function(selector, node) {
Expand Down Expand Up @@ -76,28 +60,45 @@
});


var DomRoot = function(node, host) {
Base.DomRoot = function(node, host) {
this.node = node;
};

DomRoot.prototype = {
Base.DomRoot.prototype = {

domRoot: true,

get children() {
return this.node.childNodes;
children: function(node) {
node = node || this.node;
return Array.prototope.slice.call(node.childNodes);
},

appendChild: function(node) {
this.node.appendChild(node);
batch: function(fn) {
if (fn) {
fn.call(this.host);
}
},

distribute: function() {},

appendChild: function(node, container) {
container = container || this.node;
container.appendChild(node);
},

insertBefore: function(node, ref_node) {
this.node.insertBefore(node, ref_node);
insertBefore: function(node, ref_node, container) {
container = container || this.node;
container.insertBefore(node, ref_node);
},

removeChild: function(node) {
this.node.removeChild(node);
removeChild: function(node, container) {
container = container || this.node;
container.removeChild(node);
},

elementParent: function(node) {
node = node || this.node;
return node.parentNode;
},

querySelector: function(selector) {
Expand All @@ -112,6 +113,8 @@

};

Polymer.dom = new Base.DomRoot(document.body);

});

</script>
1 change: 1 addition & 0 deletions test/runner.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'unit/ready.html',
'unit/content.html',
'unit/projection.html',
'unit/projection-shadow.html',
'unit/bind.html',
'unit/notify-path.html'
]);
Expand Down
26 changes: 26 additions & 0 deletions test/unit/projection-shadow.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!doctype html>
<!--
@license
Copyright (c) 2014 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
-->
<html>
<head>
<meta charset="utf-8">
<script src="../../../webcomponentsjs/webcomponents.js"></script>
<script src="../../../web-component-tester/browser.js"></script>
<link rel="import" href="../../polymer-shadow.html">
<link rel="import" href="projection-elements.html">
</head>
<body>

<x-test></x-test>

<script src="projection.js"></script>

</body>
</html>
Loading

0 comments on commit a68e2fc

Please sign in to comment.