@@ -9,10 +9,22 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
99*/
1010
1111import './boot.js' ;
12+ import { wrap } from './wrap.js' ;
1213
1314const ShadyDOM = window . ShadyDOM ;
1415const ShadyCSS = window . ShadyCSS ;
1516
17+ /**
18+ * Return true if node scope is correct.
19+ *
20+ * @param {!Element } node Node to check scope
21+ * @param {!Node } scope Scope reference
22+ * @return {boolean } True if node is in scope
23+ */
24+ function sameScope ( node , scope ) {
25+ return wrap ( node ) . getRootNode ( ) === scope ;
26+ }
27+
1628/**
1729 * Ensure that elements in a ShadowDOM container are scoped correctly.
1830 * This function is only needed when ShadyDOM is used and unpatched DOM APIs are used in third party code.
@@ -38,13 +50,20 @@ export function scopeSubtree(container, shouldObserve = false) {
3850 }
3951 // capture correct scope for container
4052 const containerScope = ScopingShim [ 'scopeForNode' ] ( container ) ;
53+ const root = wrap ( container ) . getRootNode ( ) ;
4154
4255 const scopify = ( node ) => {
56+ if ( ! sameScope ( node , root ) ) {
57+ return ;
58+ }
4359 // NOTE: native qSA does not honor scoped DOM, but it is faster, and the same behavior as Polymer v1
4460 const elements = Array . from ( ShadyDOM [ 'nativeMethods' ] [ 'querySelectorAll' ] . call ( node , '*' ) ) ;
4561 elements . push ( node ) ;
4662 for ( let i = 0 ; i < elements . length ; i ++ ) {
4763 const el = elements [ i ] ;
64+ if ( ! sameScope ( el , root ) ) {
65+ continue ;
66+ }
4867 const currentScope = ScopingShim [ 'currentScopeForNode' ] ( el ) ;
4968 if ( currentScope !== containerScope ) {
5069 if ( currentScope !== '' ) {
0 commit comments