Skip to content

feat: add support to css url #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
224 changes: 115 additions & 109 deletions README.md

Large diffs are not rendered by default.

108 changes: 75 additions & 33 deletions src/plugins/source-plugin.js
Original file line number Diff line number Diff line change
@@ -368,7 +368,6 @@ function parseSrc(input) {
while (isASCIIWhitespace(value.substring(value.length - 1, value.length))) {
value = value.substring(0, value.length - 1);
}

if (!value) {
throw new Error('Must be non-empty');
}
@@ -388,30 +387,34 @@ function getAttributeValue(attributes, name) {
}

const defaultAttributes = [
{
attribute: 'style',
type: 'src'
},
{
tag: 'audio',
attribute: 'src',
type: 'src',
type: 'src'
},
{
tag: 'embed',
attribute: 'src',
type: 'src',
type: 'src'
},
{
tag: 'img',
attribute: 'src',
type: 'src',
type: 'src'
},
{
tag: 'img',
attribute: 'srcset',
type: 'srcset',
type: 'srcset'
},
{
tag: 'input',
attribute: 'src',
type: 'src',
type: 'src'
},
{
tag: 'link',
@@ -424,53 +427,54 @@ const defaultAttributes = [

if (
attributes.type &&
getAttributeValue(attributes, 'type').trim().toLowerCase() !==
'text/css'
getAttributeValue(attributes, 'type')
.trim()
.toLowerCase() !== 'text/css'
) {
return false;
}

return true;
},
}
},
{
tag: 'object',
attribute: 'data',
type: 'src',
type: 'src'
},
{
tag: 'script',
attribute: 'src',
type: 'src',
type: 'src'
},
{
tag: 'source',
attribute: 'src',
type: 'src',
type: 'src'
},
{
tag: 'source',
attribute: 'srcset',
type: 'srcset',
type: 'srcset'
},
{
tag: 'track',
attribute: 'src',
type: 'src',
type: 'src'
},
{
tag: 'video',
attribute: 'poster',
type: 'src',
type: 'src'
},
{
tag: 'video',
attribute: 'src',
type: 'src',
},
type: 'src'
}
];

export default (options) =>
export default options =>
function process(html, result) {
let attributeList;
let maybeUrlFilter;
@@ -488,12 +492,12 @@ export default (options) =>
}

const sources = [];
const urlFilter = getFilter(maybeUrlFilter, (value) =>
const urlFilter = getFilter(maybeUrlFilter, value =>
isUrlRequest(value, root)
);
const getAttribute = (tag, attribute, attributes, resourcePath) => {
return attributeList.find(
(element) =>
element =>
(typeof element.tag === 'undefined' ||
(typeof element.tag !== 'undefined' &&
element.tag.toLowerCase() === tag.toLowerCase())) &&
@@ -516,11 +520,11 @@ export default (options) =>
this.attributesMeta[name] = { startIndex, unquoted };
},
onopentag(tag, attributes) {
Object.keys(attributes).forEach((attribute) => {
Object.keys(attributes).forEach(attribute => {
const value = attributes[attribute];
const {
startIndex: valueStartIndex,
unquoted,
unquoted
} = this.attributesMeta[attribute];

const foundAttribute = getAttribute(
@@ -549,13 +553,13 @@ export default (options) =>
parser.startIndex,
parser.endIndex,
html
),
)
});

return;
}

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

if (!urlFilter(attribute, source.value, resourcePath)) {
@@ -582,11 +586,49 @@ export default (options) =>
parser.startIndex,
parser.endIndex,
html
),
)
});

return;
}
if (attribute.toLowerCase() === 'style') {
const urlSources = [];

source.value.split('url(').forEach((urlSource, index) => {
/* Ignoring first index because the first is just the css propertie for example:
[0:'background-image: ', 1:'
"image.png");\ntext-align: center;text-decoration: overline; color:red;background-image:',
'"image.png")']*/

if (index !== 0) {
// removing the rest of the url value ');....' and removing single and douple quotations
urlSource = urlSource
.substring(0, urlSource.indexOf(')'))
.replace(/["']/g, '');
urlSources.push(urlSource);
}
});
let currIndex = 0;

urlSources.forEach(urlSource => {
if (!urlFilter(attribute, urlSource, resourcePath)) {
return;
}

// currIndex to indicate to the current urlSource
const sourceStartIndex = source.value.indexOf(
urlSource,
currIndex
);

const startIndex = valueStartIndex + sourceStartIndex;
// incrementing currIndex so we can go to the next urlSource
currIndex = sourceStartIndex + 1;

sources.push({ startIndex, value: urlSource, unquoted });
});
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very bad idea, we should use css-loader for that:

  1. Introduce new attribute type: type: src | srcset | style
  2. Run css-loader for style
  3. Stringify result

Ideally we should have two tests:

  • only css-loader
  • css-loader +postcss-loader, it is allow to autoprefixing in style attribute

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evilebottnawi I couldn't find a way to run css-loader on a string
all I can think about doing is to export style content to temporary file tmp.css and then find a way to run webpack with css-loader on that file, this will make html-loader depend on css-loader and webpack.
Is there a way to run css-loader only on a string ?

Copy link
Member

@alexander-akait alexander-akait Apr 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where we should think. In theory we should create virtual module. Maybe we right now we can use css-loader.call(content), maybe we need more options/API for that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need time to think about it.


if (!urlFilter(attribute, source.value, resourcePath)) {
return;
@@ -602,16 +644,16 @@ export default (options) =>
onerror(error) {
result.messages.push({
type: 'error',
value: error,
value: error
});
},
}
},
{
decodeEntities: false,
lowerCaseTags: false,
lowerCaseAttributeNames: false,
recognizeCDATA: true,
recognizeSelfClosing: true,
recognizeSelfClosing: true
}
);

@@ -647,15 +689,15 @@ export default (options) =>
value: {
type: 'source',
source: importKey,
importName,
},
importName
}
});
}

const replacerKey = JSON.stringify({
importKey,
unquoted,
hash,
hash
});
let replacerName = replacersMap.get(replacerKey);

@@ -670,8 +712,8 @@ export default (options) =>
hash,
importName,
replacerName,
unquoted,
},
unquoted
}
});
}

396 changes: 385 additions & 11 deletions test/__snapshots__/attributes-option.test.js.snap

Large diffs are not rendered by default.

108 changes: 105 additions & 3 deletions test/__snapshots__/esModule-option.test.js.snap

Large diffs are not rendered by default.

36 changes: 35 additions & 1 deletion test/__snapshots__/loader.test.js.snap

Large diffs are not rendered by default.

120 changes: 111 additions & 9 deletions test/__snapshots__/minimize-option.test.js.snap

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions test/fixtures/simple.html
Original file line number Diff line number Diff line change
@@ -271,3 +271,37 @@ <h2>An Ordered HTML List</h2>
<img data-srcset="image.png 480w, image.png 800w" sizes="(max-width: 600px) 480px, 800px" data-src="image.png" alt="Elva dressed as a fairy">

<img src=~aliasImageWithSpace#hash />


<div style="background-image:url(image.png)">First & Last</div>
<div style="background-image:url('image.png')">First & Last</div>
<div style='background-image:url(image.png)'>First & Last</div>
<div style='background-image:url("image.png")'>First & Last</div>
<div style='background-color:red;text-align: center;background-image:url("image.png");text-decoration: overline; color:red;'>Middle</div>
<div style='background-color:red;text-align: center;background-image:url(image.png);text-decoration: overline; color:red;'>Middle</div>
<div style='background-color:red;text-align: center;background-image:url(image.png);text-decoration: overline; color:red;'>Middle</div>
<div style="background-color:red;text-align: center;background-image:url('image.png');text-decoration: overline; color:red;">Middle</div>

<div style='background-color:red;
background-image:url("image.png");
text-align: center;text-decoration: overline;
background-image:url("image.png");
color:red;background-image:url(image.png)'>Multiple</div>

<div style="background-image:url('image.png');
background-color:red;
background-image:url(image.png);
background-image:url('image.png');
text-align: center;text-decoration: overline;
color:red;">Multiple</div>

<div STYLE="background-color:red;text-align: center;background-image:url('image.png');text-decoration: overline; color:red;">style attribute</div>
<div sTyLe="background-color:red;text-align: center;background-image:url('image.png');text-decoration: overline; color:red;">style attribute</div>