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