Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Side by Side Diff: packages/web_components/lib/ShadowDOM.js

Issue 2312183003: Removed Polymer from Observatory deps (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « packages/web_components/lib/README.md ('k') | packages/web_components/lib/ShadowDOM.min.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /**
2 * @license
3 * Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
4 * This code may only be used under the BSD style license found at http://polyme r.github.io/LICENSE.txt
5 * The complete set of authors may be found at http://polymer.github.io/AUTHORS. txt
6 * The complete set of contributors may be found at http://polymer.github.io/CON TRIBUTORS.txt
7 * Code distributed by Google as part of the polymer project is also
8 * subject to an additional IP rights grant found at http://polymer.github.io/PA TENTS.txt
9 */
10 // @version 0.7.21
11 if (typeof WeakMap === "undefined") {
12 (function() {
13 var defineProperty = Object.defineProperty;
14 var counter = Date.now() % 1e9;
15 var WeakMap = function() {
16 this.name = "__st" + (Math.random() * 1e9 >>> 0) + (counter++ + "__");
17 };
18 WeakMap.prototype = {
19 set: function(key, value) {
20 var entry = key[this.name];
21 if (entry && entry[0] === key) entry[1] = value; else defineProperty(key , this.name, {
22 value: [ key, value ],
23 writable: true
24 });
25 return this;
26 },
27 get: function(key) {
28 var entry;
29 return (entry = key[this.name]) && entry[0] === key ? entry[1] : undefin ed;
30 },
31 "delete": function(key) {
32 var entry = key[this.name];
33 if (!entry || entry[0] !== key) return false;
34 entry[0] = entry[1] = undefined;
35 return true;
36 },
37 has: function(key) {
38 var entry = key[this.name];
39 if (!entry) return false;
40 return entry[0] === key;
41 }
42 };
43 window.WeakMap = WeakMap;
44 })();
45 }
46
47 window.ShadowDOMPolyfill = {};
48
49 (function(scope) {
50 "use strict";
51 var constructorTable = new WeakMap();
52 var nativePrototypeTable = new WeakMap();
53 var wrappers = Object.create(null);
54 function detectEval() {
55 if (typeof chrome !== "undefined" && chrome.app && chrome.app.runtime) {
56 return false;
57 }
58 if (navigator.getDeviceStorage) {
59 return false;
60 }
61 try {
62 var f = new Function("return true;");
63 return f();
64 } catch (ex) {
65 return false;
66 }
67 }
68 var hasEval = detectEval();
69 function assert(b) {
70 if (!b) throw new Error("Assertion failed");
71 }
72 var defineProperty = Object.defineProperty;
73 var getOwnPropertyNames = Object.getOwnPropertyNames;
74 var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
75 function mixin(to, from) {
76 var names = getOwnPropertyNames(from);
77 for (var i = 0; i < names.length; i++) {
78 var name = names[i];
79 defineProperty(to, name, getOwnPropertyDescriptor(from, name));
80 }
81 return to;
82 }
83 function mixinStatics(to, from) {
84 var names = getOwnPropertyNames(from);
85 for (var i = 0; i < names.length; i++) {
86 var name = names[i];
87 switch (name) {
88 case "arguments":
89 case "caller":
90 case "length":
91 case "name":
92 case "prototype":
93 case "toString":
94 continue;
95 }
96 defineProperty(to, name, getOwnPropertyDescriptor(from, name));
97 }
98 return to;
99 }
100 function oneOf(object, propertyNames) {
101 for (var i = 0; i < propertyNames.length; i++) {
102 if (propertyNames[i] in object) return propertyNames[i];
103 }
104 }
105 var nonEnumerableDataDescriptor = {
106 value: undefined,
107 configurable: true,
108 enumerable: false,
109 writable: true
110 };
111 function defineNonEnumerableDataProperty(object, name, value) {
112 nonEnumerableDataDescriptor.value = value;
113 defineProperty(object, name, nonEnumerableDataDescriptor);
114 }
115 getOwnPropertyNames(window);
116 function getWrapperConstructor(node, opt_instance) {
117 var nativePrototype = node.__proto__ || Object.getPrototypeOf(node);
118 if (isFirefox) {
119 try {
120 getOwnPropertyNames(nativePrototype);
121 } catch (error) {
122 nativePrototype = nativePrototype.__proto__;
123 }
124 }
125 var wrapperConstructor = constructorTable.get(nativePrototype);
126 if (wrapperConstructor) return wrapperConstructor;
127 var parentWrapperConstructor = getWrapperConstructor(nativePrototype);
128 var GeneratedWrapper = createWrapperConstructor(parentWrapperConstructor);
129 registerInternal(nativePrototype, GeneratedWrapper, opt_instance);
130 return GeneratedWrapper;
131 }
132 function addForwardingProperties(nativePrototype, wrapperPrototype) {
133 installProperty(nativePrototype, wrapperPrototype, true);
134 }
135 function registerInstanceProperties(wrapperPrototype, instanceObject) {
136 installProperty(instanceObject, wrapperPrototype, false);
137 }
138 var isFirefox = /Firefox/.test(navigator.userAgent);
139 var dummyDescriptor = {
140 get: function() {},
141 set: function(v) {},
142 configurable: true,
143 enumerable: true
144 };
145 function isEventHandlerName(name) {
146 return /^on[a-z]+$/.test(name);
147 }
148 function isIdentifierName(name) {
149 return /^[a-zA-Z_$][a-zA-Z_$0-9]*$/.test(name);
150 }
151 function getGetter(name) {
152 return hasEval && isIdentifierName(name) ? new Function("return this.__impl4 cf1e782hg__." + name) : function() {
153 return this.__impl4cf1e782hg__[name];
154 };
155 }
156 function getSetter(name) {
157 return hasEval && isIdentifierName(name) ? new Function("v", "this.__impl4cf 1e782hg__." + name + " = v") : function(v) {
158 this.__impl4cf1e782hg__[name] = v;
159 };
160 }
161 function getMethod(name) {
162 return hasEval && isIdentifierName(name) ? new Function("return this.__impl4 cf1e782hg__." + name + ".apply(this.__impl4cf1e782hg__, arguments)") : function( ) {
163 return this.__impl4cf1e782hg__[name].apply(this.__impl4cf1e782hg__, argume nts);
164 };
165 }
166 function getDescriptor(source, name) {
167 try {
168 return Object.getOwnPropertyDescriptor(source, name);
169 } catch (ex) {
170 return dummyDescriptor;
171 }
172 }
173 var isBrokenSafari = function() {
174 var descr = Object.getOwnPropertyDescriptor(Node.prototype, "nodeType");
175 return descr && !descr.get && !descr.set;
176 }();
177 function installProperty(source, target, allowMethod, opt_blacklist) {
178 var names = getOwnPropertyNames(source);
179 for (var i = 0; i < names.length; i++) {
180 var name = names[i];
181 if (name === "polymerBlackList_") continue;
182 if (name in target) continue;
183 if (source.polymerBlackList_ && source.polymerBlackList_[name]) continue;
184 if (isFirefox) {
185 source.__lookupGetter__(name);
186 }
187 var descriptor = getDescriptor(source, name);
188 var getter, setter;
189 if (typeof descriptor.value === "function") {
190 if (allowMethod) {
191 target[name] = getMethod(name);
192 }
193 continue;
194 }
195 var isEvent = isEventHandlerName(name);
196 if (isEvent) getter = scope.getEventHandlerGetter(name); else getter = get Getter(name);
197 if (descriptor.writable || descriptor.set || isBrokenSafari) {
198 if (isEvent) setter = scope.getEventHandlerSetter(name); else setter = g etSetter(name);
199 }
200 var configurable = isBrokenSafari || descriptor.configurable;
201 defineProperty(target, name, {
202 get: getter,
203 set: setter,
204 configurable: configurable,
205 enumerable: descriptor.enumerable
206 });
207 }
208 }
209 function register(nativeConstructor, wrapperConstructor, opt_instance) {
210 if (nativeConstructor == null) {
211 return;
212 }
213 var nativePrototype = nativeConstructor.prototype;
214 registerInternal(nativePrototype, wrapperConstructor, opt_instance);
215 mixinStatics(wrapperConstructor, nativeConstructor);
216 }
217 function registerInternal(nativePrototype, wrapperConstructor, opt_instance) {
218 var wrapperPrototype = wrapperConstructor.prototype;
219 assert(constructorTable.get(nativePrototype) === undefined);
220 constructorTable.set(nativePrototype, wrapperConstructor);
221 nativePrototypeTable.set(wrapperPrototype, nativePrototype);
222 addForwardingProperties(nativePrototype, wrapperPrototype);
223 if (opt_instance) registerInstanceProperties(wrapperPrototype, opt_instance) ;
224 defineNonEnumerableDataProperty(wrapperPrototype, "constructor", wrapperCons tructor);
225 wrapperConstructor.prototype = wrapperPrototype;
226 }
227 function isWrapperFor(wrapperConstructor, nativeConstructor) {
228 return constructorTable.get(nativeConstructor.prototype) === wrapperConstruc tor;
229 }
230 function registerObject(object) {
231 var nativePrototype = Object.getPrototypeOf(object);
232 var superWrapperConstructor = getWrapperConstructor(nativePrototype);
233 var GeneratedWrapper = createWrapperConstructor(superWrapperConstructor);
234 registerInternal(nativePrototype, GeneratedWrapper, object);
235 return GeneratedWrapper;
236 }
237 function createWrapperConstructor(superWrapperConstructor) {
238 function GeneratedWrapper(node) {
239 superWrapperConstructor.call(this, node);
240 }
241 var p = Object.create(superWrapperConstructor.prototype);
242 p.constructor = GeneratedWrapper;
243 GeneratedWrapper.prototype = p;
244 return GeneratedWrapper;
245 }
246 function isWrapper(object) {
247 return object && object.__impl4cf1e782hg__;
248 }
249 function isNative(object) {
250 return !isWrapper(object);
251 }
252 function wrap(impl) {
253 if (impl === null) return null;
254 assert(isNative(impl));
255 var wrapper = impl.__wrapper8e3dd93a60__;
256 if (wrapper != null) {
257 return wrapper;
258 }
259 return impl.__wrapper8e3dd93a60__ = new (getWrapperConstructor(impl, impl))( impl);
260 }
261 function unwrap(wrapper) {
262 if (wrapper === null) return null;
263 assert(isWrapper(wrapper));
264 return wrapper.__impl4cf1e782hg__;
265 }
266 function unsafeUnwrap(wrapper) {
267 return wrapper.__impl4cf1e782hg__;
268 }
269 function setWrapper(impl, wrapper) {
270 wrapper.__impl4cf1e782hg__ = impl;
271 impl.__wrapper8e3dd93a60__ = wrapper;
272 }
273 function unwrapIfNeeded(object) {
274 return object && isWrapper(object) ? unwrap(object) : object;
275 }
276 function wrapIfNeeded(object) {
277 return object && !isWrapper(object) ? wrap(object) : object;
278 }
279 function rewrap(node, wrapper) {
280 if (wrapper === null) return;
281 assert(isNative(node));
282 assert(wrapper === undefined || isWrapper(wrapper));
283 node.__wrapper8e3dd93a60__ = wrapper;
284 }
285 var getterDescriptor = {
286 get: undefined,
287 configurable: true,
288 enumerable: true
289 };
290 function defineGetter(constructor, name, getter) {
291 getterDescriptor.get = getter;
292 defineProperty(constructor.prototype, name, getterDescriptor);
293 }
294 function defineWrapGetter(constructor, name) {
295 defineGetter(constructor, name, function() {
296 return wrap(this.__impl4cf1e782hg__[name]);
297 });
298 }
299 function forwardMethodsToWrapper(constructors, names) {
300 constructors.forEach(function(constructor) {
301 names.forEach(function(name) {
302 constructor.prototype[name] = function() {
303 var w = wrapIfNeeded(this);
304 return w[name].apply(w, arguments);
305 };
306 });
307 });
308 }
309 scope.addForwardingProperties = addForwardingProperties;
310 scope.assert = assert;
311 scope.constructorTable = constructorTable;
312 scope.defineGetter = defineGetter;
313 scope.defineWrapGetter = defineWrapGetter;
314 scope.forwardMethodsToWrapper = forwardMethodsToWrapper;
315 scope.isIdentifierName = isIdentifierName;
316 scope.isWrapper = isWrapper;
317 scope.isWrapperFor = isWrapperFor;
318 scope.mixin = mixin;
319 scope.nativePrototypeTable = nativePrototypeTable;
320 scope.oneOf = oneOf;
321 scope.registerObject = registerObject;
322 scope.registerWrapper = register;
323 scope.rewrap = rewrap;
324 scope.setWrapper = setWrapper;
325 scope.unsafeUnwrap = unsafeUnwrap;
326 scope.unwrap = unwrap;
327 scope.unwrapIfNeeded = unwrapIfNeeded;
328 scope.wrap = wrap;
329 scope.wrapIfNeeded = wrapIfNeeded;
330 scope.wrappers = wrappers;
331 })(window.ShadowDOMPolyfill);
332
333 (function(scope) {
334 "use strict";
335 function newSplice(index, removed, addedCount) {
336 return {
337 index: index,
338 removed: removed,
339 addedCount: addedCount
340 };
341 }
342 var EDIT_LEAVE = 0;
343 var EDIT_UPDATE = 1;
344 var EDIT_ADD = 2;
345 var EDIT_DELETE = 3;
346 function ArraySplice() {}
347 ArraySplice.prototype = {
348 calcEditDistances: function(current, currentStart, currentEnd, old, oldStart , oldEnd) {
349 var rowCount = oldEnd - oldStart + 1;
350 var columnCount = currentEnd - currentStart + 1;
351 var distances = new Array(rowCount);
352 for (var i = 0; i < rowCount; i++) {
353 distances[i] = new Array(columnCount);
354 distances[i][0] = i;
355 }
356 for (var j = 0; j < columnCount; j++) distances[0][j] = j;
357 for (var i = 1; i < rowCount; i++) {
358 for (var j = 1; j < columnCount; j++) {
359 if (this.equals(current[currentStart + j - 1], old[oldStart + i - 1])) distances[i][j] = distances[i - 1][j - 1]; else {
360 var north = distances[i - 1][j] + 1;
361 var west = distances[i][j - 1] + 1;
362 distances[i][j] = north < west ? north : west;
363 }
364 }
365 }
366 return distances;
367 },
368 spliceOperationsFromEditDistances: function(distances) {
369 var i = distances.length - 1;
370 var j = distances[0].length - 1;
371 var current = distances[i][j];
372 var edits = [];
373 while (i > 0 || j > 0) {
374 if (i == 0) {
375 edits.push(EDIT_ADD);
376 j--;
377 continue;
378 }
379 if (j == 0) {
380 edits.push(EDIT_DELETE);
381 i--;
382 continue;
383 }
384 var northWest = distances[i - 1][j - 1];
385 var west = distances[i - 1][j];
386 var north = distances[i][j - 1];
387 var min;
388 if (west < north) min = west < northWest ? west : northWest; else min = north < northWest ? north : northWest;
389 if (min == northWest) {
390 if (northWest == current) {
391 edits.push(EDIT_LEAVE);
392 } else {
393 edits.push(EDIT_UPDATE);
394 current = northWest;
395 }
396 i--;
397 j--;
398 } else if (min == west) {
399 edits.push(EDIT_DELETE);
400 i--;
401 current = west;
402 } else {
403 edits.push(EDIT_ADD);
404 j--;
405 current = north;
406 }
407 }
408 edits.reverse();
409 return edits;
410 },
411 calcSplices: function(current, currentStart, currentEnd, old, oldStart, oldE nd) {
412 var prefixCount = 0;
413 var suffixCount = 0;
414 var minLength = Math.min(currentEnd - currentStart, oldEnd - oldStart);
415 if (currentStart == 0 && oldStart == 0) prefixCount = this.sharedPrefix(cu rrent, old, minLength);
416 if (currentEnd == current.length && oldEnd == old.length) suffixCount = th is.sharedSuffix(current, old, minLength - prefixCount);
417 currentStart += prefixCount;
418 oldStart += prefixCount;
419 currentEnd -= suffixCount;
420 oldEnd -= suffixCount;
421 if (currentEnd - currentStart == 0 && oldEnd - oldStart == 0) return [];
422 if (currentStart == currentEnd) {
423 var splice = newSplice(currentStart, [], 0);
424 while (oldStart < oldEnd) splice.removed.push(old[oldStart++]);
425 return [ splice ];
426 } else if (oldStart == oldEnd) return [ newSplice(currentStart, [], curren tEnd - currentStart) ];
427 var ops = this.spliceOperationsFromEditDistances(this.calcEditDistances(cu rrent, currentStart, currentEnd, old, oldStart, oldEnd));
428 var splice = undefined;
429 var splices = [];
430 var index = currentStart;
431 var oldIndex = oldStart;
432 for (var i = 0; i < ops.length; i++) {
433 switch (ops[i]) {
434 case EDIT_LEAVE:
435 if (splice) {
436 splices.push(splice);
437 splice = undefined;
438 }
439 index++;
440 oldIndex++;
441 break;
442
443 case EDIT_UPDATE:
444 if (!splice) splice = newSplice(index, [], 0);
445 splice.addedCount++;
446 index++;
447 splice.removed.push(old[oldIndex]);
448 oldIndex++;
449 break;
450
451 case EDIT_ADD:
452 if (!splice) splice = newSplice(index, [], 0);
453 splice.addedCount++;
454 index++;
455 break;
456
457 case EDIT_DELETE:
458 if (!splice) splice = newSplice(index, [], 0);
459 splice.removed.push(old[oldIndex]);
460 oldIndex++;
461 break;
462 }
463 }
464 if (splice) {
465 splices.push(splice);
466 }
467 return splices;
468 },
469 sharedPrefix: function(current, old, searchLength) {
470 for (var i = 0; i < searchLength; i++) if (!this.equals(current[i], old[i] )) return i;
471 return searchLength;
472 },
473 sharedSuffix: function(current, old, searchLength) {
474 var index1 = current.length;
475 var index2 = old.length;
476 var count = 0;
477 while (count < searchLength && this.equals(current[--index1], old[--index2 ])) count++;
478 return count;
479 },
480 calculateSplices: function(current, previous) {
481 return this.calcSplices(current, 0, current.length, previous, 0, previous. length);
482 },
483 equals: function(currentValue, previousValue) {
484 return currentValue === previousValue;
485 }
486 };
487 scope.ArraySplice = ArraySplice;
488 })(window.ShadowDOMPolyfill);
489
490 (function(context) {
491 "use strict";
492 var OriginalMutationObserver = window.MutationObserver;
493 var callbacks = [];
494 var pending = false;
495 var timerFunc;
496 function handle() {
497 pending = false;
498 var copies = callbacks.slice(0);
499 callbacks = [];
500 for (var i = 0; i < copies.length; i++) {
501 (0, copies[i])();
502 }
503 }
504 if (OriginalMutationObserver) {
505 var counter = 1;
506 var observer = new OriginalMutationObserver(handle);
507 var textNode = document.createTextNode(counter);
508 observer.observe(textNode, {
509 characterData: true
510 });
511 timerFunc = function() {
512 counter = (counter + 1) % 2;
513 textNode.data = counter;
514 };
515 } else {
516 timerFunc = window.setTimeout;
517 }
518 function setEndOfMicrotask(func) {
519 callbacks.push(func);
520 if (pending) return;
521 pending = true;
522 timerFunc(handle, 0);
523 }
524 context.setEndOfMicrotask = setEndOfMicrotask;
525 })(window.ShadowDOMPolyfill);
526
527 (function(scope) {
528 "use strict";
529 var setEndOfMicrotask = scope.setEndOfMicrotask;
530 var wrapIfNeeded = scope.wrapIfNeeded;
531 var wrappers = scope.wrappers;
532 var registrationsTable = new WeakMap();
533 var globalMutationObservers = [];
534 var isScheduled = false;
535 function scheduleCallback(observer) {
536 if (observer.scheduled_) return;
537 observer.scheduled_ = true;
538 globalMutationObservers.push(observer);
539 if (isScheduled) return;
540 setEndOfMicrotask(notifyObservers);
541 isScheduled = true;
542 }
543 function notifyObservers() {
544 isScheduled = false;
545 while (globalMutationObservers.length) {
546 var notifyList = globalMutationObservers;
547 globalMutationObservers = [];
548 notifyList.sort(function(x, y) {
549 return x.uid_ - y.uid_;
550 });
551 for (var i = 0; i < notifyList.length; i++) {
552 var mo = notifyList[i];
553 mo.scheduled_ = false;
554 var queue = mo.takeRecords();
555 removeTransientObserversFor(mo);
556 if (queue.length) {
557 mo.callback_(queue, mo);
558 }
559 }
560 }
561 }
562 function MutationRecord(type, target) {
563 this.type = type;
564 this.target = target;
565 this.addedNodes = new wrappers.NodeList();
566 this.removedNodes = new wrappers.NodeList();
567 this.previousSibling = null;
568 this.nextSibling = null;
569 this.attributeName = null;
570 this.attributeNamespace = null;
571 this.oldValue = null;
572 }
573 function registerTransientObservers(ancestor, node) {
574 for (;ancestor; ancestor = ancestor.parentNode) {
575 var registrations = registrationsTable.get(ancestor);
576 if (!registrations) continue;
577 for (var i = 0; i < registrations.length; i++) {
578 var registration = registrations[i];
579 if (registration.options.subtree) registration.addTransientObserver(node );
580 }
581 }
582 }
583 function removeTransientObserversFor(observer) {
584 for (var i = 0; i < observer.nodes_.length; i++) {
585 var node = observer.nodes_[i];
586 var registrations = registrationsTable.get(node);
587 if (!registrations) return;
588 for (var j = 0; j < registrations.length; j++) {
589 var registration = registrations[j];
590 if (registration.observer === observer) registration.removeTransientObse rvers();
591 }
592 }
593 }
594 function enqueueMutation(target, type, data) {
595 var interestedObservers = Object.create(null);
596 var associatedStrings = Object.create(null);
597 for (var node = target; node; node = node.parentNode) {
598 var registrations = registrationsTable.get(node);
599 if (!registrations) continue;
600 for (var j = 0; j < registrations.length; j++) {
601 var registration = registrations[j];
602 var options = registration.options;
603 if (node !== target && !options.subtree) continue;
604 if (type === "attributes" && !options.attributes) continue;
605 if (type === "attributes" && options.attributeFilter && (data.namespace !== null || options.attributeFilter.indexOf(data.name) === -1)) {
606 continue;
607 }
608 if (type === "characterData" && !options.characterData) continue;
609 if (type === "childList" && !options.childList) continue;
610 var observer = registration.observer;
611 interestedObservers[observer.uid_] = observer;
612 if (type === "attributes" && options.attributeOldValue || type === "char acterData" && options.characterDataOldValue) {
613 associatedStrings[observer.uid_] = data.oldValue;
614 }
615 }
616 }
617 for (var uid in interestedObservers) {
618 var observer = interestedObservers[uid];
619 var record = new MutationRecord(type, target);
620 if ("name" in data && "namespace" in data) {
621 record.attributeName = data.name;
622 record.attributeNamespace = data.namespace;
623 }
624 if (data.addedNodes) record.addedNodes = data.addedNodes;
625 if (data.removedNodes) record.removedNodes = data.removedNodes;
626 if (data.previousSibling) record.previousSibling = data.previousSibling;
627 if (data.nextSibling) record.nextSibling = data.nextSibling;
628 if (associatedStrings[uid] !== undefined) record.oldValue = associatedStri ngs[uid];
629 scheduleCallback(observer);
630 observer.records_.push(record);
631 }
632 }
633 var slice = Array.prototype.slice;
634 function MutationObserverOptions(options) {
635 this.childList = !!options.childList;
636 this.subtree = !!options.subtree;
637 if (!("attributes" in options) && ("attributeOldValue" in options || "attrib uteFilter" in options)) {
638 this.attributes = true;
639 } else {
640 this.attributes = !!options.attributes;
641 }
642 if ("characterDataOldValue" in options && !("characterData" in options)) thi s.characterData = true; else this.characterData = !!options.characterData;
643 if (!this.attributes && (options.attributeOldValue || "attributeFilter" in o ptions) || !this.characterData && options.characterDataOldValue) {
644 throw new TypeError();
645 }
646 this.characterData = !!options.characterData;
647 this.attributeOldValue = !!options.attributeOldValue;
648 this.characterDataOldValue = !!options.characterDataOldValue;
649 if ("attributeFilter" in options) {
650 if (options.attributeFilter == null || typeof options.attributeFilter !== "object") {
651 throw new TypeError();
652 }
653 this.attributeFilter = slice.call(options.attributeFilter);
654 } else {
655 this.attributeFilter = null;
656 }
657 }
658 var uidCounter = 0;
659 function MutationObserver(callback) {
660 this.callback_ = callback;
661 this.nodes_ = [];
662 this.records_ = [];
663 this.uid_ = ++uidCounter;
664 this.scheduled_ = false;
665 }
666 MutationObserver.prototype = {
667 constructor: MutationObserver,
668 observe: function(target, options) {
669 target = wrapIfNeeded(target);
670 var newOptions = new MutationObserverOptions(options);
671 var registration;
672 var registrations = registrationsTable.get(target);
673 if (!registrations) registrationsTable.set(target, registrations = []);
674 for (var i = 0; i < registrations.length; i++) {
675 if (registrations[i].observer === this) {
676 registration = registrations[i];
677 registration.removeTransientObservers();
678 registration.options = newOptions;
679 }
680 }
681 if (!registration) {
682 registration = new Registration(this, target, newOptions);
683 registrations.push(registration);
684 this.nodes_.push(target);
685 }
686 },
687 disconnect: function() {
688 this.nodes_.forEach(function(node) {
689 var registrations = registrationsTable.get(node);
690 for (var i = 0; i < registrations.length; i++) {
691 var registration = registrations[i];
692 if (registration.observer === this) {
693 registrations.splice(i, 1);
694 break;
695 }
696 }
697 }, this);
698 this.records_ = [];
699 },
700 takeRecords: function() {
701 var copyOfRecords = this.records_;
702 this.records_ = [];
703 return copyOfRecords;
704 }
705 };
706 function Registration(observer, target, options) {
707 this.observer = observer;
708 this.target = target;
709 this.options = options;
710 this.transientObservedNodes = [];
711 }
712 Registration.prototype = {
713 addTransientObserver: function(node) {
714 if (node === this.target) return;
715 scheduleCallback(this.observer);
716 this.transientObservedNodes.push(node);
717 var registrations = registrationsTable.get(node);
718 if (!registrations) registrationsTable.set(node, registrations = []);
719 registrations.push(this);
720 },
721 removeTransientObservers: function() {
722 var transientObservedNodes = this.transientObservedNodes;
723 this.transientObservedNodes = [];
724 for (var i = 0; i < transientObservedNodes.length; i++) {
725 var node = transientObservedNodes[i];
726 var registrations = registrationsTable.get(node);
727 for (var j = 0; j < registrations.length; j++) {
728 if (registrations[j] === this) {
729 registrations.splice(j, 1);
730 break;
731 }
732 }
733 }
734 }
735 };
736 scope.enqueueMutation = enqueueMutation;
737 scope.registerTransientObservers = registerTransientObservers;
738 scope.wrappers.MutationObserver = MutationObserver;
739 scope.wrappers.MutationRecord = MutationRecord;
740 })(window.ShadowDOMPolyfill);
741
742 (function(scope) {
743 "use strict";
744 function TreeScope(root, parent) {
745 this.root = root;
746 this.parent = parent;
747 }
748 TreeScope.prototype = {
749 get renderer() {
750 if (this.root instanceof scope.wrappers.ShadowRoot) {
751 return scope.getRendererForHost(this.root.host);
752 }
753 return null;
754 },
755 contains: function(treeScope) {
756 for (;treeScope; treeScope = treeScope.parent) {
757 if (treeScope === this) return true;
758 }
759 return false;
760 }
761 };
762 function setTreeScope(node, treeScope) {
763 if (node.treeScope_ !== treeScope) {
764 node.treeScope_ = treeScope;
765 for (var sr = node.shadowRoot; sr; sr = sr.olderShadowRoot) {
766 sr.treeScope_.parent = treeScope;
767 }
768 for (var child = node.firstChild; child; child = child.nextSibling) {
769 setTreeScope(child, treeScope);
770 }
771 }
772 }
773 function getTreeScope(node) {
774 if (node instanceof scope.wrappers.Window) {
775 debugger;
776 }
777 if (node.treeScope_) return node.treeScope_;
778 var parent = node.parentNode;
779 var treeScope;
780 if (parent) treeScope = getTreeScope(parent); else treeScope = new TreeScope (node, null);
781 return node.treeScope_ = treeScope;
782 }
783 scope.TreeScope = TreeScope;
784 scope.getTreeScope = getTreeScope;
785 scope.setTreeScope = setTreeScope;
786 })(window.ShadowDOMPolyfill);
787
788 (function(scope) {
789 "use strict";
790 var forwardMethodsToWrapper = scope.forwardMethodsToWrapper;
791 var getTreeScope = scope.getTreeScope;
792 var mixin = scope.mixin;
793 var registerWrapper = scope.registerWrapper;
794 var setWrapper = scope.setWrapper;
795 var unsafeUnwrap = scope.unsafeUnwrap;
796 var unwrap = scope.unwrap;
797 var wrap = scope.wrap;
798 var wrappers = scope.wrappers;
799 var wrappedFuns = new WeakMap();
800 var listenersTable = new WeakMap();
801 var handledEventsTable = new WeakMap();
802 var currentlyDispatchingEvents = new WeakMap();
803 var targetTable = new WeakMap();
804 var currentTargetTable = new WeakMap();
805 var relatedTargetTable = new WeakMap();
806 var eventPhaseTable = new WeakMap();
807 var stopPropagationTable = new WeakMap();
808 var stopImmediatePropagationTable = new WeakMap();
809 var eventHandlersTable = new WeakMap();
810 var eventPathTable = new WeakMap();
811 function isShadowRoot(node) {
812 return node instanceof wrappers.ShadowRoot;
813 }
814 function rootOfNode(node) {
815 return getTreeScope(node).root;
816 }
817 function getEventPath(node, event) {
818 var path = [];
819 var current = node;
820 path.push(current);
821 while (current) {
822 var destinationInsertionPoints = getDestinationInsertionPoints(current);
823 if (destinationInsertionPoints && destinationInsertionPoints.length > 0) {
824 for (var i = 0; i < destinationInsertionPoints.length; i++) {
825 var insertionPoint = destinationInsertionPoints[i];
826 if (isShadowInsertionPoint(insertionPoint)) {
827 var shadowRoot = rootOfNode(insertionPoint);
828 var olderShadowRoot = shadowRoot.olderShadowRoot;
829 if (olderShadowRoot) path.push(olderShadowRoot);
830 }
831 path.push(insertionPoint);
832 }
833 current = destinationInsertionPoints[destinationInsertionPoints.length - 1];
834 } else {
835 if (isShadowRoot(current)) {
836 if (inSameTree(node, current) && eventMustBeStopped(event)) {
837 break;
838 }
839 current = current.host;
840 path.push(current);
841 } else {
842 current = current.parentNode;
843 if (current) path.push(current);
844 }
845 }
846 }
847 return path;
848 }
849 function eventMustBeStopped(event) {
850 if (!event) return false;
851 switch (event.type) {
852 case "abort":
853 case "error":
854 case "select":
855 case "change":
856 case "load":
857 case "reset":
858 case "resize":
859 case "scroll":
860 case "selectstart":
861 return true;
862 }
863 return false;
864 }
865 function isShadowInsertionPoint(node) {
866 return node instanceof HTMLShadowElement;
867 }
868 function getDestinationInsertionPoints(node) {
869 return scope.getDestinationInsertionPoints(node);
870 }
871 function eventRetargetting(path, currentTarget) {
872 if (path.length === 0) return currentTarget;
873 if (currentTarget instanceof wrappers.Window) currentTarget = currentTarget. document;
874 var currentTargetTree = getTreeScope(currentTarget);
875 var originalTarget = path[0];
876 var originalTargetTree = getTreeScope(originalTarget);
877 var relativeTargetTree = lowestCommonInclusiveAncestor(currentTargetTree, or iginalTargetTree);
878 for (var i = 0; i < path.length; i++) {
879 var node = path[i];
880 if (getTreeScope(node) === relativeTargetTree) return node;
881 }
882 return path[path.length - 1];
883 }
884 function getTreeScopeAncestors(treeScope) {
885 var ancestors = [];
886 for (;treeScope; treeScope = treeScope.parent) {
887 ancestors.push(treeScope);
888 }
889 return ancestors;
890 }
891 function lowestCommonInclusiveAncestor(tsA, tsB) {
892 var ancestorsA = getTreeScopeAncestors(tsA);
893 var ancestorsB = getTreeScopeAncestors(tsB);
894 var result = null;
895 while (ancestorsA.length > 0 && ancestorsB.length > 0) {
896 var a = ancestorsA.pop();
897 var b = ancestorsB.pop();
898 if (a === b) result = a; else break;
899 }
900 return result;
901 }
902 function getTreeScopeRoot(ts) {
903 if (!ts.parent) return ts;
904 return getTreeScopeRoot(ts.parent);
905 }
906 function relatedTargetResolution(event, currentTarget, relatedTarget) {
907 if (currentTarget instanceof wrappers.Window) currentTarget = currentTarget. document;
908 var currentTargetTree = getTreeScope(currentTarget);
909 var relatedTargetTree = getTreeScope(relatedTarget);
910 var relatedTargetEventPath = getEventPath(relatedTarget, event);
911 var lowestCommonAncestorTree;
912 var lowestCommonAncestorTree = lowestCommonInclusiveAncestor(currentTargetTr ee, relatedTargetTree);
913 if (!lowestCommonAncestorTree) lowestCommonAncestorTree = relatedTargetTree. root;
914 for (var commonAncestorTree = lowestCommonAncestorTree; commonAncestorTree; commonAncestorTree = commonAncestorTree.parent) {
915 var adjustedRelatedTarget;
916 for (var i = 0; i < relatedTargetEventPath.length; i++) {
917 var node = relatedTargetEventPath[i];
918 if (getTreeScope(node) === commonAncestorTree) return node;
919 }
920 }
921 return null;
922 }
923 function inSameTree(a, b) {
924 return getTreeScope(a) === getTreeScope(b);
925 }
926 var NONE = 0;
927 var CAPTURING_PHASE = 1;
928 var AT_TARGET = 2;
929 var BUBBLING_PHASE = 3;
930 var pendingError;
931 function dispatchOriginalEvent(originalEvent) {
932 if (handledEventsTable.get(originalEvent)) return;
933 handledEventsTable.set(originalEvent, true);
934 dispatchEvent(wrap(originalEvent), wrap(originalEvent.target));
935 if (pendingError) {
936 var err = pendingError;
937 pendingError = null;
938 throw err;
939 }
940 }
941 function isLoadLikeEvent(event) {
942 switch (event.type) {
943 case "load":
944 case "beforeunload":
945 case "unload":
946 return true;
947 }
948 return false;
949 }
950 function dispatchEvent(event, originalWrapperTarget) {
951 if (currentlyDispatchingEvents.get(event)) throw new Error("InvalidStateErro r");
952 currentlyDispatchingEvents.set(event, true);
953 scope.renderAllPending();
954 var eventPath;
955 var overrideTarget;
956 var win;
957 if (isLoadLikeEvent(event) && !event.bubbles) {
958 var doc = originalWrapperTarget;
959 if (doc instanceof wrappers.Document && (win = doc.defaultView)) {
960 overrideTarget = doc;
961 eventPath = [];
962 }
963 }
964 if (!eventPath) {
965 if (originalWrapperTarget instanceof wrappers.Window) {
966 win = originalWrapperTarget;
967 eventPath = [];
968 } else {
969 eventPath = getEventPath(originalWrapperTarget, event);
970 if (!isLoadLikeEvent(event)) {
971 var doc = eventPath[eventPath.length - 1];
972 if (doc instanceof wrappers.Document) win = doc.defaultView;
973 }
974 }
975 }
976 eventPathTable.set(event, eventPath);
977 if (dispatchCapturing(event, eventPath, win, overrideTarget)) {
978 if (dispatchAtTarget(event, eventPath, win, overrideTarget)) {
979 dispatchBubbling(event, eventPath, win, overrideTarget);
980 }
981 }
982 eventPhaseTable.set(event, NONE);
983 currentTargetTable.delete(event, null);
984 currentlyDispatchingEvents.delete(event);
985 return event.defaultPrevented;
986 }
987 function dispatchCapturing(event, eventPath, win, overrideTarget) {
988 var phase = CAPTURING_PHASE;
989 if (win) {
990 if (!invoke(win, event, phase, eventPath, overrideTarget)) return false;
991 }
992 for (var i = eventPath.length - 1; i > 0; i--) {
993 if (!invoke(eventPath[i], event, phase, eventPath, overrideTarget)) return false;
994 }
995 return true;
996 }
997 function dispatchAtTarget(event, eventPath, win, overrideTarget) {
998 var phase = AT_TARGET;
999 var currentTarget = eventPath[0] || win;
1000 return invoke(currentTarget, event, phase, eventPath, overrideTarget);
1001 }
1002 function dispatchBubbling(event, eventPath, win, overrideTarget) {
1003 var phase = BUBBLING_PHASE;
1004 for (var i = 1; i < eventPath.length; i++) {
1005 if (!invoke(eventPath[i], event, phase, eventPath, overrideTarget)) return ;
1006 }
1007 if (win && eventPath.length > 0) {
1008 invoke(win, event, phase, eventPath, overrideTarget);
1009 }
1010 }
1011 function invoke(currentTarget, event, phase, eventPath, overrideTarget) {
1012 var listeners = listenersTable.get(currentTarget);
1013 if (!listeners) return true;
1014 var target = overrideTarget || eventRetargetting(eventPath, currentTarget);
1015 if (target === currentTarget) {
1016 if (phase === CAPTURING_PHASE) return true;
1017 if (phase === BUBBLING_PHASE) phase = AT_TARGET;
1018 } else if (phase === BUBBLING_PHASE && !event.bubbles) {
1019 return true;
1020 }
1021 if ("relatedTarget" in event) {
1022 var originalEvent = unwrap(event);
1023 var unwrappedRelatedTarget = originalEvent.relatedTarget;
1024 if (unwrappedRelatedTarget) {
1025 if (unwrappedRelatedTarget instanceof Object && unwrappedRelatedTarget.a ddEventListener) {
1026 var relatedTarget = wrap(unwrappedRelatedTarget);
1027 var adjusted = relatedTargetResolution(event, currentTarget, relatedTa rget);
1028 if (adjusted === target) return true;
1029 } else {
1030 adjusted = null;
1031 }
1032 relatedTargetTable.set(event, adjusted);
1033 }
1034 }
1035 eventPhaseTable.set(event, phase);
1036 var type = event.type;
1037 var anyRemoved = false;
1038 targetTable.set(event, target);
1039 currentTargetTable.set(event, currentTarget);
1040 listeners.depth++;
1041 for (var i = 0, len = listeners.length; i < len; i++) {
1042 var listener = listeners[i];
1043 if (listener.removed) {
1044 anyRemoved = true;
1045 continue;
1046 }
1047 if (listener.type !== type || !listener.capture && phase === CAPTURING_PHA SE || listener.capture && phase === BUBBLING_PHASE) {
1048 continue;
1049 }
1050 try {
1051 if (typeof listener.handler === "function") listener.handler.call(curren tTarget, event); else listener.handler.handleEvent(event);
1052 if (stopImmediatePropagationTable.get(event)) return false;
1053 } catch (ex) {
1054 if (!pendingError) pendingError = ex;
1055 }
1056 }
1057 listeners.depth--;
1058 if (anyRemoved && listeners.depth === 0) {
1059 var copy = listeners.slice();
1060 listeners.length = 0;
1061 for (var i = 0; i < copy.length; i++) {
1062 if (!copy[i].removed) listeners.push(copy[i]);
1063 }
1064 }
1065 return !stopPropagationTable.get(event);
1066 }
1067 function Listener(type, handler, capture) {
1068 this.type = type;
1069 this.handler = handler;
1070 this.capture = Boolean(capture);
1071 }
1072 Listener.prototype = {
1073 equals: function(that) {
1074 return this.handler === that.handler && this.type === that.type && this.ca pture === that.capture;
1075 },
1076 get removed() {
1077 return this.handler === null;
1078 },
1079 remove: function() {
1080 this.handler = null;
1081 }
1082 };
1083 var OriginalEvent = window.Event;
1084 OriginalEvent.prototype.polymerBlackList_ = {
1085 returnValue: true,
1086 keyLocation: true
1087 };
1088 function Event(type, options) {
1089 if (type instanceof OriginalEvent) {
1090 var impl = type;
1091 if (!OriginalBeforeUnloadEvent && impl.type === "beforeunload" && !(this i nstanceof BeforeUnloadEvent)) {
1092 return new BeforeUnloadEvent(impl);
1093 }
1094 setWrapper(impl, this);
1095 } else {
1096 return wrap(constructEvent(OriginalEvent, "Event", type, options));
1097 }
1098 }
1099 Event.prototype = {
1100 get target() {
1101 return targetTable.get(this);
1102 },
1103 get currentTarget() {
1104 return currentTargetTable.get(this);
1105 },
1106 get eventPhase() {
1107 return eventPhaseTable.get(this);
1108 },
1109 get path() {
1110 var eventPath = eventPathTable.get(this);
1111 if (!eventPath) return [];
1112 return eventPath.slice();
1113 },
1114 stopPropagation: function() {
1115 stopPropagationTable.set(this, true);
1116 },
1117 stopImmediatePropagation: function() {
1118 stopPropagationTable.set(this, true);
1119 stopImmediatePropagationTable.set(this, true);
1120 }
1121 };
1122 var supportsDefaultPrevented = function() {
1123 var e = document.createEvent("Event");
1124 e.initEvent("test", true, true);
1125 e.preventDefault();
1126 return e.defaultPrevented;
1127 }();
1128 if (!supportsDefaultPrevented) {
1129 Event.prototype.preventDefault = function() {
1130 if (!this.cancelable) return;
1131 unsafeUnwrap(this).preventDefault();
1132 Object.defineProperty(this, "defaultPrevented", {
1133 get: function() {
1134 return true;
1135 },
1136 configurable: true
1137 });
1138 };
1139 }
1140 registerWrapper(OriginalEvent, Event, document.createEvent("Event"));
1141 function unwrapOptions(options) {
1142 if (!options || !options.relatedTarget) return options;
1143 return Object.create(options, {
1144 relatedTarget: {
1145 value: unwrap(options.relatedTarget)
1146 }
1147 });
1148 }
1149 function registerGenericEvent(name, SuperEvent, prototype) {
1150 var OriginalEvent = window[name];
1151 var GenericEvent = function(type, options) {
1152 if (type instanceof OriginalEvent) setWrapper(type, this); else return wra p(constructEvent(OriginalEvent, name, type, options));
1153 };
1154 GenericEvent.prototype = Object.create(SuperEvent.prototype);
1155 if (prototype) mixin(GenericEvent.prototype, prototype);
1156 if (OriginalEvent) {
1157 try {
1158 registerWrapper(OriginalEvent, GenericEvent, new OriginalEvent("temp"));
1159 } catch (ex) {
1160 registerWrapper(OriginalEvent, GenericEvent, document.createEvent(name)) ;
1161 }
1162 }
1163 return GenericEvent;
1164 }
1165 var UIEvent = registerGenericEvent("UIEvent", Event);
1166 var CustomEvent = registerGenericEvent("CustomEvent", Event);
1167 var relatedTargetProto = {
1168 get relatedTarget() {
1169 var relatedTarget = relatedTargetTable.get(this);
1170 if (relatedTarget !== undefined) return relatedTarget;
1171 return wrap(unwrap(this).relatedTarget);
1172 }
1173 };
1174 function getInitFunction(name, relatedTargetIndex) {
1175 return function() {
1176 arguments[relatedTargetIndex] = unwrap(arguments[relatedTargetIndex]);
1177 var impl = unwrap(this);
1178 impl[name].apply(impl, arguments);
1179 };
1180 }
1181 var mouseEventProto = mixin({
1182 initMouseEvent: getInitFunction("initMouseEvent", 14)
1183 }, relatedTargetProto);
1184 var focusEventProto = mixin({
1185 initFocusEvent: getInitFunction("initFocusEvent", 5)
1186 }, relatedTargetProto);
1187 var MouseEvent = registerGenericEvent("MouseEvent", UIEvent, mouseEventProto);
1188 var FocusEvent = registerGenericEvent("FocusEvent", UIEvent, focusEventProto);
1189 var defaultInitDicts = Object.create(null);
1190 var supportsEventConstructors = function() {
1191 try {
1192 new window.FocusEvent("focus");
1193 } catch (ex) {
1194 return false;
1195 }
1196 return true;
1197 }();
1198 function constructEvent(OriginalEvent, name, type, options) {
1199 if (supportsEventConstructors) return new OriginalEvent(type, unwrapOptions( options));
1200 var event = unwrap(document.createEvent(name));
1201 var defaultDict = defaultInitDicts[name];
1202 var args = [ type ];
1203 Object.keys(defaultDict).forEach(function(key) {
1204 var v = options != null && key in options ? options[key] : defaultDict[key ];
1205 if (key === "relatedTarget") v = unwrap(v);
1206 args.push(v);
1207 });
1208 event["init" + name].apply(event, args);
1209 return event;
1210 }
1211 if (!supportsEventConstructors) {
1212 var configureEventConstructor = function(name, initDict, superName) {
1213 if (superName) {
1214 var superDict = defaultInitDicts[superName];
1215 initDict = mixin(mixin({}, superDict), initDict);
1216 }
1217 defaultInitDicts[name] = initDict;
1218 };
1219 configureEventConstructor("Event", {
1220 bubbles: false,
1221 cancelable: false
1222 });
1223 configureEventConstructor("CustomEvent", {
1224 detail: null
1225 }, "Event");
1226 configureEventConstructor("UIEvent", {
1227 view: null,
1228 detail: 0
1229 }, "Event");
1230 configureEventConstructor("MouseEvent", {
1231 screenX: 0,
1232 screenY: 0,
1233 clientX: 0,
1234 clientY: 0,
1235 ctrlKey: false,
1236 altKey: false,
1237 shiftKey: false,
1238 metaKey: false,
1239 button: 0,
1240 relatedTarget: null
1241 }, "UIEvent");
1242 configureEventConstructor("FocusEvent", {
1243 relatedTarget: null
1244 }, "UIEvent");
1245 }
1246 var OriginalBeforeUnloadEvent = window.BeforeUnloadEvent;
1247 function BeforeUnloadEvent(impl) {
1248 Event.call(this, impl);
1249 }
1250 BeforeUnloadEvent.prototype = Object.create(Event.prototype);
1251 mixin(BeforeUnloadEvent.prototype, {
1252 get returnValue() {
1253 return unsafeUnwrap(this).returnValue;
1254 },
1255 set returnValue(v) {
1256 unsafeUnwrap(this).returnValue = v;
1257 }
1258 });
1259 if (OriginalBeforeUnloadEvent) registerWrapper(OriginalBeforeUnloadEvent, Befo reUnloadEvent);
1260 function isValidListener(fun) {
1261 if (typeof fun === "function") return true;
1262 return fun && fun.handleEvent;
1263 }
1264 function isMutationEvent(type) {
1265 switch (type) {
1266 case "DOMAttrModified":
1267 case "DOMAttributeNameChanged":
1268 case "DOMCharacterDataModified":
1269 case "DOMElementNameChanged":
1270 case "DOMNodeInserted":
1271 case "DOMNodeInsertedIntoDocument":
1272 case "DOMNodeRemoved":
1273 case "DOMNodeRemovedFromDocument":
1274 case "DOMSubtreeModified":
1275 return true;
1276 }
1277 return false;
1278 }
1279 var OriginalEventTarget = window.EventTarget;
1280 function EventTarget(impl) {
1281 setWrapper(impl, this);
1282 }
1283 var methodNames = [ "addEventListener", "removeEventListener", "dispatchEvent" ];
1284 [ Node, Window ].forEach(function(constructor) {
1285 var p = constructor.prototype;
1286 methodNames.forEach(function(name) {
1287 Object.defineProperty(p, name + "_", {
1288 value: p[name]
1289 });
1290 });
1291 });
1292 function getTargetToListenAt(wrapper) {
1293 if (wrapper instanceof wrappers.ShadowRoot) wrapper = wrapper.host;
1294 return unwrap(wrapper);
1295 }
1296 EventTarget.prototype = {
1297 addEventListener: function(type, fun, capture) {
1298 if (!isValidListener(fun) || isMutationEvent(type)) return;
1299 var listener = new Listener(type, fun, capture);
1300 var listeners = listenersTable.get(this);
1301 if (!listeners) {
1302 listeners = [];
1303 listeners.depth = 0;
1304 listenersTable.set(this, listeners);
1305 } else {
1306 for (var i = 0; i < listeners.length; i++) {
1307 if (listener.equals(listeners[i])) return;
1308 }
1309 }
1310 listeners.push(listener);
1311 var target = getTargetToListenAt(this);
1312 target.addEventListener_(type, dispatchOriginalEvent, true);
1313 },
1314 removeEventListener: function(type, fun, capture) {
1315 capture = Boolean(capture);
1316 var listeners = listenersTable.get(this);
1317 if (!listeners) return;
1318 var count = 0, found = false;
1319 for (var i = 0; i < listeners.length; i++) {
1320 if (listeners[i].type === type && listeners[i].capture === capture) {
1321 count++;
1322 if (listeners[i].handler === fun) {
1323 found = true;
1324 listeners[i].remove();
1325 }
1326 }
1327 }
1328 if (found && count === 1) {
1329 var target = getTargetToListenAt(this);
1330 target.removeEventListener_(type, dispatchOriginalEvent, true);
1331 }
1332 },
1333 dispatchEvent: function(event) {
1334 var nativeEvent = unwrap(event);
1335 var eventType = nativeEvent.type;
1336 handledEventsTable.set(nativeEvent, false);
1337 scope.renderAllPending();
1338 var tempListener;
1339 if (!hasListenerInAncestors(this, eventType)) {
1340 tempListener = function() {};
1341 this.addEventListener(eventType, tempListener, true);
1342 }
1343 try {
1344 return unwrap(this).dispatchEvent_(nativeEvent);
1345 } finally {
1346 if (tempListener) this.removeEventListener(eventType, tempListener, true );
1347 }
1348 }
1349 };
1350 function hasListener(node, type) {
1351 var listeners = listenersTable.get(node);
1352 if (listeners) {
1353 for (var i = 0; i < listeners.length; i++) {
1354 if (!listeners[i].removed && listeners[i].type === type) return true;
1355 }
1356 }
1357 return false;
1358 }
1359 function hasListenerInAncestors(target, type) {
1360 for (var node = unwrap(target); node; node = node.parentNode) {
1361 if (hasListener(wrap(node), type)) return true;
1362 }
1363 return false;
1364 }
1365 if (OriginalEventTarget) registerWrapper(OriginalEventTarget, EventTarget);
1366 function wrapEventTargetMethods(constructors) {
1367 forwardMethodsToWrapper(constructors, methodNames);
1368 }
1369 var originalElementFromPoint = document.elementFromPoint;
1370 function elementFromPoint(self, document, x, y) {
1371 scope.renderAllPending();
1372 var element = wrap(originalElementFromPoint.call(unsafeUnwrap(document), x, y));
1373 if (!element) return null;
1374 var path = getEventPath(element, null);
1375 var idx = path.lastIndexOf(self);
1376 if (idx == -1) return null; else path = path.slice(0, idx);
1377 return eventRetargetting(path, self);
1378 }
1379 function getEventHandlerGetter(name) {
1380 return function() {
1381 var inlineEventHandlers = eventHandlersTable.get(this);
1382 return inlineEventHandlers && inlineEventHandlers[name] && inlineEventHand lers[name].value || null;
1383 };
1384 }
1385 function getEventHandlerSetter(name) {
1386 var eventType = name.slice(2);
1387 return function(value) {
1388 var inlineEventHandlers = eventHandlersTable.get(this);
1389 if (!inlineEventHandlers) {
1390 inlineEventHandlers = Object.create(null);
1391 eventHandlersTable.set(this, inlineEventHandlers);
1392 }
1393 var old = inlineEventHandlers[name];
1394 if (old) this.removeEventListener(eventType, old.wrapped, false);
1395 if (typeof value === "function") {
1396 var wrapped = function(e) {
1397 var rv = value.call(this, e);
1398 if (rv === false) e.preventDefault(); else if (name === "onbeforeunloa d" && typeof rv === "string") e.returnValue = rv;
1399 };
1400 this.addEventListener(eventType, wrapped, false);
1401 inlineEventHandlers[name] = {
1402 value: value,
1403 wrapped: wrapped
1404 };
1405 }
1406 };
1407 }
1408 scope.elementFromPoint = elementFromPoint;
1409 scope.getEventHandlerGetter = getEventHandlerGetter;
1410 scope.getEventHandlerSetter = getEventHandlerSetter;
1411 scope.wrapEventTargetMethods = wrapEventTargetMethods;
1412 scope.wrappers.BeforeUnloadEvent = BeforeUnloadEvent;
1413 scope.wrappers.CustomEvent = CustomEvent;
1414 scope.wrappers.Event = Event;
1415 scope.wrappers.EventTarget = EventTarget;
1416 scope.wrappers.FocusEvent = FocusEvent;
1417 scope.wrappers.MouseEvent = MouseEvent;
1418 scope.wrappers.UIEvent = UIEvent;
1419 })(window.ShadowDOMPolyfill);
1420
1421 (function(scope) {
1422 "use strict";
1423 var UIEvent = scope.wrappers.UIEvent;
1424 var mixin = scope.mixin;
1425 var registerWrapper = scope.registerWrapper;
1426 var setWrapper = scope.setWrapper;
1427 var unsafeUnwrap = scope.unsafeUnwrap;
1428 var wrap = scope.wrap;
1429 var OriginalTouchEvent = window.TouchEvent;
1430 if (!OriginalTouchEvent) return;
1431 var nativeEvent;
1432 try {
1433 nativeEvent = document.createEvent("TouchEvent");
1434 } catch (ex) {
1435 return;
1436 }
1437 var nonEnumDescriptor = {
1438 enumerable: false
1439 };
1440 function nonEnum(obj, prop) {
1441 Object.defineProperty(obj, prop, nonEnumDescriptor);
1442 }
1443 function Touch(impl) {
1444 setWrapper(impl, this);
1445 }
1446 Touch.prototype = {
1447 get target() {
1448 return wrap(unsafeUnwrap(this).target);
1449 }
1450 };
1451 var descr = {
1452 configurable: true,
1453 enumerable: true,
1454 get: null
1455 };
1456 [ "clientX", "clientY", "screenX", "screenY", "pageX", "pageY", "identifier", "webkitRadiusX", "webkitRadiusY", "webkitRotationAngle", "webkitForce" ].forEach (function(name) {
1457 descr.get = function() {
1458 return unsafeUnwrap(this)[name];
1459 };
1460 Object.defineProperty(Touch.prototype, name, descr);
1461 });
1462 function TouchList() {
1463 this.length = 0;
1464 nonEnum(this, "length");
1465 }
1466 TouchList.prototype = {
1467 item: function(index) {
1468 return this[index];
1469 }
1470 };
1471 function wrapTouchList(nativeTouchList) {
1472 var list = new TouchList();
1473 for (var i = 0; i < nativeTouchList.length; i++) {
1474 list[i] = new Touch(nativeTouchList[i]);
1475 }
1476 list.length = i;
1477 return list;
1478 }
1479 function TouchEvent(impl) {
1480 UIEvent.call(this, impl);
1481 }
1482 TouchEvent.prototype = Object.create(UIEvent.prototype);
1483 mixin(TouchEvent.prototype, {
1484 get touches() {
1485 return wrapTouchList(unsafeUnwrap(this).touches);
1486 },
1487 get targetTouches() {
1488 return wrapTouchList(unsafeUnwrap(this).targetTouches);
1489 },
1490 get changedTouches() {
1491 return wrapTouchList(unsafeUnwrap(this).changedTouches);
1492 },
1493 initTouchEvent: function() {
1494 throw new Error("Not implemented");
1495 }
1496 });
1497 registerWrapper(OriginalTouchEvent, TouchEvent, nativeEvent);
1498 scope.wrappers.Touch = Touch;
1499 scope.wrappers.TouchEvent = TouchEvent;
1500 scope.wrappers.TouchList = TouchList;
1501 })(window.ShadowDOMPolyfill);
1502
1503 (function(scope) {
1504 "use strict";
1505 var unsafeUnwrap = scope.unsafeUnwrap;
1506 var wrap = scope.wrap;
1507 var nonEnumDescriptor = {
1508 enumerable: false
1509 };
1510 function nonEnum(obj, prop) {
1511 Object.defineProperty(obj, prop, nonEnumDescriptor);
1512 }
1513 function NodeList() {
1514 this.length = 0;
1515 nonEnum(this, "length");
1516 }
1517 NodeList.prototype = {
1518 item: function(index) {
1519 return this[index];
1520 }
1521 };
1522 nonEnum(NodeList.prototype, "item");
1523 function wrapNodeList(list) {
1524 if (list == null) return list;
1525 var wrapperList = new NodeList();
1526 for (var i = 0, length = list.length; i < length; i++) {
1527 wrapperList[i] = wrap(list[i]);
1528 }
1529 wrapperList.length = length;
1530 return wrapperList;
1531 }
1532 function addWrapNodeListMethod(wrapperConstructor, name) {
1533 wrapperConstructor.prototype[name] = function() {
1534 return wrapNodeList(unsafeUnwrap(this)[name].apply(unsafeUnwrap(this), arg uments));
1535 };
1536 }
1537 scope.wrappers.NodeList = NodeList;
1538 scope.addWrapNodeListMethod = addWrapNodeListMethod;
1539 scope.wrapNodeList = wrapNodeList;
1540 })(window.ShadowDOMPolyfill);
1541
1542 (function(scope) {
1543 "use strict";
1544 scope.wrapHTMLCollection = scope.wrapNodeList;
1545 scope.wrappers.HTMLCollection = scope.wrappers.NodeList;
1546 })(window.ShadowDOMPolyfill);
1547
1548 (function(scope) {
1549 "use strict";
1550 var EventTarget = scope.wrappers.EventTarget;
1551 var NodeList = scope.wrappers.NodeList;
1552 var TreeScope = scope.TreeScope;
1553 var assert = scope.assert;
1554 var defineWrapGetter = scope.defineWrapGetter;
1555 var enqueueMutation = scope.enqueueMutation;
1556 var getTreeScope = scope.getTreeScope;
1557 var isWrapper = scope.isWrapper;
1558 var mixin = scope.mixin;
1559 var registerTransientObservers = scope.registerTransientObservers;
1560 var registerWrapper = scope.registerWrapper;
1561 var setTreeScope = scope.setTreeScope;
1562 var unsafeUnwrap = scope.unsafeUnwrap;
1563 var unwrap = scope.unwrap;
1564 var unwrapIfNeeded = scope.unwrapIfNeeded;
1565 var wrap = scope.wrap;
1566 var wrapIfNeeded = scope.wrapIfNeeded;
1567 var wrappers = scope.wrappers;
1568 function assertIsNodeWrapper(node) {
1569 assert(node instanceof Node);
1570 }
1571 function createOneElementNodeList(node) {
1572 var nodes = new NodeList();
1573 nodes[0] = node;
1574 nodes.length = 1;
1575 return nodes;
1576 }
1577 var surpressMutations = false;
1578 function enqueueRemovalForInsertedNodes(node, parent, nodes) {
1579 enqueueMutation(parent, "childList", {
1580 removedNodes: nodes,
1581 previousSibling: node.previousSibling,
1582 nextSibling: node.nextSibling
1583 });
1584 }
1585 function enqueueRemovalForInsertedDocumentFragment(df, nodes) {
1586 enqueueMutation(df, "childList", {
1587 removedNodes: nodes
1588 });
1589 }
1590 function collectNodes(node, parentNode, previousNode, nextNode) {
1591 if (node instanceof DocumentFragment) {
1592 var nodes = collectNodesForDocumentFragment(node);
1593 surpressMutations = true;
1594 for (var i = nodes.length - 1; i >= 0; i--) {
1595 node.removeChild(nodes[i]);
1596 nodes[i].parentNode_ = parentNode;
1597 }
1598 surpressMutations = false;
1599 for (var i = 0; i < nodes.length; i++) {
1600 nodes[i].previousSibling_ = nodes[i - 1] || previousNode;
1601 nodes[i].nextSibling_ = nodes[i + 1] || nextNode;
1602 }
1603 if (previousNode) previousNode.nextSibling_ = nodes[0];
1604 if (nextNode) nextNode.previousSibling_ = nodes[nodes.length - 1];
1605 return nodes;
1606 }
1607 var nodes = createOneElementNodeList(node);
1608 var oldParent = node.parentNode;
1609 if (oldParent) {
1610 oldParent.removeChild(node);
1611 }
1612 node.parentNode_ = parentNode;
1613 node.previousSibling_ = previousNode;
1614 node.nextSibling_ = nextNode;
1615 if (previousNode) previousNode.nextSibling_ = node;
1616 if (nextNode) nextNode.previousSibling_ = node;
1617 return nodes;
1618 }
1619 function collectNodesNative(node) {
1620 if (node instanceof DocumentFragment) return collectNodesForDocumentFragment (node);
1621 var nodes = createOneElementNodeList(node);
1622 var oldParent = node.parentNode;
1623 if (oldParent) enqueueRemovalForInsertedNodes(node, oldParent, nodes);
1624 return nodes;
1625 }
1626 function collectNodesForDocumentFragment(node) {
1627 var nodes = new NodeList();
1628 var i = 0;
1629 for (var child = node.firstChild; child; child = child.nextSibling) {
1630 nodes[i++] = child;
1631 }
1632 nodes.length = i;
1633 enqueueRemovalForInsertedDocumentFragment(node, nodes);
1634 return nodes;
1635 }
1636 function snapshotNodeList(nodeList) {
1637 return nodeList;
1638 }
1639 function nodeWasAdded(node, treeScope) {
1640 setTreeScope(node, treeScope);
1641 node.nodeIsInserted_();
1642 }
1643 function nodesWereAdded(nodes, parent) {
1644 var treeScope = getTreeScope(parent);
1645 for (var i = 0; i < nodes.length; i++) {
1646 nodeWasAdded(nodes[i], treeScope);
1647 }
1648 }
1649 function nodeWasRemoved(node) {
1650 setTreeScope(node, new TreeScope(node, null));
1651 }
1652 function nodesWereRemoved(nodes) {
1653 for (var i = 0; i < nodes.length; i++) {
1654 nodeWasRemoved(nodes[i]);
1655 }
1656 }
1657 function ensureSameOwnerDocument(parent, child) {
1658 var ownerDoc = parent.nodeType === Node.DOCUMENT_NODE ? parent : parent.owne rDocument;
1659 if (ownerDoc !== child.ownerDocument) ownerDoc.adoptNode(child);
1660 }
1661 function adoptNodesIfNeeded(owner, nodes) {
1662 if (!nodes.length) return;
1663 var ownerDoc = owner.ownerDocument;
1664 if (ownerDoc === nodes[0].ownerDocument) return;
1665 for (var i = 0; i < nodes.length; i++) {
1666 scope.adoptNodeNoRemove(nodes[i], ownerDoc);
1667 }
1668 }
1669 function unwrapNodesForInsertion(owner, nodes) {
1670 adoptNodesIfNeeded(owner, nodes);
1671 var length = nodes.length;
1672 if (length === 1) return unwrap(nodes[0]);
1673 var df = unwrap(owner.ownerDocument.createDocumentFragment());
1674 for (var i = 0; i < length; i++) {
1675 df.appendChild(unwrap(nodes[i]));
1676 }
1677 return df;
1678 }
1679 function clearChildNodes(wrapper) {
1680 if (wrapper.firstChild_ !== undefined) {
1681 var child = wrapper.firstChild_;
1682 while (child) {
1683 var tmp = child;
1684 child = child.nextSibling_;
1685 tmp.parentNode_ = tmp.previousSibling_ = tmp.nextSibling_ = undefined;
1686 }
1687 }
1688 wrapper.firstChild_ = wrapper.lastChild_ = undefined;
1689 }
1690 function removeAllChildNodes(wrapper) {
1691 if (wrapper.invalidateShadowRenderer()) {
1692 var childWrapper = wrapper.firstChild;
1693 while (childWrapper) {
1694 assert(childWrapper.parentNode === wrapper);
1695 var nextSibling = childWrapper.nextSibling;
1696 var childNode = unwrap(childWrapper);
1697 var parentNode = childNode.parentNode;
1698 if (parentNode) originalRemoveChild.call(parentNode, childNode);
1699 childWrapper.previousSibling_ = childWrapper.nextSibling_ = childWrapper .parentNode_ = null;
1700 childWrapper = nextSibling;
1701 }
1702 wrapper.firstChild_ = wrapper.lastChild_ = null;
1703 } else {
1704 var node = unwrap(wrapper);
1705 var child = node.firstChild;
1706 var nextSibling;
1707 while (child) {
1708 nextSibling = child.nextSibling;
1709 originalRemoveChild.call(node, child);
1710 child = nextSibling;
1711 }
1712 }
1713 }
1714 function invalidateParent(node) {
1715 var p = node.parentNode;
1716 return p && p.invalidateShadowRenderer();
1717 }
1718 function cleanupNodes(nodes) {
1719 for (var i = 0, n; i < nodes.length; i++) {
1720 n = nodes[i];
1721 n.parentNode.removeChild(n);
1722 }
1723 }
1724 var originalImportNode = document.importNode;
1725 var originalCloneNode = window.Node.prototype.cloneNode;
1726 function cloneNode(node, deep, opt_doc) {
1727 var clone;
1728 if (opt_doc) clone = wrap(originalImportNode.call(opt_doc, unsafeUnwrap(node ), false)); else clone = wrap(originalCloneNode.call(unsafeUnwrap(node), false)) ;
1729 if (deep) {
1730 for (var child = node.firstChild; child; child = child.nextSibling) {
1731 clone.appendChild(cloneNode(child, true, opt_doc));
1732 }
1733 if (node instanceof wrappers.HTMLTemplateElement) {
1734 var cloneContent = clone.content;
1735 for (var child = node.content.firstChild; child; child = child.nextSibli ng) {
1736 cloneContent.appendChild(cloneNode(child, true, opt_doc));
1737 }
1738 }
1739 }
1740 return clone;
1741 }
1742 function contains(self, child) {
1743 if (!child || getTreeScope(self) !== getTreeScope(child)) return false;
1744 for (var node = child; node; node = node.parentNode) {
1745 if (node === self) return true;
1746 }
1747 return false;
1748 }
1749 var OriginalNode = window.Node;
1750 function Node(original) {
1751 assert(original instanceof OriginalNode);
1752 EventTarget.call(this, original);
1753 this.parentNode_ = undefined;
1754 this.firstChild_ = undefined;
1755 this.lastChild_ = undefined;
1756 this.nextSibling_ = undefined;
1757 this.previousSibling_ = undefined;
1758 this.treeScope_ = undefined;
1759 }
1760 var OriginalDocumentFragment = window.DocumentFragment;
1761 var originalAppendChild = OriginalNode.prototype.appendChild;
1762 var originalCompareDocumentPosition = OriginalNode.prototype.compareDocumentPo sition;
1763 var originalIsEqualNode = OriginalNode.prototype.isEqualNode;
1764 var originalInsertBefore = OriginalNode.prototype.insertBefore;
1765 var originalRemoveChild = OriginalNode.prototype.removeChild;
1766 var originalReplaceChild = OriginalNode.prototype.replaceChild;
1767 var isIEOrEdge = /Trident|Edge/.test(navigator.userAgent);
1768 var removeChildOriginalHelper = isIEOrEdge ? function(parent, child) {
1769 try {
1770 originalRemoveChild.call(parent, child);
1771 } catch (ex) {
1772 if (!(parent instanceof OriginalDocumentFragment)) throw ex;
1773 }
1774 } : function(parent, child) {
1775 originalRemoveChild.call(parent, child);
1776 };
1777 Node.prototype = Object.create(EventTarget.prototype);
1778 mixin(Node.prototype, {
1779 appendChild: function(childWrapper) {
1780 return this.insertBefore(childWrapper, null);
1781 },
1782 insertBefore: function(childWrapper, refWrapper) {
1783 assertIsNodeWrapper(childWrapper);
1784 var refNode;
1785 if (refWrapper) {
1786 if (isWrapper(refWrapper)) {
1787 refNode = unwrap(refWrapper);
1788 } else {
1789 refNode = refWrapper;
1790 refWrapper = wrap(refNode);
1791 }
1792 } else {
1793 refWrapper = null;
1794 refNode = null;
1795 }
1796 refWrapper && assert(refWrapper.parentNode === this);
1797 var nodes;
1798 var previousNode = refWrapper ? refWrapper.previousSibling : this.lastChil d;
1799 var useNative = !this.invalidateShadowRenderer() && !invalidateParent(chil dWrapper);
1800 if (useNative) nodes = collectNodesNative(childWrapper); else nodes = coll ectNodes(childWrapper, this, previousNode, refWrapper);
1801 if (useNative) {
1802 ensureSameOwnerDocument(this, childWrapper);
1803 clearChildNodes(this);
1804 originalInsertBefore.call(unsafeUnwrap(this), unwrap(childWrapper), refN ode);
1805 } else {
1806 if (!previousNode) this.firstChild_ = nodes[0];
1807 if (!refWrapper) {
1808 this.lastChild_ = nodes[nodes.length - 1];
1809 if (this.firstChild_ === undefined) this.firstChild_ = this.firstChild ;
1810 }
1811 var parentNode = refNode ? refNode.parentNode : unsafeUnwrap(this);
1812 if (parentNode) {
1813 originalInsertBefore.call(parentNode, unwrapNodesForInsertion(this, no des), refNode);
1814 } else {
1815 adoptNodesIfNeeded(this, nodes);
1816 }
1817 }
1818 enqueueMutation(this, "childList", {
1819 addedNodes: nodes,
1820 nextSibling: refWrapper,
1821 previousSibling: previousNode
1822 });
1823 nodesWereAdded(nodes, this);
1824 return childWrapper;
1825 },
1826 removeChild: function(childWrapper) {
1827 assertIsNodeWrapper(childWrapper);
1828 if (childWrapper.parentNode !== this) {
1829 var found = false;
1830 var childNodes = this.childNodes;
1831 for (var ieChild = this.firstChild; ieChild; ieChild = ieChild.nextSibli ng) {
1832 if (ieChild === childWrapper) {
1833 found = true;
1834 break;
1835 }
1836 }
1837 if (!found) {
1838 throw new Error("NotFoundError");
1839 }
1840 }
1841 var childNode = unwrap(childWrapper);
1842 var childWrapperNextSibling = childWrapper.nextSibling;
1843 var childWrapperPreviousSibling = childWrapper.previousSibling;
1844 if (this.invalidateShadowRenderer()) {
1845 var thisFirstChild = this.firstChild;
1846 var thisLastChild = this.lastChild;
1847 var parentNode = childNode.parentNode;
1848 if (parentNode) removeChildOriginalHelper(parentNode, childNode);
1849 if (thisFirstChild === childWrapper) this.firstChild_ = childWrapperNext Sibling;
1850 if (thisLastChild === childWrapper) this.lastChild_ = childWrapperPrevio usSibling;
1851 if (childWrapperPreviousSibling) childWrapperPreviousSibling.nextSibling _ = childWrapperNextSibling;
1852 if (childWrapperNextSibling) {
1853 childWrapperNextSibling.previousSibling_ = childWrapperPreviousSibling ;
1854 }
1855 childWrapper.previousSibling_ = childWrapper.nextSibling_ = childWrapper .parentNode_ = undefined;
1856 } else {
1857 clearChildNodes(this);
1858 removeChildOriginalHelper(unsafeUnwrap(this), childNode);
1859 }
1860 if (!surpressMutations) {
1861 enqueueMutation(this, "childList", {
1862 removedNodes: createOneElementNodeList(childWrapper),
1863 nextSibling: childWrapperNextSibling,
1864 previousSibling: childWrapperPreviousSibling
1865 });
1866 }
1867 registerTransientObservers(this, childWrapper);
1868 return childWrapper;
1869 },
1870 replaceChild: function(newChildWrapper, oldChildWrapper) {
1871 assertIsNodeWrapper(newChildWrapper);
1872 var oldChildNode;
1873 if (isWrapper(oldChildWrapper)) {
1874 oldChildNode = unwrap(oldChildWrapper);
1875 } else {
1876 oldChildNode = oldChildWrapper;
1877 oldChildWrapper = wrap(oldChildNode);
1878 }
1879 if (oldChildWrapper.parentNode !== this) {
1880 throw new Error("NotFoundError");
1881 }
1882 var nextNode = oldChildWrapper.nextSibling;
1883 var previousNode = oldChildWrapper.previousSibling;
1884 var nodes;
1885 var useNative = !this.invalidateShadowRenderer() && !invalidateParent(newC hildWrapper);
1886 if (useNative) {
1887 nodes = collectNodesNative(newChildWrapper);
1888 } else {
1889 if (nextNode === newChildWrapper) nextNode = newChildWrapper.nextSibling ;
1890 nodes = collectNodes(newChildWrapper, this, previousNode, nextNode);
1891 }
1892 if (!useNative) {
1893 if (this.firstChild === oldChildWrapper) this.firstChild_ = nodes[0];
1894 if (this.lastChild === oldChildWrapper) this.lastChild_ = nodes[nodes.le ngth - 1];
1895 oldChildWrapper.previousSibling_ = oldChildWrapper.nextSibling_ = oldChi ldWrapper.parentNode_ = undefined;
1896 if (oldChildNode.parentNode) {
1897 originalReplaceChild.call(oldChildNode.parentNode, unwrapNodesForInser tion(this, nodes), oldChildNode);
1898 }
1899 } else {
1900 ensureSameOwnerDocument(this, newChildWrapper);
1901 clearChildNodes(this);
1902 originalReplaceChild.call(unsafeUnwrap(this), unwrap(newChildWrapper), o ldChildNode);
1903 }
1904 enqueueMutation(this, "childList", {
1905 addedNodes: nodes,
1906 removedNodes: createOneElementNodeList(oldChildWrapper),
1907 nextSibling: nextNode,
1908 previousSibling: previousNode
1909 });
1910 nodeWasRemoved(oldChildWrapper);
1911 nodesWereAdded(nodes, this);
1912 return oldChildWrapper;
1913 },
1914 nodeIsInserted_: function() {
1915 for (var child = this.firstChild; child; child = child.nextSibling) {
1916 child.nodeIsInserted_();
1917 }
1918 },
1919 hasChildNodes: function() {
1920 return this.firstChild !== null;
1921 },
1922 get parentNode() {
1923 return this.parentNode_ !== undefined ? this.parentNode_ : wrap(unsafeUnwr ap(this).parentNode);
1924 },
1925 get firstChild() {
1926 return this.firstChild_ !== undefined ? this.firstChild_ : wrap(unsafeUnwr ap(this).firstChild);
1927 },
1928 get lastChild() {
1929 return this.lastChild_ !== undefined ? this.lastChild_ : wrap(unsafeUnwrap (this).lastChild);
1930 },
1931 get nextSibling() {
1932 return this.nextSibling_ !== undefined ? this.nextSibling_ : wrap(unsafeUn wrap(this).nextSibling);
1933 },
1934 get previousSibling() {
1935 return this.previousSibling_ !== undefined ? this.previousSibling_ : wrap( unsafeUnwrap(this).previousSibling);
1936 },
1937 get parentElement() {
1938 var p = this.parentNode;
1939 while (p && p.nodeType !== Node.ELEMENT_NODE) {
1940 p = p.parentNode;
1941 }
1942 return p;
1943 },
1944 get textContent() {
1945 var s = "";
1946 for (var child = this.firstChild; child; child = child.nextSibling) {
1947 if (child.nodeType != Node.COMMENT_NODE) {
1948 s += child.textContent;
1949 }
1950 }
1951 return s;
1952 },
1953 set textContent(textContent) {
1954 if (textContent == null) textContent = "";
1955 var removedNodes = snapshotNodeList(this.childNodes);
1956 if (this.invalidateShadowRenderer()) {
1957 removeAllChildNodes(this);
1958 if (textContent !== "") {
1959 var textNode = unsafeUnwrap(this).ownerDocument.createTextNode(textCon tent);
1960 this.appendChild(textNode);
1961 }
1962 } else {
1963 clearChildNodes(this);
1964 unsafeUnwrap(this).textContent = textContent;
1965 }
1966 var addedNodes = snapshotNodeList(this.childNodes);
1967 enqueueMutation(this, "childList", {
1968 addedNodes: addedNodes,
1969 removedNodes: removedNodes
1970 });
1971 nodesWereRemoved(removedNodes);
1972 nodesWereAdded(addedNodes, this);
1973 },
1974 get childNodes() {
1975 var wrapperList = new NodeList();
1976 var i = 0;
1977 for (var child = this.firstChild; child; child = child.nextSibling) {
1978 wrapperList[i++] = child;
1979 }
1980 wrapperList.length = i;
1981 return wrapperList;
1982 },
1983 cloneNode: function(deep) {
1984 return cloneNode(this, deep);
1985 },
1986 contains: function(child) {
1987 return contains(this, wrapIfNeeded(child));
1988 },
1989 compareDocumentPosition: function(otherNode) {
1990 return originalCompareDocumentPosition.call(unsafeUnwrap(this), unwrapIfNe eded(otherNode));
1991 },
1992 isEqualNode: function(otherNode) {
1993 return originalIsEqualNode.call(unsafeUnwrap(this), unwrapIfNeeded(otherNo de));
1994 },
1995 normalize: function() {
1996 var nodes = snapshotNodeList(this.childNodes);
1997 var remNodes = [];
1998 var s = "";
1999 var modNode;
2000 for (var i = 0, n; i < nodes.length; i++) {
2001 n = nodes[i];
2002 if (n.nodeType === Node.TEXT_NODE) {
2003 if (!modNode && !n.data.length) this.removeChild(n); else if (!modNode ) modNode = n; else {
2004 s += n.data;
2005 remNodes.push(n);
2006 }
2007 } else {
2008 if (modNode && remNodes.length) {
2009 modNode.data += s;
2010 cleanupNodes(remNodes);
2011 }
2012 remNodes = [];
2013 s = "";
2014 modNode = null;
2015 if (n.childNodes.length) n.normalize();
2016 }
2017 }
2018 if (modNode && remNodes.length) {
2019 modNode.data += s;
2020 cleanupNodes(remNodes);
2021 }
2022 }
2023 });
2024 defineWrapGetter(Node, "ownerDocument");
2025 registerWrapper(OriginalNode, Node, document.createDocumentFragment());
2026 delete Node.prototype.querySelector;
2027 delete Node.prototype.querySelectorAll;
2028 Node.prototype = mixin(Object.create(EventTarget.prototype), Node.prototype);
2029 scope.cloneNode = cloneNode;
2030 scope.nodeWasAdded = nodeWasAdded;
2031 scope.nodeWasRemoved = nodeWasRemoved;
2032 scope.nodesWereAdded = nodesWereAdded;
2033 scope.nodesWereRemoved = nodesWereRemoved;
2034 scope.originalInsertBefore = originalInsertBefore;
2035 scope.originalRemoveChild = originalRemoveChild;
2036 scope.snapshotNodeList = snapshotNodeList;
2037 scope.wrappers.Node = Node;
2038 })(window.ShadowDOMPolyfill);
2039
2040 (function(scope) {
2041 "use strict";
2042 var HTMLCollection = scope.wrappers.HTMLCollection;
2043 var NodeList = scope.wrappers.NodeList;
2044 var getTreeScope = scope.getTreeScope;
2045 var unsafeUnwrap = scope.unsafeUnwrap;
2046 var wrap = scope.wrap;
2047 var originalDocumentQuerySelector = document.querySelector;
2048 var originalElementQuerySelector = document.documentElement.querySelector;
2049 var originalDocumentQuerySelectorAll = document.querySelectorAll;
2050 var originalElementQuerySelectorAll = document.documentElement.querySelectorAl l;
2051 var originalDocumentGetElementsByTagName = document.getElementsByTagName;
2052 var originalElementGetElementsByTagName = document.documentElement.getElements ByTagName;
2053 var originalDocumentGetElementsByTagNameNS = document.getElementsByTagNameNS;
2054 var originalElementGetElementsByTagNameNS = document.documentElement.getElemen tsByTagNameNS;
2055 var OriginalElement = window.Element;
2056 var OriginalDocument = window.HTMLDocument || window.Document;
2057 function filterNodeList(list, index, result, deep) {
2058 var wrappedItem = null;
2059 var root = null;
2060 for (var i = 0, length = list.length; i < length; i++) {
2061 wrappedItem = wrap(list[i]);
2062 if (!deep && (root = getTreeScope(wrappedItem).root)) {
2063 if (root instanceof scope.wrappers.ShadowRoot) {
2064 continue;
2065 }
2066 }
2067 result[index++] = wrappedItem;
2068 }
2069 return index;
2070 }
2071 function shimSelector(selector) {
2072 return String(selector).replace(/\/deep\/|::shadow|>>>/g, " ");
2073 }
2074 function shimMatchesSelector(selector) {
2075 return String(selector).replace(/:host\(([^\s]+)\)/g, "$1").replace(/([^\s]) :host/g, "$1").replace(":host", "*").replace(/\^|\/shadow\/|\/shadow-deep\/|::sh adow|\/deep\/|::content|>>>/g, " ");
2076 }
2077 function findOne(node, selector) {
2078 var m, el = node.firstElementChild;
2079 while (el) {
2080 if (el.matches(selector)) return el;
2081 m = findOne(el, selector);
2082 if (m) return m;
2083 el = el.nextElementSibling;
2084 }
2085 return null;
2086 }
2087 function matchesSelector(el, selector) {
2088 return el.matches(selector);
2089 }
2090 var XHTML_NS = "http://www.w3.org/1999/xhtml";
2091 function matchesTagName(el, localName, localNameLowerCase) {
2092 var ln = el.localName;
2093 return ln === localName || ln === localNameLowerCase && el.namespaceURI === XHTML_NS;
2094 }
2095 function matchesEveryThing() {
2096 return true;
2097 }
2098 function matchesLocalNameOnly(el, ns, localName) {
2099 return el.localName === localName;
2100 }
2101 function matchesNameSpace(el, ns) {
2102 return el.namespaceURI === ns;
2103 }
2104 function matchesLocalNameNS(el, ns, localName) {
2105 return el.namespaceURI === ns && el.localName === localName;
2106 }
2107 function findElements(node, index, result, p, arg0, arg1) {
2108 var el = node.firstElementChild;
2109 while (el) {
2110 if (p(el, arg0, arg1)) result[index++] = el;
2111 index = findElements(el, index, result, p, arg0, arg1);
2112 el = el.nextElementSibling;
2113 }
2114 return index;
2115 }
2116 function querySelectorAllFiltered(p, index, result, selector, deep) {
2117 var target = unsafeUnwrap(this);
2118 var list;
2119 var root = getTreeScope(this).root;
2120 if (root instanceof scope.wrappers.ShadowRoot) {
2121 return findElements(this, index, result, p, selector, null);
2122 } else if (target instanceof OriginalElement) {
2123 list = originalElementQuerySelectorAll.call(target, selector);
2124 } else if (target instanceof OriginalDocument) {
2125 list = originalDocumentQuerySelectorAll.call(target, selector);
2126 } else {
2127 return findElements(this, index, result, p, selector, null);
2128 }
2129 return filterNodeList(list, index, result, deep);
2130 }
2131 var SelectorsInterface = {
2132 querySelector: function(selector) {
2133 var shimmed = shimSelector(selector);
2134 var deep = shimmed !== selector;
2135 selector = shimmed;
2136 var target = unsafeUnwrap(this);
2137 var wrappedItem;
2138 var root = getTreeScope(this).root;
2139 if (root instanceof scope.wrappers.ShadowRoot) {
2140 return findOne(this, selector);
2141 } else if (target instanceof OriginalElement) {
2142 wrappedItem = wrap(originalElementQuerySelector.call(target, selector));
2143 } else if (target instanceof OriginalDocument) {
2144 wrappedItem = wrap(originalDocumentQuerySelector.call(target, selector)) ;
2145 } else {
2146 return findOne(this, selector);
2147 }
2148 if (!wrappedItem) {
2149 return wrappedItem;
2150 } else if (!deep && (root = getTreeScope(wrappedItem).root)) {
2151 if (root instanceof scope.wrappers.ShadowRoot) {
2152 return findOne(this, selector);
2153 }
2154 }
2155 return wrappedItem;
2156 },
2157 querySelectorAll: function(selector) {
2158 var shimmed = shimSelector(selector);
2159 var deep = shimmed !== selector;
2160 selector = shimmed;
2161 var result = new NodeList();
2162 result.length = querySelectorAllFiltered.call(this, matchesSelector, 0, re sult, selector, deep);
2163 return result;
2164 }
2165 };
2166 var MatchesInterface = {
2167 matches: function(selector) {
2168 selector = shimMatchesSelector(selector);
2169 return scope.originalMatches.call(unsafeUnwrap(this), selector);
2170 }
2171 };
2172 function getElementsByTagNameFiltered(p, index, result, localName, lowercase) {
2173 var target = unsafeUnwrap(this);
2174 var list;
2175 var root = getTreeScope(this).root;
2176 if (root instanceof scope.wrappers.ShadowRoot) {
2177 return findElements(this, index, result, p, localName, lowercase);
2178 } else if (target instanceof OriginalElement) {
2179 list = originalElementGetElementsByTagName.call(target, localName, lowerca se);
2180 } else if (target instanceof OriginalDocument) {
2181 list = originalDocumentGetElementsByTagName.call(target, localName, lowerc ase);
2182 } else {
2183 return findElements(this, index, result, p, localName, lowercase);
2184 }
2185 return filterNodeList(list, index, result, false);
2186 }
2187 function getElementsByTagNameNSFiltered(p, index, result, ns, localName) {
2188 var target = unsafeUnwrap(this);
2189 var list;
2190 var root = getTreeScope(this).root;
2191 if (root instanceof scope.wrappers.ShadowRoot) {
2192 return findElements(this, index, result, p, ns, localName);
2193 } else if (target instanceof OriginalElement) {
2194 list = originalElementGetElementsByTagNameNS.call(target, ns, localName);
2195 } else if (target instanceof OriginalDocument) {
2196 list = originalDocumentGetElementsByTagNameNS.call(target, ns, localName);
2197 } else {
2198 return findElements(this, index, result, p, ns, localName);
2199 }
2200 return filterNodeList(list, index, result, false);
2201 }
2202 var GetElementsByInterface = {
2203 getElementsByTagName: function(localName) {
2204 var result = new HTMLCollection();
2205 var match = localName === "*" ? matchesEveryThing : matchesTagName;
2206 result.length = getElementsByTagNameFiltered.call(this, match, 0, result, localName, localName.toLowerCase());
2207 return result;
2208 },
2209 getElementsByClassName: function(className) {
2210 return this.querySelectorAll("." + className);
2211 },
2212 getElementsByTagNameNS: function(ns, localName) {
2213 var result = new HTMLCollection();
2214 var match = null;
2215 if (ns === "*") {
2216 match = localName === "*" ? matchesEveryThing : matchesLocalNameOnly;
2217 } else {
2218 match = localName === "*" ? matchesNameSpace : matchesLocalNameNS;
2219 }
2220 result.length = getElementsByTagNameNSFiltered.call(this, match, 0, result , ns || null, localName);
2221 return result;
2222 }
2223 };
2224 scope.GetElementsByInterface = GetElementsByInterface;
2225 scope.SelectorsInterface = SelectorsInterface;
2226 scope.MatchesInterface = MatchesInterface;
2227 })(window.ShadowDOMPolyfill);
2228
2229 (function(scope) {
2230 "use strict";
2231 var NodeList = scope.wrappers.NodeList;
2232 function forwardElement(node) {
2233 while (node && node.nodeType !== Node.ELEMENT_NODE) {
2234 node = node.nextSibling;
2235 }
2236 return node;
2237 }
2238 function backwardsElement(node) {
2239 while (node && node.nodeType !== Node.ELEMENT_NODE) {
2240 node = node.previousSibling;
2241 }
2242 return node;
2243 }
2244 var ParentNodeInterface = {
2245 get firstElementChild() {
2246 return forwardElement(this.firstChild);
2247 },
2248 get lastElementChild() {
2249 return backwardsElement(this.lastChild);
2250 },
2251 get childElementCount() {
2252 var count = 0;
2253 for (var child = this.firstElementChild; child; child = child.nextElementS ibling) {
2254 count++;
2255 }
2256 return count;
2257 },
2258 get children() {
2259 var wrapperList = new NodeList();
2260 var i = 0;
2261 for (var child = this.firstElementChild; child; child = child.nextElementS ibling) {
2262 wrapperList[i++] = child;
2263 }
2264 wrapperList.length = i;
2265 return wrapperList;
2266 },
2267 remove: function() {
2268 var p = this.parentNode;
2269 if (p) p.removeChild(this);
2270 }
2271 };
2272 var ChildNodeInterface = {
2273 get nextElementSibling() {
2274 return forwardElement(this.nextSibling);
2275 },
2276 get previousElementSibling() {
2277 return backwardsElement(this.previousSibling);
2278 }
2279 };
2280 var NonElementParentNodeInterface = {
2281 getElementById: function(id) {
2282 if (/[ \t\n\r\f]/.test(id)) return null;
2283 return this.querySelector('[id="' + id + '"]');
2284 }
2285 };
2286 scope.ChildNodeInterface = ChildNodeInterface;
2287 scope.NonElementParentNodeInterface = NonElementParentNodeInterface;
2288 scope.ParentNodeInterface = ParentNodeInterface;
2289 })(window.ShadowDOMPolyfill);
2290
2291 (function(scope) {
2292 "use strict";
2293 var ChildNodeInterface = scope.ChildNodeInterface;
2294 var Node = scope.wrappers.Node;
2295 var enqueueMutation = scope.enqueueMutation;
2296 var mixin = scope.mixin;
2297 var registerWrapper = scope.registerWrapper;
2298 var unsafeUnwrap = scope.unsafeUnwrap;
2299 var OriginalCharacterData = window.CharacterData;
2300 function CharacterData(node) {
2301 Node.call(this, node);
2302 }
2303 CharacterData.prototype = Object.create(Node.prototype);
2304 mixin(CharacterData.prototype, {
2305 get nodeValue() {
2306 return this.data;
2307 },
2308 set nodeValue(data) {
2309 this.data = data;
2310 },
2311 get textContent() {
2312 return this.data;
2313 },
2314 set textContent(value) {
2315 this.data = value;
2316 },
2317 get data() {
2318 return unsafeUnwrap(this).data;
2319 },
2320 set data(value) {
2321 var oldValue = unsafeUnwrap(this).data;
2322 enqueueMutation(this, "characterData", {
2323 oldValue: oldValue
2324 });
2325 unsafeUnwrap(this).data = value;
2326 }
2327 });
2328 mixin(CharacterData.prototype, ChildNodeInterface);
2329 registerWrapper(OriginalCharacterData, CharacterData, document.createTextNode( ""));
2330 scope.wrappers.CharacterData = CharacterData;
2331 })(window.ShadowDOMPolyfill);
2332
2333 (function(scope) {
2334 "use strict";
2335 var CharacterData = scope.wrappers.CharacterData;
2336 var enqueueMutation = scope.enqueueMutation;
2337 var mixin = scope.mixin;
2338 var registerWrapper = scope.registerWrapper;
2339 function toUInt32(x) {
2340 return x >>> 0;
2341 }
2342 var OriginalText = window.Text;
2343 function Text(node) {
2344 CharacterData.call(this, node);
2345 }
2346 Text.prototype = Object.create(CharacterData.prototype);
2347 mixin(Text.prototype, {
2348 splitText: function(offset) {
2349 offset = toUInt32(offset);
2350 var s = this.data;
2351 if (offset > s.length) throw new Error("IndexSizeError");
2352 var head = s.slice(0, offset);
2353 var tail = s.slice(offset);
2354 this.data = head;
2355 var newTextNode = this.ownerDocument.createTextNode(tail);
2356 if (this.parentNode) this.parentNode.insertBefore(newTextNode, this.nextSi bling);
2357 return newTextNode;
2358 }
2359 });
2360 registerWrapper(OriginalText, Text, document.createTextNode(""));
2361 scope.wrappers.Text = Text;
2362 })(window.ShadowDOMPolyfill);
2363
2364 (function(scope) {
2365 "use strict";
2366 if (!window.DOMTokenList) {
2367 console.warn("Missing DOMTokenList prototype, please include a " + "compatib le classList polyfill such as http://goo.gl/uTcepH.");
2368 return;
2369 }
2370 var unsafeUnwrap = scope.unsafeUnwrap;
2371 var enqueueMutation = scope.enqueueMutation;
2372 function getClass(el) {
2373 return unsafeUnwrap(el).getAttribute("class");
2374 }
2375 function enqueueClassAttributeChange(el, oldValue) {
2376 enqueueMutation(el, "attributes", {
2377 name: "class",
2378 namespace: null,
2379 oldValue: oldValue
2380 });
2381 }
2382 function invalidateClass(el) {
2383 scope.invalidateRendererBasedOnAttribute(el, "class");
2384 }
2385 function changeClass(tokenList, method, args) {
2386 var ownerElement = tokenList.ownerElement_;
2387 if (ownerElement == null) {
2388 return method.apply(tokenList, args);
2389 }
2390 var oldValue = getClass(ownerElement);
2391 var retv = method.apply(tokenList, args);
2392 if (getClass(ownerElement) !== oldValue) {
2393 enqueueClassAttributeChange(ownerElement, oldValue);
2394 invalidateClass(ownerElement);
2395 }
2396 return retv;
2397 }
2398 var oldAdd = DOMTokenList.prototype.add;
2399 DOMTokenList.prototype.add = function() {
2400 changeClass(this, oldAdd, arguments);
2401 };
2402 var oldRemove = DOMTokenList.prototype.remove;
2403 DOMTokenList.prototype.remove = function() {
2404 changeClass(this, oldRemove, arguments);
2405 };
2406 var oldToggle = DOMTokenList.prototype.toggle;
2407 DOMTokenList.prototype.toggle = function() {
2408 return changeClass(this, oldToggle, arguments);
2409 };
2410 })(window.ShadowDOMPolyfill);
2411
2412 (function(scope) {
2413 "use strict";
2414 var ChildNodeInterface = scope.ChildNodeInterface;
2415 var GetElementsByInterface = scope.GetElementsByInterface;
2416 var Node = scope.wrappers.Node;
2417 var ParentNodeInterface = scope.ParentNodeInterface;
2418 var SelectorsInterface = scope.SelectorsInterface;
2419 var MatchesInterface = scope.MatchesInterface;
2420 var addWrapNodeListMethod = scope.addWrapNodeListMethod;
2421 var enqueueMutation = scope.enqueueMutation;
2422 var mixin = scope.mixin;
2423 var oneOf = scope.oneOf;
2424 var registerWrapper = scope.registerWrapper;
2425 var unsafeUnwrap = scope.unsafeUnwrap;
2426 var wrappers = scope.wrappers;
2427 var OriginalElement = window.Element;
2428 var matchesNames = [ "matches", "mozMatchesSelector", "msMatchesSelector", "we bkitMatchesSelector" ].filter(function(name) {
2429 return OriginalElement.prototype[name];
2430 });
2431 var matchesName = matchesNames[0];
2432 var originalMatches = OriginalElement.prototype[matchesName];
2433 function invalidateRendererBasedOnAttribute(element, name) {
2434 var p = element.parentNode;
2435 if (!p || !p.shadowRoot) return;
2436 var renderer = scope.getRendererForHost(p);
2437 if (renderer.dependsOnAttribute(name)) renderer.invalidate();
2438 }
2439 function enqueAttributeChange(element, name, oldValue) {
2440 enqueueMutation(element, "attributes", {
2441 name: name,
2442 namespace: null,
2443 oldValue: oldValue
2444 });
2445 }
2446 var classListTable = new WeakMap();
2447 function Element(node) {
2448 Node.call(this, node);
2449 }
2450 Element.prototype = Object.create(Node.prototype);
2451 mixin(Element.prototype, {
2452 createShadowRoot: function() {
2453 var newShadowRoot = new wrappers.ShadowRoot(this);
2454 unsafeUnwrap(this).polymerShadowRoot_ = newShadowRoot;
2455 var renderer = scope.getRendererForHost(this);
2456 renderer.invalidate();
2457 return newShadowRoot;
2458 },
2459 get shadowRoot() {
2460 return unsafeUnwrap(this).polymerShadowRoot_ || null;
2461 },
2462 setAttribute: function(name, value) {
2463 var oldValue = unsafeUnwrap(this).getAttribute(name);
2464 unsafeUnwrap(this).setAttribute(name, value);
2465 enqueAttributeChange(this, name, oldValue);
2466 invalidateRendererBasedOnAttribute(this, name);
2467 },
2468 removeAttribute: function(name) {
2469 var oldValue = unsafeUnwrap(this).getAttribute(name);
2470 unsafeUnwrap(this).removeAttribute(name);
2471 enqueAttributeChange(this, name, oldValue);
2472 invalidateRendererBasedOnAttribute(this, name);
2473 },
2474 get classList() {
2475 var list = classListTable.get(this);
2476 if (!list) {
2477 list = unsafeUnwrap(this).classList;
2478 if (!list) return;
2479 list.ownerElement_ = this;
2480 classListTable.set(this, list);
2481 }
2482 return list;
2483 },
2484 get className() {
2485 return unsafeUnwrap(this).className;
2486 },
2487 set className(v) {
2488 this.setAttribute("class", v);
2489 },
2490 get id() {
2491 return unsafeUnwrap(this).id;
2492 },
2493 set id(v) {
2494 this.setAttribute("id", v);
2495 }
2496 });
2497 matchesNames.forEach(function(name) {
2498 if (name !== "matches") {
2499 Element.prototype[name] = function(selector) {
2500 return this.matches(selector);
2501 };
2502 }
2503 });
2504 if (OriginalElement.prototype.webkitCreateShadowRoot) {
2505 Element.prototype.webkitCreateShadowRoot = Element.prototype.createShadowRoo t;
2506 }
2507 mixin(Element.prototype, ChildNodeInterface);
2508 mixin(Element.prototype, GetElementsByInterface);
2509 mixin(Element.prototype, ParentNodeInterface);
2510 mixin(Element.prototype, SelectorsInterface);
2511 mixin(Element.prototype, MatchesInterface);
2512 registerWrapper(OriginalElement, Element, document.createElementNS(null, "x")) ;
2513 scope.invalidateRendererBasedOnAttribute = invalidateRendererBasedOnAttribute;
2514 scope.matchesNames = matchesNames;
2515 scope.originalMatches = originalMatches;
2516 scope.wrappers.Element = Element;
2517 })(window.ShadowDOMPolyfill);
2518
2519 (function(scope) {
2520 "use strict";
2521 var Element = scope.wrappers.Element;
2522 var defineGetter = scope.defineGetter;
2523 var enqueueMutation = scope.enqueueMutation;
2524 var mixin = scope.mixin;
2525 var nodesWereAdded = scope.nodesWereAdded;
2526 var nodesWereRemoved = scope.nodesWereRemoved;
2527 var registerWrapper = scope.registerWrapper;
2528 var snapshotNodeList = scope.snapshotNodeList;
2529 var unsafeUnwrap = scope.unsafeUnwrap;
2530 var unwrap = scope.unwrap;
2531 var wrap = scope.wrap;
2532 var wrappers = scope.wrappers;
2533 var escapeAttrRegExp = /[&\u00A0"]/g;
2534 var escapeDataRegExp = /[&\u00A0<>]/g;
2535 function escapeReplace(c) {
2536 switch (c) {
2537 case "&":
2538 return "&amp;";
2539
2540 case "<":
2541 return "&lt;";
2542
2543 case ">":
2544 return "&gt;";
2545
2546 case '"':
2547 return "&quot;";
2548
2549 case " ":
2550 return "&nbsp;";
2551 }
2552 }
2553 function escapeAttr(s) {
2554 return s.replace(escapeAttrRegExp, escapeReplace);
2555 }
2556 function escapeData(s) {
2557 return s.replace(escapeDataRegExp, escapeReplace);
2558 }
2559 function makeSet(arr) {
2560 var set = {};
2561 for (var i = 0; i < arr.length; i++) {
2562 set[arr[i]] = true;
2563 }
2564 return set;
2565 }
2566 var voidElements = makeSet([ "area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr " ]);
2567 var plaintextParents = makeSet([ "style", "script", "xmp", "iframe", "noembed" , "noframes", "plaintext", "noscript" ]);
2568 var XHTML_NS = "http://www.w3.org/1999/xhtml";
2569 function needsSelfClosingSlash(node) {
2570 if (node.namespaceURI !== XHTML_NS) return true;
2571 var doctype = node.ownerDocument.doctype;
2572 return doctype && doctype.publicId && doctype.systemId;
2573 }
2574 function getOuterHTML(node, parentNode) {
2575 switch (node.nodeType) {
2576 case Node.ELEMENT_NODE:
2577 var tagName = node.tagName.toLowerCase();
2578 var s = "<" + tagName;
2579 var attrs = node.attributes;
2580 for (var i = 0, attr; attr = attrs[i]; i++) {
2581 s += " " + attr.name + '="' + escapeAttr(attr.value) + '"';
2582 }
2583 if (voidElements[tagName]) {
2584 if (needsSelfClosingSlash(node)) s += "/";
2585 return s + ">";
2586 }
2587 return s + ">" + getInnerHTML(node) + "</" + tagName + ">";
2588
2589 case Node.TEXT_NODE:
2590 var data = node.data;
2591 if (parentNode && plaintextParents[parentNode.localName]) return data;
2592 return escapeData(data);
2593
2594 case Node.COMMENT_NODE:
2595 return "<!--" + node.data + "-->";
2596
2597 default:
2598 console.error(node);
2599 throw new Error("not implemented");
2600 }
2601 }
2602 function getInnerHTML(node) {
2603 if (node instanceof wrappers.HTMLTemplateElement) node = node.content;
2604 var s = "";
2605 for (var child = node.firstChild; child; child = child.nextSibling) {
2606 s += getOuterHTML(child, node);
2607 }
2608 return s;
2609 }
2610 function setInnerHTML(node, value, opt_tagName) {
2611 var tagName = opt_tagName || "div";
2612 node.textContent = "";
2613 var tempElement = unwrap(node.ownerDocument.createElement(tagName));
2614 tempElement.innerHTML = value;
2615 var firstChild;
2616 while (firstChild = tempElement.firstChild) {
2617 node.appendChild(wrap(firstChild));
2618 }
2619 }
2620 var oldIe = /MSIE/.test(navigator.userAgent);
2621 var OriginalHTMLElement = window.HTMLElement;
2622 var OriginalHTMLTemplateElement = window.HTMLTemplateElement;
2623 function HTMLElement(node) {
2624 Element.call(this, node);
2625 }
2626 HTMLElement.prototype = Object.create(Element.prototype);
2627 mixin(HTMLElement.prototype, {
2628 get innerHTML() {
2629 return getInnerHTML(this);
2630 },
2631 set innerHTML(value) {
2632 if (oldIe && plaintextParents[this.localName]) {
2633 this.textContent = value;
2634 return;
2635 }
2636 var removedNodes = snapshotNodeList(this.childNodes);
2637 if (this.invalidateShadowRenderer()) {
2638 if (this instanceof wrappers.HTMLTemplateElement) setInnerHTML(this.cont ent, value); else setInnerHTML(this, value, this.tagName);
2639 } else if (!OriginalHTMLTemplateElement && this instanceof wrappers.HTMLTe mplateElement) {
2640 setInnerHTML(this.content, value);
2641 } else {
2642 unsafeUnwrap(this).innerHTML = value;
2643 }
2644 var addedNodes = snapshotNodeList(this.childNodes);
2645 enqueueMutation(this, "childList", {
2646 addedNodes: addedNodes,
2647 removedNodes: removedNodes
2648 });
2649 nodesWereRemoved(removedNodes);
2650 nodesWereAdded(addedNodes, this);
2651 },
2652 get outerHTML() {
2653 return getOuterHTML(this, this.parentNode);
2654 },
2655 set outerHTML(value) {
2656 var p = this.parentNode;
2657 if (p) {
2658 p.invalidateShadowRenderer();
2659 var df = frag(p, value);
2660 p.replaceChild(df, this);
2661 }
2662 },
2663 insertAdjacentHTML: function(position, text) {
2664 var contextElement, refNode;
2665 switch (String(position).toLowerCase()) {
2666 case "beforebegin":
2667 contextElement = this.parentNode;
2668 refNode = this;
2669 break;
2670
2671 case "afterend":
2672 contextElement = this.parentNode;
2673 refNode = this.nextSibling;
2674 break;
2675
2676 case "afterbegin":
2677 contextElement = this;
2678 refNode = this.firstChild;
2679 break;
2680
2681 case "beforeend":
2682 contextElement = this;
2683 refNode = null;
2684 break;
2685
2686 default:
2687 return;
2688 }
2689 var df = frag(contextElement, text);
2690 contextElement.insertBefore(df, refNode);
2691 },
2692 get hidden() {
2693 return this.hasAttribute("hidden");
2694 },
2695 set hidden(v) {
2696 if (v) {
2697 this.setAttribute("hidden", "");
2698 } else {
2699 this.removeAttribute("hidden");
2700 }
2701 }
2702 });
2703 function frag(contextElement, html) {
2704 var p = unwrap(contextElement.cloneNode(false));
2705 p.innerHTML = html;
2706 var df = unwrap(document.createDocumentFragment());
2707 var c;
2708 while (c = p.firstChild) {
2709 df.appendChild(c);
2710 }
2711 return wrap(df);
2712 }
2713 function getter(name) {
2714 return function() {
2715 scope.renderAllPending();
2716 return unsafeUnwrap(this)[name];
2717 };
2718 }
2719 function getterRequiresRendering(name) {
2720 defineGetter(HTMLElement, name, getter(name));
2721 }
2722 [ "clientHeight", "clientLeft", "clientTop", "clientWidth", "offsetHeight", "o ffsetLeft", "offsetTop", "offsetWidth", "scrollHeight", "scrollWidth" ].forEach( getterRequiresRendering);
2723 function getterAndSetterRequiresRendering(name) {
2724 Object.defineProperty(HTMLElement.prototype, name, {
2725 get: getter(name),
2726 set: function(v) {
2727 scope.renderAllPending();
2728 unsafeUnwrap(this)[name] = v;
2729 },
2730 configurable: true,
2731 enumerable: true
2732 });
2733 }
2734 [ "scrollLeft", "scrollTop" ].forEach(getterAndSetterRequiresRendering);
2735 function methodRequiresRendering(name) {
2736 Object.defineProperty(HTMLElement.prototype, name, {
2737 value: function() {
2738 scope.renderAllPending();
2739 return unsafeUnwrap(this)[name].apply(unsafeUnwrap(this), arguments);
2740 },
2741 configurable: true,
2742 enumerable: true
2743 });
2744 }
2745 [ "focus", "getBoundingClientRect", "getClientRects", "scrollIntoView" ].forEa ch(methodRequiresRendering);
2746 registerWrapper(OriginalHTMLElement, HTMLElement, document.createElement("b")) ;
2747 scope.wrappers.HTMLElement = HTMLElement;
2748 scope.getInnerHTML = getInnerHTML;
2749 scope.setInnerHTML = setInnerHTML;
2750 })(window.ShadowDOMPolyfill);
2751
2752 (function(scope) {
2753 "use strict";
2754 var HTMLElement = scope.wrappers.HTMLElement;
2755 var mixin = scope.mixin;
2756 var registerWrapper = scope.registerWrapper;
2757 var unsafeUnwrap = scope.unsafeUnwrap;
2758 var wrap = scope.wrap;
2759 var OriginalHTMLCanvasElement = window.HTMLCanvasElement;
2760 function HTMLCanvasElement(node) {
2761 HTMLElement.call(this, node);
2762 }
2763 HTMLCanvasElement.prototype = Object.create(HTMLElement.prototype);
2764 mixin(HTMLCanvasElement.prototype, {
2765 getContext: function() {
2766 var context = unsafeUnwrap(this).getContext.apply(unsafeUnwrap(this), argu ments);
2767 return context && wrap(context);
2768 }
2769 });
2770 registerWrapper(OriginalHTMLCanvasElement, HTMLCanvasElement, document.createE lement("canvas"));
2771 scope.wrappers.HTMLCanvasElement = HTMLCanvasElement;
2772 })(window.ShadowDOMPolyfill);
2773
2774 (function(scope) {
2775 "use strict";
2776 var HTMLElement = scope.wrappers.HTMLElement;
2777 var mixin = scope.mixin;
2778 var registerWrapper = scope.registerWrapper;
2779 var OriginalHTMLContentElement = window.HTMLContentElement;
2780 function HTMLContentElement(node) {
2781 HTMLElement.call(this, node);
2782 }
2783 HTMLContentElement.prototype = Object.create(HTMLElement.prototype);
2784 mixin(HTMLContentElement.prototype, {
2785 constructor: HTMLContentElement,
2786 get select() {
2787 return this.getAttribute("select");
2788 },
2789 set select(value) {
2790 this.setAttribute("select", value);
2791 },
2792 setAttribute: function(n, v) {
2793 HTMLElement.prototype.setAttribute.call(this, n, v);
2794 if (String(n).toLowerCase() === "select") this.invalidateShadowRenderer(tr ue);
2795 }
2796 });
2797 if (OriginalHTMLContentElement) registerWrapper(OriginalHTMLContentElement, HT MLContentElement);
2798 scope.wrappers.HTMLContentElement = HTMLContentElement;
2799 })(window.ShadowDOMPolyfill);
2800
2801 (function(scope) {
2802 "use strict";
2803 var HTMLElement = scope.wrappers.HTMLElement;
2804 var mixin = scope.mixin;
2805 var registerWrapper = scope.registerWrapper;
2806 var wrapHTMLCollection = scope.wrapHTMLCollection;
2807 var unwrap = scope.unwrap;
2808 var OriginalHTMLFormElement = window.HTMLFormElement;
2809 function HTMLFormElement(node) {
2810 HTMLElement.call(this, node);
2811 }
2812 HTMLFormElement.prototype = Object.create(HTMLElement.prototype);
2813 mixin(HTMLFormElement.prototype, {
2814 get elements() {
2815 return wrapHTMLCollection(unwrap(this).elements);
2816 }
2817 });
2818 registerWrapper(OriginalHTMLFormElement, HTMLFormElement, document.createEleme nt("form"));
2819 scope.wrappers.HTMLFormElement = HTMLFormElement;
2820 })(window.ShadowDOMPolyfill);
2821
2822 (function(scope) {
2823 "use strict";
2824 var HTMLElement = scope.wrappers.HTMLElement;
2825 var registerWrapper = scope.registerWrapper;
2826 var unwrap = scope.unwrap;
2827 var rewrap = scope.rewrap;
2828 var OriginalHTMLImageElement = window.HTMLImageElement;
2829 function HTMLImageElement(node) {
2830 HTMLElement.call(this, node);
2831 }
2832 HTMLImageElement.prototype = Object.create(HTMLElement.prototype);
2833 registerWrapper(OriginalHTMLImageElement, HTMLImageElement, document.createEle ment("img"));
2834 function Image(width, height) {
2835 if (!(this instanceof Image)) {
2836 throw new TypeError("DOM object constructor cannot be called as a function .");
2837 }
2838 var node = unwrap(document.createElement("img"));
2839 HTMLElement.call(this, node);
2840 rewrap(node, this);
2841 if (width !== undefined) node.width = width;
2842 if (height !== undefined) node.height = height;
2843 }
2844 Image.prototype = HTMLImageElement.prototype;
2845 scope.wrappers.HTMLImageElement = HTMLImageElement;
2846 scope.wrappers.Image = Image;
2847 })(window.ShadowDOMPolyfill);
2848
2849 (function(scope) {
2850 "use strict";
2851 var HTMLElement = scope.wrappers.HTMLElement;
2852 var mixin = scope.mixin;
2853 var NodeList = scope.wrappers.NodeList;
2854 var registerWrapper = scope.registerWrapper;
2855 var OriginalHTMLShadowElement = window.HTMLShadowElement;
2856 function HTMLShadowElement(node) {
2857 HTMLElement.call(this, node);
2858 }
2859 HTMLShadowElement.prototype = Object.create(HTMLElement.prototype);
2860 HTMLShadowElement.prototype.constructor = HTMLShadowElement;
2861 if (OriginalHTMLShadowElement) registerWrapper(OriginalHTMLShadowElement, HTML ShadowElement);
2862 scope.wrappers.HTMLShadowElement = HTMLShadowElement;
2863 })(window.ShadowDOMPolyfill);
2864
2865 (function(scope) {
2866 "use strict";
2867 var HTMLElement = scope.wrappers.HTMLElement;
2868 var mixin = scope.mixin;
2869 var registerWrapper = scope.registerWrapper;
2870 var unsafeUnwrap = scope.unsafeUnwrap;
2871 var unwrap = scope.unwrap;
2872 var wrap = scope.wrap;
2873 var contentTable = new WeakMap();
2874 var templateContentsOwnerTable = new WeakMap();
2875 function getTemplateContentsOwner(doc) {
2876 if (!doc.defaultView) return doc;
2877 var d = templateContentsOwnerTable.get(doc);
2878 if (!d) {
2879 d = doc.implementation.createHTMLDocument("");
2880 while (d.lastChild) {
2881 d.removeChild(d.lastChild);
2882 }
2883 templateContentsOwnerTable.set(doc, d);
2884 }
2885 return d;
2886 }
2887 function extractContent(templateElement) {
2888 var doc = getTemplateContentsOwner(templateElement.ownerDocument);
2889 var df = unwrap(doc.createDocumentFragment());
2890 var child;
2891 while (child = templateElement.firstChild) {
2892 df.appendChild(child);
2893 }
2894 return df;
2895 }
2896 var OriginalHTMLTemplateElement = window.HTMLTemplateElement;
2897 function HTMLTemplateElement(node) {
2898 HTMLElement.call(this, node);
2899 if (!OriginalHTMLTemplateElement) {
2900 var content = extractContent(node);
2901 contentTable.set(this, wrap(content));
2902 }
2903 }
2904 HTMLTemplateElement.prototype = Object.create(HTMLElement.prototype);
2905 mixin(HTMLTemplateElement.prototype, {
2906 constructor: HTMLTemplateElement,
2907 get content() {
2908 if (OriginalHTMLTemplateElement) return wrap(unsafeUnwrap(this).content);
2909 return contentTable.get(this);
2910 }
2911 });
2912 if (OriginalHTMLTemplateElement) registerWrapper(OriginalHTMLTemplateElement, HTMLTemplateElement);
2913 scope.wrappers.HTMLTemplateElement = HTMLTemplateElement;
2914 })(window.ShadowDOMPolyfill);
2915
2916 (function(scope) {
2917 "use strict";
2918 var HTMLElement = scope.wrappers.HTMLElement;
2919 var registerWrapper = scope.registerWrapper;
2920 var OriginalHTMLMediaElement = window.HTMLMediaElement;
2921 if (!OriginalHTMLMediaElement) return;
2922 function HTMLMediaElement(node) {
2923 HTMLElement.call(this, node);
2924 }
2925 HTMLMediaElement.prototype = Object.create(HTMLElement.prototype);
2926 registerWrapper(OriginalHTMLMediaElement, HTMLMediaElement, document.createEle ment("audio"));
2927 scope.wrappers.HTMLMediaElement = HTMLMediaElement;
2928 })(window.ShadowDOMPolyfill);
2929
2930 (function(scope) {
2931 "use strict";
2932 var HTMLMediaElement = scope.wrappers.HTMLMediaElement;
2933 var registerWrapper = scope.registerWrapper;
2934 var unwrap = scope.unwrap;
2935 var rewrap = scope.rewrap;
2936 var OriginalHTMLAudioElement = window.HTMLAudioElement;
2937 if (!OriginalHTMLAudioElement) return;
2938 function HTMLAudioElement(node) {
2939 HTMLMediaElement.call(this, node);
2940 }
2941 HTMLAudioElement.prototype = Object.create(HTMLMediaElement.prototype);
2942 registerWrapper(OriginalHTMLAudioElement, HTMLAudioElement, document.createEle ment("audio"));
2943 function Audio(src) {
2944 if (!(this instanceof Audio)) {
2945 throw new TypeError("DOM object constructor cannot be called as a function .");
2946 }
2947 var node = unwrap(document.createElement("audio"));
2948 HTMLMediaElement.call(this, node);
2949 rewrap(node, this);
2950 node.setAttribute("preload", "auto");
2951 if (src !== undefined) node.setAttribute("src", src);
2952 }
2953 Audio.prototype = HTMLAudioElement.prototype;
2954 scope.wrappers.HTMLAudioElement = HTMLAudioElement;
2955 scope.wrappers.Audio = Audio;
2956 })(window.ShadowDOMPolyfill);
2957
2958 (function(scope) {
2959 "use strict";
2960 var HTMLElement = scope.wrappers.HTMLElement;
2961 var mixin = scope.mixin;
2962 var registerWrapper = scope.registerWrapper;
2963 var rewrap = scope.rewrap;
2964 var unwrap = scope.unwrap;
2965 var wrap = scope.wrap;
2966 var OriginalHTMLOptionElement = window.HTMLOptionElement;
2967 function trimText(s) {
2968 return s.replace(/\s+/g, " ").trim();
2969 }
2970 function HTMLOptionElement(node) {
2971 HTMLElement.call(this, node);
2972 }
2973 HTMLOptionElement.prototype = Object.create(HTMLElement.prototype);
2974 mixin(HTMLOptionElement.prototype, {
2975 get text() {
2976 return trimText(this.textContent);
2977 },
2978 set text(value) {
2979 this.textContent = trimText(String(value));
2980 },
2981 get form() {
2982 return wrap(unwrap(this).form);
2983 }
2984 });
2985 registerWrapper(OriginalHTMLOptionElement, HTMLOptionElement, document.createE lement("option"));
2986 function Option(text, value, defaultSelected, selected) {
2987 if (!(this instanceof Option)) {
2988 throw new TypeError("DOM object constructor cannot be called as a function .");
2989 }
2990 var node = unwrap(document.createElement("option"));
2991 HTMLElement.call(this, node);
2992 rewrap(node, this);
2993 if (text !== undefined) node.text = text;
2994 if (value !== undefined) node.setAttribute("value", value);
2995 if (defaultSelected === true) node.setAttribute("selected", "");
2996 node.selected = selected === true;
2997 }
2998 Option.prototype = HTMLOptionElement.prototype;
2999 scope.wrappers.HTMLOptionElement = HTMLOptionElement;
3000 scope.wrappers.Option = Option;
3001 })(window.ShadowDOMPolyfill);
3002
3003 (function(scope) {
3004 "use strict";
3005 var HTMLElement = scope.wrappers.HTMLElement;
3006 var mixin = scope.mixin;
3007 var registerWrapper = scope.registerWrapper;
3008 var unwrap = scope.unwrap;
3009 var wrap = scope.wrap;
3010 var OriginalHTMLSelectElement = window.HTMLSelectElement;
3011 function HTMLSelectElement(node) {
3012 HTMLElement.call(this, node);
3013 }
3014 HTMLSelectElement.prototype = Object.create(HTMLElement.prototype);
3015 mixin(HTMLSelectElement.prototype, {
3016 add: function(element, before) {
3017 if (typeof before === "object") before = unwrap(before);
3018 unwrap(this).add(unwrap(element), before);
3019 },
3020 remove: function(indexOrNode) {
3021 if (indexOrNode === undefined) {
3022 HTMLElement.prototype.remove.call(this);
3023 return;
3024 }
3025 if (typeof indexOrNode === "object") indexOrNode = unwrap(indexOrNode);
3026 unwrap(this).remove(indexOrNode);
3027 },
3028 get form() {
3029 return wrap(unwrap(this).form);
3030 }
3031 });
3032 registerWrapper(OriginalHTMLSelectElement, HTMLSelectElement, document.createE lement("select"));
3033 scope.wrappers.HTMLSelectElement = HTMLSelectElement;
3034 })(window.ShadowDOMPolyfill);
3035
3036 (function(scope) {
3037 "use strict";
3038 var HTMLElement = scope.wrappers.HTMLElement;
3039 var mixin = scope.mixin;
3040 var registerWrapper = scope.registerWrapper;
3041 var unwrap = scope.unwrap;
3042 var wrap = scope.wrap;
3043 var wrapHTMLCollection = scope.wrapHTMLCollection;
3044 var OriginalHTMLTableElement = window.HTMLTableElement;
3045 function HTMLTableElement(node) {
3046 HTMLElement.call(this, node);
3047 }
3048 HTMLTableElement.prototype = Object.create(HTMLElement.prototype);
3049 mixin(HTMLTableElement.prototype, {
3050 get caption() {
3051 return wrap(unwrap(this).caption);
3052 },
3053 createCaption: function() {
3054 return wrap(unwrap(this).createCaption());
3055 },
3056 get tHead() {
3057 return wrap(unwrap(this).tHead);
3058 },
3059 createTHead: function() {
3060 return wrap(unwrap(this).createTHead());
3061 },
3062 createTFoot: function() {
3063 return wrap(unwrap(this).createTFoot());
3064 },
3065 get tFoot() {
3066 return wrap(unwrap(this).tFoot);
3067 },
3068 get tBodies() {
3069 return wrapHTMLCollection(unwrap(this).tBodies);
3070 },
3071 createTBody: function() {
3072 return wrap(unwrap(this).createTBody());
3073 },
3074 get rows() {
3075 return wrapHTMLCollection(unwrap(this).rows);
3076 },
3077 insertRow: function(index) {
3078 return wrap(unwrap(this).insertRow(index));
3079 }
3080 });
3081 registerWrapper(OriginalHTMLTableElement, HTMLTableElement, document.createEle ment("table"));
3082 scope.wrappers.HTMLTableElement = HTMLTableElement;
3083 })(window.ShadowDOMPolyfill);
3084
3085 (function(scope) {
3086 "use strict";
3087 var HTMLElement = scope.wrappers.HTMLElement;
3088 var mixin = scope.mixin;
3089 var registerWrapper = scope.registerWrapper;
3090 var wrapHTMLCollection = scope.wrapHTMLCollection;
3091 var unwrap = scope.unwrap;
3092 var wrap = scope.wrap;
3093 var OriginalHTMLTableSectionElement = window.HTMLTableSectionElement;
3094 function HTMLTableSectionElement(node) {
3095 HTMLElement.call(this, node);
3096 }
3097 HTMLTableSectionElement.prototype = Object.create(HTMLElement.prototype);
3098 mixin(HTMLTableSectionElement.prototype, {
3099 constructor: HTMLTableSectionElement,
3100 get rows() {
3101 return wrapHTMLCollection(unwrap(this).rows);
3102 },
3103 insertRow: function(index) {
3104 return wrap(unwrap(this).insertRow(index));
3105 }
3106 });
3107 registerWrapper(OriginalHTMLTableSectionElement, HTMLTableSectionElement, docu ment.createElement("thead"));
3108 scope.wrappers.HTMLTableSectionElement = HTMLTableSectionElement;
3109 })(window.ShadowDOMPolyfill);
3110
3111 (function(scope) {
3112 "use strict";
3113 var HTMLElement = scope.wrappers.HTMLElement;
3114 var mixin = scope.mixin;
3115 var registerWrapper = scope.registerWrapper;
3116 var wrapHTMLCollection = scope.wrapHTMLCollection;
3117 var unwrap = scope.unwrap;
3118 var wrap = scope.wrap;
3119 var OriginalHTMLTableRowElement = window.HTMLTableRowElement;
3120 function HTMLTableRowElement(node) {
3121 HTMLElement.call(this, node);
3122 }
3123 HTMLTableRowElement.prototype = Object.create(HTMLElement.prototype);
3124 mixin(HTMLTableRowElement.prototype, {
3125 get cells() {
3126 return wrapHTMLCollection(unwrap(this).cells);
3127 },
3128 insertCell: function(index) {
3129 return wrap(unwrap(this).insertCell(index));
3130 }
3131 });
3132 registerWrapper(OriginalHTMLTableRowElement, HTMLTableRowElement, document.cre ateElement("tr"));
3133 scope.wrappers.HTMLTableRowElement = HTMLTableRowElement;
3134 })(window.ShadowDOMPolyfill);
3135
3136 (function(scope) {
3137 "use strict";
3138 var HTMLContentElement = scope.wrappers.HTMLContentElement;
3139 var HTMLElement = scope.wrappers.HTMLElement;
3140 var HTMLShadowElement = scope.wrappers.HTMLShadowElement;
3141 var HTMLTemplateElement = scope.wrappers.HTMLTemplateElement;
3142 var mixin = scope.mixin;
3143 var registerWrapper = scope.registerWrapper;
3144 var OriginalHTMLUnknownElement = window.HTMLUnknownElement;
3145 function HTMLUnknownElement(node) {
3146 switch (node.localName) {
3147 case "content":
3148 return new HTMLContentElement(node);
3149
3150 case "shadow":
3151 return new HTMLShadowElement(node);
3152
3153 case "template":
3154 return new HTMLTemplateElement(node);
3155 }
3156 HTMLElement.call(this, node);
3157 }
3158 HTMLUnknownElement.prototype = Object.create(HTMLElement.prototype);
3159 registerWrapper(OriginalHTMLUnknownElement, HTMLUnknownElement);
3160 scope.wrappers.HTMLUnknownElement = HTMLUnknownElement;
3161 })(window.ShadowDOMPolyfill);
3162
3163 (function(scope) {
3164 "use strict";
3165 var Element = scope.wrappers.Element;
3166 var HTMLElement = scope.wrappers.HTMLElement;
3167 var registerWrapper = scope.registerWrapper;
3168 var defineWrapGetter = scope.defineWrapGetter;
3169 var unsafeUnwrap = scope.unsafeUnwrap;
3170 var wrap = scope.wrap;
3171 var mixin = scope.mixin;
3172 var SVG_NS = "http://www.w3.org/2000/svg";
3173 var OriginalSVGElement = window.SVGElement;
3174 var svgTitleElement = document.createElementNS(SVG_NS, "title");
3175 if (!("classList" in svgTitleElement)) {
3176 var descr = Object.getOwnPropertyDescriptor(Element.prototype, "classList");
3177 Object.defineProperty(HTMLElement.prototype, "classList", descr);
3178 delete Element.prototype.classList;
3179 }
3180 function SVGElement(node) {
3181 Element.call(this, node);
3182 }
3183 SVGElement.prototype = Object.create(Element.prototype);
3184 mixin(SVGElement.prototype, {
3185 get ownerSVGElement() {
3186 return wrap(unsafeUnwrap(this).ownerSVGElement);
3187 }
3188 });
3189 registerWrapper(OriginalSVGElement, SVGElement, document.createElementNS(SVG_N S, "title"));
3190 scope.wrappers.SVGElement = SVGElement;
3191 })(window.ShadowDOMPolyfill);
3192
3193 (function(scope) {
3194 "use strict";
3195 var mixin = scope.mixin;
3196 var registerWrapper = scope.registerWrapper;
3197 var unwrap = scope.unwrap;
3198 var wrap = scope.wrap;
3199 var OriginalSVGUseElement = window.SVGUseElement;
3200 var SVG_NS = "http://www.w3.org/2000/svg";
3201 var gWrapper = wrap(document.createElementNS(SVG_NS, "g"));
3202 var useElement = document.createElementNS(SVG_NS, "use");
3203 var SVGGElement = gWrapper.constructor;
3204 var parentInterfacePrototype = Object.getPrototypeOf(SVGGElement.prototype);
3205 var parentInterface = parentInterfacePrototype.constructor;
3206 function SVGUseElement(impl) {
3207 parentInterface.call(this, impl);
3208 }
3209 SVGUseElement.prototype = Object.create(parentInterfacePrototype);
3210 if ("instanceRoot" in useElement) {
3211 mixin(SVGUseElement.prototype, {
3212 get instanceRoot() {
3213 return wrap(unwrap(this).instanceRoot);
3214 },
3215 get animatedInstanceRoot() {
3216 return wrap(unwrap(this).animatedInstanceRoot);
3217 }
3218 });
3219 }
3220 registerWrapper(OriginalSVGUseElement, SVGUseElement, useElement);
3221 scope.wrappers.SVGUseElement = SVGUseElement;
3222 })(window.ShadowDOMPolyfill);
3223
3224 (function(scope) {
3225 "use strict";
3226 var EventTarget = scope.wrappers.EventTarget;
3227 var mixin = scope.mixin;
3228 var registerWrapper = scope.registerWrapper;
3229 var unsafeUnwrap = scope.unsafeUnwrap;
3230 var wrap = scope.wrap;
3231 var OriginalSVGElementInstance = window.SVGElementInstance;
3232 if (!OriginalSVGElementInstance) return;
3233 function SVGElementInstance(impl) {
3234 EventTarget.call(this, impl);
3235 }
3236 SVGElementInstance.prototype = Object.create(EventTarget.prototype);
3237 mixin(SVGElementInstance.prototype, {
3238 get correspondingElement() {
3239 return wrap(unsafeUnwrap(this).correspondingElement);
3240 },
3241 get correspondingUseElement() {
3242 return wrap(unsafeUnwrap(this).correspondingUseElement);
3243 },
3244 get parentNode() {
3245 return wrap(unsafeUnwrap(this).parentNode);
3246 },
3247 get childNodes() {
3248 throw new Error("Not implemented");
3249 },
3250 get firstChild() {
3251 return wrap(unsafeUnwrap(this).firstChild);
3252 },
3253 get lastChild() {
3254 return wrap(unsafeUnwrap(this).lastChild);
3255 },
3256 get previousSibling() {
3257 return wrap(unsafeUnwrap(this).previousSibling);
3258 },
3259 get nextSibling() {
3260 return wrap(unsafeUnwrap(this).nextSibling);
3261 }
3262 });
3263 registerWrapper(OriginalSVGElementInstance, SVGElementInstance);
3264 scope.wrappers.SVGElementInstance = SVGElementInstance;
3265 })(window.ShadowDOMPolyfill);
3266
3267 (function(scope) {
3268 "use strict";
3269 var mixin = scope.mixin;
3270 var registerWrapper = scope.registerWrapper;
3271 var setWrapper = scope.setWrapper;
3272 var unsafeUnwrap = scope.unsafeUnwrap;
3273 var unwrap = scope.unwrap;
3274 var unwrapIfNeeded = scope.unwrapIfNeeded;
3275 var wrap = scope.wrap;
3276 var OriginalCanvasRenderingContext2D = window.CanvasRenderingContext2D;
3277 function CanvasRenderingContext2D(impl) {
3278 setWrapper(impl, this);
3279 }
3280 mixin(CanvasRenderingContext2D.prototype, {
3281 get canvas() {
3282 return wrap(unsafeUnwrap(this).canvas);
3283 },
3284 drawImage: function() {
3285 arguments[0] = unwrapIfNeeded(arguments[0]);
3286 unsafeUnwrap(this).drawImage.apply(unsafeUnwrap(this), arguments);
3287 },
3288 createPattern: function() {
3289 arguments[0] = unwrap(arguments[0]);
3290 return unsafeUnwrap(this).createPattern.apply(unsafeUnwrap(this), argument s);
3291 }
3292 });
3293 registerWrapper(OriginalCanvasRenderingContext2D, CanvasRenderingContext2D, do cument.createElement("canvas").getContext("2d"));
3294 scope.wrappers.CanvasRenderingContext2D = CanvasRenderingContext2D;
3295 })(window.ShadowDOMPolyfill);
3296
3297 (function(scope) {
3298 "use strict";
3299 var addForwardingProperties = scope.addForwardingProperties;
3300 var mixin = scope.mixin;
3301 var registerWrapper = scope.registerWrapper;
3302 var setWrapper = scope.setWrapper;
3303 var unsafeUnwrap = scope.unsafeUnwrap;
3304 var unwrapIfNeeded = scope.unwrapIfNeeded;
3305 var wrap = scope.wrap;
3306 var OriginalWebGLRenderingContext = window.WebGLRenderingContext;
3307 if (!OriginalWebGLRenderingContext) return;
3308 function WebGLRenderingContext(impl) {
3309 setWrapper(impl, this);
3310 }
3311 mixin(WebGLRenderingContext.prototype, {
3312 get canvas() {
3313 return wrap(unsafeUnwrap(this).canvas);
3314 },
3315 texImage2D: function() {
3316 arguments[5] = unwrapIfNeeded(arguments[5]);
3317 unsafeUnwrap(this).texImage2D.apply(unsafeUnwrap(this), arguments);
3318 },
3319 texSubImage2D: function() {
3320 arguments[6] = unwrapIfNeeded(arguments[6]);
3321 unsafeUnwrap(this).texSubImage2D.apply(unsafeUnwrap(this), arguments);
3322 }
3323 });
3324 var OriginalWebGLRenderingContextBase = Object.getPrototypeOf(OriginalWebGLRen deringContext.prototype);
3325 if (OriginalWebGLRenderingContextBase !== Object.prototype) {
3326 addForwardingProperties(OriginalWebGLRenderingContextBase, WebGLRenderingCon text.prototype);
3327 }
3328 var instanceProperties = /WebKit/.test(navigator.userAgent) ? {
3329 drawingBufferHeight: null,
3330 drawingBufferWidth: null
3331 } : {};
3332 registerWrapper(OriginalWebGLRenderingContext, WebGLRenderingContext, instance Properties);
3333 scope.wrappers.WebGLRenderingContext = WebGLRenderingContext;
3334 })(window.ShadowDOMPolyfill);
3335
3336 (function(scope) {
3337 "use strict";
3338 var Node = scope.wrappers.Node;
3339 var GetElementsByInterface = scope.GetElementsByInterface;
3340 var NonElementParentNodeInterface = scope.NonElementParentNodeInterface;
3341 var ParentNodeInterface = scope.ParentNodeInterface;
3342 var SelectorsInterface = scope.SelectorsInterface;
3343 var mixin = scope.mixin;
3344 var registerObject = scope.registerObject;
3345 var registerWrapper = scope.registerWrapper;
3346 var OriginalDocumentFragment = window.DocumentFragment;
3347 function DocumentFragment(node) {
3348 Node.call(this, node);
3349 }
3350 DocumentFragment.prototype = Object.create(Node.prototype);
3351 mixin(DocumentFragment.prototype, ParentNodeInterface);
3352 mixin(DocumentFragment.prototype, SelectorsInterface);
3353 mixin(DocumentFragment.prototype, GetElementsByInterface);
3354 mixin(DocumentFragment.prototype, NonElementParentNodeInterface);
3355 registerWrapper(OriginalDocumentFragment, DocumentFragment, document.createDoc umentFragment());
3356 scope.wrappers.DocumentFragment = DocumentFragment;
3357 var Comment = registerObject(document.createComment(""));
3358 scope.wrappers.Comment = Comment;
3359 })(window.ShadowDOMPolyfill);
3360
3361 (function(scope) {
3362 "use strict";
3363 var DocumentFragment = scope.wrappers.DocumentFragment;
3364 var TreeScope = scope.TreeScope;
3365 var elementFromPoint = scope.elementFromPoint;
3366 var getInnerHTML = scope.getInnerHTML;
3367 var getTreeScope = scope.getTreeScope;
3368 var mixin = scope.mixin;
3369 var rewrap = scope.rewrap;
3370 var setInnerHTML = scope.setInnerHTML;
3371 var unsafeUnwrap = scope.unsafeUnwrap;
3372 var unwrap = scope.unwrap;
3373 var wrap = scope.wrap;
3374 var shadowHostTable = new WeakMap();
3375 var nextOlderShadowTreeTable = new WeakMap();
3376 function ShadowRoot(hostWrapper) {
3377 var node = unwrap(unsafeUnwrap(hostWrapper).ownerDocument.createDocumentFrag ment());
3378 DocumentFragment.call(this, node);
3379 rewrap(node, this);
3380 var oldShadowRoot = hostWrapper.shadowRoot;
3381 nextOlderShadowTreeTable.set(this, oldShadowRoot);
3382 this.treeScope_ = new TreeScope(this, getTreeScope(oldShadowRoot || hostWrap per));
3383 shadowHostTable.set(this, hostWrapper);
3384 }
3385 ShadowRoot.prototype = Object.create(DocumentFragment.prototype);
3386 mixin(ShadowRoot.prototype, {
3387 constructor: ShadowRoot,
3388 get innerHTML() {
3389 return getInnerHTML(this);
3390 },
3391 set innerHTML(value) {
3392 setInnerHTML(this, value);
3393 this.invalidateShadowRenderer();
3394 },
3395 get olderShadowRoot() {
3396 return nextOlderShadowTreeTable.get(this) || null;
3397 },
3398 get host() {
3399 return shadowHostTable.get(this) || null;
3400 },
3401 invalidateShadowRenderer: function() {
3402 return shadowHostTable.get(this).invalidateShadowRenderer();
3403 },
3404 elementFromPoint: function(x, y) {
3405 return elementFromPoint(this, this.ownerDocument, x, y);
3406 },
3407 getSelection: function() {
3408 return document.getSelection();
3409 },
3410 get activeElement() {
3411 var unwrappedActiveElement = unwrap(this).ownerDocument.activeElement;
3412 if (!unwrappedActiveElement || !unwrappedActiveElement.nodeType) return nu ll;
3413 var activeElement = wrap(unwrappedActiveElement);
3414 while (!this.contains(activeElement)) {
3415 while (activeElement.parentNode) {
3416 activeElement = activeElement.parentNode;
3417 }
3418 if (activeElement.host) {
3419 activeElement = activeElement.host;
3420 } else {
3421 return null;
3422 }
3423 }
3424 return activeElement;
3425 }
3426 });
3427 scope.wrappers.ShadowRoot = ShadowRoot;
3428 })(window.ShadowDOMPolyfill);
3429
3430 (function(scope) {
3431 "use strict";
3432 var registerWrapper = scope.registerWrapper;
3433 var setWrapper = scope.setWrapper;
3434 var unsafeUnwrap = scope.unsafeUnwrap;
3435 var unwrap = scope.unwrap;
3436 var unwrapIfNeeded = scope.unwrapIfNeeded;
3437 var wrap = scope.wrap;
3438 var getTreeScope = scope.getTreeScope;
3439 var OriginalRange = window.Range;
3440 var ShadowRoot = scope.wrappers.ShadowRoot;
3441 function getHost(node) {
3442 var root = getTreeScope(node).root;
3443 if (root instanceof ShadowRoot) {
3444 return root.host;
3445 }
3446 return null;
3447 }
3448 function hostNodeToShadowNode(refNode, offset) {
3449 if (refNode.shadowRoot) {
3450 offset = Math.min(refNode.childNodes.length - 1, offset);
3451 var child = refNode.childNodes[offset];
3452 if (child) {
3453 var insertionPoint = scope.getDestinationInsertionPoints(child);
3454 if (insertionPoint.length > 0) {
3455 var parentNode = insertionPoint[0].parentNode;
3456 if (parentNode.nodeType == Node.ELEMENT_NODE) {
3457 refNode = parentNode;
3458 }
3459 }
3460 }
3461 }
3462 return refNode;
3463 }
3464 function shadowNodeToHostNode(node) {
3465 node = wrap(node);
3466 return getHost(node) || node;
3467 }
3468 function Range(impl) {
3469 setWrapper(impl, this);
3470 }
3471 Range.prototype = {
3472 get startContainer() {
3473 return shadowNodeToHostNode(unsafeUnwrap(this).startContainer);
3474 },
3475 get endContainer() {
3476 return shadowNodeToHostNode(unsafeUnwrap(this).endContainer);
3477 },
3478 get commonAncestorContainer() {
3479 return shadowNodeToHostNode(unsafeUnwrap(this).commonAncestorContainer);
3480 },
3481 setStart: function(refNode, offset) {
3482 refNode = hostNodeToShadowNode(refNode, offset);
3483 unsafeUnwrap(this).setStart(unwrapIfNeeded(refNode), offset);
3484 },
3485 setEnd: function(refNode, offset) {
3486 refNode = hostNodeToShadowNode(refNode, offset);
3487 unsafeUnwrap(this).setEnd(unwrapIfNeeded(refNode), offset);
3488 },
3489 setStartBefore: function(refNode) {
3490 unsafeUnwrap(this).setStartBefore(unwrapIfNeeded(refNode));
3491 },
3492 setStartAfter: function(refNode) {
3493 unsafeUnwrap(this).setStartAfter(unwrapIfNeeded(refNode));
3494 },
3495 setEndBefore: function(refNode) {
3496 unsafeUnwrap(this).setEndBefore(unwrapIfNeeded(refNode));
3497 },
3498 setEndAfter: function(refNode) {
3499 unsafeUnwrap(this).setEndAfter(unwrapIfNeeded(refNode));
3500 },
3501 selectNode: function(refNode) {
3502 unsafeUnwrap(this).selectNode(unwrapIfNeeded(refNode));
3503 },
3504 selectNodeContents: function(refNode) {
3505 unsafeUnwrap(this).selectNodeContents(unwrapIfNeeded(refNode));
3506 },
3507 compareBoundaryPoints: function(how, sourceRange) {
3508 return unsafeUnwrap(this).compareBoundaryPoints(how, unwrap(sourceRange));
3509 },
3510 extractContents: function() {
3511 return wrap(unsafeUnwrap(this).extractContents());
3512 },
3513 cloneContents: function() {
3514 return wrap(unsafeUnwrap(this).cloneContents());
3515 },
3516 insertNode: function(node) {
3517 unsafeUnwrap(this).insertNode(unwrapIfNeeded(node));
3518 },
3519 surroundContents: function(newParent) {
3520 unsafeUnwrap(this).surroundContents(unwrapIfNeeded(newParent));
3521 },
3522 cloneRange: function() {
3523 return wrap(unsafeUnwrap(this).cloneRange());
3524 },
3525 isPointInRange: function(node, offset) {
3526 return unsafeUnwrap(this).isPointInRange(unwrapIfNeeded(node), offset);
3527 },
3528 comparePoint: function(node, offset) {
3529 return unsafeUnwrap(this).comparePoint(unwrapIfNeeded(node), offset);
3530 },
3531 intersectsNode: function(node) {
3532 return unsafeUnwrap(this).intersectsNode(unwrapIfNeeded(node));
3533 },
3534 toString: function() {
3535 return unsafeUnwrap(this).toString();
3536 }
3537 };
3538 if (OriginalRange.prototype.createContextualFragment) {
3539 Range.prototype.createContextualFragment = function(html) {
3540 return wrap(unsafeUnwrap(this).createContextualFragment(html));
3541 };
3542 }
3543 registerWrapper(window.Range, Range, document.createRange());
3544 scope.wrappers.Range = Range;
3545 })(window.ShadowDOMPolyfill);
3546
3547 (function(scope) {
3548 "use strict";
3549 var Element = scope.wrappers.Element;
3550 var HTMLContentElement = scope.wrappers.HTMLContentElement;
3551 var HTMLShadowElement = scope.wrappers.HTMLShadowElement;
3552 var Node = scope.wrappers.Node;
3553 var ShadowRoot = scope.wrappers.ShadowRoot;
3554 var assert = scope.assert;
3555 var getTreeScope = scope.getTreeScope;
3556 var mixin = scope.mixin;
3557 var oneOf = scope.oneOf;
3558 var unsafeUnwrap = scope.unsafeUnwrap;
3559 var unwrap = scope.unwrap;
3560 var wrap = scope.wrap;
3561 var ArraySplice = scope.ArraySplice;
3562 function updateWrapperUpAndSideways(wrapper) {
3563 wrapper.previousSibling_ = wrapper.previousSibling;
3564 wrapper.nextSibling_ = wrapper.nextSibling;
3565 wrapper.parentNode_ = wrapper.parentNode;
3566 }
3567 function updateWrapperDown(wrapper) {
3568 wrapper.firstChild_ = wrapper.firstChild;
3569 wrapper.lastChild_ = wrapper.lastChild;
3570 }
3571 function updateAllChildNodes(parentNodeWrapper) {
3572 assert(parentNodeWrapper instanceof Node);
3573 for (var childWrapper = parentNodeWrapper.firstChild; childWrapper; childWra pper = childWrapper.nextSibling) {
3574 updateWrapperUpAndSideways(childWrapper);
3575 }
3576 updateWrapperDown(parentNodeWrapper);
3577 }
3578 function insertBefore(parentNodeWrapper, newChildWrapper, refChildWrapper) {
3579 var parentNode = unwrap(parentNodeWrapper);
3580 var newChild = unwrap(newChildWrapper);
3581 var refChild = refChildWrapper ? unwrap(refChildWrapper) : null;
3582 remove(newChildWrapper);
3583 updateWrapperUpAndSideways(newChildWrapper);
3584 if (!refChildWrapper) {
3585 parentNodeWrapper.lastChild_ = parentNodeWrapper.lastChild;
3586 if (parentNodeWrapper.lastChild === parentNodeWrapper.firstChild) parentNo deWrapper.firstChild_ = parentNodeWrapper.firstChild;
3587 var lastChildWrapper = wrap(parentNode.lastChild);
3588 if (lastChildWrapper) lastChildWrapper.nextSibling_ = lastChildWrapper.nex tSibling;
3589 } else {
3590 if (parentNodeWrapper.firstChild === refChildWrapper) parentNodeWrapper.fi rstChild_ = refChildWrapper;
3591 refChildWrapper.previousSibling_ = refChildWrapper.previousSibling;
3592 }
3593 scope.originalInsertBefore.call(parentNode, newChild, refChild);
3594 }
3595 function remove(nodeWrapper) {
3596 var node = unwrap(nodeWrapper);
3597 var parentNode = node.parentNode;
3598 if (!parentNode) return;
3599 var parentNodeWrapper = wrap(parentNode);
3600 updateWrapperUpAndSideways(nodeWrapper);
3601 if (nodeWrapper.previousSibling) nodeWrapper.previousSibling.nextSibling_ = nodeWrapper;
3602 if (nodeWrapper.nextSibling) nodeWrapper.nextSibling.previousSibling_ = node Wrapper;
3603 if (parentNodeWrapper.lastChild === nodeWrapper) parentNodeWrapper.lastChild _ = nodeWrapper;
3604 if (parentNodeWrapper.firstChild === nodeWrapper) parentNodeWrapper.firstChi ld_ = nodeWrapper;
3605 scope.originalRemoveChild.call(parentNode, node);
3606 }
3607 var distributedNodesTable = new WeakMap();
3608 var destinationInsertionPointsTable = new WeakMap();
3609 var rendererForHostTable = new WeakMap();
3610 function resetDistributedNodes(insertionPoint) {
3611 distributedNodesTable.set(insertionPoint, []);
3612 }
3613 function getDistributedNodes(insertionPoint) {
3614 var rv = distributedNodesTable.get(insertionPoint);
3615 if (!rv) distributedNodesTable.set(insertionPoint, rv = []);
3616 return rv;
3617 }
3618 function getChildNodesSnapshot(node) {
3619 var result = [], i = 0;
3620 for (var child = node.firstChild; child; child = child.nextSibling) {
3621 result[i++] = child;
3622 }
3623 return result;
3624 }
3625 var request = oneOf(window, [ "requestAnimationFrame", "mozRequestAnimationFra me", "webkitRequestAnimationFrame", "setTimeout" ]);
3626 var pendingDirtyRenderers = [];
3627 var renderTimer;
3628 function renderAllPending() {
3629 for (var i = 0; i < pendingDirtyRenderers.length; i++) {
3630 var renderer = pendingDirtyRenderers[i];
3631 var parentRenderer = renderer.parentRenderer;
3632 if (parentRenderer && parentRenderer.dirty) continue;
3633 renderer.render();
3634 }
3635 pendingDirtyRenderers = [];
3636 }
3637 function handleRequestAnimationFrame() {
3638 renderTimer = null;
3639 renderAllPending();
3640 }
3641 function getRendererForHost(host) {
3642 var renderer = rendererForHostTable.get(host);
3643 if (!renderer) {
3644 renderer = new ShadowRenderer(host);
3645 rendererForHostTable.set(host, renderer);
3646 }
3647 return renderer;
3648 }
3649 function getShadowRootAncestor(node) {
3650 var root = getTreeScope(node).root;
3651 if (root instanceof ShadowRoot) return root;
3652 return null;
3653 }
3654 function getRendererForShadowRoot(shadowRoot) {
3655 return getRendererForHost(shadowRoot.host);
3656 }
3657 var spliceDiff = new ArraySplice();
3658 spliceDiff.equals = function(renderNode, rawNode) {
3659 return unwrap(renderNode.node) === rawNode;
3660 };
3661 function RenderNode(node) {
3662 this.skip = false;
3663 this.node = node;
3664 this.childNodes = [];
3665 }
3666 RenderNode.prototype = {
3667 append: function(node) {
3668 var rv = new RenderNode(node);
3669 this.childNodes.push(rv);
3670 return rv;
3671 },
3672 sync: function(opt_added) {
3673 if (this.skip) return;
3674 var nodeWrapper = this.node;
3675 var newChildren = this.childNodes;
3676 var oldChildren = getChildNodesSnapshot(unwrap(nodeWrapper));
3677 var added = opt_added || new WeakMap();
3678 var splices = spliceDiff.calculateSplices(newChildren, oldChildren);
3679 var newIndex = 0, oldIndex = 0;
3680 var lastIndex = 0;
3681 for (var i = 0; i < splices.length; i++) {
3682 var splice = splices[i];
3683 for (;lastIndex < splice.index; lastIndex++) {
3684 oldIndex++;
3685 newChildren[newIndex++].sync(added);
3686 }
3687 var removedCount = splice.removed.length;
3688 for (var j = 0; j < removedCount; j++) {
3689 var wrapper = wrap(oldChildren[oldIndex++]);
3690 if (!added.get(wrapper)) remove(wrapper);
3691 }
3692 var addedCount = splice.addedCount;
3693 var refNode = oldChildren[oldIndex] && wrap(oldChildren[oldIndex]);
3694 for (var j = 0; j < addedCount; j++) {
3695 var newChildRenderNode = newChildren[newIndex++];
3696 var newChildWrapper = newChildRenderNode.node;
3697 insertBefore(nodeWrapper, newChildWrapper, refNode);
3698 added.set(newChildWrapper, true);
3699 newChildRenderNode.sync(added);
3700 }
3701 lastIndex += addedCount;
3702 }
3703 for (var i = lastIndex; i < newChildren.length; i++) {
3704 newChildren[i].sync(added);
3705 }
3706 }
3707 };
3708 function ShadowRenderer(host) {
3709 this.host = host;
3710 this.dirty = false;
3711 this.invalidateAttributes();
3712 this.associateNode(host);
3713 }
3714 ShadowRenderer.prototype = {
3715 render: function(opt_renderNode) {
3716 if (!this.dirty) return;
3717 this.invalidateAttributes();
3718 var host = this.host;
3719 this.distribution(host);
3720 var renderNode = opt_renderNode || new RenderNode(host);
3721 this.buildRenderTree(renderNode, host);
3722 var topMostRenderer = !opt_renderNode;
3723 if (topMostRenderer) renderNode.sync();
3724 this.dirty = false;
3725 },
3726 get parentRenderer() {
3727 return getTreeScope(this.host).renderer;
3728 },
3729 invalidate: function() {
3730 if (!this.dirty) {
3731 this.dirty = true;
3732 var parentRenderer = this.parentRenderer;
3733 if (parentRenderer) parentRenderer.invalidate();
3734 pendingDirtyRenderers.push(this);
3735 if (renderTimer) return;
3736 renderTimer = window[request](handleRequestAnimationFrame, 0);
3737 }
3738 },
3739 distribution: function(root) {
3740 this.resetAllSubtrees(root);
3741 this.distributionResolution(root);
3742 },
3743 resetAll: function(node) {
3744 if (isInsertionPoint(node)) resetDistributedNodes(node); else resetDestina tionInsertionPoints(node);
3745 this.resetAllSubtrees(node);
3746 },
3747 resetAllSubtrees: function(node) {
3748 for (var child = node.firstChild; child; child = child.nextSibling) {
3749 this.resetAll(child);
3750 }
3751 if (node.shadowRoot) this.resetAll(node.shadowRoot);
3752 if (node.olderShadowRoot) this.resetAll(node.olderShadowRoot);
3753 },
3754 distributionResolution: function(node) {
3755 if (isShadowHost(node)) {
3756 var shadowHost = node;
3757 var pool = poolPopulation(shadowHost);
3758 var shadowTrees = getShadowTrees(shadowHost);
3759 for (var i = 0; i < shadowTrees.length; i++) {
3760 this.poolDistribution(shadowTrees[i], pool);
3761 }
3762 for (var i = shadowTrees.length - 1; i >= 0; i--) {
3763 var shadowTree = shadowTrees[i];
3764 var shadow = getShadowInsertionPoint(shadowTree);
3765 if (shadow) {
3766 var olderShadowRoot = shadowTree.olderShadowRoot;
3767 if (olderShadowRoot) {
3768 pool = poolPopulation(olderShadowRoot);
3769 }
3770 for (var j = 0; j < pool.length; j++) {
3771 destributeNodeInto(pool[j], shadow);
3772 }
3773 }
3774 this.distributionResolution(shadowTree);
3775 }
3776 }
3777 for (var child = node.firstChild; child; child = child.nextSibling) {
3778 this.distributionResolution(child);
3779 }
3780 },
3781 poolDistribution: function(node, pool) {
3782 if (node instanceof HTMLShadowElement) return;
3783 if (node instanceof HTMLContentElement) {
3784 var content = node;
3785 this.updateDependentAttributes(content.getAttribute("select"));
3786 var anyDistributed = false;
3787 for (var i = 0; i < pool.length; i++) {
3788 var node = pool[i];
3789 if (!node) continue;
3790 if (matches(node, content)) {
3791 destributeNodeInto(node, content);
3792 pool[i] = undefined;
3793 anyDistributed = true;
3794 }
3795 }
3796 if (!anyDistributed) {
3797 for (var child = content.firstChild; child; child = child.nextSibling) {
3798 destributeNodeInto(child, content);
3799 }
3800 }
3801 return;
3802 }
3803 for (var child = node.firstChild; child; child = child.nextSibling) {
3804 this.poolDistribution(child, pool);
3805 }
3806 },
3807 buildRenderTree: function(renderNode, node) {
3808 var children = this.compose(node);
3809 for (var i = 0; i < children.length; i++) {
3810 var child = children[i];
3811 var childRenderNode = renderNode.append(child);
3812 this.buildRenderTree(childRenderNode, child);
3813 }
3814 if (isShadowHost(node)) {
3815 var renderer = getRendererForHost(node);
3816 renderer.dirty = false;
3817 }
3818 },
3819 compose: function(node) {
3820 var children = [];
3821 var p = node.shadowRoot || node;
3822 for (var child = p.firstChild; child; child = child.nextSibling) {
3823 if (isInsertionPoint(child)) {
3824 this.associateNode(p);
3825 var distributedNodes = getDistributedNodes(child);
3826 for (var j = 0; j < distributedNodes.length; j++) {
3827 var distributedNode = distributedNodes[j];
3828 if (isFinalDestination(child, distributedNode)) children.push(distri butedNode);
3829 }
3830 } else {
3831 children.push(child);
3832 }
3833 }
3834 return children;
3835 },
3836 invalidateAttributes: function() {
3837 this.attributes = Object.create(null);
3838 },
3839 updateDependentAttributes: function(selector) {
3840 if (!selector) return;
3841 var attributes = this.attributes;
3842 if (/\.\w+/.test(selector)) attributes["class"] = true;
3843 if (/#\w+/.test(selector)) attributes["id"] = true;
3844 selector.replace(/\[\s*([^\s=\|~\]]+)/g, function(_, name) {
3845 attributes[name] = true;
3846 });
3847 },
3848 dependsOnAttribute: function(name) {
3849 return this.attributes[name];
3850 },
3851 associateNode: function(node) {
3852 unsafeUnwrap(node).polymerShadowRenderer_ = this;
3853 }
3854 };
3855 function poolPopulation(node) {
3856 var pool = [];
3857 for (var child = node.firstChild; child; child = child.nextSibling) {
3858 if (isInsertionPoint(child)) {
3859 pool.push.apply(pool, getDistributedNodes(child));
3860 } else {
3861 pool.push(child);
3862 }
3863 }
3864 return pool;
3865 }
3866 function getShadowInsertionPoint(node) {
3867 if (node instanceof HTMLShadowElement) return node;
3868 if (node instanceof HTMLContentElement) return null;
3869 for (var child = node.firstChild; child; child = child.nextSibling) {
3870 var res = getShadowInsertionPoint(child);
3871 if (res) return res;
3872 }
3873 return null;
3874 }
3875 function destributeNodeInto(child, insertionPoint) {
3876 getDistributedNodes(insertionPoint).push(child);
3877 var points = destinationInsertionPointsTable.get(child);
3878 if (!points) destinationInsertionPointsTable.set(child, [ insertionPoint ]); else points.push(insertionPoint);
3879 }
3880 function getDestinationInsertionPoints(node) {
3881 return destinationInsertionPointsTable.get(node);
3882 }
3883 function resetDestinationInsertionPoints(node) {
3884 destinationInsertionPointsTable.set(node, undefined);
3885 }
3886 var selectorStartCharRe = /^(:not\()?[*.#[a-zA-Z_|]/;
3887 function matches(node, contentElement) {
3888 var select = contentElement.getAttribute("select");
3889 if (!select) return true;
3890 select = select.trim();
3891 if (!select) return true;
3892 if (!(node instanceof Element)) return false;
3893 if (!selectorStartCharRe.test(select)) return false;
3894 try {
3895 return node.matches(select);
3896 } catch (ex) {
3897 return false;
3898 }
3899 }
3900 function isFinalDestination(insertionPoint, node) {
3901 var points = getDestinationInsertionPoints(node);
3902 return points && points[points.length - 1] === insertionPoint;
3903 }
3904 function isInsertionPoint(node) {
3905 return node instanceof HTMLContentElement || node instanceof HTMLShadowEleme nt;
3906 }
3907 function isShadowHost(shadowHost) {
3908 return shadowHost.shadowRoot;
3909 }
3910 function getShadowTrees(host) {
3911 var trees = [];
3912 for (var tree = host.shadowRoot; tree; tree = tree.olderShadowRoot) {
3913 trees.push(tree);
3914 }
3915 return trees;
3916 }
3917 function render(host) {
3918 new ShadowRenderer(host).render();
3919 }
3920 Node.prototype.invalidateShadowRenderer = function(force) {
3921 var renderer = unsafeUnwrap(this).polymerShadowRenderer_;
3922 if (renderer) {
3923 renderer.invalidate();
3924 return true;
3925 }
3926 return false;
3927 };
3928 HTMLContentElement.prototype.getDistributedNodes = HTMLShadowElement.prototype .getDistributedNodes = function() {
3929 renderAllPending();
3930 return getDistributedNodes(this);
3931 };
3932 Element.prototype.getDestinationInsertionPoints = function() {
3933 renderAllPending();
3934 return getDestinationInsertionPoints(this) || [];
3935 };
3936 HTMLContentElement.prototype.nodeIsInserted_ = HTMLShadowElement.prototype.nod eIsInserted_ = function() {
3937 this.invalidateShadowRenderer();
3938 var shadowRoot = getShadowRootAncestor(this);
3939 var renderer;
3940 if (shadowRoot) renderer = getRendererForShadowRoot(shadowRoot);
3941 unsafeUnwrap(this).polymerShadowRenderer_ = renderer;
3942 if (renderer) renderer.invalidate();
3943 };
3944 scope.getRendererForHost = getRendererForHost;
3945 scope.getShadowTrees = getShadowTrees;
3946 scope.renderAllPending = renderAllPending;
3947 scope.getDestinationInsertionPoints = getDestinationInsertionPoints;
3948 scope.visual = {
3949 insertBefore: insertBefore,
3950 remove: remove
3951 };
3952 })(window.ShadowDOMPolyfill);
3953
3954 (function(scope) {
3955 "use strict";
3956 var HTMLElement = scope.wrappers.HTMLElement;
3957 var assert = scope.assert;
3958 var mixin = scope.mixin;
3959 var registerWrapper = scope.registerWrapper;
3960 var unwrap = scope.unwrap;
3961 var wrap = scope.wrap;
3962 var elementsWithFormProperty = [ "HTMLButtonElement", "HTMLFieldSetElement", " HTMLInputElement", "HTMLKeygenElement", "HTMLLabelElement", "HTMLLegendElement", "HTMLObjectElement", "HTMLOutputElement", "HTMLTextAreaElement" ];
3963 function createWrapperConstructor(name) {
3964 if (!window[name]) return;
3965 assert(!scope.wrappers[name]);
3966 var GeneratedWrapper = function(node) {
3967 HTMLElement.call(this, node);
3968 };
3969 GeneratedWrapper.prototype = Object.create(HTMLElement.prototype);
3970 mixin(GeneratedWrapper.prototype, {
3971 get form() {
3972 return wrap(unwrap(this).form);
3973 }
3974 });
3975 registerWrapper(window[name], GeneratedWrapper, document.createElement(name. slice(4, -7)));
3976 scope.wrappers[name] = GeneratedWrapper;
3977 }
3978 elementsWithFormProperty.forEach(createWrapperConstructor);
3979 })(window.ShadowDOMPolyfill);
3980
3981 (function(scope) {
3982 "use strict";
3983 var registerWrapper = scope.registerWrapper;
3984 var setWrapper = scope.setWrapper;
3985 var unsafeUnwrap = scope.unsafeUnwrap;
3986 var unwrap = scope.unwrap;
3987 var unwrapIfNeeded = scope.unwrapIfNeeded;
3988 var wrap = scope.wrap;
3989 var OriginalSelection = window.Selection;
3990 function Selection(impl) {
3991 setWrapper(impl, this);
3992 }
3993 Selection.prototype = {
3994 get anchorNode() {
3995 return wrap(unsafeUnwrap(this).anchorNode);
3996 },
3997 get focusNode() {
3998 return wrap(unsafeUnwrap(this).focusNode);
3999 },
4000 addRange: function(range) {
4001 unsafeUnwrap(this).addRange(unwrapIfNeeded(range));
4002 },
4003 collapse: function(node, index) {
4004 unsafeUnwrap(this).collapse(unwrapIfNeeded(node), index);
4005 },
4006 containsNode: function(node, allowPartial) {
4007 return unsafeUnwrap(this).containsNode(unwrapIfNeeded(node), allowPartial) ;
4008 },
4009 getRangeAt: function(index) {
4010 return wrap(unsafeUnwrap(this).getRangeAt(index));
4011 },
4012 removeRange: function(range) {
4013 unsafeUnwrap(this).removeRange(unwrap(range));
4014 },
4015 selectAllChildren: function(node) {
4016 unsafeUnwrap(this).selectAllChildren(node instanceof ShadowRoot ? unsafeUn wrap(node.host) : unwrapIfNeeded(node));
4017 },
4018 toString: function() {
4019 return unsafeUnwrap(this).toString();
4020 }
4021 };
4022 if (OriginalSelection.prototype.extend) {
4023 Selection.prototype.extend = function(node, offset) {
4024 unsafeUnwrap(this).extend(unwrapIfNeeded(node), offset);
4025 };
4026 }
4027 registerWrapper(window.Selection, Selection, window.getSelection());
4028 scope.wrappers.Selection = Selection;
4029 })(window.ShadowDOMPolyfill);
4030
4031 (function(scope) {
4032 "use strict";
4033 var registerWrapper = scope.registerWrapper;
4034 var setWrapper = scope.setWrapper;
4035 var unsafeUnwrap = scope.unsafeUnwrap;
4036 var unwrapIfNeeded = scope.unwrapIfNeeded;
4037 var wrap = scope.wrap;
4038 var OriginalTreeWalker = window.TreeWalker;
4039 function TreeWalker(impl) {
4040 setWrapper(impl, this);
4041 }
4042 TreeWalker.prototype = {
4043 get root() {
4044 return wrap(unsafeUnwrap(this).root);
4045 },
4046 get currentNode() {
4047 return wrap(unsafeUnwrap(this).currentNode);
4048 },
4049 set currentNode(node) {
4050 unsafeUnwrap(this).currentNode = unwrapIfNeeded(node);
4051 },
4052 get filter() {
4053 return unsafeUnwrap(this).filter;
4054 },
4055 parentNode: function() {
4056 return wrap(unsafeUnwrap(this).parentNode());
4057 },
4058 firstChild: function() {
4059 return wrap(unsafeUnwrap(this).firstChild());
4060 },
4061 lastChild: function() {
4062 return wrap(unsafeUnwrap(this).lastChild());
4063 },
4064 previousSibling: function() {
4065 return wrap(unsafeUnwrap(this).previousSibling());
4066 },
4067 previousNode: function() {
4068 return wrap(unsafeUnwrap(this).previousNode());
4069 },
4070 nextNode: function() {
4071 return wrap(unsafeUnwrap(this).nextNode());
4072 }
4073 };
4074 registerWrapper(OriginalTreeWalker, TreeWalker);
4075 scope.wrappers.TreeWalker = TreeWalker;
4076 })(window.ShadowDOMPolyfill);
4077
4078 (function(scope) {
4079 "use strict";
4080 var GetElementsByInterface = scope.GetElementsByInterface;
4081 var Node = scope.wrappers.Node;
4082 var ParentNodeInterface = scope.ParentNodeInterface;
4083 var NonElementParentNodeInterface = scope.NonElementParentNodeInterface;
4084 var Selection = scope.wrappers.Selection;
4085 var SelectorsInterface = scope.SelectorsInterface;
4086 var ShadowRoot = scope.wrappers.ShadowRoot;
4087 var TreeScope = scope.TreeScope;
4088 var cloneNode = scope.cloneNode;
4089 var defineGetter = scope.defineGetter;
4090 var defineWrapGetter = scope.defineWrapGetter;
4091 var elementFromPoint = scope.elementFromPoint;
4092 var forwardMethodsToWrapper = scope.forwardMethodsToWrapper;
4093 var matchesNames = scope.matchesNames;
4094 var mixin = scope.mixin;
4095 var registerWrapper = scope.registerWrapper;
4096 var renderAllPending = scope.renderAllPending;
4097 var rewrap = scope.rewrap;
4098 var setWrapper = scope.setWrapper;
4099 var unsafeUnwrap = scope.unsafeUnwrap;
4100 var unwrap = scope.unwrap;
4101 var wrap = scope.wrap;
4102 var wrapEventTargetMethods = scope.wrapEventTargetMethods;
4103 var wrapNodeList = scope.wrapNodeList;
4104 var implementationTable = new WeakMap();
4105 function Document(node) {
4106 Node.call(this, node);
4107 this.treeScope_ = new TreeScope(this, null);
4108 }
4109 Document.prototype = Object.create(Node.prototype);
4110 defineWrapGetter(Document, "documentElement");
4111 defineWrapGetter(Document, "body");
4112 defineWrapGetter(Document, "head");
4113 defineGetter(Document, "activeElement", function() {
4114 var unwrappedActiveElement = unwrap(this).activeElement;
4115 if (!unwrappedActiveElement || !unwrappedActiveElement.nodeType) return null ;
4116 var activeElement = wrap(unwrappedActiveElement);
4117 while (!this.contains(activeElement)) {
4118 while (activeElement.parentNode) {
4119 activeElement = activeElement.parentNode;
4120 }
4121 if (activeElement.host) {
4122 activeElement = activeElement.host;
4123 } else {
4124 return null;
4125 }
4126 }
4127 return activeElement;
4128 });
4129 function wrapMethod(name) {
4130 var original = document[name];
4131 Document.prototype[name] = function() {
4132 return wrap(original.apply(unsafeUnwrap(this), arguments));
4133 };
4134 }
4135 [ "createComment", "createDocumentFragment", "createElement", "createElementNS ", "createEvent", "createEventNS", "createRange", "createTextNode" ].forEach(wra pMethod);
4136 var originalAdoptNode = document.adoptNode;
4137 function adoptNodeNoRemove(node, doc) {
4138 originalAdoptNode.call(unsafeUnwrap(doc), unwrap(node));
4139 adoptSubtree(node, doc);
4140 }
4141 function adoptSubtree(node, doc) {
4142 if (node.shadowRoot) doc.adoptNode(node.shadowRoot);
4143 if (node instanceof ShadowRoot) adoptOlderShadowRoots(node, doc);
4144 for (var child = node.firstChild; child; child = child.nextSibling) {
4145 adoptSubtree(child, doc);
4146 }
4147 }
4148 function adoptOlderShadowRoots(shadowRoot, doc) {
4149 var oldShadowRoot = shadowRoot.olderShadowRoot;
4150 if (oldShadowRoot) doc.adoptNode(oldShadowRoot);
4151 }
4152 var originalGetSelection = document.getSelection;
4153 mixin(Document.prototype, {
4154 adoptNode: function(node) {
4155 if (node.parentNode) node.parentNode.removeChild(node);
4156 adoptNodeNoRemove(node, this);
4157 return node;
4158 },
4159 elementFromPoint: function(x, y) {
4160 return elementFromPoint(this, this, x, y);
4161 },
4162 importNode: function(node, deep) {
4163 return cloneNode(node, deep, unsafeUnwrap(this));
4164 },
4165 getSelection: function() {
4166 renderAllPending();
4167 return new Selection(originalGetSelection.call(unwrap(this)));
4168 },
4169 getElementsByName: function(name) {
4170 return SelectorsInterface.querySelectorAll.call(this, "[name=" + JSON.stri ngify(String(name)) + "]");
4171 }
4172 });
4173 var originalCreateTreeWalker = document.createTreeWalker;
4174 var TreeWalkerWrapper = scope.wrappers.TreeWalker;
4175 Document.prototype.createTreeWalker = function(root, whatToShow, filter, expan dEntityReferences) {
4176 var newFilter = null;
4177 if (filter) {
4178 if (filter.acceptNode && typeof filter.acceptNode === "function") {
4179 newFilter = {
4180 acceptNode: function(node) {
4181 return filter.acceptNode(wrap(node));
4182 }
4183 };
4184 } else if (typeof filter === "function") {
4185 newFilter = function(node) {
4186 return filter(wrap(node));
4187 };
4188 }
4189 }
4190 return new TreeWalkerWrapper(originalCreateTreeWalker.call(unwrap(this), unw rap(root), whatToShow, newFilter, expandEntityReferences));
4191 };
4192 if (document.registerElement) {
4193 var originalRegisterElement = document.registerElement;
4194 Document.prototype.registerElement = function(tagName, object) {
4195 var prototype, extendsOption;
4196 if (object !== undefined) {
4197 prototype = object.prototype;
4198 extendsOption = object.extends;
4199 }
4200 if (!prototype) prototype = Object.create(HTMLElement.prototype);
4201 if (scope.nativePrototypeTable.get(prototype)) {
4202 throw new Error("NotSupportedError");
4203 }
4204 var proto = Object.getPrototypeOf(prototype);
4205 var nativePrototype;
4206 var prototypes = [];
4207 while (proto) {
4208 nativePrototype = scope.nativePrototypeTable.get(proto);
4209 if (nativePrototype) break;
4210 prototypes.push(proto);
4211 proto = Object.getPrototypeOf(proto);
4212 }
4213 if (!nativePrototype) {
4214 throw new Error("NotSupportedError");
4215 }
4216 var newPrototype = Object.create(nativePrototype);
4217 for (var i = prototypes.length - 1; i >= 0; i--) {
4218 newPrototype = Object.create(newPrototype);
4219 }
4220 [ "createdCallback", "attachedCallback", "detachedCallback", "attributeCha ngedCallback" ].forEach(function(name) {
4221 var f = prototype[name];
4222 if (!f) return;
4223 newPrototype[name] = function() {
4224 if (!(wrap(this) instanceof CustomElementConstructor)) {
4225 rewrap(this);
4226 }
4227 f.apply(wrap(this), arguments);
4228 };
4229 });
4230 var p = {
4231 prototype: newPrototype
4232 };
4233 if (extendsOption) p.extends = extendsOption;
4234 function CustomElementConstructor(node) {
4235 if (!node) {
4236 if (extendsOption) {
4237 return document.createElement(extendsOption, tagName);
4238 } else {
4239 return document.createElement(tagName);
4240 }
4241 }
4242 setWrapper(node, this);
4243 }
4244 CustomElementConstructor.prototype = prototype;
4245 CustomElementConstructor.prototype.constructor = CustomElementConstructor;
4246 scope.constructorTable.set(newPrototype, CustomElementConstructor);
4247 scope.nativePrototypeTable.set(prototype, newPrototype);
4248 var nativeConstructor = originalRegisterElement.call(unwrap(this), tagName , p);
4249 return CustomElementConstructor;
4250 };
4251 forwardMethodsToWrapper([ window.HTMLDocument || window.Document ], [ "regis terElement" ]);
4252 }
4253 forwardMethodsToWrapper([ window.HTMLBodyElement, window.HTMLDocument || windo w.Document, window.HTMLHeadElement, window.HTMLHtmlElement ], [ "appendChild", " compareDocumentPosition", "contains", "getElementsByClassName", "getElementsByTa gName", "getElementsByTagNameNS", "insertBefore", "querySelector", "querySelecto rAll", "removeChild", "replaceChild" ]);
4254 forwardMethodsToWrapper([ window.HTMLBodyElement, window.HTMLHeadElement, wind ow.HTMLHtmlElement ], matchesNames);
4255 forwardMethodsToWrapper([ window.HTMLDocument || window.Document ], [ "adoptNo de", "importNode", "contains", "createComment", "createDocumentFragment", "creat eElement", "createElementNS", "createEvent", "createEventNS", "createRange", "cr eateTextNode", "createTreeWalker", "elementFromPoint", "getElementById", "getEle mentsByName", "getSelection" ]);
4256 mixin(Document.prototype, GetElementsByInterface);
4257 mixin(Document.prototype, ParentNodeInterface);
4258 mixin(Document.prototype, SelectorsInterface);
4259 mixin(Document.prototype, NonElementParentNodeInterface);
4260 mixin(Document.prototype, {
4261 get implementation() {
4262 var implementation = implementationTable.get(this);
4263 if (implementation) return implementation;
4264 implementation = new DOMImplementation(unwrap(this).implementation);
4265 implementationTable.set(this, implementation);
4266 return implementation;
4267 },
4268 get defaultView() {
4269 return wrap(unwrap(this).defaultView);
4270 }
4271 });
4272 registerWrapper(window.Document, Document, document.implementation.createHTMLD ocument(""));
4273 if (window.HTMLDocument) registerWrapper(window.HTMLDocument, Document);
4274 wrapEventTargetMethods([ window.HTMLBodyElement, window.HTMLDocument || window .Document, window.HTMLHeadElement ]);
4275 function DOMImplementation(impl) {
4276 setWrapper(impl, this);
4277 }
4278 var originalCreateDocument = document.implementation.createDocument;
4279 DOMImplementation.prototype.createDocument = function() {
4280 arguments[2] = unwrap(arguments[2]);
4281 return wrap(originalCreateDocument.apply(unsafeUnwrap(this), arguments));
4282 };
4283 function wrapImplMethod(constructor, name) {
4284 var original = document.implementation[name];
4285 constructor.prototype[name] = function() {
4286 return wrap(original.apply(unsafeUnwrap(this), arguments));
4287 };
4288 }
4289 function forwardImplMethod(constructor, name) {
4290 var original = document.implementation[name];
4291 constructor.prototype[name] = function() {
4292 return original.apply(unsafeUnwrap(this), arguments);
4293 };
4294 }
4295 wrapImplMethod(DOMImplementation, "createDocumentType");
4296 wrapImplMethod(DOMImplementation, "createHTMLDocument");
4297 forwardImplMethod(DOMImplementation, "hasFeature");
4298 registerWrapper(window.DOMImplementation, DOMImplementation);
4299 forwardMethodsToWrapper([ window.DOMImplementation ], [ "createDocument", "cre ateDocumentType", "createHTMLDocument", "hasFeature" ]);
4300 scope.adoptNodeNoRemove = adoptNodeNoRemove;
4301 scope.wrappers.DOMImplementation = DOMImplementation;
4302 scope.wrappers.Document = Document;
4303 })(window.ShadowDOMPolyfill);
4304
4305 (function(scope) {
4306 "use strict";
4307 var EventTarget = scope.wrappers.EventTarget;
4308 var Selection = scope.wrappers.Selection;
4309 var mixin = scope.mixin;
4310 var registerWrapper = scope.registerWrapper;
4311 var renderAllPending = scope.renderAllPending;
4312 var unwrap = scope.unwrap;
4313 var unwrapIfNeeded = scope.unwrapIfNeeded;
4314 var wrap = scope.wrap;
4315 var OriginalWindow = window.Window;
4316 var originalGetComputedStyle = window.getComputedStyle;
4317 var originalGetDefaultComputedStyle = window.getDefaultComputedStyle;
4318 var originalGetSelection = window.getSelection;
4319 function Window(impl) {
4320 EventTarget.call(this, impl);
4321 }
4322 Window.prototype = Object.create(EventTarget.prototype);
4323 OriginalWindow.prototype.getComputedStyle = function(el, pseudo) {
4324 return wrap(this || window).getComputedStyle(unwrapIfNeeded(el), pseudo);
4325 };
4326 if (originalGetDefaultComputedStyle) {
4327 OriginalWindow.prototype.getDefaultComputedStyle = function(el, pseudo) {
4328 return wrap(this || window).getDefaultComputedStyle(unwrapIfNeeded(el), ps eudo);
4329 };
4330 }
4331 OriginalWindow.prototype.getSelection = function() {
4332 return wrap(this || window).getSelection();
4333 };
4334 delete window.getComputedStyle;
4335 delete window.getDefaultComputedStyle;
4336 delete window.getSelection;
4337 [ "addEventListener", "removeEventListener", "dispatchEvent" ].forEach(functio n(name) {
4338 OriginalWindow.prototype[name] = function() {
4339 var w = wrap(this || window);
4340 return w[name].apply(w, arguments);
4341 };
4342 delete window[name];
4343 });
4344 mixin(Window.prototype, {
4345 getComputedStyle: function(el, pseudo) {
4346 renderAllPending();
4347 return originalGetComputedStyle.call(unwrap(this), unwrapIfNeeded(el), pse udo);
4348 },
4349 getSelection: function() {
4350 renderAllPending();
4351 return new Selection(originalGetSelection.call(unwrap(this)));
4352 },
4353 get document() {
4354 return wrap(unwrap(this).document);
4355 }
4356 });
4357 if (originalGetDefaultComputedStyle) {
4358 Window.prototype.getDefaultComputedStyle = function(el, pseudo) {
4359 renderAllPending();
4360 return originalGetDefaultComputedStyle.call(unwrap(this), unwrapIfNeeded(e l), pseudo);
4361 };
4362 }
4363 registerWrapper(OriginalWindow, Window, window);
4364 scope.wrappers.Window = Window;
4365 })(window.ShadowDOMPolyfill);
4366
4367 (function(scope) {
4368 "use strict";
4369 var unwrap = scope.unwrap;
4370 var OriginalDataTransfer = window.DataTransfer || window.Clipboard;
4371 var OriginalDataTransferSetDragImage = OriginalDataTransfer.prototype.setDragI mage;
4372 if (OriginalDataTransferSetDragImage) {
4373 OriginalDataTransfer.prototype.setDragImage = function(image, x, y) {
4374 OriginalDataTransferSetDragImage.call(this, unwrap(image), x, y);
4375 };
4376 }
4377 })(window.ShadowDOMPolyfill);
4378
4379 (function(scope) {
4380 "use strict";
4381 var registerWrapper = scope.registerWrapper;
4382 var setWrapper = scope.setWrapper;
4383 var unwrap = scope.unwrap;
4384 var OriginalFormData = window.FormData;
4385 if (!OriginalFormData) return;
4386 function FormData(formElement) {
4387 var impl;
4388 if (formElement instanceof OriginalFormData) {
4389 impl = formElement;
4390 } else {
4391 impl = new OriginalFormData(formElement && unwrap(formElement));
4392 }
4393 setWrapper(impl, this);
4394 }
4395 registerWrapper(OriginalFormData, FormData, new OriginalFormData());
4396 scope.wrappers.FormData = FormData;
4397 })(window.ShadowDOMPolyfill);
4398
4399 (function(scope) {
4400 "use strict";
4401 var unwrapIfNeeded = scope.unwrapIfNeeded;
4402 var originalSend = XMLHttpRequest.prototype.send;
4403 XMLHttpRequest.prototype.send = function(obj) {
4404 return originalSend.call(this, unwrapIfNeeded(obj));
4405 };
4406 })(window.ShadowDOMPolyfill);
4407
4408 (function(scope) {
4409 "use strict";
4410 var isWrapperFor = scope.isWrapperFor;
4411 var elements = {
4412 a: "HTMLAnchorElement",
4413 area: "HTMLAreaElement",
4414 audio: "HTMLAudioElement",
4415 base: "HTMLBaseElement",
4416 body: "HTMLBodyElement",
4417 br: "HTMLBRElement",
4418 button: "HTMLButtonElement",
4419 canvas: "HTMLCanvasElement",
4420 caption: "HTMLTableCaptionElement",
4421 col: "HTMLTableColElement",
4422 content: "HTMLContentElement",
4423 data: "HTMLDataElement",
4424 datalist: "HTMLDataListElement",
4425 del: "HTMLModElement",
4426 dir: "HTMLDirectoryElement",
4427 div: "HTMLDivElement",
4428 dl: "HTMLDListElement",
4429 embed: "HTMLEmbedElement",
4430 fieldset: "HTMLFieldSetElement",
4431 font: "HTMLFontElement",
4432 form: "HTMLFormElement",
4433 frame: "HTMLFrameElement",
4434 frameset: "HTMLFrameSetElement",
4435 h1: "HTMLHeadingElement",
4436 head: "HTMLHeadElement",
4437 hr: "HTMLHRElement",
4438 html: "HTMLHtmlElement",
4439 iframe: "HTMLIFrameElement",
4440 img: "HTMLImageElement",
4441 input: "HTMLInputElement",
4442 keygen: "HTMLKeygenElement",
4443 label: "HTMLLabelElement",
4444 legend: "HTMLLegendElement",
4445 li: "HTMLLIElement",
4446 link: "HTMLLinkElement",
4447 map: "HTMLMapElement",
4448 marquee: "HTMLMarqueeElement",
4449 menu: "HTMLMenuElement",
4450 menuitem: "HTMLMenuItemElement",
4451 meta: "HTMLMetaElement",
4452 meter: "HTMLMeterElement",
4453 object: "HTMLObjectElement",
4454 ol: "HTMLOListElement",
4455 optgroup: "HTMLOptGroupElement",
4456 option: "HTMLOptionElement",
4457 output: "HTMLOutputElement",
4458 p: "HTMLParagraphElement",
4459 param: "HTMLParamElement",
4460 pre: "HTMLPreElement",
4461 progress: "HTMLProgressElement",
4462 q: "HTMLQuoteElement",
4463 script: "HTMLScriptElement",
4464 select: "HTMLSelectElement",
4465 shadow: "HTMLShadowElement",
4466 source: "HTMLSourceElement",
4467 span: "HTMLSpanElement",
4468 style: "HTMLStyleElement",
4469 table: "HTMLTableElement",
4470 tbody: "HTMLTableSectionElement",
4471 template: "HTMLTemplateElement",
4472 textarea: "HTMLTextAreaElement",
4473 thead: "HTMLTableSectionElement",
4474 time: "HTMLTimeElement",
4475 title: "HTMLTitleElement",
4476 tr: "HTMLTableRowElement",
4477 track: "HTMLTrackElement",
4478 ul: "HTMLUListElement",
4479 video: "HTMLVideoElement"
4480 };
4481 function overrideConstructor(tagName) {
4482 var nativeConstructorName = elements[tagName];
4483 var nativeConstructor = window[nativeConstructorName];
4484 if (!nativeConstructor) return;
4485 var element = document.createElement(tagName);
4486 var wrapperConstructor = element.constructor;
4487 window[nativeConstructorName] = wrapperConstructor;
4488 }
4489 Object.keys(elements).forEach(overrideConstructor);
4490 Object.getOwnPropertyNames(scope.wrappers).forEach(function(name) {
4491 window[name] = scope.wrappers[name];
4492 });
4493 })(window.ShadowDOMPolyfill);
OLDNEW
« no previous file with comments | « packages/web_components/lib/README.md ('k') | packages/web_components/lib/ShadowDOM.min.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698