Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Supported tags and attributes:
- the `href` attribute of the `link` tag when the `rel` attribute contains `stylesheet`, `icon`, `shortcut icon`, `mask-icon`, `apple-touch-icon`, `apple-touch-icon-precomposed`, `apple-touch-startup-image`, `manifest`, `prefetch`, `preload` or when the `itemprop` attribute is `image`, `logo`, `screenshot`, `thumbnailurl`, `contenturl`, `downloadurl`, `duringmedia`, `embedurl`, `installurl`, `layoutimage`
- the `imagesrcset` attribute of the `link` tag when the `rel` attribute contains `stylesheet`, `icon`, `shortcut icon`, `mask-icon`, `apple-touch-icon`, `apple-touch-icon-precomposed`, `apple-touch-startup-image`, `manifest`, `prefetch`, `preload`
- the `content` attribute of the `meta` tag when the `name` attribute is `msapplication-tileimage`, `msapplication-square70x70logo`, `msapplication-square150x150logo`, `msapplication-wide310x150logo`, `msapplication-square310x310logo`, `msapplication-config`, `twitter:image` or when the `property` attribute is `og:image`, `og:image:url`, `og:image:secure_url`, `og:audio`, `og:audio:secure_url`, `og:video`, `og:video:secure_url`, `vk:image` or when the `itemprop` attribute is `image`, `logo`, `screenshot`, `thumbnailurl`, `contenturl`, `downloadurl`, `duringmedia`, `embedurl`, `installurl`, `layoutimage`
- the `icon-uri` value component in `content` attribute of the `meta` tag when the `name` attribute is `msapplication-task`

#### `Boolean`

Expand Down
117 changes: 34 additions & 83 deletions src/plugins/sources-plugin.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import SAXParser from 'parse5-sax-parser';

import HtmlSourceError from '../HtmlSourceError';
import {
getFilter,
parseSrc,
parseSrcset,
normalizeUrl,
requestify,
isUrlRequestable,
c0ControlCodesExclude,
stringifyRequest,
typeSrc,
typeSrcset,
} from '../utils';

export default (options) =>
Expand Down Expand Up @@ -46,7 +44,7 @@ export default (options) =>
const { tagName, attrs, sourceCodeLocation } = node;

attrs.forEach((attribute) => {
const { value, prefix } = attribute;
const { prefix } = attribute;
let { name } = attribute;

name = prefix ? `${prefix}:${name}` : name;
Expand All @@ -72,96 +70,49 @@ export default (options) =>
target[target.length - 1] !== '"' &&
target[target.length - 1] !== "'";

const result = [];

// eslint-disable-next-line default-case
switch (type) {
case 'src': {
let source;

try {
source = parseSrc(value);
} catch (error) {
options.errors.push(
new HtmlSourceError(
`Bad value for attribute "${attribute.name}" on element "${tagName}": ${error.message}`,
sourceCodeLocation.attrs[name].startOffset,
sourceCodeLocation.attrs[name].endOffset,
html
)
);

return;
}

source = c0ControlCodesExclude(source);

if (!isUrlRequestable(source.value)) {
return;
}

const startOffset =
sourceCodeLocation.attrs[name].startOffset +
target.indexOf(source.value, name.length);

sources.push({
name,
value: source.value,
unquoted,
startIndex: startOffset,
endIndex: startOffset + source.value.length,
});

typeSrc({ name, attribute, node, target, html, options }).forEach(
(i) => {
result.push(i);
}
);
break;
}

case 'srcset': {
let sourceSet;

try {
sourceSet = parseSrcset(value);
} catch (error) {
options.errors.push(
new HtmlSourceError(
`Bad value for attribute "${attribute.name}" on element "${tagName}": ${error.message}`,
sourceCodeLocation.attrs[name].startOffset,
sourceCodeLocation.attrs[name].endOffset,
html
)
);

return;
}

sourceSet = sourceSet.map((item) => {
return {
source: c0ControlCodesExclude(item.source),
};
typeSrcset({
name,
attribute,
node,
target,
html,
options,
}).forEach((i) => {
result.push(i);
});
break;
}

let searchFrom = name.length;

sourceSet.forEach((sourceItem) => {
const { source } = sourceItem;

if (!isUrlRequestable(source.value)) {
return;
default: {
type({ name, attribute, node, target, html, options }).forEach(
(i) => {
result.push(i);
}
);
}
}

const startOffset =
sourceCodeLocation.attrs[name].startOffset +
target.indexOf(source.value, searchFrom);

searchFrom = target.indexOf(source.value, searchFrom) + 1;

sources.push({
name,
value: source.value,
unquoted,
startIndex: startOffset,
endIndex: startOffset + source.value.length,
});
for (const i of result) {
if (i) {
sources.push({
...i,
name,
unquoted,
});

break;
}
}
});
Expand Down
182 changes: 181 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import path from 'path';

import HtmlSourceError from './HtmlSourceError';

function isASCIIWhitespace(character) {
return (
// Horizontal tab
Expand Down Expand Up @@ -608,6 +610,7 @@ const META = new Map([
'layoutimage',
]),
],
['name', new Set(['msapplication-task'])],
]);

function linkItempropFilter(tag, attribute, attributes) {
Expand Down Expand Up @@ -658,6 +661,183 @@ function metaContentFilter(tag, attribute, attributes) {
return false;
}

export function typeSrc({ name, attribute, node, target, html, options }) {
const { tagName, sourceCodeLocation } = node;
const { value } = attribute;
const result = [];
let source;

try {
source = parseSrc(value);
} catch (error) {
options.errors.push(
new HtmlSourceError(
`Bad value for attribute "${attribute.name}" on element "${tagName}": ${error.message}`,
sourceCodeLocation.attrs[name].startOffset,
sourceCodeLocation.attrs[name].endOffset,
html
)
);

return result;
}

source = c0ControlCodesExclude(source);

if (!isUrlRequestable(source.value)) {
return result;
}

const startOffset =
sourceCodeLocation.attrs[name].startOffset +
target.indexOf(source.value, name.length);

result.push({
value: source.value,
startIndex: startOffset,
endIndex: startOffset + source.value.length,
});

return result;
}

export function typeSrcset({ name, attribute, node, target, html, options }) {
const { tagName, sourceCodeLocation } = node;
const { value } = attribute;
const result = [];
let sourceSet;

try {
sourceSet = parseSrcset(value);
} catch (error) {
options.errors.push(
new HtmlSourceError(
`Bad value for attribute "${attribute.name}" on element "${tagName}": ${error.message}`,
sourceCodeLocation.attrs[name].startOffset,
sourceCodeLocation.attrs[name].endOffset,
html
)
);

return result;
}

sourceSet = sourceSet.map((item) => {
return {
source: c0ControlCodesExclude(item.source),
};
});

let searchFrom = name.length;

sourceSet.forEach((sourceItem) => {
const { source } = sourceItem;

if (!isUrlRequestable(source.value)) {
return false;
}

const startOffset =
sourceCodeLocation.attrs[name].startOffset +
target.indexOf(source.value, searchFrom);

searchFrom = target.indexOf(source.value, searchFrom) + 1;

result.push({
value: source.value,
startIndex: startOffset,
endIndex: startOffset + source.value.length,
});

return false;
});

return result;
}

function typeMsapplicationTask({
name,
attribute,
node,
target,
html,
options,
}) {
const { tagName, sourceCodeLocation } = node;
const [content] = typeSrc({ name, attribute, node, target, html, options });
const result = [];

if (!content) {
return result;
}

let startIndex = 0;
let endIndex = 0;
let foundIconUri;
let source;

content.value.split(';').forEach((i) => {
if (foundIconUri) {
return;
}

if (!i.includes('icon-uri')) {
// +1 because of ";"
startIndex += i.length + 1;
return;
}

foundIconUri = true;

const [, aValue] = i.split('=');

try {
source = parseSrc(aValue);
} catch (error) {
options.errors.push(
new HtmlSourceError(
`Bad value for attribute "icon-uri" on element "${tagName}": ${error.message}`,
sourceCodeLocation.attrs[name].startOffset,
sourceCodeLocation.attrs[name].endOffset,
html
)
);

return;
}

// +1 because of "="
startIndex += i.indexOf('=') + source.startIndex + 1;
endIndex = startIndex + source.value.length;
});

if (!source) {
return result;
}

result.push({
...content,
startIndex: content.startIndex + startIndex,
endIndex: content.startIndex + endIndex,
name: 'icon-uri',
value: source.value,
});

return result;
}

function metaContentType({ name, attribute, node, target, html, options }) {
const isMsapplicationTask = node.attrs.filter(
(i) =>
i.name.toLowerCase() === 'name' &&
i.value.toLowerCase() === 'msapplication-task'
);

return isMsapplicationTask.length === 0
? typeSrc({ name, attribute, node, target, html, options })
: typeMsapplicationTask({ name, attribute, node, target, html, options });
}

const defaultAttributes = [
{
tag: 'audio',
Expand Down Expand Up @@ -699,7 +879,7 @@ const defaultAttributes = [
{
tag: 'meta',
attribute: 'content',
type: 'src',
type: metaContentType,
filter: metaContentFilter,
},
{
Expand Down
66 changes: 33 additions & 33 deletions test/__snapshots__/esModule-option.test.js.snap

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions test/__snapshots__/loader.test.js.snap

Large diffs are not rendered by default.

84 changes: 42 additions & 42 deletions test/__snapshots__/minimize-option.test.js.snap

Large diffs are not rendered by default.

237 changes: 125 additions & 112 deletions test/__snapshots__/sources-option.test.js.snap

Large diffs are not rendered by default.

Binary file added test/fixtures/favicon.ico
Binary file not shown.
12 changes: 12 additions & 0 deletions test/fixtures/sources.html
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,15 @@
<link rel="preload" as="image" imagesrcset="image.png 400w, image.png 800w, image.png 1600w" imagesizes="100vw">
<link rel="preload" as="image" imagesrcset="image.png 400w" imagesizes="100vw">
<link rel="preload" as="image" imagesrcset="" imagesizes="100vw">

<meta content="name=Check Order Status;
action-uri=./orderStatus.aspx?src=IE9;
icon-uri= ./image.png" name="msapplication-task">

<meta content="name=Check Order Status;
icon-uri= ./image.png;
action-uri=./orderStatus.aspx?src=IE9" name="msapplication-task">

<meta content="name=Check Order Status;
action-uri=./orderStatus.aspx?src=IE9;
icon-uri=" name="msapplication-task">
2 changes: 1 addition & 1 deletion test/helpers/getCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default (fixture, loaderOptions = {}, config = {}) => {
type: 'asset/inline',
},
{
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2|ogg|pdf|vtt|webp|xml|webmanifest|mp3|mp4)$/i,
test: /\.(png|jpg|gif|svg|ico|eot|ttf|woff|woff2|ogg|pdf|vtt|webp|xml|webmanifest|mp3|mp4)$/i,
resourceQuery: /^(?!.*\?url).*$/,
type: 'asset/resource',
},
Expand Down
Loading