@adobe/helix-importer 1.4.1 → 1.5.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,10 @@
1
+ # [1.5.0](https://github.com/adobe/helix-importer/compare/v1.4.1...v1.5.0) (2022-02-24)
2
+
3
+
4
+ ### Features
5
+
6
+ * 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))
7
+
1
8
  ## [1.4.1](https://github.com/adobe/helix-importer/compare/v1.4.0...v1.4.1) (2022-02-24)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/helix-importer",
3
- "version": "1.4.1",
3
+ "version": "1.5.0",
4
4
  "description": "Helix Importer tool: create md / docx from html",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -231,14 +231,22 @@ export default class DOMUtils {
231
231
  }
232
232
 
233
233
  static replaceBackgroundByImg(element, document) {
234
+ const img = DOMUtils.getImgFromBackground(element, document);
235
+ if (img) {
236
+ element.replaceWith(img);
237
+ return img;
238
+ }
239
+ return element;
240
+ }
241
+
242
+ static getImgFromBackground(element, document) {
234
243
  const url = element?.style?.['background-image'];
235
244
  if (url) {
236
245
  const src = url.replace(/url\(/gm, '').replace(/'/gm, '').replace(/\)/gm, '');
237
246
  const img = document.createElement('img');
238
247
  img.src = src;
239
- element.replaceWith(img);
240
248
  return img;
241
249
  }
242
- return element;
250
+ return null;
243
251
  }
244
252
  }
@@ -324,22 +324,22 @@ describe('DOM#createTable tests', () => {
324
324
  });
325
325
  });
326
326
 
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
- };
327
+ const createElement = (tag, attrs, styles, innerHTML) => {
328
+ const { document } = (new JSDOM()).window;
329
+ const element = document.createElement(tag);
330
+ // eslint-disable-next-line no-restricted-syntax, guard-for-in
331
+ for (const a in attrs) {
332
+ element.setAttribute(a, attrs[a]);
333
+ }
334
+ // eslint-disable-next-line no-restricted-syntax, guard-for-in
335
+ for (const p in styles) {
336
+ element.style[p] = styles[p];
337
+ }
338
+ element.innerHTML = innerHTML;
339
+ return element;
340
+ };
342
341
 
342
+ describe('DOMUtils#replaceBackgroundByImg', () => {
343
343
  const test = (element, expected) => {
344
344
  const { document } = (new JSDOM()).window;
345
345
  const ret = DOMUtils.replaceBackgroundByImg(element, document);
@@ -358,3 +358,22 @@ describe('DOMUtils#replaceBackgroundByImg', () => {
358
358
  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
359
  });
360
360
  });
361
+
362
+ describe('DOMUtils#getImgFromBackground', () => {
363
+ const test = (element, expected) => {
364
+ const { document } = (new JSDOM()).window;
365
+ const ret = DOMUtils.getImgFromBackground(element, document);
366
+ strictEqual(ret ? ret.outerHTML : null, expected);
367
+ };
368
+
369
+ it('no background-image style', () => {
370
+ test(createElement('p', {}, {}, 'Some content'), null);
371
+
372
+ test(createElement('img', { src: 'https://www.server.com/image.jpg', title: 'Some title' }, {}, ''), null);
373
+ });
374
+
375
+ it('with background-image style', () => {
376
+ test(createElement('p', {}, { 'background-image': 'url(https://www.server.com/image.jpg)' }, 'Some content'), '<img src="https://www.server.com/image.jpg">');
377
+ 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">');
378
+ });
379
+ });