unprint 0.15.7 → 0.16.1

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": "unprint",
3
- "version": "0.15.7",
3
+ "version": "0.16.1",
4
4
  "description": "Simplify common web scraping tasks while staying in control of the data.",
5
5
  "main": "src/app.js",
6
6
  "scripts": {
@@ -30,6 +30,7 @@
30
30
  "eslint-config-airbnb-base": "^15.0.0",
31
31
  "jsdom": "^17.0.0",
32
32
  "moment-timezone": "^0.5.34",
33
+ "srcset": "^4.0.0",
33
34
  "tunnel": "^0.0.6"
34
35
  },
35
36
  "devDependencies": {
package/src/app.js CHANGED
@@ -9,6 +9,7 @@ const axios = require('axios').default;
9
9
  const Bottleneck = require('bottleneck');
10
10
  const moment = require('moment-timezone');
11
11
  const merge = require('deepmerge');
12
+ const srcset = require('srcset');
12
13
 
13
14
  const settings = {
14
15
  throwErrors: false,
@@ -175,7 +176,12 @@ function queryContent(context, selector, customOptions) {
175
176
  }
176
177
 
177
178
  function queryContents(context, selector, customOptions) {
178
- const options = { ...context.options, ...customOptions };
179
+ const options = {
180
+ ...context.options,
181
+ trim: true,
182
+ ...customOptions,
183
+ };
184
+
179
185
  const targets = queryElements(context, selector, options);
180
186
 
181
187
  return targets.map((target) => extractContent(target, options)).filter(Boolean);
@@ -448,20 +454,34 @@ function queryImages(context, selector = 'img', customOptions) {
448
454
  return imageUrls.map((imageUrl) => prefixUrl(imageUrl, options.origin, options));
449
455
  }
450
456
 
457
+ function getSourceSetDescriptor(source) {
458
+ if (source.width) {
459
+ return `${source.width}w`;
460
+ }
461
+
462
+ if (source.height) {
463
+ return `${source.height}w`;
464
+ }
465
+
466
+ if (source.density) {
467
+ return `${source.density}x`;
468
+ }
469
+
470
+ return 'fallback';
471
+ }
472
+
451
473
  function extractSourceSet(sourceSet, customOptions) {
452
474
  if (!sourceSet) {
453
475
  return null;
454
476
  }
455
477
 
456
- const sources = sourceSet
457
- .split(/\s*,\s+/)
478
+ const sources = srcset.parse(sourceSet)
458
479
  .map((source) => {
459
- const [link, descriptor] = source.trim().split(' ');
460
-
461
- if (link) {
480
+ if (source.url) {
462
481
  return {
463
- descriptor: descriptor || 'fallback',
464
- url: prefixUrl(link, customOptions.origin, customOptions.protocol),
482
+ ...source,
483
+ descriptor: getSourceSetDescriptor(source),
484
+ url: prefixUrl(source.url, customOptions.origin, customOptions.protocol),
465
485
  };
466
486
  }
467
487
 
@@ -469,11 +489,27 @@ function extractSourceSet(sourceSet, customOptions) {
469
489
  })
470
490
  .filter(Boolean)
471
491
  .sort((sourceA, sourceB) => {
472
- if (sourceB.descriptor === 'fallback' || parseInt(sourceA.descriptor, 10) > parseInt(sourceB.descriptor, 10)) {
492
+ if (customOptions.sort === false) {
493
+ return 0;
494
+ }
495
+
496
+ if (sourceB.description === 'fallback') {
497
+ return -1;
498
+ }
499
+
500
+ if (sourceA.width && sourceB.width && sourceA.width > sourceB.width) {
473
501
  return -1;
474
502
  }
475
503
 
476
- if (parseInt(sourceA.descriptor, 10) < parseInt(sourceB.descriptor, 10)) {
504
+ if (sourceA.height && sourceB.height && sourceA.height > sourceB.height) {
505
+ return -1;
506
+ }
507
+
508
+ if (sourceA.width && sourceB.width && sourceA.width < sourceB.width) {
509
+ return 1;
510
+ }
511
+
512
+ if (sourceA.height && sourceB.height && sourceA.height < sourceB.height) {
477
513
  return 1;
478
514
  }
479
515
 
@@ -482,7 +518,7 @@ function extractSourceSet(sourceSet, customOptions) {
482
518
 
483
519
  if (customOptions.includeDescriptor) {
484
520
  return sources.map((source) => ({
485
- descriptor: source.descriptor,
521
+ ...source,
486
522
  url: prefixUrl(source.url),
487
523
  }));
488
524
  }
package/tests/index.html CHANGED
@@ -40,7 +40,8 @@
40
40
  <img class="image" src="https://i.redd.it/e91oo4ueyeb71.jpg">
41
41
 
42
42
  <img class="srcset" srcset="https://i.redd.it/e91oo4ueyeb71.jpg 240w, https://i.redd.it/vn9h981hlx281.png 480w, https://i.redd.it/e91oo4ueyeb71.jpg 640w">
43
- <img class="srcset" srcset="https://i.redd.it/e91oo4ueyeb71.jpg 240w, https://i.redd.it/vn9h981hlx281.png 480w, https://i.redd.it/e91oo4ueyeb71.jpg 640w">
43
+ <img class="srcset" srcset="https://i.redd.it/e91oo4ueyeb71.jpg 240w,https://i.redd.it/e91oo4ueyeb71.jpg 640w,https://i.redd.it/vn9h981hlx281.png 480w">
44
+ <img class="srcset" srcset="https://i.redd.it/e91oo4ueyeb71,comma,test.jpg 240w,https://i.redd.it/,sub/vn9h981hlx281.png 480w,https://i.redd.it/e91oo4ueyeb71.jpg 2x">
44
45
 
45
46
  <!-- deliberate space in url( ) to test JSDOM's quirky handling of this -->
46
47
  <div class="style" style="background-image: url( https://i.imgur.com/eDQmLys.jpg ); color: red;"></div>