OLD | NEW |
1 /** | 1 /** |
2 * Copyright (c) 2014 The Polymer Project Authors. All rights reserved. | 2 * Copyright (c) 2014 The Polymer Project Authors. All rights reserved. |
3 * This code may only be used under the BSD style license found at http://polyme
r.github.io/LICENSE.txt | 3 * This code may only be used under the BSD style license found at http://polyme
r.github.io/LICENSE.txt |
4 * The complete set of authors may be found at http://polymer.github.io/AUTHORS.
txt | 4 * The complete set of authors may be found at http://polymer.github.io/AUTHORS.
txt |
5 * The complete set of contributors may be found at http://polymer.github.io/CON
TRIBUTORS.txt | 5 * The complete set of contributors may be found at http://polymer.github.io/CON
TRIBUTORS.txt |
6 * Code distributed by Google as part of the polymer project is also | 6 * Code distributed by Google as part of the polymer project is also |
7 * subject to an additional IP rights grant found at http://polymer.github.io/PA
TENTS.txt | 7 * subject to an additional IP rights grant found at http://polymer.github.io/PA
TENTS.txt |
8 */ | 8 */ |
9 | 9 |
10 window.Platform = window.Platform || {}; | 10 window.Platform = window.Platform || {}; |
(...skipping 2299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2310 'use strict'; | 2310 'use strict'; |
2311 | 2311 |
2312 var setEndOfMicrotask = scope.setEndOfMicrotask | 2312 var setEndOfMicrotask = scope.setEndOfMicrotask |
2313 var wrapIfNeeded = scope.wrapIfNeeded | 2313 var wrapIfNeeded = scope.wrapIfNeeded |
2314 var wrappers = scope.wrappers; | 2314 var wrappers = scope.wrappers; |
2315 | 2315 |
2316 var registrationsTable = new WeakMap(); | 2316 var registrationsTable = new WeakMap(); |
2317 var globalMutationObservers = []; | 2317 var globalMutationObservers = []; |
2318 var isScheduled = false; | 2318 var isScheduled = false; |
2319 | 2319 |
2320 function scheduleCallback() { | 2320 function scheduleCallback(observer) { |
| 2321 if (observer.scheduled_) |
| 2322 return; |
| 2323 |
| 2324 observer.scheduled_ = true; |
| 2325 globalMutationObservers.push(observer); |
| 2326 |
2321 if (isScheduled) | 2327 if (isScheduled) |
2322 return; | 2328 return; |
2323 setEndOfMicrotask(notifyObservers); | 2329 setEndOfMicrotask(notifyObservers); |
2324 isScheduled = true; | 2330 isScheduled = true; |
2325 } | 2331 } |
2326 | 2332 |
2327 // http://dom.spec.whatwg.org/#mutation-observers | 2333 // http://dom.spec.whatwg.org/#mutation-observers |
2328 function notifyObservers() { | 2334 function notifyObservers() { |
2329 isScheduled = false; | 2335 isScheduled = false; |
2330 | 2336 |
2331 while (globalMutationObservers.length) { | 2337 while (globalMutationObservers.length) { |
2332 var notifyList = globalMutationObservers; | 2338 var notifyList = globalMutationObservers; |
2333 globalMutationObservers = []; | 2339 globalMutationObservers = []; |
2334 | 2340 |
2335 // Deliver changes in birth order of the MutationObservers. | 2341 // Deliver changes in birth order of the MutationObservers. |
2336 notifyList.sort(function(x, y) { return x.uid_ - y.uid_; }); | 2342 notifyList.sort(function(x, y) { return x.uid_ - y.uid_; }); |
2337 | 2343 |
2338 for (var i = 0; i < notifyList.length; i++) { | 2344 for (var i = 0; i < notifyList.length; i++) { |
2339 var mo = notifyList[i]; | 2345 var mo = notifyList[i]; |
| 2346 mo.scheduled_ = false; |
2340 var queue = mo.takeRecords(); | 2347 var queue = mo.takeRecords(); |
2341 removeTransientObserversFor(mo); | 2348 removeTransientObserversFor(mo); |
2342 if (queue.length) { | 2349 if (queue.length) { |
2343 mo.callback_(queue, mo); | 2350 mo.callback_(queue, mo); |
2344 } | 2351 } |
2345 } | 2352 } |
2346 } | 2353 } |
2347 } | 2354 } |
2348 | 2355 |
2349 | 2356 |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2445 // true, or type is "characterData" and options's characterDataOldValue | 2452 // true, or type is "characterData" and options's characterDataOldValue |
2446 // is true, set the paired string of registered observer's observer in | 2453 // is true, set the paired string of registered observer's observer in |
2447 // interested observers to oldValue. | 2454 // interested observers to oldValue. |
2448 if (type === 'attributes' && options.attributeOldValue || | 2455 if (type === 'attributes' && options.attributeOldValue || |
2449 type === 'characterData' && options.characterDataOldValue) { | 2456 type === 'characterData' && options.characterDataOldValue) { |
2450 associatedStrings[observer.uid_] = data.oldValue; | 2457 associatedStrings[observer.uid_] = data.oldValue; |
2451 } | 2458 } |
2452 } | 2459 } |
2453 } | 2460 } |
2454 | 2461 |
2455 var anyObserversEnqueued = false; | |
2456 | |
2457 // 4. | 2462 // 4. |
2458 for (var uid in interestedObservers) { | 2463 for (var uid in interestedObservers) { |
2459 var observer = interestedObservers[uid]; | 2464 var observer = interestedObservers[uid]; |
2460 var record = new MutationRecord(type, target); | 2465 var record = new MutationRecord(type, target); |
2461 | 2466 |
2462 // 2. | 2467 // 2. |
2463 if ('name' in data && 'namespace' in data) { | 2468 if ('name' in data && 'namespace' in data) { |
2464 record.attributeName = data.name; | 2469 record.attributeName = data.name; |
2465 record.attributeNamespace = data.namespace; | 2470 record.attributeNamespace = data.namespace; |
2466 } | 2471 } |
(...skipping 12 matching lines...) Expand all Loading... |
2479 | 2484 |
2480 // 6. | 2485 // 6. |
2481 if (data.nextSibling) | 2486 if (data.nextSibling) |
2482 record.nextSibling = data.nextSibling; | 2487 record.nextSibling = data.nextSibling; |
2483 | 2488 |
2484 // 7. | 2489 // 7. |
2485 if (associatedStrings[uid] !== undefined) | 2490 if (associatedStrings[uid] !== undefined) |
2486 record.oldValue = associatedStrings[uid]; | 2491 record.oldValue = associatedStrings[uid]; |
2487 | 2492 |
2488 // 8. | 2493 // 8. |
2489 if (!observer.records_.length) { | 2494 scheduleCallback(observer); |
2490 globalMutationObservers.push(observer); | |
2491 anyObserversEnqueued = true; | |
2492 } | |
2493 observer.records_.push(record); | 2495 observer.records_.push(record); |
2494 } | 2496 } |
2495 | |
2496 if (anyObserversEnqueued) | |
2497 scheduleCallback(); | |
2498 } | 2497 } |
2499 | 2498 |
2500 var slice = Array.prototype.slice; | 2499 var slice = Array.prototype.slice; |
2501 | 2500 |
2502 /** | 2501 /** |
2503 * @param {!Object} options | 2502 * @param {!Object} options |
2504 * @constructor | 2503 * @constructor |
2505 */ | 2504 */ |
2506 function MutationObserverOptions(options) { | 2505 function MutationObserverOptions(options) { |
2507 this.childList = !!options.childList; | 2506 this.childList = !!options.childList; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2550 /** | 2549 /** |
2551 * The class that maps to the DOM MutationObserver interface. | 2550 * The class that maps to the DOM MutationObserver interface. |
2552 * @param {Function} callback. | 2551 * @param {Function} callback. |
2553 * @constructor | 2552 * @constructor |
2554 */ | 2553 */ |
2555 function MutationObserver(callback) { | 2554 function MutationObserver(callback) { |
2556 this.callback_ = callback; | 2555 this.callback_ = callback; |
2557 this.nodes_ = []; | 2556 this.nodes_ = []; |
2558 this.records_ = []; | 2557 this.records_ = []; |
2559 this.uid_ = ++uidCounter; | 2558 this.uid_ = ++uidCounter; |
| 2559 this.scheduled_ = false; |
2560 } | 2560 } |
2561 | 2561 |
2562 MutationObserver.prototype = { | 2562 MutationObserver.prototype = { |
2563 constructor: MutationObserver, | 2563 constructor: MutationObserver, |
2564 | 2564 |
2565 // http://dom.spec.whatwg.org/#dom-mutationobserver-observe | 2565 // http://dom.spec.whatwg.org/#dom-mutationobserver-observe |
2566 observe: function(target, options) { | 2566 observe: function(target, options) { |
2567 target = wrapIfNeeded(target); | 2567 target = wrapIfNeeded(target); |
2568 | 2568 |
2569 var newOptions = new MutationObserverOptions(options); | 2569 var newOptions = new MutationObserverOptions(options); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2635 * Adds a transient observer on node. The transient observer gets removed | 2635 * Adds a transient observer on node. The transient observer gets removed |
2636 * next time we deliver the change records. | 2636 * next time we deliver the change records. |
2637 * @param {Node} node | 2637 * @param {Node} node |
2638 */ | 2638 */ |
2639 addTransientObserver: function(node) { | 2639 addTransientObserver: function(node) { |
2640 // Don't add transient observers on the target itself. We already have all | 2640 // Don't add transient observers on the target itself. We already have all |
2641 // the required listeners set up on the target. | 2641 // the required listeners set up on the target. |
2642 if (node === this.target) | 2642 if (node === this.target) |
2643 return; | 2643 return; |
2644 | 2644 |
| 2645 // Make sure we remove transient observers at the end of microtask, even |
| 2646 // if we didn't get any change records. |
| 2647 scheduleCallback(this.observer); |
| 2648 |
2645 this.transientObservedNodes.push(node); | 2649 this.transientObservedNodes.push(node); |
2646 var registrations = registrationsTable.get(node); | 2650 var registrations = registrationsTable.get(node); |
2647 if (!registrations) | 2651 if (!registrations) |
2648 registrationsTable.set(node, registrations = []); | 2652 registrationsTable.set(node, registrations = []); |
2649 | 2653 |
2650 // We know that registrations does not contain this because we already | 2654 // We know that registrations does not contain this because we already |
2651 // checked if node === this.target. | 2655 // checked if node === this.target. |
2652 registrations.push(this); | 2656 registrations.push(this); |
2653 }, | 2657 }, |
2654 | 2658 |
(...skipping 8711 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
11366 // watch all unwatched roots... | 11370 // watch all unwatched roots... |
11367 var root = node.shadowRoot; | 11371 var root = node.shadowRoot; |
11368 while (root) { | 11372 while (root) { |
11369 watchRoot(root); | 11373 watchRoot(root); |
11370 root = root.olderShadowRoot; | 11374 root = root.olderShadowRoot; |
11371 } | 11375 } |
11372 } | 11376 } |
11373 } | 11377 } |
11374 | 11378 |
11375 function watchRoot(root) { | 11379 function watchRoot(root) { |
11376 if (!root.__watched) { | 11380 observe(root); |
11377 observe(root); | |
11378 root.__watched = true; | |
11379 } | |
11380 } | 11381 } |
11381 | 11382 |
11382 function handler(mutations) { | 11383 function handler(mutations) { |
11383 // | 11384 // |
11384 if (logFlags.dom) { | 11385 if (logFlags.dom) { |
11385 var mx = mutations[0]; | 11386 var mx = mutations[0]; |
11386 if (mx && mx.type === 'childList' && mx.addedNodes) { | 11387 if (mx && mx.type === 'childList' && mx.addedNodes) { |
11387 if (mx.addedNodes) { | 11388 if (mx.addedNodes) { |
11388 var d = mx.addedNodes[0]; | 11389 var d = mx.addedNodes[0]; |
11389 while (d && d !== document && !d.host) { | 11390 while (d && d !== document && !d.host) { |
(...skipping 24 matching lines...) Expand all Loading... |
11414 return; | 11415 return; |
11415 } | 11416 } |
11416 removedNode(n); | 11417 removedNode(n); |
11417 }); | 11418 }); |
11418 } | 11419 } |
11419 //logFlags.dom && console.groupEnd(); | 11420 //logFlags.dom && console.groupEnd(); |
11420 }); | 11421 }); |
11421 logFlags.dom && console.groupEnd(); | 11422 logFlags.dom && console.groupEnd(); |
11422 }; | 11423 }; |
11423 | 11424 |
11424 var observer = new MutationObserver(handler); | 11425 function takeRecords(node) { |
| 11426 // If the optional node is not supplied, assume we mean the whole document. |
| 11427 if (node === undefined) node = document; |
11425 | 11428 |
11426 function takeRecords() { | 11429 // Find the root of the tree, which will be an Document or ShadowRoot. |
11427 // TODO(sjmiles): ask Raf why we have to call handler ourselves | 11430 while (node.parentNode) { |
11428 handler(observer.takeRecords()); | 11431 node = node.parentNode; |
11429 takeMutations(); | 11432 } |
| 11433 |
| 11434 var observer = node.__observer; |
| 11435 if (observer) { |
| 11436 handler(observer.takeRecords()); |
| 11437 takeMutations(); |
| 11438 } |
11430 } | 11439 } |
11431 | 11440 |
11432 var forEach = Array.prototype.forEach.call.bind(Array.prototype.forEach); | 11441 var forEach = Array.prototype.forEach.call.bind(Array.prototype.forEach); |
11433 | 11442 |
11434 function observe(inRoot) { | 11443 function observe(inRoot) { |
| 11444 if (inRoot.__observer) return; |
| 11445 |
| 11446 // For each ShadowRoot, we create a new MutationObserver, so the root can be |
| 11447 // garbage collected once all references to the `inRoot` node are gone. |
| 11448 var observer = new MutationObserver(handler); |
11435 observer.observe(inRoot, {childList: true, subtree: true}); | 11449 observer.observe(inRoot, {childList: true, subtree: true}); |
| 11450 inRoot.__observer = observer; |
11436 } | 11451 } |
11437 | 11452 |
11438 function observeDocument(doc) { | 11453 function observeDocument(doc) { |
11439 observe(doc); | 11454 observe(doc); |
11440 } | 11455 } |
11441 | 11456 |
11442 function upgradeDocument(doc) { | 11457 function upgradeDocument(doc) { |
11443 logFlags.dom && console.group('upgradeDocument: ', (doc.baseURI).split('/').po
p()); | 11458 logFlags.dom && console.group('upgradeDocument: ', (doc.baseURI).split('/').po
p()); |
11444 addedNode(doc); | 11459 addedNode(doc); |
11445 logFlags.dom && console.groupEnd(); | 11460 logFlags.dom && console.groupEnd(); |
(...skipping 884 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
12330 // exports | 12345 // exports |
12331 | 12346 |
12332 scope.marshal = marshal; | 12347 scope.marshal = marshal; |
12333 // `module` confuses commonjs detectors | 12348 // `module` confuses commonjs detectors |
12334 scope.modularize = module; | 12349 scope.modularize = module; |
12335 scope.using = using; | 12350 scope.using = using; |
12336 | 12351 |
12337 })(window); | 12352 })(window); |
12338 | 12353 |
12339 //# sourceMappingURL=platform.concat.js.map | 12354 //# sourceMappingURL=platform.concat.js.map |
OLD | NEW |