Skip to content

Commit

Permalink
refactor: improve source parse (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Mar 12, 2020
1 parent 079d623 commit c9c8dad
Show file tree
Hide file tree
Showing 7 changed files with 530 additions and 116 deletions.
32 changes: 9 additions & 23 deletions src/plugins/source-plugin.js
Expand Up @@ -350,40 +350,26 @@ function parseSrcset(input) {
}

function parseSrc(input) {
let startIndex = 0;

if (!input) {
throw new Error('Must be non-empty');
}

for (let position = 0; position < input.length; position++) {
const character = input.charAt(position);
let startIndex = 0;
let value = input;

if (isASCIIWhitespace(character)) {
startIndex = position;
} else {
break;
}
while (isASCIIWhitespace(value.substring(0, 1))) {
startIndex += 1;
value = value.substring(1, value.length);
}

if (startIndex === input.length - 1) {
throw new Error('Must be non-empty');
while (isASCIIWhitespace(value.substring(value.length - 1, value.length))) {
value = value.substring(0, value.length - 1);
}

let endIndex = input.length;

for (let position = input.length - 1; position >= 0; position--) {
const character = input.charAt(position);

if (isASCIIWhitespace(character)) {
endIndex = position;
} else {
break;
}
if (!value) {
throw new Error('Must be non-empty');
}

const value = input.slice(startIndex, endIndex + 1);

return { value, startIndex };
}

Expand Down
296 changes: 254 additions & 42 deletions test/__snapshots__/attributes-option.test.js.snap

Large diffs are not rendered by default.

96 changes: 81 additions & 15 deletions test/__snapshots__/esModule-option.test.js.snap

Large diffs are not rendered by default.

32 changes: 27 additions & 5 deletions test/__snapshots__/loader.test.js.snap

Large diffs are not rendered by default.

108 changes: 87 additions & 21 deletions test/__snapshots__/minimize-option.test.js.snap

Large diffs are not rendered by default.

64 changes: 54 additions & 10 deletions test/__snapshots__/root-option.test.js.snap

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions test/fixtures/simple.html
Expand Up @@ -232,3 +232,21 @@ <h2>An Ordered HTML List</h2>
<img src=image.png# />
<img srcset="image.png#hash" />
<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">
<img src="#" alt="test"/>
<img src="#" srcset="#" alt="test" />
<img src="###" srcset="#" alt="test" />
<img src=" # " alt="test"/>
<img src="
#
" alt="test"/>
<img src=


image.png


/>

0 comments on commit c9c8dad

Please sign in to comment.