@geolonia/embed 5.2.2 → 6.0.0-pre.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/.github/workflows/build.yml +25 -1
  2. package/CHANGELOG.md +9 -0
  3. package/README.md +24 -1
  4. package/dist/embed +1 -1
  5. package/dist/embed-chunks/4ada77b78cc0cc5ccef9.js +1 -0
  6. package/dist/embed-chunks/8176630945512b29f294.js +2 -0
  7. package/dist/embed-chunks/c783fffcd28083f7d5cb.js +2 -0
  8. package/dist/embed-chunks/f2124097fda51d0079ee.js +1 -0
  9. package/dist/embed-core.d.ts +6 -7
  10. package/dist/embed-core.js +1 -1
  11. package/dist/embed.d.ts +2 -4
  12. package/dist/embed.js +1 -1
  13. package/dist/lib/parse-atts.d.ts +19 -0
  14. package/dist/lib/render.d.ts +1 -1
  15. package/dist/lib/util.d.ts +14 -55
  16. package/dist/types.d.ts +155 -1
  17. package/dist/version.d.ts +1 -1
  18. package/e2e/auto-render.spec.ts +44 -0
  19. package/e2e/basic.spec.ts +10 -74
  20. package/e2e/cleanup.spec.ts +43 -0
  21. package/e2e/data-attributes.spec.ts +56 -0
  22. package/e2e/globals.spec.ts +51 -0
  23. package/e2e/helper.ts +170 -4
  24. package/e2e/iframe.spec.ts +35 -0
  25. package/e2e/lazy-loading.spec.ts +32 -0
  26. package/e2e/legacy-constructor.spec.ts +102 -0
  27. package/e2e/plugin.spec.ts +25 -0
  28. package/e2e/pmtiles.spec.ts +13 -0
  29. package/package.json +8 -11
  30. package/playwright.config.ts +6 -32
  31. package/src/embed-core.ts +12 -14
  32. package/src/embed.ts +2 -4
  33. package/src/lib/parse-atts.test.ts +186 -18
  34. package/src/lib/parse-atts.ts +151 -5
  35. package/src/lib/render.ts +50 -12
  36. package/src/lib/util.ts +37 -350
  37. package/src/types.ts +155 -1
  38. package/src/version.ts +1 -1
  39. package/typedoc.json +13 -0
  40. package/vitest.config.ts +7 -0
  41. package/dist/embed-chunks/12cbd0d1cb4cf744c32f.js +0 -2
  42. package/dist/embed-chunks/8a521e8312a5b4da123d.js +0 -2
  43. package/dist/lib/CustomAttributionControl.d.ts +0 -47
  44. package/dist/lib/controls/geolonia-control.d.ts +0 -7
  45. package/dist/lib/geolonia-map.d.ts +0 -30
  46. package/dist/lib/geolonia-marker.d.ts +0 -9
  47. package/dist/lib/keyring.d.ts +0 -45
  48. package/dist/lib/maplibre-util.d.ts +0 -25
  49. package/dist/lib/simplestyle-vector.d.ts +0 -26
  50. package/dist/lib/simplestyle.d.ts +0 -31
  51. package/e2e/external-style.spec.ts +0 -25
  52. package/e2e/map.spec.ts +0 -69
  53. package/src/lib/CustomAttributionControl.ts +0 -394
  54. package/src/lib/controls/geolonia-control.ts +0 -38
  55. package/src/lib/geolonia-map.ts +0 -429
  56. package/src/lib/geolonia-marker.ts +0 -53
  57. package/src/lib/keyring.test.ts +0 -127
  58. package/src/lib/keyring.ts +0 -136
  59. package/src/lib/maplibre-util.ts +0 -137
  60. package/src/lib/marker.svg +0 -1
  61. package/src/lib/simplestyle-vector.ts +0 -257
  62. package/src/lib/simplestyle.test.ts +0 -289
  63. package/src/lib/simplestyle.ts +0 -435
  64. package/src/lib/util.test.ts +0 -364
  65. /package/dist/embed-chunks/{12cbd0d1cb4cf744c32f.js.LICENSE.txt → 8176630945512b29f294.js.LICENSE.txt} +0 -0
  66. /package/dist/embed-chunks/{8a521e8312a5b4da123d.js.LICENSE.txt → c783fffcd28083f7d5cb.js.LICENSE.txt} +0 -0
@@ -1,364 +0,0 @@
1
- import { describe, it, expect, beforeAll } from 'vitest';
2
-
3
- import { JSDOM } from 'jsdom';
4
- import {
5
- getContainer,
6
- getLang,
7
- getOptions,
8
- getStyle,
9
- handleMarkerOptions,
10
- isDomElement,
11
- isURL,
12
- isGeoloniaTilesHost,
13
- parseControlOption,
14
- parseSimpleVector,
15
- sanitizeDescription,
16
- loadImageCompatibility,
17
- } from './util';
18
-
19
- const base = 'https://base.example.com/parent/';
20
-
21
- beforeAll(() => {
22
- global.location = {
23
- ...global.location,
24
- href: base,
25
- };
26
- });
27
-
28
- describe('Tests for util.js', () => {
29
- it('URL should be detected', () => {
30
- expect(true).toEqual(!!isURL('http://example.com'));
31
- });
32
-
33
- it('URL with SSL should be detected', () => {
34
- expect(true).toEqual(!!isURL('https://example.com'));
35
- });
36
-
37
- it('Path should be detected', () => {
38
- expect('https://base.example.com/parent/example-path').toEqual(isURL('./example-path'),
39
- );
40
- });
41
-
42
- it('Parent path should be detected', () => {
43
- expect('https://base.example.com/example-path').toEqual(isURL('../example-path'),
44
- );
45
- });
46
-
47
- it('Absolute path should be detected', () => {
48
- expect('https://base.example.com/example-path').toEqual(isURL('/example-path'),
49
- );
50
- });
51
-
52
- it('Name should not be detected', () => {
53
- expect(false).toEqual(isURL('example.com/hello'));
54
- });
55
-
56
- describe('isGeoloniaTilesHost', () => {
57
- it('detects primary tileserver hostname', () => {
58
- const url =
59
- 'https://tileserver.geolonia.com/v3/tiles.json?key=YOUR-API-KEY';
60
- expect(isGeoloniaTilesHost(url)).toBe(true);
61
- });
62
-
63
- it('detects tiles.geolonia.com subdomains', () => {
64
- const url = new URL('https://osm.v2.tiles.geolonia.com/tiles.json');
65
- expect(isGeoloniaTilesHost(url)).toBe(true);
66
- });
67
-
68
- it('detects tiles.geolonia.com subdomains', () => {
69
- const url = new URL('https://osm.v3.tiles.geolonia.com/tiles.json');
70
- expect(isGeoloniaTilesHost(url)).toBe(true);
71
- });
72
-
73
- it('returns false for other hosts or invalid urls', () => {
74
- expect(isGeoloniaTilesHost('https://example.com/tiles')).toBe(false);
75
- expect(isGeoloniaTilesHost('not-a-url')).toBe(false);
76
- });
77
- });
78
-
79
- it('should detect the object is DOM correctly', () => {
80
- const { document: mocDocument } = new JSDOM(`<html><body>
81
- <div class="test-class"></div>
82
- </body></html>`).window;
83
-
84
- expect(true).toEqual(isDomElement(mocDocument.querySelector('.test-class')),
85
- );
86
- expect(false).toEqual(isDomElement('hello world'));
87
- expect(false).toEqual(isDomElement({ hello: 'world' }));
88
- });
89
-
90
- it('should be able to get dom', () => {
91
- const dom = new JSDOM(`<html><body>
92
- <div id="test-element"></div>
93
- </body></html>`);
94
-
95
- // @ts-ignore
96
- global.window = dom.window;
97
- global.document = dom.window.document;
98
-
99
- const el = document.querySelector('#test-element') as HTMLElement;
100
- expect(el).toEqual(getContainer(el));
101
- const params = { container: el };
102
- expect(el).toEqual(getContainer(params));
103
-
104
- // specify as a selector
105
- expect(el).toEqual(getContainer('#test-element'));
106
- expect(el).toEqual(getContainer({ container: '#test-element' }));
107
-
108
- // specify as an id attribute value
109
- expect(el).toEqual(getContainer('test-element'));
110
- expect(el).toEqual(getContainer({ container: 'test-element' }));
111
-
112
- // negative cases
113
- expect(false).toEqual(getContainer('#fail-element'));
114
- expect(false).toEqual(getContainer({ container: '#fail-element' }));
115
- expect(false).toEqual(getContainer('fail-element'));
116
- expect(false).toEqual(getContainer({ container: 'fail-element' }));
117
- });
118
-
119
- it('should merge legacy options into options as expected.', () => {
120
- const dom = new JSDOM(`<html><body>
121
- <div id="test-element"></div>
122
- </body></html>`);
123
-
124
- // @ts-ignore
125
- global.window = dom.window;
126
- global.document = dom.window.document;
127
-
128
- const options1 = handleMarkerOptions(
129
- document.getElementById('test-element'),
130
- { color: '#FF1122' },
131
- );
132
- expect(document.getElementById('test-element')).toEqual(options1.element);
133
- expect(options1.color).toEqual('#FF1122');
134
-
135
- const options2 = handleMarkerOptions(false, { color: '#FF1122' });
136
- expect(options2.color).toEqual('#FF1122');
137
-
138
- const options3 = handleMarkerOptions({ scale: 2 }, { color: '#FF1122' });
139
- expect(options3.scale).toEqual(2);
140
- });
141
-
142
- describe('language detection', () => {
143
- it('should work with Chrome', () => {
144
- global.window = {
145
- // @ts-ignore forcefully assigning values to readonly properties
146
- navigator: {
147
- language: 'ja',
148
- languages: ['ja', 'en', 'en-US', 'ar'],
149
- },
150
- };
151
- expect(getLang()).toEqual('ja');
152
- });
153
-
154
- it('should work with iOS safari', () => {
155
- global.window = {
156
- // @ts-ignore forcefully assigning values to readonly properties
157
- navigator: {
158
- language: 'ja-JP',
159
- languages: ['ja-JP'],
160
- },
161
- };
162
- expect(getLang()).toEqual('ja');
163
- });
164
- });
165
-
166
- it('should get correct style url', () => {
167
- const atts = {
168
- apiUrl: 'https://example.com',
169
- key: '1234',
170
- lang: 'en',
171
- };
172
-
173
- // 従来モード: 論理名(API キー必須)
174
- expect('https://cdn.geolonia.com/style/hello/world/en.json').toEqual(getStyle('hello/world', atts),
175
- );
176
-
177
- // URL モード: https://(外部 URL、API キー不要)
178
- expect('https://example.com/style.json').toEqual(getStyle('https://example.com/style.json', atts),
179
- );
180
-
181
- // URL モード: 相対パス ./(外部 URL、API キー不要)
182
- expect('https://base.example.com/parent/style.json').toEqual(getStyle('./style.json', atts),
183
- );
184
-
185
- // URL モード: 絶対パス /(外部 URL、API キー不要)
186
- expect('https://base.example.com/style.json').toEqual(getStyle('/style.json', atts),
187
- );
188
-
189
- // URL モード: .json で終わる(外部 URL として解決)
190
- expect('https://base.example.com/parent/custom.json').toEqual(getStyle('custom.json', atts),
191
- );
192
-
193
- // デフォルト: 空文字列(API キー必須)
194
- expect('https://cdn.geolonia.com/style/geolonia/basic-v2/en.json').toEqual(getStyle('', atts),
195
- );
196
-
197
- // デフォルト: null/undefined(API キー必須)
198
- expect('https://cdn.geolonia.com/style/geolonia/basic-v2/en.json').toEqual(getStyle(null, atts),
199
- );
200
-
201
- // 日本語環境でのデフォルト
202
- const attsJa = { ...atts, lang: 'ja' };
203
- expect('https://cdn.geolonia.com/style/geolonia/basic-v2/ja.json').toEqual(getStyle('', attsJa),
204
- );
205
-
206
- // 日本語環境での論理名
207
- expect('https://cdn.geolonia.com/style/geolonia/basic/ja.json').toEqual(getStyle('geolonia/basic', attsJa),
208
- );
209
- });
210
-
211
- it('should throw error when using Geolonia styles without API key', () => {
212
- const attsNoKey = {
213
- apiUrl: 'https://example.com',
214
- key: '',
215
- lang: 'en',
216
- };
217
-
218
- // 論理名: API キー無しでエラー
219
- expect(
220
- () => getStyle('geolonia/basic', attsNoKey),
221
- ).toThrow(/API key is required/);
222
-
223
- // デフォルト: API キー無しでエラー
224
- expect(
225
- () => getStyle('', attsNoKey),
226
- ).toThrow(/API key is required/);
227
-
228
- // Geolonia CDN の URL: API キー無しでエラー
229
- expect(
230
- () => getStyle('https://cdn.geolonia.com/style/geolonia/basic/en.json', attsNoKey),
231
- ).toThrow(/API key is required/);
232
-
233
- // 外部 URL: API キー無しでも OK
234
- expect(() => {
235
- getStyle('https://tile.openstreetmap.jp/styles/osm-bright/style.json', attsNoKey);
236
- }).not.toThrow();
237
-
238
- expect(() => {
239
- getStyle('./my-style.json', attsNoKey);
240
- }).not.toThrow();
241
- });
242
-
243
- it('should handle maplibregl options `minZoom` and `maxZoom` well', () => {
244
- {
245
- const atts = { minZoom: '', maxZoom: '10' };
246
- const options = getOptions({}, {}, atts);
247
- expect('undefined').toEqual(typeof options.minZoom);
248
- expect(10).toEqual(options.maxZoom);
249
- }
250
-
251
- {
252
- const atts = { minZoom: '0', maxZoom: '' };
253
- const options = getOptions({}, {}, atts);
254
- expect(0).toEqual(options.minZoom);
255
- expect('undefined').toEqual(typeof options.maxZoom);
256
- }
257
-
258
- {
259
- const atts = { minZoom: '0', maxZoom: '' };
260
- const params = { minZoom: 7 };
261
- const options = getOptions({}, params, atts);
262
- expect(7).toEqual(options.minZoom);
263
- expect('undefined').toEqual(typeof options.maxZoom);
264
- }
265
- });
266
-
267
- it('should handle control position options.', () => {
268
- const att = 'top-left';
269
- const { enabled, position } = parseControlOption(att);
270
- expect(true).toBe(enabled);
271
- expect('top-left').toBe(position);
272
- });
273
-
274
- it('should handle control position on.', () => {
275
- const att = 'on';
276
- const { enabled, position } = parseControlOption(att);
277
- expect(true).toBe(enabled);
278
- expect(void 0).toBe(position);
279
- });
280
-
281
- it('should handle control position off.', () => {
282
- const att = 'off';
283
- const { enabled, position } = parseControlOption(att);
284
- expect(false).toBe(enabled);
285
- expect(void 0).toBe(position);
286
- });
287
-
288
- it('should parse simple vector value with http.', () => {
289
- const attributeValue = 'https://example.com/path/to/tile.json';
290
- expect(attributeValue).toBe(parseSimpleVector(attributeValue));
291
- });
292
-
293
- it('should parse simple vector value with geolonia schema.', () => {
294
- const attributeValue = 'geolonia://tiles/username/ct_123';
295
- expect(attributeValue).toBe(parseSimpleVector(attributeValue));
296
- });
297
-
298
- it('should parse simple vector value with custom tile ID', () => {
299
- const attributeValue = 'ct_123';
300
- expect('geolonia://tiles/custom/ct_123').toBe(parseSimpleVector(attributeValue),
301
- );
302
- });
303
-
304
- describe('Tests for sanitizeDescription', async () => {
305
- it('should sanitize description', async () => {
306
- const description =
307
- '<script>alert("hello");</script>ここが集合場所です。13時までに集合してください。';
308
- expect('ここが集合場所です。13時までに集合してください。').toBe(await sanitizeDescription(description),
309
- );
310
- });
311
-
312
- it('should not sanitize img tag, but should sanitize attributes other than "src", "srcset", "alt", "title", "width", "height", "loading"', async () => {
313
- // Ref. https://www.npmjs.com/package/sanitize-html
314
- const description =
315
- '<img decoding="auto" src="hibiya-park.jpeg" /><br />ここが集合場所です。13時までに集合してください。';
316
- expect('<img src="hibiya-park.jpeg" /><br />ここが集合場所です。13時までに集合してください。').toBe(await sanitizeDescription(description),
317
- );
318
- });
319
-
320
- it('should not sanitize "class" attribute', async () => {
321
- const description =
322
- '<span class="red">ここが集合場所です。13時までに集合してください。</span>';
323
- expect('<span class="red">ここが集合場所です。13時までに集合してください。</span>').toBe(await sanitizeDescription(description),
324
- );
325
- });
326
- });
327
- });
328
-
329
- describe('loadImageCompatibility', () => {
330
- it('should call the callback with response data when the promise resolves', async () => {
331
- const mockResponse = {
332
- data: new Image(),
333
- cacheControl: 'public, max-age=3600',
334
- expires: '1609459200',
335
- };
336
- const promise = Promise.resolve(mockResponse);
337
-
338
- await new Promise<void>((resolve) => {
339
- loadImageCompatibility(promise, (error, data, expiry) => {
340
- expect(error).toEqual(null);
341
- expect(data).toEqual(mockResponse.data);
342
- expect(expiry).toEqual({
343
- cacheControl: mockResponse.cacheControl,
344
- expires: mockResponse.expires,
345
- });
346
- resolve();
347
- });
348
- });
349
- });
350
-
351
- it('should call the callback with error when the promise rejects', async () => {
352
- const mockError = new Error('Failed to load image');
353
- const promise = Promise.reject(mockError);
354
-
355
- await new Promise<void>((resolve) => {
356
- loadImageCompatibility(promise, (error, data, expiry) => {
357
- expect(error).toEqual(mockError);
358
- expect(data).toBe(undefined);
359
- expect(expiry).toBe(undefined);
360
- resolve();
361
- });
362
- });
363
- });
364
- });