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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ Supported tags and attributes:
- the `href` attribute of the `image` tag
- the `xlink:href` attribute of the `use` tag
- the `href` attribute of the `use` tag
- 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`
- 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`
- 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`

#### `Boolean`

Expand Down
104 changes: 71 additions & 33 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,19 +562,10 @@ function linkHrefFilter(tag, attribute, attributes) {
return allowedRels.filter((value) => usedRels.includes(value)).length > 0;
}

function metaContentFilter(tag, attribute, attributes) {
let name = getAttributeValue(attributes, 'name');

if (name) {
name = name.trim();

if (!name) {
return false;
}

name = name.toLowerCase();

const allowedNames = [
const META = new Map([
[
'name',
new Set([
// msapplication-TileImage
'msapplication-tileimage',
'msapplication-square70x70logo',
Expand All @@ -583,23 +574,11 @@ function metaContentFilter(tag, attribute, attributes) {
'msapplication-square310x310logo',
'msapplication-config',
'twitter:image',
];

return allowedNames.includes(name);
}

let property = getAttributeValue(attributes, 'property');

if (property) {
property = property.trim();

if (!property) {
return false;
}

property = property.toLowerCase();

const allowedProperties = [
]),
],
[
'property',
new Set([
'og:image',
'og:image:url',
'og:image:secure_url',
Expand All @@ -608,9 +587,68 @@ function metaContentFilter(tag, attribute, attributes) {
'og:video',
'og:video:secure_url',
'vk:image',
];
]),
],
[
'itemprop',
new Set([
'image',
'logo',
'screenshot',
'thumbnailurl',
'contenturl',
'downloadurl',
'duringmedia',
'embedurl',
'installurl',
'layoutimage',
]),
],
]);

function linkItempropFilter(tag, attribute, attributes) {
let name = getAttributeValue(attributes, 'itemprop');

if (name) {
name = name.trim();

return allowedProperties.includes(property);
if (!name) {
return false;
}

name = name.toLowerCase();

return META.get('itemprop').has(name);
}

return false;
}

function linkUnionFilter(tag, attribute, attributes) {
return (
linkHrefFilter(tag, attribute, attributes) ||
linkItempropFilter(tag, attribute, attributes)
);
}

function metaContentFilter(tag, attribute, attributes) {
for (const item of META) {
const [key, allowedNames] = item;

let name = getAttributeValue(attributes, key);

if (name) {
name = name.trim();

if (!name) {
// eslint-disable-next-line no-continue
continue;
}

name = name.toLowerCase();

return allowedNames.has(name);
}
}

return false;
Expand Down Expand Up @@ -646,7 +684,7 @@ const defaultAttributes = [
tag: 'link',
attribute: 'href',
type: 'src',
filter: linkHrefFilter,
filter: linkUnionFilter,
},
{
tag: 'link',
Expand Down
81 changes: 75 additions & 6 deletions test/__snapshots__/esModule-option.test.js.snap

Large diffs are not rendered by default.

27 changes: 25 additions & 2 deletions test/__snapshots__/loader.test.js.snap

Large diffs are not rendered by default.

93 changes: 81 additions & 12 deletions test/__snapshots__/minimize-option.test.js.snap

Large diffs are not rendered by default.

378 changes: 350 additions & 28 deletions test/__snapshots__/sources-option.test.js.snap

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions test/fixtures/simple.html
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,27 @@ <h2>An Ordered HTML List</h2>
<meta property="og:video:height" content="300" />

<meta property="vk:image" content="./image.png" />

<link itemprop="downloadUrl" href="image.png">
<link itemprop="contentUrl" href="image.png">
<link itemprop="installUrl" href="image.png">

<link itemprop="a" href="image.png">
<link itemprop="b" href="image.png">
<link itemprop=" " href="image.png">
<link href="image.png">

<meta itemprop="image" content="./image.png" />
<meta itemprop="logo" content="./image.png" />
<meta itemprop="screenshot" content="./image.png" />
<meta itemprop="thumbnailUrl" content="./image.png" />
<meta itemprop="contentUrl" content="./image.png" />
<meta itemprop="downloadUrl" content="./image.png" />
<meta itemprop="duringMedia" content="./image.png" />
<meta itemprop="embedUrl" content="./image.png" />
<meta itemprop="installUrl" content="./image.png" />
<meta itemprop="layoutImage" content="./image.png" />

<meta itemprop="a" content="./image.png" />
<meta itemprop="b" content="./image.png" />
<meta itemprop=" " content="./image.png" />