@@ -111,19 +111,6 @@ const createElm = (oldParentVNode: d.VNode, newParentVNode: d.VNode, childIndex:
111
111
elm . classList . add ( ( elm [ 's-si' ] = scopeId ) ) ;
112
112
}
113
113
114
- if ( BUILD . scoped ) {
115
- // to be able to style the deep nested scoped component from the root component,
116
- // root component's scope id needs to be added to the child nodes since sass compiler
117
- // adds scope id to the nested selectors during compilation phase
118
- const rootScopeId =
119
- newParentVNode . $elm$ ?. [ 's-rsc' ] || newParentVNode . $elm$ ?. [ 's-si' ] || newParentVNode . $elm$ ?. [ 's-sc' ] ;
120
-
121
- if ( rootScopeId ) {
122
- elm [ 's-rsc' ] = rootScopeId ;
123
- ! elm . classList . contains ( rootScopeId ) && elm . classList . add ( rootScopeId ) ;
124
- }
125
- }
126
-
127
114
if ( newVNode . $children$ ) {
128
115
for ( i = 0 ; i < newVNode . $children$ . length ; ++ i ) {
129
116
// create the node
@@ -206,7 +193,7 @@ const relocateToHostRoot = (parentElm: Element) => {
206
193
for ( const childNode of contentRefNode ? childNodeArray . reverse ( ) : childNodeArray ) {
207
194
// Only relocate nodes that were slotted in
208
195
if ( childNode [ 's-sh' ] != null ) {
209
- host . insertBefore ( childNode , contentRefNode ?? null ) ;
196
+ insertBefore ( host , childNode , contentRefNode ?? null ) ;
210
197
211
198
// Reset so we can correctly move the node around again.
212
199
childNode [ 's-sh' ] = undefined ;
@@ -237,7 +224,7 @@ const putBackInOriginalLocation = (parentElm: d.RenderNode, recursive: boolean)
237
224
const childNode = oldSlotChildNodes [ i ] as any ;
238
225
if ( childNode [ 's-hn' ] !== hostTagName && childNode [ 's-ol' ] ) {
239
226
// and relocate it back to it's original location
240
- parentReferenceNode ( childNode ) . insertBefore ( childNode , referenceNode ( childNode ) ) ;
227
+ insertBefore ( parentReferenceNode ( childNode ) , childNode , referenceNode ( childNode ) ) ;
241
228
242
229
// remove the old original location comment entirely
243
230
// later on the patch function will know what to do
@@ -293,7 +280,7 @@ const addVnodes = (
293
280
childNode = createElm ( null , parentVNode , startIdx , parentElm ) ;
294
281
if ( childNode ) {
295
282
vnodes [ startIdx ] . $elm$ = childNode as any ;
296
- containerElm . insertBefore ( childNode , BUILD . slotRelocation ? referenceNode ( before ) : before ) ;
283
+ insertBefore ( containerElm , childNode , BUILD . slotRelocation ? referenceNode ( before ) : before ) ;
297
284
}
298
285
}
299
286
}
@@ -490,7 +477,7 @@ const updateChildren = (
490
477
// `parentElm`. Luckily, `Node.nextSibling` will return `null` if there
491
478
// aren't any siblings, and passing `null` to `Node.insertBefore` will
492
479
// append it to the children of the parent element.
493
- parentElm . insertBefore ( oldStartVnode . $elm$ , oldEndVnode . $elm$ . nextSibling as any ) ;
480
+ insertBefore ( parentElm , oldStartVnode . $elm$ , oldEndVnode . $elm$ . nextSibling as any ) ;
494
481
oldStartVnode = oldCh [ ++ oldStartIdx ] ;
495
482
newEndVnode = newCh [ -- newEndIdx ] ;
496
483
} else if ( isSameVnode ( oldEndVnode , newStartVnode , isInitialRender ) ) {
@@ -518,7 +505,7 @@ const updateChildren = (
518
505
// can move the element for `oldEndVnode` _before_ the element for
519
506
// `oldStartVnode`, leaving `oldStartVnode` to be reconciled in the
520
507
// future.
521
- parentElm . insertBefore ( oldEndVnode . $elm$ , oldStartVnode . $elm$ ) ;
508
+ insertBefore ( parentElm , oldEndVnode . $elm$ , oldStartVnode . $elm$ ) ;
522
509
oldEndVnode = oldCh [ -- oldEndIdx ] ;
523
510
newStartVnode = newCh [ ++ newStartIdx ] ;
524
511
} else {
@@ -569,9 +556,9 @@ const updateChildren = (
569
556
if ( node ) {
570
557
// if we created a new node then handle inserting it to the DOM
571
558
if ( BUILD . slotRelocation ) {
572
- parentReferenceNode ( oldStartVnode . $elm$ ) . insertBefore ( node , referenceNode ( oldStartVnode . $elm$ ) ) ;
559
+ insertBefore ( parentReferenceNode ( oldStartVnode . $elm$ ) , node , referenceNode ( oldStartVnode . $elm$ ) ) ;
573
560
} else {
574
- oldStartVnode . $elm$ . parentNode . insertBefore ( node , oldStartVnode . $elm$ ) ;
561
+ insertBefore ( oldStartVnode . $elm$ . parentNode , node , oldStartVnode . $elm$ ) ;
575
562
}
576
563
}
577
564
}
@@ -924,6 +911,52 @@ export const nullifyVNodeRefs = (vNode: d.VNode) => {
924
911
}
925
912
} ;
926
913
914
+ /**
915
+ * Inserts a node before a reference node as a child of a specified parent node.
916
+ * Additionally, adds parent element's scope id as class to the new node.
917
+ *
918
+ * @param parent parent node
919
+ * @param newNode element to be inserted
920
+ * @param reference anchor element
921
+ * @returns inserted node
922
+ */
923
+ export const insertBefore = ( parent : Node , newNode : Node , reference ?: Node ) : Node => {
924
+ const inserted = parent ?. insertBefore ( newNode , reference ) ;
925
+
926
+ if ( BUILD . scoped ) {
927
+ setParentScopeIdAsClassName ( newNode as d . RenderNode , parent as d . RenderNode ) ;
928
+ }
929
+
930
+ return inserted ;
931
+ } ;
932
+
933
+ const findParentScopeId = ( element : d . RenderNode ) : string | undefined => {
934
+ return element
935
+ ? element [ 's-rsc' ] || element [ 's-si' ] || element [ 's-sc' ] || findParentScopeId ( element . parentElement )
936
+ : undefined ;
937
+ } ;
938
+
939
+ /**
940
+ * to be able to style the deep nested scoped component from the root component,
941
+ * root component's scope id needs to be added to the child nodes since sass compiler
942
+ * adds scope id to the nested selectors during compilation phase
943
+ *
944
+ * @param element an element to be updated
945
+ * @param parent a parent element that scope id is retrieved
946
+ */
947
+ export const setParentScopeIdAsClassName = ( element : d . RenderNode , parent : d . RenderNode ) => {
948
+ if ( element && parent ) {
949
+ const oldRootScopeId = element [ 's-rsc' ] ;
950
+ const newRootScopeId = findParentScopeId ( parent ) ;
951
+
952
+ oldRootScopeId && element . classList ?. contains ( oldRootScopeId ) && element . classList . remove ( oldRootScopeId ) ;
953
+ if ( newRootScopeId ) {
954
+ element [ 's-rsc' ] = newRootScopeId ;
955
+ ! element . classList ?. contains ( newRootScopeId ) && element . classList ?. add ( newRootScopeId ) ;
956
+ }
957
+ }
958
+ } ;
959
+
927
960
/**
928
961
* Information about nodes to be relocated in order to support
929
962
* `<slot>` elements in scoped (i.e. non-shadow DOM) components
@@ -1045,7 +1078,7 @@ render() {
1045
1078
: ( doc . createTextNode ( '' ) as any ) ;
1046
1079
orgLocationNode [ 's-nr' ] = nodeToRelocate ;
1047
1080
1048
- nodeToRelocate . parentNode . insertBefore ( ( nodeToRelocate [ 's-ol' ] = orgLocationNode ) , nodeToRelocate ) ;
1081
+ insertBefore ( nodeToRelocate . parentNode , ( nodeToRelocate [ 's-ol' ] = orgLocationNode ) , nodeToRelocate ) ;
1049
1082
}
1050
1083
}
1051
1084
@@ -1115,7 +1148,7 @@ render() {
1115
1148
// If we get to this point and `insertBeforeNode` is `null`, that means
1116
1149
// we're just going to append the node as the last child of the parent. Passing
1117
1150
// `null` as the second arg here will trigger that behavior.
1118
- parentNodeRef . insertBefore ( nodeToRelocate , insertBeforeNode ) ;
1151
+ insertBefore ( parentNodeRef , nodeToRelocate , insertBeforeNode ) ;
1119
1152
1120
1153
// Reset the `hidden` value back to what it was defined as originally
1121
1154
// This solves a problem where a `slot` is dynamically rendered and `hidden` may have
0 commit comments