Skip to content
This repository was archived by the owner on Mar 13, 2018. It is now read-only.

Commit 1ef334f

Browse files
committed
- change entered/leftDocumentCallback to entered/leftViewCallback to match spec change
- update tests and make pass with native blink implementation
1 parent 8b2bd94 commit 1ef334f

3 files changed

Lines changed: 54 additions & 60 deletions

File tree

src/Observer.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function inserted(element) {
110110
// TODO(sjmiles): when logging, do work on all custom elements so we can
111111
// track behavior even when callbacks not defined
112112
//console.log('inserted: ', element.localName);
113-
if (element.enteredDocumentCallback || (element.__upgraded__ && logFlags.dom)) {
113+
if (element.enteredViewCallback || (element.__upgraded__ && logFlags.dom)) {
114114
logFlags.dom && console.group('inserted:', element.localName);
115115
if (inDocument(element)) {
116116
element.__inserted = (element.__inserted || 0) + 1;
@@ -122,9 +122,9 @@ function inserted(element) {
122122
if (element.__inserted > 1) {
123123
logFlags.dom && console.warn('inserted:', element.localName,
124124
'insert/remove count:', element.__inserted)
125-
} else if (element.enteredDocumentCallback) {
125+
} else if (element.enteredViewCallback) {
126126
logFlags.dom && console.log('inserted:', element.localName);
127-
element.enteredDocumentCallback();
127+
element.enteredViewCallback();
128128
}
129129
}
130130
logFlags.dom && console.groupEnd();
@@ -141,7 +141,7 @@ function removedNode(node) {
141141
function removed(element) {
142142
// TODO(sjmiles): temporary: do work on all custom elements so we can track
143143
// behavior even when callbacks not defined
144-
if (element.leftDocumentCallback || (element.__upgraded__ && logFlags.dom)) {
144+
if (element.leftViewCallback || (element.__upgraded__ && logFlags.dom)) {
145145
logFlags.dom && console.log('removed:', element.localName);
146146
if (!inDocument(element)) {
147147
element.__inserted = (element.__inserted || 0) - 1;
@@ -153,8 +153,8 @@ function removed(element) {
153153
if (element.__inserted < 0) {
154154
logFlags.dom && console.warn('removed:', element.localName,
155155
'insert/remove count:', element.__inserted)
156-
} else if (element.leftDocumentCallback) {
157-
element.leftDocumentCallback();
156+
} else if (element.leftViewCallback) {
157+
element.leftViewCallback();
158158
}
159159
}
160160
}
@@ -241,16 +241,21 @@ function handler(mutations) {
241241
logFlags.dom && console.groupEnd();
242242
};
243243

244-
var observer = new MutationObserver(handler);
245-
246244
function takeRecords() {
247245
// TODO(sjmiles): ask Raf why we have to call handler ourselves
248246
handler(observer.takeRecords());
249247
}
250248

251249
var forEach = Array.prototype.forEach.call.bind(Array.prototype.forEach);
252250

251+
var observer = new MutationObserver(handler);
252+
253253
function observe(inRoot) {
254+
// TODO(sorvell): delay creation of mutation observer to help with
255+
// IE flakiness
256+
if (!observer) {
257+
observer = new MutationObserver(handler);
258+
}
254259
observer.observe(inRoot, {childList: true, subtree: true});
255260
}
256261

test/js/customElements.js

Lines changed: 40 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
var XBarBarPrototype = Object.create(XBarPrototype);
103103
var XBarBar = document.register('x-barbar', {
104104
prototype: XBarBarPrototype,
105-
extends: 'x-bar'
105+
extends: 'button'
106106
});
107107
var xbarbar = new XBarBar();
108108
work.appendChild(xbarbar).textContent = 'x-barbar';
@@ -113,7 +113,7 @@
113113
var XBarBarBarPrototype = Object.create(XBarBarPrototype);
114114
var XBarBarBar = document.register('x-barbarbar', {
115115
prototype: XBarBarBarPrototype,
116-
extends: 'x-barbar'
116+
extends: 'button'
117117
});
118118
var xbarbarbar = new XBarBarBar();
119119
work.appendChild(xbarbarbar).textContent = 'x-barbarbar';
@@ -139,25 +139,24 @@
139139
this.style.fontSize = '32pt';
140140
};
141141
var XBooBoo = document.register('x-booboo', {
142-
prototype: XBooBooPrototype,
143-
extends: 'x-boo'
142+
prototype: XBooBooPrototype
144143
});
145144
var xbooboo = new XBooBoo();
146145
assert.equal(xbooboo.style.fontStyle, 'italic');
147146
assert.equal(xbooboo.style.fontSize, '32pt');
148147
});
149148

150149

151-
test('document.register [created|enteredDocument|leftDocument]Callbacks in prototype', function(done) {
150+
test('document.register [created|enteredView|leftView]Callbacks in prototype', function(done) {
152151
var ready, inserted, removed;
153152
var XBooPrototype = Object.create(HTMLElement.prototype);
154153
XBooPrototype.createdCallback = function() {
155154
ready = true;
156155
}
157-
XBooPrototype.enteredDocumentCallback = function() {
156+
XBooPrototype.enteredViewCallback = function() {
158157
inserted = true;
159158
}
160-
XBooPrototype.leftDocumentCallback = function() {
159+
XBooPrototype.leftViewCallback = function() {
161160
removed = true;
162161
}
163162
var XBoo = document.register('x-boo-ir', {
@@ -169,45 +168,36 @@
169168
assert(!removed, 'removed must be false [XBoo]');
170169
work.appendChild(xboo);
171170
CustomElements.takeRecords();
172-
setTimeout(function() {
173-
assert(inserted, 'inserted must be true [XBoo]');
174-
work.removeChild(xboo);
175-
CustomElements.takeRecords();
176-
setTimeout(function() {
177-
assert(removed, 'removed must be true [XBoo]');
178-
//
179-
ready = inserted = removed = false;
180-
var XBooBooPrototype = Object.create(XBooPrototype);
181-
XBooBooPrototype.createdCallback = function() {
182-
XBoo.prototype.createdCallback.call(this);
183-
};
184-
XBooBooPrototype.enteredDocumentCallback = function() {
185-
XBoo.prototype.enteredDocumentCallback.call(this);
186-
};
187-
XBooBooPrototype.leftDocumentCallback = function() {
188-
XBoo.prototype.leftDocumentCallback.call(this);
189-
};
190-
var XBooBoo = document.register('x-booboo-ir', {
191-
prototype: XBooBooPrototype,
192-
extends: 'x-boo-ir'
193-
});
194-
var xbooboo = new XBooBoo();
195-
assert(ready, 'ready must be true [XBooBoo]');
196-
assert(!inserted, 'inserted must be false [XBooBoo]');
197-
assert(!removed, 'removed must be false [XBooBoo]');
198-
work.appendChild(xbooboo);
199-
CustomElements.takeRecords();
200-
//setTimeout(function() {
201-
assert(inserted, 'inserted must be true [XBooBoo]');
202-
work.removeChild(xbooboo);
203-
CustomElements.takeRecords();
204-
//setTimeout(function() {
205-
assert(removed, 'removed must be true [XBooBoo]');
206-
done();
207-
//}, 1);
208-
//}, 1);
209-
}, 1);
210-
}, 1);
171+
assert(inserted, 'inserted must be true [XBoo]');
172+
work.removeChild(xboo);
173+
CustomElements.takeRecords();
174+
assert(removed, 'removed must be true [XBoo]');
175+
//
176+
ready = inserted = removed = false;
177+
var XBooBooPrototype = Object.create(XBooPrototype);
178+
XBooBooPrototype.createdCallback = function() {
179+
XBoo.prototype.createdCallback.call(this);
180+
};
181+
XBooBooPrototype.enteredViewCallback = function() {
182+
XBoo.prototype.enteredViewCallback.call(this);
183+
};
184+
XBooBooPrototype.leftViewCallback = function() {
185+
XBoo.prototype.leftViewCallback.call(this);
186+
};
187+
var XBooBoo = document.register('x-booboo-ir', {
188+
prototype: XBooBooPrototype
189+
});
190+
var xbooboo = new XBooBoo();
191+
assert(ready, 'ready must be true [XBooBoo]');
192+
assert(!inserted, 'inserted must be false [XBooBoo]');
193+
assert(!removed, 'removed must be false [XBooBoo]');
194+
work.appendChild(xbooboo);
195+
CustomElements.takeRecords();
196+
assert(inserted, 'inserted must be true [XBooBoo]');
197+
work.removeChild(xbooboo);
198+
CustomElements.takeRecords();
199+
assert(removed, 'removed must be true [XBooBoo]');
200+
done();
211201
});
212202

213203
test('document.register attributeChangedCallback in prototype', function(done) {
@@ -236,11 +226,10 @@
236226
});
237227
var xboo = new XBoo();
238228
work.appendChild(xboo);
239-
setTimeout(function() {
240-
var xboo2 = xboo.cloneNode(true);
241-
assert(xboo2.__ready__, 'clone createdCallback must be called');
242-
done();
243-
}, 0);
229+
CustomElements.takeRecords();
230+
var xboo2 = xboo.cloneNode(true);
231+
assert(xboo2.__ready__, 'clone createdCallback must be called');
232+
done();
244233
});
245234
});
246235

test/js/upgrade.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ suite('upgradeElements', function() {
8484
var XSpecialInputProto = Object.create(XInput.prototype);
8585
XSpecialInputProto.xSpecialInput = 'xSpecialInput';
8686
var XSpecialInput = document.register('x-special-input', {
87-
extends: 'x-input',
87+
extends: 'input',
8888
prototype: XSpecialInputProto
8989
});
9090
work.innerHTML = '<input is="x-special-input">';

0 commit comments

Comments
 (0)