Skip to content

Commit c9c8dad

Browse files
refactor: improve source parse (#250)
1 parent 079d623 commit c9c8dad

File tree

7 files changed

+530
-116
lines changed

7 files changed

+530
-116
lines changed

src/plugins/source-plugin.js

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -350,40 +350,26 @@ function parseSrcset(input) {
350350
}
351351

352352
function parseSrc(input) {
353-
let startIndex = 0;
354-
355353
if (!input) {
356354
throw new Error('Must be non-empty');
357355
}
358356

359-
for (let position = 0; position < input.length; position++) {
360-
const character = input.charAt(position);
357+
let startIndex = 0;
358+
let value = input;
361359

362-
if (isASCIIWhitespace(character)) {
363-
startIndex = position;
364-
} else {
365-
break;
366-
}
360+
while (isASCIIWhitespace(value.substring(0, 1))) {
361+
startIndex += 1;
362+
value = value.substring(1, value.length);
367363
}
368364

369-
if (startIndex === input.length - 1) {
370-
throw new Error('Must be non-empty');
365+
while (isASCIIWhitespace(value.substring(value.length - 1, value.length))) {
366+
value = value.substring(0, value.length - 1);
371367
}
372368

373-
let endIndex = input.length;
374-
375-
for (let position = input.length - 1; position >= 0; position--) {
376-
const character = input.charAt(position);
377-
378-
if (isASCIIWhitespace(character)) {
379-
endIndex = position;
380-
} else {
381-
break;
382-
}
369+
if (!value) {
370+
throw new Error('Must be non-empty');
383371
}
384372

385-
const value = input.slice(startIndex, endIndex + 1);
386-
387373
return { value, startIndex };
388374
}
389375

test/__snapshots__/attributes-option.test.js.snap

Lines changed: 254 additions & 42 deletions
Large diffs are not rendered by default.

test/__snapshots__/esModule-option.test.js.snap

Lines changed: 81 additions & 15 deletions
Large diffs are not rendered by default.

test/__snapshots__/loader.test.js.snap

Lines changed: 27 additions & 5 deletions
Large diffs are not rendered by default.

test/__snapshots__/minimize-option.test.js.snap

Lines changed: 87 additions & 21 deletions
Large diffs are not rendered by default.

test/__snapshots__/root-option.test.js.snap

Lines changed: 54 additions & 10 deletions
Large diffs are not rendered by default.

test/fixtures/simple.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,21 @@ <h2>An Ordered HTML List</h2>
232232
<img src=image.png# />
233233
<img srcset="image.png#hash" />
234234
<img srcset="image.png#foo 480w, image.png#bar 800w" sizes="(max-width: 600px) 480px, 800px" src="image.png#baz" alt="Elva dressed as a fairy">
235+
<img src="#" alt="test"/>
236+
<img src="#" srcset="#" alt="test" />
237+
<img src="###" srcset="#" alt="test" />
238+
<img src=" # " alt="test"/>
239+
<img src="
240+
241+
242+
#
243+
244+
245+
" alt="test"/>
246+
<img src=
247+
248+
249+
image.png
250+
251+
252+
/>

0 commit comments

Comments
 (0)