OLD | NEW |
(Empty) | |
| 1 /** |
| 2 * @license |
| 3 * Copyright (c) 2014 The Polymer Project Authors. All rights reserved. |
| 4 * This code may only be used under the BSD style license found at http://polyme
r.github.io/LICENSE.txt |
| 5 * The complete set of authors may be found at http://polymer.github.io/AUTHORS.
txt |
| 6 * The complete set of contributors may be found at http://polymer.github.io/CON
TRIBUTORS.txt |
| 7 * Code distributed by Google as part of the polymer project is also |
| 8 * subject to an additional IP rights grant found at http://polymer.github.io/PA
TENTS.txt |
| 9 */ |
| 10 // @version 0.5.1-1 |
| 11 if (typeof WeakMap === "undefined") { |
| 12 (function() { |
| 13 var defineProperty = Object.defineProperty; |
| 14 var counter = Date.now() % 1e9; |
| 15 var WeakMap = function() { |
| 16 this.name = "__st" + (Math.random() * 1e9 >>> 0) + (counter++ + "__"); |
| 17 }; |
| 18 WeakMap.prototype = { |
| 19 set: function(key, value) { |
| 20 var entry = key[this.name]; |
| 21 if (entry && entry[0] === key) entry[1] = value; else defineProperty(key
, this.name, { |
| 22 value: [ key, value ], |
| 23 writable: true |
| 24 }); |
| 25 return this; |
| 26 }, |
| 27 get: function(key) { |
| 28 var entry; |
| 29 return (entry = key[this.name]) && entry[0] === key ? entry[1] : undefin
ed; |
| 30 }, |
| 31 "delete": function(key) { |
| 32 var entry = key[this.name]; |
| 33 if (!entry || entry[0] !== key) return false; |
| 34 entry[0] = entry[1] = undefined; |
| 35 return true; |
| 36 }, |
| 37 has: function(key) { |
| 38 var entry = key[this.name]; |
| 39 if (!entry) return false; |
| 40 return entry[0] === key; |
| 41 } |
| 42 }; |
| 43 window.WeakMap = WeakMap; |
| 44 })(); |
| 45 } |
| 46 |
| 47 (function(global) { |
| 48 var registrationsTable = new WeakMap(); |
| 49 var setImmediate; |
| 50 if (/Trident|Edge/.test(navigator.userAgent)) { |
| 51 setImmediate = setTimeout; |
| 52 } else if (window.setImmediate) { |
| 53 setImmediate = window.setImmediate; |
| 54 } else { |
| 55 var setImmediateQueue = []; |
| 56 var sentinel = String(Math.random()); |
| 57 window.addEventListener("message", function(e) { |
| 58 if (e.data === sentinel) { |
| 59 var queue = setImmediateQueue; |
| 60 setImmediateQueue = []; |
| 61 queue.forEach(function(func) { |
| 62 func(); |
| 63 }); |
| 64 } |
| 65 }); |
| 66 setImmediate = function(func) { |
| 67 setImmediateQueue.push(func); |
| 68 window.postMessage(sentinel, "*"); |
| 69 }; |
| 70 } |
| 71 var isScheduled = false; |
| 72 var scheduledObservers = []; |
| 73 function scheduleCallback(observer) { |
| 74 scheduledObservers.push(observer); |
| 75 if (!isScheduled) { |
| 76 isScheduled = true; |
| 77 setImmediate(dispatchCallbacks); |
| 78 } |
| 79 } |
| 80 function wrapIfNeeded(node) { |
| 81 return window.ShadowDOMPolyfill && window.ShadowDOMPolyfill.wrapIfNeeded(nod
e) || node; |
| 82 } |
| 83 function dispatchCallbacks() { |
| 84 isScheduled = false; |
| 85 var observers = scheduledObservers; |
| 86 scheduledObservers = []; |
| 87 observers.sort(function(o1, o2) { |
| 88 return o1.uid_ - o2.uid_; |
| 89 }); |
| 90 var anyNonEmpty = false; |
| 91 observers.forEach(function(observer) { |
| 92 var queue = observer.takeRecords(); |
| 93 removeTransientObserversFor(observer); |
| 94 if (queue.length) { |
| 95 observer.callback_(queue, observer); |
| 96 anyNonEmpty = true; |
| 97 } |
| 98 }); |
| 99 if (anyNonEmpty) dispatchCallbacks(); |
| 100 } |
| 101 function removeTransientObserversFor(observer) { |
| 102 observer.nodes_.forEach(function(node) { |
| 103 var registrations = registrationsTable.get(node); |
| 104 if (!registrations) return; |
| 105 registrations.forEach(function(registration) { |
| 106 if (registration.observer === observer) registration.removeTransientObse
rvers(); |
| 107 }); |
| 108 }); |
| 109 } |
| 110 function forEachAncestorAndObserverEnqueueRecord(target, callback) { |
| 111 for (var node = target; node; node = node.parentNode) { |
| 112 var registrations = registrationsTable.get(node); |
| 113 if (registrations) { |
| 114 for (var j = 0; j < registrations.length; j++) { |
| 115 var registration = registrations[j]; |
| 116 var options = registration.options; |
| 117 if (node !== target && !options.subtree) continue; |
| 118 var record = callback(options); |
| 119 if (record) registration.enqueue(record); |
| 120 } |
| 121 } |
| 122 } |
| 123 } |
| 124 var uidCounter = 0; |
| 125 function JsMutationObserver(callback) { |
| 126 this.callback_ = callback; |
| 127 this.nodes_ = []; |
| 128 this.records_ = []; |
| 129 this.uid_ = ++uidCounter; |
| 130 } |
| 131 JsMutationObserver.prototype = { |
| 132 observe: function(target, options) { |
| 133 target = wrapIfNeeded(target); |
| 134 if (!options.childList && !options.attributes && !options.characterData ||
options.attributeOldValue && !options.attributes || options.attributeFilter &&
options.attributeFilter.length && !options.attributes || options.characterDataOl
dValue && !options.characterData) { |
| 135 throw new SyntaxError(); |
| 136 } |
| 137 var registrations = registrationsTable.get(target); |
| 138 if (!registrations) registrationsTable.set(target, registrations = []); |
| 139 var registration; |
| 140 for (var i = 0; i < registrations.length; i++) { |
| 141 if (registrations[i].observer === this) { |
| 142 registration = registrations[i]; |
| 143 registration.removeListeners(); |
| 144 registration.options = options; |
| 145 break; |
| 146 } |
| 147 } |
| 148 if (!registration) { |
| 149 registration = new Registration(this, target, options); |
| 150 registrations.push(registration); |
| 151 this.nodes_.push(target); |
| 152 } |
| 153 registration.addListeners(); |
| 154 }, |
| 155 disconnect: function() { |
| 156 this.nodes_.forEach(function(node) { |
| 157 var registrations = registrationsTable.get(node); |
| 158 for (var i = 0; i < registrations.length; i++) { |
| 159 var registration = registrations[i]; |
| 160 if (registration.observer === this) { |
| 161 registration.removeListeners(); |
| 162 registrations.splice(i, 1); |
| 163 break; |
| 164 } |
| 165 } |
| 166 }, this); |
| 167 this.records_ = []; |
| 168 }, |
| 169 takeRecords: function() { |
| 170 var copyOfRecords = this.records_; |
| 171 this.records_ = []; |
| 172 return copyOfRecords; |
| 173 } |
| 174 }; |
| 175 function MutationRecord(type, target) { |
| 176 this.type = type; |
| 177 this.target = target; |
| 178 this.addedNodes = []; |
| 179 this.removedNodes = []; |
| 180 this.previousSibling = null; |
| 181 this.nextSibling = null; |
| 182 this.attributeName = null; |
| 183 this.attributeNamespace = null; |
| 184 this.oldValue = null; |
| 185 } |
| 186 function copyMutationRecord(original) { |
| 187 var record = new MutationRecord(original.type, original.target); |
| 188 record.addedNodes = original.addedNodes.slice(); |
| 189 record.removedNodes = original.removedNodes.slice(); |
| 190 record.previousSibling = original.previousSibling; |
| 191 record.nextSibling = original.nextSibling; |
| 192 record.attributeName = original.attributeName; |
| 193 record.attributeNamespace = original.attributeNamespace; |
| 194 record.oldValue = original.oldValue; |
| 195 return record; |
| 196 } |
| 197 var currentRecord, recordWithOldValue; |
| 198 function getRecord(type, target) { |
| 199 return currentRecord = new MutationRecord(type, target); |
| 200 } |
| 201 function getRecordWithOldValue(oldValue) { |
| 202 if (recordWithOldValue) return recordWithOldValue; |
| 203 recordWithOldValue = copyMutationRecord(currentRecord); |
| 204 recordWithOldValue.oldValue = oldValue; |
| 205 return recordWithOldValue; |
| 206 } |
| 207 function clearRecords() { |
| 208 currentRecord = recordWithOldValue = undefined; |
| 209 } |
| 210 function recordRepresentsCurrentMutation(record) { |
| 211 return record === recordWithOldValue || record === currentRecord; |
| 212 } |
| 213 function selectRecord(lastRecord, newRecord) { |
| 214 if (lastRecord === newRecord) return lastRecord; |
| 215 if (recordWithOldValue && recordRepresentsCurrentMutation(lastRecord)) retur
n recordWithOldValue; |
| 216 return null; |
| 217 } |
| 218 function Registration(observer, target, options) { |
| 219 this.observer = observer; |
| 220 this.target = target; |
| 221 this.options = options; |
| 222 this.transientObservedNodes = []; |
| 223 } |
| 224 Registration.prototype = { |
| 225 enqueue: function(record) { |
| 226 var records = this.observer.records_; |
| 227 var length = records.length; |
| 228 if (records.length > 0) { |
| 229 var lastRecord = records[length - 1]; |
| 230 var recordToReplaceLast = selectRecord(lastRecord, record); |
| 231 if (recordToReplaceLast) { |
| 232 records[length - 1] = recordToReplaceLast; |
| 233 return; |
| 234 } |
| 235 } else { |
| 236 scheduleCallback(this.observer); |
| 237 } |
| 238 records[length] = record; |
| 239 }, |
| 240 addListeners: function() { |
| 241 this.addListeners_(this.target); |
| 242 }, |
| 243 addListeners_: function(node) { |
| 244 var options = this.options; |
| 245 if (options.attributes) node.addEventListener("DOMAttrModified", this, tru
e); |
| 246 if (options.characterData) node.addEventListener("DOMCharacterDataModified
", this, true); |
| 247 if (options.childList) node.addEventListener("DOMNodeInserted", this, true
); |
| 248 if (options.childList || options.subtree) node.addEventListener("DOMNodeRe
moved", this, true); |
| 249 }, |
| 250 removeListeners: function() { |
| 251 this.removeListeners_(this.target); |
| 252 }, |
| 253 removeListeners_: function(node) { |
| 254 var options = this.options; |
| 255 if (options.attributes) node.removeEventListener("DOMAttrModified", this,
true); |
| 256 if (options.characterData) node.removeEventListener("DOMCharacterDataModif
ied", this, true); |
| 257 if (options.childList) node.removeEventListener("DOMNodeInserted", this, t
rue); |
| 258 if (options.childList || options.subtree) node.removeEventListener("DOMNod
eRemoved", this, true); |
| 259 }, |
| 260 addTransientObserver: function(node) { |
| 261 if (node === this.target) return; |
| 262 this.addListeners_(node); |
| 263 this.transientObservedNodes.push(node); |
| 264 var registrations = registrationsTable.get(node); |
| 265 if (!registrations) registrationsTable.set(node, registrations = []); |
| 266 registrations.push(this); |
| 267 }, |
| 268 removeTransientObservers: function() { |
| 269 var transientObservedNodes = this.transientObservedNodes; |
| 270 this.transientObservedNodes = []; |
| 271 transientObservedNodes.forEach(function(node) { |
| 272 this.removeListeners_(node); |
| 273 var registrations = registrationsTable.get(node); |
| 274 for (var i = 0; i < registrations.length; i++) { |
| 275 if (registrations[i] === this) { |
| 276 registrations.splice(i, 1); |
| 277 break; |
| 278 } |
| 279 } |
| 280 }, this); |
| 281 }, |
| 282 handleEvent: function(e) { |
| 283 e.stopImmediatePropagation(); |
| 284 switch (e.type) { |
| 285 case "DOMAttrModified": |
| 286 var name = e.attrName; |
| 287 var namespace = e.relatedNode.namespaceURI; |
| 288 var target = e.target; |
| 289 var record = new getRecord("attributes", target); |
| 290 record.attributeName = name; |
| 291 record.attributeNamespace = namespace; |
| 292 var oldValue = e.attrChange === MutationEvent.ADDITION ? null : e.prevVa
lue; |
| 293 forEachAncestorAndObserverEnqueueRecord(target, function(options) { |
| 294 if (!options.attributes) return; |
| 295 if (options.attributeFilter && options.attributeFilter.length && optio
ns.attributeFilter.indexOf(name) === -1 && options.attributeFilter.indexOf(names
pace) === -1) { |
| 296 return; |
| 297 } |
| 298 if (options.attributeOldValue) return getRecordWithOldValue(oldValue); |
| 299 return record; |
| 300 }); |
| 301 break; |
| 302 |
| 303 case "DOMCharacterDataModified": |
| 304 var target = e.target; |
| 305 var record = getRecord("characterData", target); |
| 306 var oldValue = e.prevValue; |
| 307 forEachAncestorAndObserverEnqueueRecord(target, function(options) { |
| 308 if (!options.characterData) return; |
| 309 if (options.characterDataOldValue) return getRecordWithOldValue(oldVal
ue); |
| 310 return record; |
| 311 }); |
| 312 break; |
| 313 |
| 314 case "DOMNodeRemoved": |
| 315 this.addTransientObserver(e.target); |
| 316 |
| 317 case "DOMNodeInserted": |
| 318 var target = e.relatedNode; |
| 319 var changedNode = e.target; |
| 320 var addedNodes, removedNodes; |
| 321 if (e.type === "DOMNodeInserted") { |
| 322 addedNodes = [ changedNode ]; |
| 323 removedNodes = []; |
| 324 } else { |
| 325 addedNodes = []; |
| 326 removedNodes = [ changedNode ]; |
| 327 } |
| 328 var previousSibling = changedNode.previousSibling; |
| 329 var nextSibling = changedNode.nextSibling; |
| 330 var record = getRecord("childList", target); |
| 331 record.addedNodes = addedNodes; |
| 332 record.removedNodes = removedNodes; |
| 333 record.previousSibling = previousSibling; |
| 334 record.nextSibling = nextSibling; |
| 335 forEachAncestorAndObserverEnqueueRecord(target, function(options) { |
| 336 if (!options.childList) return; |
| 337 return record; |
| 338 }); |
| 339 } |
| 340 clearRecords(); |
| 341 } |
| 342 }; |
| 343 global.JsMutationObserver = JsMutationObserver; |
| 344 if (!global.MutationObserver) global.MutationObserver = JsMutationObserver; |
| 345 })(this); |
| 346 |
| 347 window.CustomElements = window.CustomElements || { |
| 348 flags: {} |
| 349 }; |
| 350 |
| 351 (function(scope) { |
| 352 var flags = scope.flags; |
| 353 var modules = []; |
| 354 var addModule = function(module) { |
| 355 modules.push(module); |
| 356 }; |
| 357 var initializeModules = function() { |
| 358 modules.forEach(function(module) { |
| 359 module(scope); |
| 360 }); |
| 361 }; |
| 362 scope.addModule = addModule; |
| 363 scope.initializeModules = initializeModules; |
| 364 scope.hasNative = Boolean(document.registerElement); |
| 365 scope.useNative = !flags.register && scope.hasNative && !window.ShadowDOMPolyf
ill && (!window.HTMLImports || HTMLImports.useNative); |
| 366 })(CustomElements); |
| 367 |
| 368 CustomElements.addModule(function(scope) { |
| 369 var IMPORT_LINK_TYPE = window.HTMLImports ? HTMLImports.IMPORT_LINK_TYPE : "no
ne"; |
| 370 function forSubtree(node, cb) { |
| 371 findAllElements(node, function(e) { |
| 372 if (cb(e)) { |
| 373 return true; |
| 374 } |
| 375 forRoots(e, cb); |
| 376 }); |
| 377 forRoots(node, cb); |
| 378 } |
| 379 function findAllElements(node, find, data) { |
| 380 var e = node.firstElementChild; |
| 381 if (!e) { |
| 382 e = node.firstChild; |
| 383 while (e && e.nodeType !== Node.ELEMENT_NODE) { |
| 384 e = e.nextSibling; |
| 385 } |
| 386 } |
| 387 while (e) { |
| 388 if (find(e, data) !== true) { |
| 389 findAllElements(e, find, data); |
| 390 } |
| 391 e = e.nextElementSibling; |
| 392 } |
| 393 return null; |
| 394 } |
| 395 function forRoots(node, cb) { |
| 396 var root = node.shadowRoot; |
| 397 while (root) { |
| 398 forSubtree(root, cb); |
| 399 root = root.olderShadowRoot; |
| 400 } |
| 401 } |
| 402 var processingDocuments; |
| 403 function forDocumentTree(doc, cb) { |
| 404 processingDocuments = []; |
| 405 _forDocumentTree(doc, cb); |
| 406 processingDocuments = null; |
| 407 } |
| 408 function _forDocumentTree(doc, cb) { |
| 409 doc = wrap(doc); |
| 410 if (processingDocuments.indexOf(doc) >= 0) { |
| 411 return; |
| 412 } |
| 413 processingDocuments.push(doc); |
| 414 var imports = doc.querySelectorAll("link[rel=" + IMPORT_LINK_TYPE + "]"); |
| 415 for (var i = 0, l = imports.length, n; i < l && (n = imports[i]); i++) { |
| 416 if (n.import) { |
| 417 _forDocumentTree(n.import, cb); |
| 418 } |
| 419 } |
| 420 cb(doc); |
| 421 } |
| 422 scope.forDocumentTree = forDocumentTree; |
| 423 scope.forSubtree = forSubtree; |
| 424 }); |
| 425 |
| 426 CustomElements.addModule(function(scope) { |
| 427 var flags = scope.flags; |
| 428 var forSubtree = scope.forSubtree; |
| 429 var forDocumentTree = scope.forDocumentTree; |
| 430 function addedNode(node) { |
| 431 return added(node) || addedSubtree(node); |
| 432 } |
| 433 function added(node) { |
| 434 if (scope.upgrade(node)) { |
| 435 return true; |
| 436 } |
| 437 attached(node); |
| 438 } |
| 439 function addedSubtree(node) { |
| 440 forSubtree(node, function(e) { |
| 441 if (added(e)) { |
| 442 return true; |
| 443 } |
| 444 }); |
| 445 } |
| 446 function attachedNode(node) { |
| 447 attached(node); |
| 448 if (inDocument(node)) { |
| 449 forSubtree(node, function(e) { |
| 450 attached(e); |
| 451 }); |
| 452 } |
| 453 } |
| 454 var hasPolyfillMutations = !window.MutationObserver || window.MutationObserver
=== window.JsMutationObserver; |
| 455 scope.hasPolyfillMutations = hasPolyfillMutations; |
| 456 var isPendingMutations = false; |
| 457 var pendingMutations = []; |
| 458 function deferMutation(fn) { |
| 459 pendingMutations.push(fn); |
| 460 if (!isPendingMutations) { |
| 461 isPendingMutations = true; |
| 462 setTimeout(takeMutations); |
| 463 } |
| 464 } |
| 465 function takeMutations() { |
| 466 isPendingMutations = false; |
| 467 var $p = pendingMutations; |
| 468 for (var i = 0, l = $p.length, p; i < l && (p = $p[i]); i++) { |
| 469 p(); |
| 470 } |
| 471 pendingMutations = []; |
| 472 } |
| 473 function attached(element) { |
| 474 if (hasPolyfillMutations) { |
| 475 deferMutation(function() { |
| 476 _attached(element); |
| 477 }); |
| 478 } else { |
| 479 _attached(element); |
| 480 } |
| 481 } |
| 482 function _attached(element) { |
| 483 if (element.__upgraded__ && (element.attachedCallback || element.detachedCal
lback)) { |
| 484 if (!element.__attached && inDocument(element)) { |
| 485 element.__attached = true; |
| 486 if (element.attachedCallback) { |
| 487 element.attachedCallback(); |
| 488 } |
| 489 } |
| 490 } |
| 491 } |
| 492 function detachedNode(node) { |
| 493 detached(node); |
| 494 forSubtree(node, function(e) { |
| 495 detached(e); |
| 496 }); |
| 497 } |
| 498 function detached(element) { |
| 499 if (hasPolyfillMutations) { |
| 500 deferMutation(function() { |
| 501 _detached(element); |
| 502 }); |
| 503 } else { |
| 504 _detached(element); |
| 505 } |
| 506 } |
| 507 function _detached(element) { |
| 508 if (element.__upgraded__ && (element.attachedCallback || element.detachedCal
lback)) { |
| 509 if (element.__attached && !inDocument(element)) { |
| 510 element.__attached = false; |
| 511 if (element.detachedCallback) { |
| 512 element.detachedCallback(); |
| 513 } |
| 514 } |
| 515 } |
| 516 } |
| 517 function inDocument(element) { |
| 518 var p = element; |
| 519 var doc = wrap(document); |
| 520 while (p) { |
| 521 if (p == doc) { |
| 522 return true; |
| 523 } |
| 524 p = p.parentNode || p.host; |
| 525 } |
| 526 } |
| 527 function watchShadow(node) { |
| 528 if (node.shadowRoot && !node.shadowRoot.__watched) { |
| 529 flags.dom && console.log("watching shadow-root for: ", node.localName); |
| 530 var root = node.shadowRoot; |
| 531 while (root) { |
| 532 observe(root); |
| 533 root = root.olderShadowRoot; |
| 534 } |
| 535 } |
| 536 } |
| 537 function handler(mutations) { |
| 538 if (flags.dom) { |
| 539 var mx = mutations[0]; |
| 540 if (mx && mx.type === "childList" && mx.addedNodes) { |
| 541 if (mx.addedNodes) { |
| 542 var d = mx.addedNodes[0]; |
| 543 while (d && d !== document && !d.host) { |
| 544 d = d.parentNode; |
| 545 } |
| 546 var u = d && (d.URL || d._URL || d.host && d.host.localName) || ""; |
| 547 u = u.split("/?").shift().split("/").pop(); |
| 548 } |
| 549 } |
| 550 console.group("mutations (%d) [%s]", mutations.length, u || ""); |
| 551 } |
| 552 mutations.forEach(function(mx) { |
| 553 if (mx.type === "childList") { |
| 554 forEach(mx.addedNodes, function(n) { |
| 555 if (!n.localName) { |
| 556 return; |
| 557 } |
| 558 addedNode(n); |
| 559 }); |
| 560 forEach(mx.removedNodes, function(n) { |
| 561 if (!n.localName) { |
| 562 return; |
| 563 } |
| 564 detachedNode(n); |
| 565 }); |
| 566 } |
| 567 }); |
| 568 flags.dom && console.groupEnd(); |
| 569 } |
| 570 function takeRecords(node) { |
| 571 node = wrap(node); |
| 572 if (!node) { |
| 573 node = wrap(document); |
| 574 } |
| 575 while (node.parentNode) { |
| 576 node = node.parentNode; |
| 577 } |
| 578 var observer = node.__observer; |
| 579 if (observer) { |
| 580 handler(observer.takeRecords()); |
| 581 takeMutations(); |
| 582 } |
| 583 } |
| 584 var forEach = Array.prototype.forEach.call.bind(Array.prototype.forEach); |
| 585 function observe(inRoot) { |
| 586 if (inRoot.__observer) { |
| 587 return; |
| 588 } |
| 589 var observer = new MutationObserver(handler); |
| 590 observer.observe(inRoot, { |
| 591 childList: true, |
| 592 subtree: true |
| 593 }); |
| 594 inRoot.__observer = observer; |
| 595 } |
| 596 function upgradeDocument(doc) { |
| 597 doc = wrap(doc); |
| 598 flags.dom && console.group("upgradeDocument: ", doc.baseURI.split("/").pop()
); |
| 599 addedNode(doc); |
| 600 observe(doc); |
| 601 flags.dom && console.groupEnd(); |
| 602 } |
| 603 function upgradeDocumentTree(doc) { |
| 604 forDocumentTree(doc, upgradeDocument); |
| 605 } |
| 606 var originalCreateShadowRoot = Element.prototype.createShadowRoot; |
| 607 Element.prototype.createShadowRoot = function() { |
| 608 var root = originalCreateShadowRoot.call(this); |
| 609 CustomElements.watchShadow(this); |
| 610 return root; |
| 611 }; |
| 612 scope.watchShadow = watchShadow; |
| 613 scope.upgradeDocumentTree = upgradeDocumentTree; |
| 614 scope.upgradeSubtree = addedSubtree; |
| 615 scope.upgradeAll = addedNode; |
| 616 scope.attachedNode = attachedNode; |
| 617 scope.takeRecords = takeRecords; |
| 618 }); |
| 619 |
| 620 CustomElements.addModule(function(scope) { |
| 621 var flags = scope.flags; |
| 622 function upgrade(node) { |
| 623 if (!node.__upgraded__ && node.nodeType === Node.ELEMENT_NODE) { |
| 624 var is = node.getAttribute("is"); |
| 625 var definition = scope.getRegisteredDefinition(is || node.localName); |
| 626 if (definition) { |
| 627 if (is && definition.tag == node.localName) { |
| 628 return upgradeWithDefinition(node, definition); |
| 629 } else if (!is && !definition.extends) { |
| 630 return upgradeWithDefinition(node, definition); |
| 631 } |
| 632 } |
| 633 } |
| 634 } |
| 635 function upgradeWithDefinition(element, definition) { |
| 636 flags.upgrade && console.group("upgrade:", element.localName); |
| 637 if (definition.is) { |
| 638 element.setAttribute("is", definition.is); |
| 639 } |
| 640 implementPrototype(element, definition); |
| 641 element.__upgraded__ = true; |
| 642 created(element); |
| 643 scope.attachedNode(element); |
| 644 scope.upgradeSubtree(element); |
| 645 flags.upgrade && console.groupEnd(); |
| 646 return element; |
| 647 } |
| 648 function implementPrototype(element, definition) { |
| 649 if (Object.__proto__) { |
| 650 element.__proto__ = definition.prototype; |
| 651 } else { |
| 652 customMixin(element, definition.prototype, definition.native); |
| 653 element.__proto__ = definition.prototype; |
| 654 } |
| 655 } |
| 656 function customMixin(inTarget, inSrc, inNative) { |
| 657 var used = {}; |
| 658 var p = inSrc; |
| 659 while (p !== inNative && p !== HTMLElement.prototype) { |
| 660 var keys = Object.getOwnPropertyNames(p); |
| 661 for (var i = 0, k; k = keys[i]; i++) { |
| 662 if (!used[k]) { |
| 663 Object.defineProperty(inTarget, k, Object.getOwnPropertyDescriptor(p,
k)); |
| 664 used[k] = 1; |
| 665 } |
| 666 } |
| 667 p = Object.getPrototypeOf(p); |
| 668 } |
| 669 } |
| 670 function created(element) { |
| 671 if (element.createdCallback) { |
| 672 element.createdCallback(); |
| 673 } |
| 674 } |
| 675 scope.upgrade = upgrade; |
| 676 scope.upgradeWithDefinition = upgradeWithDefinition; |
| 677 scope.implementPrototype = implementPrototype; |
| 678 }); |
| 679 |
| 680 CustomElements.addModule(function(scope) { |
| 681 var upgradeDocumentTree = scope.upgradeDocumentTree; |
| 682 var upgrade = scope.upgrade; |
| 683 var upgradeWithDefinition = scope.upgradeWithDefinition; |
| 684 var implementPrototype = scope.implementPrototype; |
| 685 var useNative = scope.useNative; |
| 686 function register(name, options) { |
| 687 var definition = options || {}; |
| 688 if (!name) { |
| 689 throw new Error("document.registerElement: first argument `name` must not
be empty"); |
| 690 } |
| 691 if (name.indexOf("-") < 0) { |
| 692 throw new Error("document.registerElement: first argument ('name') must co
ntain a dash ('-'). Argument provided was '" + String(name) + "'."); |
| 693 } |
| 694 if (isReservedTag(name)) { |
| 695 throw new Error("Failed to execute 'registerElement' on 'Document': Regist
ration failed for type '" + String(name) + "'. The type name is invalid."); |
| 696 } |
| 697 if (getRegisteredDefinition(name)) { |
| 698 throw new Error("DuplicateDefinitionError: a type with name '" + String(na
me) + "' is already registered"); |
| 699 } |
| 700 if (!definition.prototype) { |
| 701 definition.prototype = Object.create(HTMLElement.prototype); |
| 702 } |
| 703 definition.__name = name.toLowerCase(); |
| 704 definition.lifecycle = definition.lifecycle || {}; |
| 705 definition.ancestry = ancestry(definition.extends); |
| 706 resolveTagName(definition); |
| 707 resolvePrototypeChain(definition); |
| 708 overrideAttributeApi(definition.prototype); |
| 709 registerDefinition(definition.__name, definition); |
| 710 definition.ctor = generateConstructor(definition); |
| 711 definition.ctor.prototype = definition.prototype; |
| 712 definition.prototype.constructor = definition.ctor; |
| 713 if (scope.ready) { |
| 714 upgradeDocumentTree(document); |
| 715 } |
| 716 return definition.ctor; |
| 717 } |
| 718 function overrideAttributeApi(prototype) { |
| 719 if (prototype.setAttribute._polyfilled) { |
| 720 return; |
| 721 } |
| 722 var setAttribute = prototype.setAttribute; |
| 723 prototype.setAttribute = function(name, value) { |
| 724 changeAttribute.call(this, name, value, setAttribute); |
| 725 }; |
| 726 var removeAttribute = prototype.removeAttribute; |
| 727 prototype.removeAttribute = function(name) { |
| 728 changeAttribute.call(this, name, null, removeAttribute); |
| 729 }; |
| 730 prototype.setAttribute._polyfilled = true; |
| 731 } |
| 732 function changeAttribute(name, value, operation) { |
| 733 name = name.toLowerCase(); |
| 734 var oldValue = this.getAttribute(name); |
| 735 operation.apply(this, arguments); |
| 736 var newValue = this.getAttribute(name); |
| 737 if (this.attributeChangedCallback && newValue !== oldValue) { |
| 738 this.attributeChangedCallback(name, oldValue, newValue); |
| 739 } |
| 740 } |
| 741 function isReservedTag(name) { |
| 742 for (var i = 0; i < reservedTagList.length; i++) { |
| 743 if (name === reservedTagList[i]) { |
| 744 return true; |
| 745 } |
| 746 } |
| 747 } |
| 748 var reservedTagList = [ "annotation-xml", "color-profile", "font-face", "font-
face-src", "font-face-uri", "font-face-format", "font-face-name", "missing-glyph
" ]; |
| 749 function ancestry(extnds) { |
| 750 var extendee = getRegisteredDefinition(extnds); |
| 751 if (extendee) { |
| 752 return ancestry(extendee.extends).concat([ extendee ]); |
| 753 } |
| 754 return []; |
| 755 } |
| 756 function resolveTagName(definition) { |
| 757 var baseTag = definition.extends; |
| 758 for (var i = 0, a; a = definition.ancestry[i]; i++) { |
| 759 baseTag = a.is && a.tag; |
| 760 } |
| 761 definition.tag = baseTag || definition.__name; |
| 762 if (baseTag) { |
| 763 definition.is = definition.__name; |
| 764 } |
| 765 } |
| 766 function resolvePrototypeChain(definition) { |
| 767 if (!Object.__proto__) { |
| 768 var nativePrototype = HTMLElement.prototype; |
| 769 if (definition.is) { |
| 770 var inst = document.createElement(definition.tag); |
| 771 var expectedPrototype = Object.getPrototypeOf(inst); |
| 772 if (expectedPrototype === definition.prototype) { |
| 773 nativePrototype = expectedPrototype; |
| 774 } |
| 775 } |
| 776 var proto = definition.prototype, ancestor; |
| 777 while (proto && proto !== nativePrototype) { |
| 778 ancestor = Object.getPrototypeOf(proto); |
| 779 proto.__proto__ = ancestor; |
| 780 proto = ancestor; |
| 781 } |
| 782 definition.native = nativePrototype; |
| 783 } |
| 784 } |
| 785 function instantiate(definition) { |
| 786 return upgradeWithDefinition(domCreateElement(definition.tag), definition); |
| 787 } |
| 788 var registry = {}; |
| 789 function getRegisteredDefinition(name) { |
| 790 if (name) { |
| 791 return registry[name.toLowerCase()]; |
| 792 } |
| 793 } |
| 794 function registerDefinition(name, definition) { |
| 795 registry[name] = definition; |
| 796 } |
| 797 function generateConstructor(definition) { |
| 798 return function() { |
| 799 return instantiate(definition); |
| 800 }; |
| 801 } |
| 802 var HTML_NAMESPACE = "http://www.w3.org/1999/xhtml"; |
| 803 function createElementNS(namespace, tag, typeExtension) { |
| 804 if (namespace === HTML_NAMESPACE) { |
| 805 return createElement(tag, typeExtension); |
| 806 } else { |
| 807 return domCreateElementNS(namespace, tag); |
| 808 } |
| 809 } |
| 810 function createElement(tag, typeExtension) { |
| 811 var definition = getRegisteredDefinition(typeExtension || tag); |
| 812 if (definition) { |
| 813 if (tag == definition.tag && typeExtension == definition.is) { |
| 814 return new definition.ctor(); |
| 815 } |
| 816 if (!typeExtension && !definition.is) { |
| 817 return new definition.ctor(); |
| 818 } |
| 819 } |
| 820 var element; |
| 821 if (typeExtension) { |
| 822 element = createElement(tag); |
| 823 element.setAttribute("is", typeExtension); |
| 824 return element; |
| 825 } |
| 826 element = domCreateElement(tag); |
| 827 if (tag.indexOf("-") >= 0) { |
| 828 implementPrototype(element, HTMLElement); |
| 829 } |
| 830 return element; |
| 831 } |
| 832 function cloneNode(deep) { |
| 833 var n = domCloneNode.call(this, deep); |
| 834 upgrade(n); |
| 835 return n; |
| 836 } |
| 837 var domCreateElement = document.createElement.bind(document); |
| 838 var domCreateElementNS = document.createElementNS.bind(document); |
| 839 var domCloneNode = Node.prototype.cloneNode; |
| 840 var isInstance; |
| 841 if (!Object.__proto__ && !useNative) { |
| 842 isInstance = function(obj, ctor) { |
| 843 var p = obj; |
| 844 while (p) { |
| 845 if (p === ctor.prototype) { |
| 846 return true; |
| 847 } |
| 848 p = p.__proto__; |
| 849 } |
| 850 return false; |
| 851 }; |
| 852 } else { |
| 853 isInstance = function(obj, base) { |
| 854 return obj instanceof base; |
| 855 }; |
| 856 } |
| 857 document.registerElement = register; |
| 858 document.createElement = createElement; |
| 859 document.createElementNS = createElementNS; |
| 860 Node.prototype.cloneNode = cloneNode; |
| 861 scope.registry = registry; |
| 862 scope.instanceof = isInstance; |
| 863 scope.reservedTagList = reservedTagList; |
| 864 scope.getRegisteredDefinition = getRegisteredDefinition; |
| 865 document.register = document.registerElement; |
| 866 }); |
| 867 |
| 868 (function(scope) { |
| 869 var useNative = scope.useNative; |
| 870 var initializeModules = scope.initializeModules; |
| 871 var isIE11OrOlder = /Trident/.test(navigator.userAgent); |
| 872 if (useNative) { |
| 873 var nop = function() {}; |
| 874 scope.watchShadow = nop; |
| 875 scope.upgrade = nop; |
| 876 scope.upgradeAll = nop; |
| 877 scope.upgradeDocumentTree = nop; |
| 878 scope.upgradeSubtree = nop; |
| 879 scope.takeRecords = nop; |
| 880 scope.instanceof = function(obj, base) { |
| 881 return obj instanceof base; |
| 882 }; |
| 883 } else { |
| 884 initializeModules(); |
| 885 } |
| 886 var upgradeDocumentTree = scope.upgradeDocumentTree; |
| 887 if (!window.wrap) { |
| 888 if (window.ShadowDOMPolyfill) { |
| 889 window.wrap = ShadowDOMPolyfill.wrapIfNeeded; |
| 890 window.unwrap = ShadowDOMPolyfill.unwrapIfNeeded; |
| 891 } else { |
| 892 window.wrap = window.unwrap = function(node) { |
| 893 return node; |
| 894 }; |
| 895 } |
| 896 } |
| 897 function bootstrap() { |
| 898 upgradeDocumentTree(wrap(document)); |
| 899 if (window.HTMLImports) { |
| 900 HTMLImports.__importsParsingHook = function(elt) { |
| 901 upgradeDocumentTree(wrap(elt.import)); |
| 902 }; |
| 903 } |
| 904 CustomElements.ready = true; |
| 905 setTimeout(function() { |
| 906 CustomElements.readyTime = Date.now(); |
| 907 if (window.HTMLImports) { |
| 908 CustomElements.elapsed = CustomElements.readyTime - HTMLImports.readyTim
e; |
| 909 } |
| 910 document.dispatchEvent(new CustomEvent("WebComponentsReady", { |
| 911 bubbles: true |
| 912 })); |
| 913 }); |
| 914 } |
| 915 if (isIE11OrOlder && typeof window.CustomEvent !== "function") { |
| 916 window.CustomEvent = function(inType, params) { |
| 917 params = params || {}; |
| 918 var e = document.createEvent("CustomEvent"); |
| 919 e.initCustomEvent(inType, Boolean(params.bubbles), Boolean(params.cancelab
le), params.detail); |
| 920 return e; |
| 921 }; |
| 922 window.CustomEvent.prototype = window.Event.prototype; |
| 923 } |
| 924 if (document.readyState === "complete" || scope.flags.eager) { |
| 925 bootstrap(); |
| 926 } else if (document.readyState === "interactive" && !window.attachEvent && (!w
indow.HTMLImports || window.HTMLImports.ready)) { |
| 927 bootstrap(); |
| 928 } else { |
| 929 var loadEvent = window.HTMLImports && !HTMLImports.ready ? "HTMLImportsLoade
d" : "DOMContentLoaded"; |
| 930 window.addEventListener(loadEvent, bootstrap); |
| 931 } |
| 932 })(window.CustomElements); |
OLD | NEW |