-
Notifications
You must be signed in to change notification settings - Fork 2k
Implement scopeSubtree for ShadyDOM noPatch mode #5537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
@license | ||
Copyright (c) 2019 The Polymer Project Authors. All rights reserved. | ||
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
Code distributed by Google as part of the polymer project is also | ||
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
*/ | ||
|
||
import './boot.js'; | ||
|
||
const ShadyDOM = window.ShadyDOM; | ||
const ShadyCSS = window.ShadyCSS; | ||
|
||
/** | ||
* Ensure that elements in a ShadowDOM container are scoped correctly. | ||
* This function is only needed when ShadyDOM is used and unpatched DOM APIs are used in third party code. | ||
* This can happen in noPatch mode or when specialized APIs like ranges or tables are used to mutate DOM. | ||
* | ||
* @param {!Element} container Container element to scope | ||
* @param {boolean=} shouldObserve if true, start a mutation observer for added nodes to the container | ||
* @return {?MutationObserver} Returns a new MutationObserver on `container` if `shouldObserve` is true. | ||
*/ | ||
export function scopeSubtree(container, shouldObserve = false) { | ||
// If using native ShadowDOM, abort | ||
if (!ShadyDOM || !ShadyCSS) { | ||
return null; | ||
} | ||
// ShadyCSS handles DOM mutations when ShadyDOM does not handle scoping itself | ||
if (!ShadyDOM['handlesDynamicScoping']) { | ||
return null; | ||
} | ||
const ScopingShim = ShadyCSS['ScopingShim']; | ||
// if ScopingShim is not available, abort | ||
if (!ScopingShim) { | ||
return null; | ||
} | ||
// capture correct scope for container | ||
const containerScope = ScopingShim['scopeForNode'](container); | ||
|
||
const scopify = (node) => { | ||
// NOTE: native qSA does not honor scoped DOM, but it is faster, and the same behavior as Polymer v1 | ||
const elements = Array.from(ShadyDOM['nativeMethods']['querySelectorAll'].call(node, '*')); | ||
elements.push(node); | ||
for (let i = 0; i < elements.length; i++) { | ||
const el = elements[i]; | ||
const currentScope = ScopingShim['currentScopeForNode'](el); | ||
if (currentScope !== containerScope) { | ||
if (currentScope !== '') { | ||
ScopingShim['unscopeNode'](el, currentScope); | ||
} | ||
ScopingShim['scopeNode'](el, containerScope); | ||
} | ||
} | ||
}; | ||
|
||
// scope everything in container | ||
scopify(container); | ||
|
||
if (shouldObserve) { | ||
const mo = new MutationObserver((mxns) => { | ||
for (let i = 0; i < mxns.length; i++) { | ||
const mxn = mxns[i]; | ||
for (let j = 0; j < mxn.addedNodes.length; j++) { | ||
const addedNode = mxn.addedNodes[j]; | ||
if (addedNode.nodeType === Node.ELEMENT_NODE) { | ||
scopify(addedNode); | ||
} | ||
} | ||
} | ||
}); | ||
mo.observe(container, {childList: true, subtree: true}); | ||
return mo; | ||
} else { | ||
return null; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<!doctype html> | ||
<!-- | ||
@license | ||
Copyright (c) 2019 The Polymer Project Authors. All rights reserved. | ||
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
Code distributed by Google as part of the polymer project is also | ||
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
--> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<script> | ||
window.ShadyDOM = {force: true, noPatch: true}; | ||
</script> | ||
<script src="../../node_modules/@webcomponents/webcomponentsjs/webcomponents-bundle.js"></script> | ||
<script src="wct-browser-config.js"></script> | ||
<script src="../../node_modules/wct-browser-legacy/browser.js"></script> | ||
</head> | ||
<body> | ||
|
||
<script type="module"> | ||
import {Polymer, html} from '../../polymer-legacy.js'; | ||
Polymer({ | ||
is: 'scope-subtree-legacy', | ||
_template: html` | ||
<style> | ||
#container * { | ||
border: 10px solid black; | ||
} | ||
</style> | ||
<div id="container"></div>` | ||
}); | ||
</script> | ||
|
||
<script type="module"> | ||
import {PolymerElement, html} from '../../polymer-element.js'; | ||
class ScopeSubtreeElement extends PolymerElement { | ||
static get template() { | ||
return html` | ||
<style> | ||
#container * { | ||
border: 10px solid black; | ||
} | ||
</style> | ||
<div id="container"></div>`; | ||
} | ||
} | ||
customElements.define('scope-subtree-element', ScopeSubtreeElement); | ||
</script> | ||
|
||
<test-fixture id="legacy"> | ||
<template> | ||
<scope-subtree-legacy></scope-subtree-legacy> | ||
</template> | ||
</test-fixture> | ||
|
||
<test-fixture id="element"> | ||
<template> | ||
<scope-subtree-element></scope-subtree-element> | ||
</template> | ||
</test-fixture> | ||
|
||
<script type="module"> | ||
import { scopeSubtree } from '../../lib/utils/scope-subtree.js'; | ||
|
||
function assertComputed(element, value, property, pseudo) { | ||
var computed = getComputedStyle(element, pseudo); | ||
property = property || 'border-top-width'; | ||
if (Array.isArray(value)) { | ||
assert.oneOf(computed[property], value, 'computed style incorrect for ' + property); | ||
} else { | ||
assert.equal(computed[property], value, 'computed style incorrect for ' + property); | ||
} | ||
} | ||
|
||
suite('LegacyElement.scopeSubtree', function() { | ||
let el; | ||
setup(function() { | ||
el = fixture('legacy'); | ||
}); | ||
test('elements are scoped', function() { | ||
const div = document.createElement('div'); | ||
const innerDiv = document.createElement('div'); | ||
div.appendChild(innerDiv); | ||
el.$.container.appendChild(div); | ||
el.scopeSubtree(el.$.container); | ||
assertComputed(div, '10px'); | ||
assertComputed(innerDiv, '10px'); | ||
}); | ||
test('second argument creates a mutation observer', async function() { | ||
const mo = el.scopeSubtree(el.$.container, true); | ||
assert.instanceOf(mo, MutationObserver); | ||
const div = document.createElement('div'); | ||
const innerDiv = document.createElement('div'); | ||
div.appendChild(innerDiv); | ||
el.$.container.appendChild(div); | ||
await new Promise((resolve) => setTimeout(resolve, 0)); | ||
assertComputed(div, '10px'); | ||
assertComputed(innerDiv, '10px'); | ||
}); | ||
}); | ||
|
||
suite('PolymerElement and scopeSubtree util', function() { | ||
let el; | ||
setup(function() { | ||
el = fixture('element'); | ||
}); | ||
test('elements are scoped', function() { | ||
const div = document.createElement('div'); | ||
const innerDiv = document.createElement('div'); | ||
div.appendChild(innerDiv); | ||
el.$.container.appendChild(div); | ||
scopeSubtree(el.$.container); | ||
assertComputed(div, '10px'); | ||
assertComputed(innerDiv, '10px'); | ||
}); | ||
test('second argument creates a mutation observer', async function() { | ||
const mo = scopeSubtree(el.$.container, true); | ||
assert.instanceOf(mo, MutationObserver); | ||
const div = document.createElement('div'); | ||
const innerDiv = document.createElement('div'); | ||
div.appendChild(innerDiv); | ||
el.$.container.appendChild(div); | ||
await new Promise((resolve) => setTimeout(resolve, 0)); | ||
assertComputed(div, '10px'); | ||
assertComputed(innerDiv, '10px'); | ||
}); | ||
}); | ||
</script> | ||
</body> | ||
</html> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.