@geolonia/embed 3.0.0 → 3.1.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geolonia/embed",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "Geolonia embed JS API",
5
5
  "main": "dist/embed.js",
6
6
  "types": "src/embed.d.ts",
@@ -25,11 +25,11 @@
25
25
  "devDependencies": {
26
26
  "@babel/cli": "^7.19.3",
27
27
  "@babel/core": "^7.20.5",
28
+ "@babel/eslint-parser": "^7.19.1",
28
29
  "@babel/plugin-transform-classes": "^7.20.2",
29
30
  "@babel/preset-env": "^7.20.2",
30
31
  "@babel/register": "^7.18.9",
31
32
  "@geolonia/eslint-config": "^0.0.1",
32
- "@babel/eslint-parser": "^7.19.1",
33
33
  "babel-loader": "^9.1.0",
34
34
  "css-loader": "^6.7.2",
35
35
  "eslint": "^8.29.0",
@@ -2,6 +2,7 @@
2
2
 
3
3
  import maplibregl from 'maplibre-gl';
4
4
  import turfCenter from '@turf/center';
5
+ import { sanitizeDescription } from './util';
5
6
 
6
7
  const textColor = '#000000';
7
8
  const textHaloColor = '#FFFFFF';
@@ -201,13 +202,13 @@ class SimpleStyleVector {
201
202
  }
202
203
 
203
204
  async setPopup(map, source) {
204
- const { default: sanitizeHtml } = await import('sanitize-html');
205
- map.on('click', source, (e) => {
205
+ map.on('click', source, async (e) => {
206
206
  const center = turfCenter(e.features[0]).geometry.coordinates;
207
207
  const description = e.features[0].properties.description;
208
208
 
209
209
  if (description) {
210
- new maplibregl.Popup().setLngLat(center).setHTML(sanitizeHtml(description)).addTo(map);
210
+ const sanitizedDescription = await sanitizeDescription(description);
211
+ new maplibregl.Popup().setLngLat(center).setHTML(sanitizedDescription).addTo(map);
211
212
  }
212
213
  });
213
214
 
@@ -3,7 +3,7 @@
3
3
  import maplibregl from 'maplibre-gl';
4
4
  import geojsonExtent from '@mapbox/geojson-extent';
5
5
  import turfCenter from '@turf/center';
6
- import { isURL } from './util';
6
+ import { isURL, sanitizeDescription } from './util';
7
7
 
8
8
  const textColor = '#000000';
9
9
  const textHaloColor = '#FFFFFF';
@@ -236,6 +236,7 @@ class SimpleStyle {
236
236
  ['literal', [0, 0.8]],
237
237
  ],
238
238
  'text-allow-overlap': false,
239
+ 'icon-allow-overlap': true,
239
240
  },
240
241
  });
241
242
 
@@ -244,13 +245,13 @@ class SimpleStyle {
244
245
  }
245
246
 
246
247
  async setPopup(map, source) {
247
- const { default: sanitizeHtml } = await import('sanitize-html');
248
- map.on('click', source, (e) => {
248
+ map.on('click', source, async (e) => {
249
249
  const center = turfCenter(e.features[0]).geometry.coordinates;
250
250
  const description = e.features[0].properties.description;
251
251
 
252
252
  if (description) {
253
- new maplibregl.Popup().setLngLat(center).setHTML(sanitizeHtml(description)).addTo(map);
253
+ const sanitizedDescription = await sanitizeDescription(description);
254
+ new maplibregl.Popup().setLngLat(center).setHTML(sanitizedDescription).addTo(map);
254
255
  }
255
256
  });
256
257
 
@@ -83,6 +83,7 @@ describe('Tests for simpleStyle()', () => {
83
83
  assert.deepEqual([ 'geolonia-simple-style', 'geolonia-simple-style-points' ], Object.keys(map.sources));
84
84
  assert.deepEqual(8, map.layers.length);
85
85
  assert.deepEqual(true, map.bounds);
86
+ assert.deepEqual(true, map.layers.find((layer) => layer.id === 'geolonia-simple-style-symbol-points').layout['icon-allow-overlap']);
86
87
  });
87
88
 
88
89
  it('should has sources and layers as expected with custom IDs', async () => {
package/src/lib/util.js CHANGED
@@ -274,3 +274,11 @@ export const handleRestrictedMode = (map) => {
274
274
  container.classList.add('geolonia__restricted-mode-image-container');
275
275
  }
276
276
  };
277
+
278
+ export const sanitizeDescription = async (description) => {
279
+ const { default: sanitizeHtml } = await import('sanitize-html');
280
+ return sanitizeHtml(description, {
281
+ allowedTags: sanitizeHtml.defaults.allowedTags.concat([ 'img' ]),
282
+ allowedAttributes: {...sanitizeHtml.defaults.allowedAttributes, '*': ['class']},
283
+ });
284
+ };
@@ -202,4 +202,31 @@ describe('Tests for util.js', () => {
202
202
  util.parseSimpleVector(attributeValue),
203
203
  );
204
204
  });
205
+
206
+ describe('Tests for sanitizeDescription', async () => {
207
+ it('should sanitize description', async () => {
208
+ const description = '<script>alert("hello");</script>ここが集合場所です。13時までに集合してください。';
209
+ assert.strictEqual(
210
+ 'ここが集合場所です。13時までに集合してください。',
211
+ await util.sanitizeDescription(description),
212
+ );
213
+ });
214
+
215
+ it('should not sanitize img tag, but should sanitize attributes other than "src", "srcset", "alt", "title", "width", "height", "loading"', async () => {
216
+ // Ref. https://www.npmjs.com/package/sanitize-html
217
+ const description = '<img decoding="auto" src="hibiya-park.jpeg" /><br />ここが集合場所です。13時までに集合してください。';
218
+ assert.strictEqual(
219
+ '<img src="hibiya-park.jpeg" /><br />ここが集合場所です。13時までに集合してください。',
220
+ await util.sanitizeDescription(description),
221
+ );
222
+ });
223
+
224
+ it('should not sanitize "class" attribute', async () => {
225
+ const description = '<span class="red">ここが集合場所です。13時までに集合してください。</span>';
226
+ assert.strictEqual(
227
+ '<span class="red">ここが集合場所です。13時までに集合してください。</span>',
228
+ await util.sanitizeDescription(description),
229
+ );
230
+ });
231
+ });
205
232
  });