@adobe/helix-importer 1.4.1 → 1.7.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/CHANGELOG.md CHANGED
@@ -1,3 +1,24 @@
1
+ # [1.7.0](https://github.com/adobe/helix-importer/compare/v1.6.0...v1.7.0) (2022-03-09)
2
+
3
+
4
+ ### Features
5
+
6
+ * do not change span with an image ([db51e1c](https://github.com/adobe/helix-importer/commit/db51e1cb068d44571c97c83d6e25b38f8e27003e))
7
+
8
+ # [1.6.0](https://github.com/adobe/helix-importer/compare/v1.5.0...v1.6.0) (2022-02-24)
9
+
10
+
11
+ ### Features
12
+
13
+ * trigger release ([43154bc](https://github.com/adobe/helix-importer/commit/43154bc2f093868324f66d431b71eea5083543bb))
14
+
15
+ # [1.5.0](https://github.com/adobe/helix-importer/compare/v1.4.1...v1.5.0) (2022-02-24)
16
+
17
+
18
+ ### Features
19
+
20
+ * helper method to get an image from background image style ([#17](https://github.com/adobe/helix-importer/issues/17)) ([63a384d](https://github.com/adobe/helix-importer/commit/63a384dceb75e6815115ffd1c37c321fc7476ab6))
21
+
1
22
  ## [1.4.1](https://github.com/adobe/helix-importer/compare/v1.4.0...v1.4.1) (2022-02-24)
2
23
 
3
24
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/helix-importer",
3
- "version": "1.4.1",
3
+ "version": "1.7.0",
4
4
  "description": "Helix Importer tool: create md / docx from html",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -144,10 +144,13 @@ export default class DOMUtils {
144
144
  static removeSpans(document) {
145
145
  // remove spans
146
146
  document.querySelectorAll('span').forEach((span) => {
147
- if (span.textContent === '') {
148
- span.remove();
149
- } else {
150
- span.replaceWith(JSDOM.fragment(span.innerHTML));
147
+ if (!span.querySelector('img')) {
148
+ // do not touch spans with images
149
+ if (span.textContent === '') {
150
+ span.remove();
151
+ } else {
152
+ span.replaceWith(JSDOM.fragment(span.innerHTML));
153
+ }
151
154
  }
152
155
  });
153
156
  }
@@ -231,14 +234,22 @@ export default class DOMUtils {
231
234
  }
232
235
 
233
236
  static replaceBackgroundByImg(element, document) {
237
+ const img = DOMUtils.getImgFromBackground(element, document);
238
+ if (img) {
239
+ element.replaceWith(img);
240
+ return img;
241
+ }
242
+ return element;
243
+ }
244
+
245
+ static getImgFromBackground(element, document) {
234
246
  const url = element?.style?.['background-image'];
235
- if (url) {
247
+ if (url && url.toLowerCase() !== 'none') {
236
248
  const src = url.replace(/url\(/gm, '').replace(/'/gm, '').replace(/\)/gm, '');
237
249
  const img = document.createElement('img');
238
250
  img.src = src;
239
- element.replaceWith(img);
240
251
  return img;
241
252
  }
242
- return element;
253
+ return null;
243
254
  }
244
255
  }
@@ -188,6 +188,8 @@ describe('DOMUtils#removeSpans tests', () => {
188
188
  test('<p><span>Content should remain</span> the same</p>', '<p>Content should remain the same</p>');
189
189
  test('<p>Spacing<span> should</span> remain the same</p>', '<p>Spacing should remain the same</p>');
190
190
  test('<p>Spacing<span> should</span> remain the <span>same even</span> with<span> multiple spans</span></p>', '<p>Spacing should remain the same even with multiple spans</p>');
191
+ test('<span><div><img src="animage.jpg"></div></span>', '<span><div><img src="animage.jpg"></div></span>');
192
+ test('<span>Some image here: <div><img src="animage.jpg"></div></span>', '<span>Some image here: <div><img src="animage.jpg"></div></span>');
191
193
  });
192
194
  });
193
195
 
@@ -324,22 +326,22 @@ describe('DOM#createTable tests', () => {
324
326
  });
325
327
  });
326
328
 
327
- describe('DOMUtils#replaceBackgroundByImg', () => {
328
- const createElement = (tag, attrs, styles, innerHTML) => {
329
- const { document } = (new JSDOM()).window;
330
- const element = document.createElement(tag);
331
- // eslint-disable-next-line no-restricted-syntax, guard-for-in
332
- for (const a in attrs) {
333
- element.setAttribute(a, attrs[a]);
334
- }
335
- // eslint-disable-next-line no-restricted-syntax, guard-for-in
336
- for (const p in styles) {
337
- element.style[p] = styles[p];
338
- }
339
- element.innerHTML = innerHTML;
340
- return element;
341
- };
329
+ const createElement = (tag, attrs, styles, innerHTML) => {
330
+ const { document } = (new JSDOM()).window;
331
+ const element = document.createElement(tag);
332
+ // eslint-disable-next-line no-restricted-syntax, guard-for-in
333
+ for (const a in attrs) {
334
+ element.setAttribute(a, attrs[a]);
335
+ }
336
+ // eslint-disable-next-line no-restricted-syntax, guard-for-in
337
+ for (const p in styles) {
338
+ element.style[p] = styles[p];
339
+ }
340
+ element.innerHTML = innerHTML;
341
+ return element;
342
+ };
342
343
 
344
+ describe('DOMUtils#replaceBackgroundByImg', () => {
343
345
  const test = (element, expected) => {
344
346
  const { document } = (new JSDOM()).window;
345
347
  const ret = DOMUtils.replaceBackgroundByImg(element, document);
@@ -350,6 +352,8 @@ describe('DOMUtils#replaceBackgroundByImg', () => {
350
352
  test(createElement('p', {}, {}, 'Some content'), '<p>Some content</p>');
351
353
 
352
354
  test(createElement('img', { src: 'https://www.server.com/image.jpg', title: 'Some title' }, {}, ''), '<img src="https://www.server.com/image.jpg" title="Some title">');
355
+
356
+ test(createElement('p', {}, { 'background-image': 'none' }, 'Some content'), '<p style="background-image: none;">Some content</p>');
353
357
  });
354
358
 
355
359
  it('with background-image style', () => {
@@ -358,3 +362,24 @@ describe('DOMUtils#replaceBackgroundByImg', () => {
358
362
  test(createElement('div', { class: 'class-is-lost' }, { 'background-image': 'url("https://www.server.com/image.jpg")' }, '<div><div>Some divs</div><div>More divs</div></div>'), '<img src="https://www.server.com/image.jpg">');
359
363
  });
360
364
  });
365
+
366
+ describe('DOMUtils#getImgFromBackground', () => {
367
+ const test = (element, expected) => {
368
+ const { document } = (new JSDOM()).window;
369
+ const ret = DOMUtils.getImgFromBackground(element, document);
370
+ strictEqual(ret ? ret.outerHTML : null, expected);
371
+ };
372
+
373
+ it('no background-image style', () => {
374
+ test(createElement('p', {}, {}, 'Some content'), null);
375
+
376
+ test(createElement('img', { src: 'https://www.server.com/image.jpg', title: 'Some title' }, {}, ''), null);
377
+
378
+ test(createElement('p', {}, { 'background-image': 'none' }, 'Some content'), null);
379
+ });
380
+
381
+ it('with background-image style', () => {
382
+ test(createElement('p', {}, { 'background-image': 'url(https://www.server.com/image.jpg)' }, 'Some content'), '<img src="https://www.server.com/image.jpg">');
383
+ test(createElement('div', { class: 'someclass' }, { 'background-image': 'url("https://www.server.com/image.jpg")', background: 'rgb(0, 0, 0) none repeat scroll 0% 0% / auto padding-box border-box' }, '<div><div>Some divs</div><div>More divs</div></div>'), '<img src="https://www.server.com/image.jpg">');
384
+ });
385
+ });