@@ -82,30 +82,57 @@ export var ExtentLayer = LayerGroup.extend({
8282 if ( this . _extentEl . _opacitySlider )
8383 this . _extentEl . _opacitySlider . value = opacity ;
8484 } ,
85- appendStyleLink : function ( mapLink ) {
86- if ( ! mapLink . link ) return ;
87- let positionAndNode = this . _getStylePositionAndNode ( ) ;
88- positionAndNode . node . insertAdjacentElement (
89- positionAndNode . position ,
90- mapLink . link
91- ) ;
92- } ,
93- _getStylePositionAndNode : function ( ) {
94- return this . _container . lastChild &&
95- ( this . _container . lastChild . nodeName . toUpperCase ( ) === 'SVG' ||
96- this . _container . lastChild . classList . contains ( 'mapml-vector-container' ) )
97- ? { position : 'beforebegin' , node : this . _container . lastChild }
98- : this . _container . lastChild
99- ? { position : 'afterend' , node : this . _container . lastChild }
100- : { position : 'afterbegin' , node : this . _container } ;
101- } ,
102- appendStyleElement : function ( mapStyle ) {
103- if ( ! mapStyle . styleElement ) return ;
104- let positionAndNode = this . _getStylePositionAndNode ( ) ;
105- positionAndNode . node . insertAdjacentElement (
106- positionAndNode . position ,
107- mapStyle . styleElement
108- ) ;
85+ renderStyles : function ( mapStyleOrLink ) {
86+ let e = mapStyleOrLink . link || mapStyleOrLink . styleElement ;
87+ if ( e === undefined ) return ;
88+
89+ const getStylePositionAndNode = ( styleOrLink ) => {
90+ // Get all already rendered <style> or <link> elements in the shadow DOM container
91+ const renderedSiblingStyles = Array . from (
92+ this . _container . querySelectorAll ( 'style, link[rel="stylesheet"]' )
93+ ) ;
94+
95+ // If there are no rendered styles or links yet, insert before any content
96+ if ( renderedSiblingStyles . length === 0 ) {
97+ return this . _container . lastChild &&
98+ ( this . _container . lastChild . nodeName . toUpperCase ( ) === 'SVG' ||
99+ this . _container . lastChild . classList . contains (
100+ 'mapml-vector-container'
101+ ) )
102+ ? { position : 'beforebegin' , node : this . _container . lastChild }
103+ : this . _container . lastChild
104+ ? { position : 'afterend' , node : this . _container . lastChild }
105+ : { position : 'afterbegin' , node : this . _container } ;
106+ }
107+
108+ // Peek into the light DOM context for comparison
109+ const mapStyleOrLinkLightDOMElement =
110+ styleOrLink . mapStyle || styleOrLink . mapLink ;
111+
112+ // Traverse the rendered siblings in the shadow DOM
113+ for ( let i = 0 ; i < renderedSiblingStyles . length ; i ++ ) {
114+ const rendered = renderedSiblingStyles [ i ] ;
115+ const siblingMapStyleOrLinkLightDOMElement =
116+ rendered . mapStyle || rendered . mapLink ;
117+
118+ // Compare the light DOM order
119+ if (
120+ siblingMapStyleOrLinkLightDOMElement &&
121+ mapStyleOrLinkLightDOMElement . compareDocumentPosition (
122+ siblingMapStyleOrLinkLightDOMElement
123+ ) & Node . DOCUMENT_POSITION_FOLLOWING
124+ ) {
125+ // Insert the new style or link before the already-rendered sibling element
126+ return { position : 'beforebegin' , node : rendered } ;
127+ }
128+ }
129+
130+ // If no preceding sibling was found, insert after the last rendered element
131+ return { position : 'afterend' , node : renderedSiblingStyles . at ( - 1 ) } ;
132+ } ;
133+
134+ let positionAndNode = getStylePositionAndNode ( e ) ;
135+ positionAndNode . node . insertAdjacentElement ( positionAndNode . position , e ) ;
109136 }
110137} ) ;
111138export var extentLayer = function ( options ) {
0 commit comments