unprint 0.15.6 → 0.16.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": "unprint",
3
- "version": "0.15.6",
3
+ "version": "0.16.0",
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,
@@ -448,20 +449,34 @@ function queryImages(context, selector = 'img', customOptions) {
448
449
  return imageUrls.map((imageUrl) => prefixUrl(imageUrl, options.origin, options));
449
450
  }
450
451
 
452
+ function getSourceSetDescriptor(source) {
453
+ if (source.width) {
454
+ return `${source.width}w`;
455
+ }
456
+
457
+ if (source.height) {
458
+ return `${source.height}w`;
459
+ }
460
+
461
+ if (source.density) {
462
+ return `${source.density}x`;
463
+ }
464
+
465
+ return 'fallback';
466
+ }
467
+
451
468
  function extractSourceSet(sourceSet, customOptions) {
452
469
  if (!sourceSet) {
453
470
  return null;
454
471
  }
455
472
 
456
- const sources = sourceSet
457
- .split(/\s*,\s*/)
473
+ const sources = srcset.parse(sourceSet)
458
474
  .map((source) => {
459
- const [link, descriptor] = source.split(' ');
460
-
461
- if (link) {
475
+ if (source.url) {
462
476
  return {
463
- descriptor: descriptor || 'fallback',
464
- url: prefixUrl(link, customOptions.origin, customOptions.protocol),
477
+ ...source,
478
+ descriptor: getSourceSetDescriptor(source),
479
+ url: prefixUrl(source.url, customOptions.origin, customOptions.protocol),
465
480
  };
466
481
  }
467
482
 
@@ -469,11 +484,27 @@ function extractSourceSet(sourceSet, customOptions) {
469
484
  })
470
485
  .filter(Boolean)
471
486
  .sort((sourceA, sourceB) => {
472
- if (sourceB.descriptor === 'fallback' || parseInt(sourceA.descriptor, 10) > parseInt(sourceB.descriptor, 10)) {
487
+ if (customOptions.sort === false) {
488
+ return 0;
489
+ }
490
+
491
+ if (sourceB.description === 'fallback') {
492
+ return -1;
493
+ }
494
+
495
+ if (sourceA.width && sourceB.width && sourceA.width > sourceB.width) {
496
+ return -1;
497
+ }
498
+
499
+ if (sourceA.height && sourceB.height && sourceA.height > sourceB.height) {
473
500
  return -1;
474
501
  }
475
502
 
476
- if (parseInt(sourceA.descriptor, 10) < parseInt(sourceB.descriptor, 10)) {
503
+ if (sourceA.width && sourceB.width && sourceA.width < sourceB.width) {
504
+ return 1;
505
+ }
506
+
507
+ if (sourceA.height && sourceB.height && sourceA.height < sourceB.height) {
477
508
  return 1;
478
509
  }
479
510
 
@@ -482,7 +513,7 @@ function extractSourceSet(sourceSet, customOptions) {
482
513
 
483
514
  if (customOptions.includeDescriptor) {
484
515
  return sources.map((source) => ({
485
- descriptor: source.descriptor,
516
+ ...source,
486
517
  url: prefixUrl(source.url),
487
518
  }));
488
519
  }
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>