Skip to content
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

Fix SrcSet & CSS issues #148

Merged
merged 23 commits into from
Sep 15, 2024
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
44 changes: 28 additions & 16 deletions src/rewrite/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,36 @@ class HTML extends EventEmitter {
return ast;
}
wrapSrcset(str, meta = this.ctx.meta) {
return str
.split(',')
.map((src) => {
const parts = src.trimStart().split(' ');
if (parts[0]) parts[0] = this.ctx.rewriteUrl(parts[0], meta);
return parts.join(' ');
})
.join(', ');
const regex = /(.*?)\s\d+\.?\d?[xyhw].?/g
const match = str.matchAll(regex);
var matched = false;

for (const point of match) {
matched = true;
str = str.replace(point[1], this.ctx.rewriteUrl(point[1], meta));
};

if (matched !== true) {
str = this.ctx.rewriteUrl(str, meta);
};

return str;
}
unwrapSrcset(str, meta = this.ctx.meta) {
return str
.split(',')
.map((src) => {
const parts = src.trimStart().split(' ');
if (parts[0]) parts[0] = this.ctx.sourceUrl(parts[0], meta);
return parts.join(' ');
})
.join(', ');
const regex = /(.*?)\s\d+\.?\d?[xyhw].?/g
const match = str.matchAll(regex);
var matched = false;

for (const point of match) {
matched = true;
str = str.replace(point[1], this.ctx.sourceUrl(point[1], meta));
};

if (matched !== true) {
str = this.ctx.sourceUrl(str, meta);
};

return str;
}
static parse = parse;
static parseFragment = parseFragment;
Expand Down
2 changes: 1 addition & 1 deletion src/rewrite/rewrite.html.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,5 +317,5 @@ export function isStyle(name) {
}

export function isSrcset(name) {
return name === 'srcset' || name === 'imagesrcset';
return name === 'srcSet' || name === 'srcset' || name === 'imagesrcset';
}
62 changes: 42 additions & 20 deletions src/uv.handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,9 @@ function __uvHook(window) {
case 'SCRIPT':
event.data.value = __uv.js.source(event.data.value);
break;
case 'STYLE':
event.data.value = __uv.sourceCSS(event.data.value);
break;
default:
event.data.value = __uv.sourceHtml(event.data.value);
}
Expand Down Expand Up @@ -677,23 +680,26 @@ function __uvHook(window) {
},
});

client.element.hookProperty([HTMLImageElement], 'srcset', {
get: (target, that) => {
return (
client.element.getAttribute.call(
client.element.hookProperty(
[HTMLImageElement, HTMLSourceElement],
'srcset',
{
get: (target, that) => {
return (
client.element.getAttribute.call(
that,
__uv.attributePrefix + '-attr-srcset'
) || target.call(that)
);
},
set: (target, that, [val]) => {
client.element.setAttribute.call(
that,
__uv.attributePrefix + '-attr-srcset'
) || target.call(that)
);
},
set: (target, that, [val]) => {
client.element.setAttribute.call(
that,
__uv.attributePrefix + '-attr-srcset',
val
);
target.call(that, __uv.html.wrapSrcset(val.toString()));
},
__uv.attributePrefix + '-attr-srcset',
val
);
target.call(that, __uv.html.wrapSrcset(val.toString()));
},
});

client.element.hookProperty(HTMLScriptElement, 'integrity', {
Expand Down Expand Up @@ -792,14 +798,30 @@ function __uvHook(window) {
});

client.node.on('getTextContent', (event) => {
if (event.that.tagName === 'SCRIPT') {
event.data.value = __uv.js.source(event.data.value);
switch (event.that.tagName) {
case 'SCRIPT':
event.data.value = __uv.js.source(event.data.value);
break;
case 'STYLE':
event.data.value = __uv.sourceCSS(event.data.value);
break;
default:
// Interferes with HTML Character Entities.
// event.data.value = __uv.sourceHtml(event.data.value);
}
});

client.node.on('setTextContent', (event) => {
if (event.that.tagName === 'SCRIPT') {
event.data.value = __uv.js.rewrite(event.data.value);
switch (event.that.tagName) {
case 'SCRIPT':
event.data.value = __uv.js.rewrite(event.data.value);
break;
case 'STYLE':
event.data.value = __uv.rewriteCSS(event.data.value);
break;
default:
// Interferes with HTML Character Entities.
// event.data.value = __uv.rewriteHtml(event.data.value);
}
});

Expand Down