From 6d49e3b3c112b7c5bc8e55ca8624b11163b9705a Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 21 May 2020 12:25:01 +0800 Subject: [PATCH] Revert #7381 (#8359) --- changelog_unreleased/html/pr-8359.md | 32 ++++++++++++++ package.json | 2 +- src/language-html/syntax-attribute.js | 18 +++++--- .../__snapshots__/jsfmt.spec.js.snap | 44 +++++++++++++++++++ tests/html/attributes/srcset.html | 21 +++++++++ yarn.lock | 9 ++-- 6 files changed, 114 insertions(+), 12 deletions(-) create mode 100644 changelog_unreleased/html/pr-8359.md diff --git a/changelog_unreleased/html/pr-8359.md b/changelog_unreleased/html/pr-8359.md new file mode 100644 index 000000000..534a2c1e3 --- /dev/null +++ b/changelog_unreleased/html/pr-8359.md @@ -0,0 +1,32 @@ +#### Fix broken URLs with commas in srcset ([#8359](https://github.com/prettier/prettier/pull/8359) by [@fisker](https://github.com/fisker)) + + +```html + + + + + + + + +``` diff --git a/package.json b/package.json index b99505b4b..e3d552388 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,7 @@ "minimist": "1.2.5", "n-readlines": "1.0.0", "outdent": "0.7.1", + "parse-srcset": "ikatyang/parse-srcset#54eb9c1cb21db5c62b4d0e275d7249516df6f0ee", "please-upgrade-node": "3.2.0", "postcss-less": "3.1.4", "postcss-media-query-parser": "0.2.3", @@ -71,7 +72,6 @@ "remark-parse": "5.0.0", "resolve": "1.17.0", "semver": "7.3.2", - "srcset": "2.0.1", "string-width": "4.2.0", "tslib": "1.13.0", "typescript": "3.9.3", diff --git a/src/language-html/syntax-attribute.js b/src/language-html/syntax-attribute.js index c3a63d49a..c17a8d625 100644 --- a/src/language-html/syntax-attribute.js +++ b/src/language-html/syntax-attribute.js @@ -3,20 +3,26 @@ const { builders: { concat, ifBreak, join, line }, } = require("../document"); -const parseSrcset = require("srcset").parse; +const parseSrcset = require("parse-srcset"); function printImgSrcset(value) { - const srcset = parseSrcset(value); + const srcset = parseSrcset(value, { + logger: { + error(message) { + throw new Error(message); + }, + }, + }); - const hasW = srcset.some((src) => src.width); - const hasH = srcset.some((src) => src.height); - const hasX = srcset.some((src) => src.density); + const hasW = srcset.some(({ w }) => w); + const hasH = srcset.some(({ h }) => h); + const hasX = srcset.some(({ d }) => d); if (hasW + hasH + hasX > 1) { throw new Error("Mixed descriptor in srcset is not supported"); } - const key = hasW ? "width" : hasH ? "height" : "density"; + const key = hasW ? "w" : hasH ? "h" : "d"; const unit = hasW ? "w" : hasH ? "h" : "x"; const getMax = (values) => Math.max(...values); diff --git a/tests/html/attributes/__snapshots__/jsfmt.spec.js.snap b/tests/html/attributes/__snapshots__/jsfmt.spec.js.snap index 3ea7ca55f..2e5de09d6 100644 --- a/tests/html/attributes/__snapshots__/jsfmt.spec.js.snap +++ b/tests/html/attributes/__snapshots__/jsfmt.spec.js.snap @@ -423,6 +423,27 @@ sizes="(max-width: 66rem) 100vw, 66rem" alt=""/> srcset=" /media/examples/surfer-240-200.jpg "> + + =====================================output===================================== + + ================================================================================ `; diff --git a/tests/html/attributes/srcset.html b/tests/html/attributes/srcset.html index 510886cfe..2544c7598 100644 --- a/tests/html/attributes/srcset.html +++ b/tests/html/attributes/srcset.html @@ -12,3 +12,24 @@ sizes="(max-width: 66rem) 100vw, 66rem" alt=""/> srcset=" /media/examples/surfer-240-200.jpg "> + + diff --git a/yarn.lock b/yarn.lock index 9d90f7949..af6c61209 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5934,6 +5934,10 @@ parse-json@^5.0.0: json-parse-better-errors "^1.0.1" lines-and-columns "^1.1.6" +parse-srcset@ikatyang/parse-srcset#54eb9c1cb21db5c62b4d0e275d7249516df6f0ee: + version "1.0.2" + resolved "https://codeload.github.com/ikatyang/parse-srcset/tar.gz/54eb9c1cb21db5c62b4d0e275d7249516df6f0ee" + parse5@5.1.1: version "5.1.1" resolved "https://registry.yarnpkg.com/parse5/-/parse5-5.1.1.tgz#f68e4e5ba1852ac2cadc00f4555fff6c2abb6178" @@ -7039,11 +7043,6 @@ sprintf-js@~1.0.2: resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c" integrity sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw= -srcset@2.0.1: - version "2.0.1" - resolved "https://registry.yarnpkg.com/srcset/-/srcset-2.0.1.tgz#8f842d357487eb797f413d9c309de7a5149df5ac" - integrity sha512-00kZI87TdRKwt+P8jj8UZxbfp7mK2ufxcIMWvhAOZNJTRROimpHeruWrGvCZneiuVDLqdyHefVp748ECTnyUBQ== - sshpk@^1.7.0: version "1.16.1" resolved "https://registry.yarnpkg.com/sshpk/-/sshpk-1.16.1.tgz#fb661c0bef29b39db40769ee39fa70093d6f6877"