Skip to content

Commit 497a2a5

Browse files
feat: added processing itemprop for tags meta and link (#352)
1 parent 480656f commit 497a2a5

File tree

7 files changed

+628
-83
lines changed

7 files changed

+628
-83
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ Supported tags and attributes:
8585
- the `href` attribute of the `image` tag
8686
- the `xlink:href` attribute of the `use` tag
8787
- the `href` attribute of the `use` tag
88-
- 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`
88+
- 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`
8989
- 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`
90-
- 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`
90+
- 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`
9191

9292
#### `Boolean`
9393

src/utils.js

Lines changed: 71 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -562,19 +562,10 @@ function linkHrefFilter(tag, attribute, attributes) {
562562
return allowedRels.filter((value) => usedRels.includes(value)).length > 0;
563563
}
564564

565-
function metaContentFilter(tag, attribute, attributes) {
566-
let name = getAttributeValue(attributes, 'name');
567-
568-
if (name) {
569-
name = name.trim();
570-
571-
if (!name) {
572-
return false;
573-
}
574-
575-
name = name.toLowerCase();
576-
577-
const allowedNames = [
565+
const META = new Map([
566+
[
567+
'name',
568+
new Set([
578569
// msapplication-TileImage
579570
'msapplication-tileimage',
580571
'msapplication-square70x70logo',
@@ -583,23 +574,11 @@ function metaContentFilter(tag, attribute, attributes) {
583574
'msapplication-square310x310logo',
584575
'msapplication-config',
585576
'twitter:image',
586-
];
587-
588-
return allowedNames.includes(name);
589-
}
590-
591-
let property = getAttributeValue(attributes, 'property');
592-
593-
if (property) {
594-
property = property.trim();
595-
596-
if (!property) {
597-
return false;
598-
}
599-
600-
property = property.toLowerCase();
601-
602-
const allowedProperties = [
577+
]),
578+
],
579+
[
580+
'property',
581+
new Set([
603582
'og:image',
604583
'og:image:url',
605584
'og:image:secure_url',
@@ -608,9 +587,68 @@ function metaContentFilter(tag, attribute, attributes) {
608587
'og:video',
609588
'og:video:secure_url',
610589
'vk:image',
611-
];
590+
]),
591+
],
592+
[
593+
'itemprop',
594+
new Set([
595+
'image',
596+
'logo',
597+
'screenshot',
598+
'thumbnailurl',
599+
'contenturl',
600+
'downloadurl',
601+
'duringmedia',
602+
'embedurl',
603+
'installurl',
604+
'layoutimage',
605+
]),
606+
],
607+
]);
608+
609+
function linkItempropFilter(tag, attribute, attributes) {
610+
let name = getAttributeValue(attributes, 'itemprop');
611+
612+
if (name) {
613+
name = name.trim();
612614

613-
return allowedProperties.includes(property);
615+
if (!name) {
616+
return false;
617+
}
618+
619+
name = name.toLowerCase();
620+
621+
return META.get('itemprop').has(name);
622+
}
623+
624+
return false;
625+
}
626+
627+
function linkUnionFilter(tag, attribute, attributes) {
628+
return (
629+
linkHrefFilter(tag, attribute, attributes) ||
630+
linkItempropFilter(tag, attribute, attributes)
631+
);
632+
}
633+
634+
function metaContentFilter(tag, attribute, attributes) {
635+
for (const item of META) {
636+
const [key, allowedNames] = item;
637+
638+
let name = getAttributeValue(attributes, key);
639+
640+
if (name) {
641+
name = name.trim();
642+
643+
if (!name) {
644+
// eslint-disable-next-line no-continue
645+
continue;
646+
}
647+
648+
name = name.toLowerCase();
649+
650+
return allowedNames.has(name);
651+
}
614652
}
615653

616654
return false;
@@ -646,7 +684,7 @@ const defaultAttributes = [
646684
tag: 'link',
647685
attribute: 'href',
648686
type: 'src',
649-
filter: linkHrefFilter,
687+
filter: linkUnionFilter,
650688
},
651689
{
652690
tag: 'link',

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

Lines changed: 75 additions & 6 deletions
Large diffs are not rendered by default.

test/__snapshots__/loader.test.js.snap

Lines changed: 25 additions & 2 deletions
Large diffs are not rendered by default.

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

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

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

Lines changed: 350 additions & 28 deletions
Large diffs are not rendered by default.

test/fixtures/simple.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,27 @@ <h2>An Ordered HTML List</h2>
413413
<meta property="og:video:height" content="300" />
414414

415415
<meta property="vk:image" content="./image.png" />
416+
417+
<link itemprop="downloadUrl" href="image.png">
418+
<link itemprop="contentUrl" href="image.png">
419+
<link itemprop="installUrl" href="image.png">
420+
421+
<link itemprop="a" href="image.png">
422+
<link itemprop="b" href="image.png">
423+
<link itemprop=" " href="image.png">
424+
<link href="image.png">
425+
426+
<meta itemprop="image" content="./image.png" />
427+
<meta itemprop="logo" content="./image.png" />
428+
<meta itemprop="screenshot" content="./image.png" />
429+
<meta itemprop="thumbnailUrl" content="./image.png" />
430+
<meta itemprop="contentUrl" content="./image.png" />
431+
<meta itemprop="downloadUrl" content="./image.png" />
432+
<meta itemprop="duringMedia" content="./image.png" />
433+
<meta itemprop="embedUrl" content="./image.png" />
434+
<meta itemprop="installUrl" content="./image.png" />
435+
<meta itemprop="layoutImage" content="./image.png" />
436+
437+
<meta itemprop="a" content="./image.png" />
438+
<meta itemprop="b" content="./image.png" />
439+
<meta itemprop=" " content="./image.png" />

0 commit comments

Comments
 (0)