Skip to content

Commit 99ee8cf

Browse files
prushforprushforth
authored andcommitted
Support for map-link[rel=stylesheet] and map-style in map-extent
Closes #944 Add sloMo to ArrowKeyNaveContextMenu.test.js
1 parent 86bafc1 commit 99ee8cf

File tree

11 files changed

+284
-105
lines changed

11 files changed

+284
-105
lines changed

src/map-extent.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,11 @@ export class MapExtent extends HTMLElement {
262262
// this._layerControlHTML is the fieldset for the extent in the LayerControl
263263
this._layerControlHTML = this._createLayerControlExtentHTML();
264264
this._calculateBounds();
265+
// instead of children using parents' whenReady which can be cyclic,
266+
// when this element is ready, run stuff that is only available after
267+
// initialization
268+
this._runMutationObserver(this.children);
269+
// make sure same thing happens when children are added
265270
this._bindMutationObserver();
266271
}
267272
/*
@@ -290,6 +295,16 @@ export class MapExtent extends HTMLElement {
290295
this._validateDisabled();
291296
});
292297
};
298+
const _addStylesheetLink = (mapLink) => {
299+
this.whenReady().then(() => {
300+
this._extentLayer.appendStyleLink(mapLink);
301+
});
302+
};
303+
const _addStyleElement = (mapStyle) => {
304+
this.whenReady().then(() => {
305+
this._extentLayer.appendStyleElement(mapStyle);
306+
});
307+
};
293308
for (let i = 0; i < elementsGroup.length; ++i) {
294309
let element = elementsGroup[i];
295310
switch (element.nodeName) {
@@ -303,7 +318,14 @@ export class MapExtent extends HTMLElement {
303318
}
304319
break;
305320
case 'MAP-LINK':
306-
// might need this in future, among others
321+
if (element.link && !element.link.isConnected)
322+
_addStylesheetLink(element);
323+
break;
324+
case 'MAP-STYLE':
325+
if (element.styleElement && !element.styleElement.isConnected) {
326+
_addStyleElement(element);
327+
}
328+
break;
307329
default:
308330
break;
309331
}

src/map-link.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ export class MapLink extends HTMLElement {
316316
this._stylesheetHost._layer.appendStyleLink(this);
317317
} else if (this._stylesheetHost._templatedLayer) {
318318
this._stylesheetHost._templatedLayer.appendStyleLink(this);
319+
} else if (this._stylesheetHost._extentLayer) {
320+
this._stylesheetHost._extentLayer.appendStyleLink(this);
319321
}
320322

321323
function copyAttributes(source, target) {

src/map-style.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ export class MapStyle extends HTMLElement {
2727
this.styleElement.mapStyle = this;
2828
this.styleElement.textContent = this.textContent;
2929
copyAttributes(this, this.styleElement);
30-
3130
if (this._stylesheetHost._layer) {
3231
this._stylesheetHost._layer.appendStyleElement(this);
3332
} else if (this._stylesheetHost._templatedLayer) {
3433
this._stylesheetHost._templatedLayer.appendStyleElement(this);
34+
} else if (this._stylesheetHost._extentLayer) {
35+
this._stylesheetHost._extentLayer.appendStyleElement(this);
3536
}
3637

3738
function copyAttributes(source, target) {

src/mapml/layers/ExtentLayer.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,31 @@ export var ExtentLayer = L.LayerGroup.extend({
7575
this._extentEl._opacity = opacity;
7676
if (this._extentEl._opacitySlider)
7777
this._extentEl._opacitySlider.value = opacity;
78+
},
79+
appendStyleLink: function (mapLink) {
80+
if (!mapLink.link) return;
81+
let positionAndNode = this._getStylePositionAndNode();
82+
positionAndNode.node.insertAdjacentElement(
83+
positionAndNode.position,
84+
mapLink.link
85+
);
86+
},
87+
_getStylePositionAndNode: function () {
88+
return this._container.lastChild &&
89+
(this._container.lastChild.nodeName.toUpperCase() === 'SVG' ||
90+
this._container.lastChild.classList.contains('mapml-vector-container'))
91+
? { position: 'beforebegin', node: this._container.lastChild }
92+
: this._container.lastChild
93+
? { position: 'afterend', node: this._container.lastChild }
94+
: { position: 'afterbegin', node: this._container };
95+
},
96+
appendStyleElement: function (mapStyle) {
97+
if (!mapStyle.styleElement) return;
98+
let positionAndNode = this._getStylePositionAndNode();
99+
positionAndNode.node.insertAdjacentElement(
100+
positionAndNode.position,
101+
mapStyle.styleElement
102+
);
78103
}
79104
});
80105
export var extentLayer = function (options) {

src/mapml/layers/TemplatedTileLayer.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,15 @@ export var TemplatedTileLayer = L.TileLayer.extend({
196196
if (href) {
197197
if (!container.querySelector("link[href='" + href + "']")) {
198198
var linkElm = document.createElement('link');
199+
copyAttributes(stylesheets[i], linkElm);
199200
linkElm.setAttribute('href', href);
200-
linkElm.setAttribute('rel', 'stylesheet');
201201
ss.push(linkElm);
202202
}
203203
}
204204
} else {
205205
// <map-style>
206206
var styleElm = document.createElement('style');
207+
copyAttributes(stylesheets[i], styleElm);
207208
styleElm.textContent = stylesheets[i].textContent;
208209
ss.push(styleElm);
209210
}
@@ -215,6 +216,12 @@ export var TemplatedTileLayer = L.TileLayer.extend({
215216
for (var s = ss.length - 1; s >= 0; s--) {
216217
container.insertAdjacentElement('afterbegin', ss[s]);
217218
}
219+
function copyAttributes(source, target) {
220+
return Array.from(source.attributes).forEach((attribute) => {
221+
if (attribute.nodeName !== 'href')
222+
target.setAttribute(attribute.nodeName, attribute.nodeValue);
223+
});
224+
}
218225
},
219226

220227
_createFeatures: function (markup, coords, tile) {

test/e2e/core/ArrowKeyNavContextMenu.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ test.describe('Using arrow keys to navigate context menu', () => {
44
let page;
55
let context;
66
test.beforeAll(async function () {
7-
context = await chromium.launchPersistentContext('');
7+
context = await chromium.launchPersistentContext('', { slowMo: 250 });
88
page =
99
context.pages().find((page) => page.url() === 'about:blank') ||
1010
(await context.newPage());

test/e2e/core/styleParsing.html

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@
2222
<body>
2323
<map data-testid="map1" is="web-map" projection="CBMTILE" zoom="2" lat="45.5052040" lon="-75.2202344" controls>
2424

25-
<layer- label="Arizona" checked>
25+
<layer- data-testid="arizona" label="Arizona" checked>
2626
<map-meta name="projection" content="CBMTILE"></map-meta>
2727
<map-meta name="zoom" content="min=0,max=5,value=0"></map-meta>
2828
<map-meta name="extent"
2929
content="cs=tilematrix,top-left-vertical=0,top-left-horizontal=0,bottom-right-vertical=5,bottom-right-horizontal=5"></map-meta>
30-
30+
<!-- Local-content styles rendered as style/link in same order as found, in expected shadow root location -->
3131
<map-link id="first" rel="stylesheet" type="text/css" href="first.css"></map-link>
32-
<map-style>
32+
<map-style id="second">
3333
.second {
3434
stroke: aqua;
3535
}
3636
</map-style>
37-
<map-style>
37+
<map-style id="third">
3838
.third {
3939
stroke: blue
4040
}
@@ -88,40 +88,50 @@ <h1>Colorado</h1>
8888
</map-geometry>
8989
</map-feature>
9090
</layer->
91-
<layer- label="Alabama" checked>
91+
<layer- data-testid="alabama" label="Alabama" checked>
9292
<map-meta name="zoom" content="min=0,max=10"></map-meta>
9393
<map-extent units="CBMTILE" checked hidden>
9494
<map-input name="z" type="zoom" min="2" max="18" ></map-input>
9595
<map-input name="xmin" type="location" units="gcrs" axis="longitude" position="top-left" min="-76" max="-74" ></map-input>
9696
<map-input name="ymin" type="location" units="gcrs" axis="latitude" position="bottom-right" min="45" max="46" ></map-input>
9797
<map-input name="xmax" type="location" units="gcrs" axis="longitude" position="bottom-right" min="-76" max="-74" ></map-input>
9898
<map-input name="ymax" type="location" units="gcrs" axis="latitude" position="top-left" min="45" max="46" ></map-input>
99-
<map-link rel="features" tref="data/alabama_feature.mapml?{xmin}{ymin}{xmax}{ymax}{z}" ></map-link>
99+
<!-- Local layer content map-link[rel=features] remote styles rendered as style/link in same order as found, in expected shadow root location -->
100+
<map-link data-testid="alabama-features" rel="features" tref="data/alabama_feature.mapml?{xmin}{ymin}{xmax}{ymax}{z}" ></map-link>
100101
</map-extent>
101102
</layer->
102103
</map>
103104
<map data-testid="map2" is="web-map" projection="WGS84" zoom="0" lat="0" lon="0" controls>
104105

105106
<layer- label="Inline Countries" checked>
106-
<map-link rel="stylesheet" href="styleParsing.css"></map-link>
107-
<map-style>
108-
.secondVector {
107+
<map-style id="one">
108+
.firstVector {
109109
color: aliceblue;
110110
}
111111
</map-style>
112+
<map-link id="two" rel="stylesheet" href="styleParsing.css"></map-link>
112113
<map-meta name="zoom" content="min=0,max=10"></map-meta>
113-
<map-extent units="WGS84" checked hidden>
114+
<map-extent data-testid="map-ext1" units="WGS84" checked hidden>
115+
<!-- Local style children of map-extent rendered as style/link in same order as found, in expected shadow root location -->
116+
<map-style id="one">.foo { bar:none; }</map-style>
117+
<map-link id="two" rel="stylesheet" href="styleParsing.css"></map-link>
114118
<map-input name="zoomLevel" type="zoom" min="0" max="2" value="0" ></map-input>
115119

116120
<map-input name="row" type="location" axis="row" units="tilematrix" min="0" max="5" ></map-input>
117121
<map-input name="col" type="location" axis="column" units="tilematrix" min="0" max="5" ></map-input>
118122

123+
<!-- map-style and map-link elements in tiles should be rendered in the
124+
container of the svg element that renders the tile, so that the style
125+
rules in them apply to the svg content elements such as path and g -->
126+
<!-- Local content map-link[rel=tile][type=text/mapml] with remote styles embedded in text/mapml tiles are rendered in same order as found in shadow root tile container -->
127+
<!-- see r0_c0.mapml -->
119128
<map-link rel='tile' type='text/mapml' title='Tiles for ne_10m_admin_0_countries (as MapML)'
120129
tref='data/wgs84/{zoomLevel}/r{row}_c{col}.mapml' ></map-link>
121130

122131
</map-extent>
123132
</layer->
124-
<layer- label="src-based Countries" src="data/vector-tile-test.mapml" checked></layer->
133+
134+
<layer- data-testid="remote" label="src-based Countries" src="data/vector-tile-test.mapml" checked></layer->
125135
</map>
126136
</body>
127137

0 commit comments

Comments
 (0)