Skip to content

Commit 9835bde

Browse files
feat: supports link:href attribute for css (#258)
BREAKING CHANGE: `lin:href` attribute now supported by default
1 parent 7af2eff commit 9835bde

File tree

9 files changed

+603
-195
lines changed

9 files changed

+603
-195
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,19 @@ You may need to specify loaders for images in your configuration (recommended `f
6767

6868
Supported tags and attributes:
6969

70-
- `source:srcset`
71-
- `img:srcset`
72-
- `img:src`
7370
- `audio:src`
74-
- `video:src`
75-
- `track:src`
7671
- `embed:src`
77-
- `source:src`
72+
- `img:src`
73+
- `img:srcset`
7874
- `input:src`
75+
- `link:href`
7976
- `object:data`
8077
- `script:src`
78+
- `source:src`
79+
- `source:srcset`
80+
- `track:src`
81+
- `video:poster`
82+
- `video:src`
8183

8284
#### `Boolean`
8385

src/plugins/source-plugin.js

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -375,18 +375,31 @@ function parseSrc(input) {
375375
return { value, startIndex };
376376
}
377377

378+
function getAttributeValue(attributes, name) {
379+
const lowercasedAttributes = Object.keys(attributes).reduce((keys, k) => {
380+
// eslint-disable-next-line no-param-reassign
381+
keys[k.toLowerCase()] = k;
382+
383+
return keys;
384+
}, {});
385+
386+
return attributes[lowercasedAttributes[name.toLowerCase()]];
387+
}
388+
378389
const defaultAttributes = [
379-
'source:srcset',
380-
'img:src',
381-
'img:srcset',
382390
'audio:src',
383-
'video:src',
384-
'track:src',
385391
'embed:src',
386-
'source:src',
392+
'img:src',
393+
'img:srcset',
387394
'input:src',
395+
'link:href',
388396
'object:data',
389397
'script:src',
398+
'source:src',
399+
'source:srcset',
400+
'track:src',
401+
'video:poster',
402+
'video:src',
390403
];
391404

392405
export default (options) =>
@@ -444,6 +457,21 @@ export default (options) =>
444457
return;
445458
}
446459

460+
if (tag.toLowerCase() === 'link') {
461+
if (!/stylesheet/i.test(getAttributeValue(attributes, 'rel'))) {
462+
return;
463+
}
464+
465+
if (
466+
attributes.type &&
467+
getAttributeValue(attributes, 'type')
468+
.trim()
469+
.toLowerCase() !== 'text/css'
470+
) {
471+
return;
472+
}
473+
}
474+
447475
if (attribute.toLowerCase() === 'srcset') {
448476
let sourceSet;
449477

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

Lines changed: 312 additions & 90 deletions
Large diffs are not rendered by default.

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

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

test/__snapshots__/loader.test.js.snap

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

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

Lines changed: 114 additions & 51 deletions
Large diffs are not rendered by default.

test/attributes-option.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ describe("'attributes' option", () => {
184184
],
185185
},
186186
{
187-
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2|ogg|pdf|vtt)$/i,
187+
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2|ogg|pdf|vtt|css)$/i,
188188
loader: 'file-loader',
189189
options: { esModule: false, name: '[name].[ext]' },
190190
},
@@ -219,7 +219,7 @@ describe("'attributes' option", () => {
219219
],
220220
},
221221
{
222-
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2|ogg|pdf|vtt)$/i,
222+
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2|ogg|pdf|vtt|css)$/i,
223223
loader: 'file-loader',
224224
options: { esModule: true, name: '[name].[ext]' },
225225
},
@@ -254,7 +254,7 @@ describe("'attributes' option", () => {
254254
],
255255
},
256256
{
257-
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2|ogg|pdf|vtt)$/i,
257+
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2|ogg|pdf|vtt|css)$/i,
258258
loader: 'file-loader',
259259
options: { esModule: false, name: '[name].[ext]' },
260260
},
@@ -289,7 +289,7 @@ describe("'attributes' option", () => {
289289
],
290290
},
291291
{
292-
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2|ogg|pdf|vtt)$/i,
292+
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2|ogg|pdf|vtt|css)$/i,
293293
loader: 'file-loader',
294294
options: { esModule: true, name: '[name].[ext]' },
295295
},

test/fixtures/simple.html

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ <h2>An Ordered HTML List</h2>
113113
Your browser does not support the video tag.
114114
</video>
115115

116+
<video controls poster="./image.png">
117+
<source src="example.ogg" type="video/ogg">
118+
<track src="example.vtt" kind="subtitles" srclang="en" label="English">
119+
</video>
120+
116121
<object width="400" height="400" data="example.pdf"></object>
117122

118123
T ex t <img src = "image.png" > <img
@@ -127,12 +132,24 @@ <h2>An Ordered HTML List</h2>
127132

128133
<![CDATA[<img src="image.png">]]><img src="image.png">
129134

130-
<link rel="stylesheet" type="text/css" href="./style.css">
135+
<link rel="stylesheet" type="text/css" href="./style.file.css">
136+
<link REL="stylesheet" type="text/css" href="./style.file.css">
137+
<link ReL="stylesheet" type="text/css" href="./style.file.css">
138+
<link rel="STYLESHEET" type="text/css" href="./style.file.css">
139+
<link REL="STYLESHEET" type="text/css" href="./style.file.css">
140+
<link REL="STYLESHEET" type="text/css" HREF="./style.file.css">
141+
<link rel="alternate stylesheet" type="text/css" href="./style.file.css">
142+
<link rel="stylesheet alternate" type="text/css" href="./style.file.css">
143+
<link rel="stylesheet alternate" type=" text/css " href="./style.file.css">
144+
<link rel="stylesheet" href="./style.file.css">
145+
<link rel="stylesheet" type="text/css" href="./style.file.css">
146+
<link rel="stylesheet" type="text/html" href="./style.file.css">
147+
<link rel="stylesheet" TYPE="text/html" href="./style.file.css">
131148
<link rel="icon" href="./image.png" sizes="16x16">
132149
<link rel="apple-touch-icon" href="./image.png">
133150
<link rel="apple-touch-startup-image" href="./image.png">
134151
<link rel="shortlink" type="text/html" href="http://flic.kr/p/6XuLyD">
135-
<link type="text/css" href="./style.css">
152+
<link type="text/css" href="./style.file.css">
136153
<link rel="pingback" href="http://bob.example.net/xmlrpcserver">
137154
<a rel="author" href="./simple.html">link text</a>
138155

File renamed without changes.

0 commit comments

Comments
 (0)