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(observer) { | 2320 function scheduleCallback() { |
2321 if (observer.scheduled_) | |
2322 return; | |
2323 | |
2324 observer.scheduled_ = true; | |
2325 globalMutationObservers.push(observer); | |
2326 | |
2327 if (isScheduled) | 2321 if (isScheduled) |
2328 return; | 2322 return; |
2329 setEndOfMicrotask(notifyObservers); | 2323 setEndOfMicrotask(notifyObservers); |
2330 isScheduled = true; | 2324 isScheduled = true; |
2331 } | 2325 } |
2332 | 2326 |
2333 // http://dom.spec.whatwg.org/#mutation-observers | 2327 // http://dom.spec.whatwg.org/#mutation-observers |
2334 function notifyObservers() { | 2328 function notifyObservers() { |
2335 isScheduled = false; | 2329 isScheduled = false; |
2336 | 2330 |
2337 while (globalMutationObservers.length) { | 2331 while (globalMutationObservers.length) { |
2338 var notifyList = globalMutationObservers; | 2332 var notifyList = globalMutationObservers; |
2339 globalMutationObservers = []; | 2333 globalMutationObservers = []; |
2340 | 2334 |
2341 // Deliver changes in birth order of the MutationObservers. | 2335 // Deliver changes in birth order of the MutationObservers. |
2342 notifyList.sort(function(x, y) { return x.uid_ - y.uid_; }); | 2336 notifyList.sort(function(x, y) { return x.uid_ - y.uid_; }); |
2343 | 2337 |
2344 for (var i = 0; i < notifyList.length; i++) { | 2338 for (var i = 0; i < notifyList.length; i++) { |
2345 var mo = notifyList[i]; | 2339 var mo = notifyList[i]; |
2346 mo.scheduled_ = false; | |
2347 var queue = mo.takeRecords(); | 2340 var queue = mo.takeRecords(); |
2348 removeTransientObserversFor(mo); | 2341 removeTransientObserversFor(mo); |
2349 if (queue.length) { | 2342 if (queue.length) { |
2350 mo.callback_(queue, mo); | 2343 mo.callback_(queue, mo); |
2351 } | 2344 } |
2352 } | 2345 } |
2353 } | 2346 } |
2354 } | 2347 } |
2355 | 2348 |
2356 | 2349 |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2452 // true, or type is "characterData" and options's characterDataOldValue | 2445 // true, or type is "characterData" and options's characterDataOldValue |
2453 // is true, set the paired string of registered observer's observer in | 2446 // is true, set the paired string of registered observer's observer in |
2454 // interested observers to oldValue. | 2447 // interested observers to oldValue. |
2455 if (type === 'attributes' && options.attributeOldValue || | 2448 if (type === 'attributes' && options.attributeOldValue || |
2456 type === 'characterData' && options.characterDataOldValue) { | 2449 type === 'characterData' && options.characterDataOldValue) { |
2457 associatedStrings[observer.uid_] = data.oldValue; | 2450 associatedStrings[observer.uid_] = data.oldValue; |
2458 } | 2451 } |
2459 } | 2452 } |
2460 } | 2453 } |
2461 | 2454 |
| 2455 var anyObserversEnqueued = false; |
| 2456 |
2462 // 4. | 2457 // 4. |
2463 for (var uid in interestedObservers) { | 2458 for (var uid in interestedObservers) { |
2464 var observer = interestedObservers[uid]; | 2459 var observer = interestedObservers[uid]; |
2465 var record = new MutationRecord(type, target); | 2460 var record = new MutationRecord(type, target); |
2466 | 2461 |
2467 // 2. | 2462 // 2. |
2468 if ('name' in data && 'namespace' in data) { | 2463 if ('name' in data && 'namespace' in data) { |
2469 record.attributeName = data.name; | 2464 record.attributeName = data.name; |
2470 record.attributeNamespace = data.namespace; | 2465 record.attributeNamespace = data.namespace; |
2471 } | 2466 } |
(...skipping 12 matching lines...) Expand all Loading... |
2484 | 2479 |
2485 // 6. | 2480 // 6. |
2486 if (data.nextSibling) | 2481 if (data.nextSibling) |
2487 record.nextSibling = data.nextSibling; | 2482 record.nextSibling = data.nextSibling; |
2488 | 2483 |
2489 // 7. | 2484 // 7. |
2490 if (associatedStrings[uid] !== undefined) | 2485 if (associatedStrings[uid] !== undefined) |
2491 record.oldValue = associatedStrings[uid]; | 2486 record.oldValue = associatedStrings[uid]; |
2492 | 2487 |
2493 // 8. | 2488 // 8. |
2494 scheduleCallback(observer); | 2489 if (!observer.records_.length) { |
| 2490 globalMutationObservers.push(observer); |
| 2491 anyObserversEnqueued = true; |
| 2492 } |
2495 observer.records_.push(record); | 2493 observer.records_.push(record); |
2496 } | 2494 } |
| 2495 |
| 2496 if (anyObserversEnqueued) |
| 2497 scheduleCallback(); |
2497 } | 2498 } |
2498 | 2499 |
2499 var slice = Array.prototype.slice; | 2500 var slice = Array.prototype.slice; |
2500 | 2501 |
2501 /** | 2502 /** |
2502 * @param {!Object} options | 2503 * @param {!Object} options |
2503 * @constructor | 2504 * @constructor |
2504 */ | 2505 */ |
2505 function MutationObserverOptions(options) { | 2506 function MutationObserverOptions(options) { |
2506 this.childList = !!options.childList; | 2507 this.childList = !!options.childList; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2549 /** | 2550 /** |
2550 * The class that maps to the DOM MutationObserver interface. | 2551 * The class that maps to the DOM MutationObserver interface. |
2551 * @param {Function} callback. | 2552 * @param {Function} callback. |
2552 * @constructor | 2553 * @constructor |
2553 */ | 2554 */ |
2554 function MutationObserver(callback) { | 2555 function MutationObserver(callback) { |
2555 this.callback_ = callback; | 2556 this.callback_ = callback; |
2556 this.nodes_ = []; | 2557 this.nodes_ = []; |
2557 this.records_ = []; | 2558 this.records_ = []; |
2558 this.uid_ = ++uidCounter; | 2559 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 | |
2649 this.transientObservedNodes.push(node); | 2645 this.transientObservedNodes.push(node); |
2650 var registrations = registrationsTable.get(node); | 2646 var registrations = registrationsTable.get(node); |
2651 if (!registrations) | 2647 if (!registrations) |
2652 registrationsTable.set(node, registrations = []); | 2648 registrationsTable.set(node, registrations = []); |
2653 | 2649 |
2654 // We know that registrations does not contain this because we already | 2650 // We know that registrations does not contain this because we already |
2655 // checked if node === this.target. | 2651 // checked if node === this.target. |
2656 registrations.push(this); | 2652 registrations.push(this); |
2657 }, | 2653 }, |
2658 | 2654 |
(...skipping 8711 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
11370 // watch all unwatched roots... | 11366 // watch all unwatched roots... |
11371 var root = node.shadowRoot; | 11367 var root = node.shadowRoot; |
11372 while (root) { | 11368 while (root) { |
11373 watchRoot(root); | 11369 watchRoot(root); |
11374 root = root.olderShadowRoot; | 11370 root = root.olderShadowRoot; |
11375 } | 11371 } |
11376 } | 11372 } |
11377 } | 11373 } |
11378 | 11374 |
11379 function watchRoot(root) { | 11375 function watchRoot(root) { |
11380 observe(root); | 11376 if (!root.__watched) { |
| 11377 observe(root); |
| 11378 root.__watched = true; |
| 11379 } |
11381 } | 11380 } |
11382 | 11381 |
11383 function handler(mutations) { | 11382 function handler(mutations) { |
11384 // | 11383 // |
11385 if (logFlags.dom) { | 11384 if (logFlags.dom) { |
11386 var mx = mutations[0]; | 11385 var mx = mutations[0]; |
11387 if (mx && mx.type === 'childList' && mx.addedNodes) { | 11386 if (mx && mx.type === 'childList' && mx.addedNodes) { |
11388 if (mx.addedNodes) { | 11387 if (mx.addedNodes) { |
11389 var d = mx.addedNodes[0]; | 11388 var d = mx.addedNodes[0]; |
11390 while (d && d !== document && !d.host) { | 11389 while (d && d !== document && !d.host) { |
(...skipping 24 matching lines...) Expand all Loading... |
11415 return; | 11414 return; |
11416 } | 11415 } |
11417 removedNode(n); | 11416 removedNode(n); |
11418 }); | 11417 }); |
11419 } | 11418 } |
11420 //logFlags.dom && console.groupEnd(); | 11419 //logFlags.dom && console.groupEnd(); |
11421 }); | 11420 }); |
11422 logFlags.dom && console.groupEnd(); | 11421 logFlags.dom && console.groupEnd(); |
11423 }; | 11422 }; |
11424 | 11423 |
11425 function takeRecords(node) { | 11424 var observer = new MutationObserver(handler); |
11426 // If the optional node is not supplied, assume we mean the whole document. | |
11427 if (node === undefined) node = document; | |
11428 | 11425 |
11429 // Find the root of the tree, which will be an Document or ShadowRoot. | 11426 function takeRecords() { |
11430 while (node.parentNode) { | 11427 // TODO(sjmiles): ask Raf why we have to call handler ourselves |
11431 node = node.parentNode; | 11428 handler(observer.takeRecords()); |
11432 } | 11429 takeMutations(); |
11433 | |
11434 var observer = node.__observer; | |
11435 if (observer) { | |
11436 handler(observer.takeRecords()); | |
11437 takeMutations(); | |
11438 } | |
11439 } | 11430 } |
11440 | 11431 |
11441 var forEach = Array.prototype.forEach.call.bind(Array.prototype.forEach); | 11432 var forEach = Array.prototype.forEach.call.bind(Array.prototype.forEach); |
11442 | 11433 |
11443 function observe(inRoot) { | 11434 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); | |
11449 observer.observe(inRoot, {childList: true, subtree: true}); | 11435 observer.observe(inRoot, {childList: true, subtree: true}); |
11450 inRoot.__observer = observer; | |
11451 } | 11436 } |
11452 | 11437 |
11453 function observeDocument(doc) { | 11438 function observeDocument(doc) { |
11454 observe(doc); | 11439 observe(doc); |
11455 } | 11440 } |
11456 | 11441 |
11457 function upgradeDocument(doc) { | 11442 function upgradeDocument(doc) { |
11458 logFlags.dom && console.group('upgradeDocument: ', (doc.baseURI).split('/').po
p()); | 11443 logFlags.dom && console.group('upgradeDocument: ', (doc.baseURI).split('/').po
p()); |
11459 addedNode(doc); | 11444 addedNode(doc); |
11460 logFlags.dom && console.groupEnd(); | 11445 logFlags.dom && console.groupEnd(); |
(...skipping 884 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
12345 // exports | 12330 // exports |
12346 | 12331 |
12347 scope.marshal = marshal; | 12332 scope.marshal = marshal; |
12348 // `module` confuses commonjs detectors | 12333 // `module` confuses commonjs detectors |
12349 scope.modularize = module; | 12334 scope.modularize = module; |
12350 scope.using = using; | 12335 scope.using = using; |
12351 | 12336 |
12352 })(window); | 12337 })(window); |
12353 | 12338 |
12354 //# sourceMappingURL=platform.concat.js.map | 12339 //# sourceMappingURL=platform.concat.js.map |
OLD | NEW |