Skip to content
Open
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: 3 additions & 1 deletion cjs/interface/attr.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const {Node} = require('./node.js');
const {escape} = require('../shared/text-escaper.js');

const QUOTE = /"/g;
const AMP_OR_NBSP = /[&\xA0]/g;
const escapeHTML = value => value.replace(AMP_OR_NBSP, c => c === '&' ? '&' : ' ');

/**
* @implements globalThis.Attr
Expand Down Expand Up @@ -46,7 +48,7 @@ class Attr extends Node {
if (emptyAttributes.has(name) && !value) {
return ignoreCase(this) ? name : `${name}=""`;
}
const escapedValue = (ignoreCase(this) ? value : escape(value)).replace(QUOTE, '"');
const escapedValue = (ignoreCase(this) ? escapeHTML(value) : escape(value)).replace(QUOTE, '"');
return `${name}="${escapedValue}"`;
}

Expand Down
4 changes: 3 additions & 1 deletion esm/interface/attr.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {Node} from './node.js';
import {escape} from '../shared/text-escaper.js';

const QUOTE = /"/g;
const AMP_OR_NBSP = /[&\xA0]/g;
const escapeHTML = value => value.replace(AMP_OR_NBSP, c => c === '&' ? '&' : ' ');

/**
* @implements globalThis.Attr
Expand Down Expand Up @@ -45,7 +47,7 @@ export class Attr extends Node {
if (emptyAttributes.has(name) && !value) {
return ignoreCase(this) ? name : `${name}=""`;
}
const escapedValue = (ignoreCase(this) ? value : escape(value)).replace(QUOTE, '"');
const escapedValue = (ignoreCase(this) ? escapeHTML(value) : escape(value)).replace(QUOTE, '"');
return `${name}="${escapedValue}"`;
}

Expand Down
4 changes: 2 additions & 2 deletions test/html/anchor-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const {document} = parseHTML('<a href="https://google.com/?q=1&page=2">click me<

const {lastElementChild: a} = document;

assert(a.toString(), '<a href="https://google.com/?q=1&page=2">click me</a>');
assert(a.toString(), '<a href="https://google.com/?q=1&amp;page=2">click me</a>');
a.setAttribute('href', 'https://google.com/?q=1&page=2&test="');
assert(a.toString(), '<a href="https://google.com/?q=1&page=2&test=&quot;">click me</a>');
assert(a.toString(), '<a href="https://google.com/?q=1&amp;page=2&amp;test=&quot;">click me</a>');
a.setAttribute('href', 'https://google.com/?q=asd&lol=<2>"');
assert(a.href, 'https://google.com/?q=asd&lol=%3C2%3E%22');
a.setAttribute('href', 'https://google.com/path%20to%20some%20file.pdf');
Expand Down
2 changes: 1 addition & 1 deletion test/html/meta-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ assert(b.charset, 'utf-8');
const {document: httpEquivRefresh} = parseHTML('<meta http-equiv="refresh" content="0; url=https://google.com/?q=1&page=2">');
const {lastElementChild: c} = httpEquivRefresh;
// assert toString
assert(c.toString(), '<meta http-equiv="refresh" content="0; url=https://google.com/?q=1&page=2">');
assert(c.toString(), '<meta http-equiv="refresh" content="0; url=https://google.com/?q=1&amp;page=2">');
// assert httpEquiv & content attribute
assert(c.httpEquiv, 'refresh');
assert(c.content, '0; url=https://google.com/?q=1&page=2');
Expand Down
18 changes: 18 additions & 0 deletions test/interface/attr.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,21 @@ assert(xmlDoc.toString(), '<?xml version="1.0" encoding="utf-8"?><element attr="
assert(xmlDoc.firstElementChild.toString(), '<element attr="a&quot;b&quot;c" />');
assert(xmlDoc.firstElementChild.outerHTML, '<element attr="a&quot;b&quot;c" />');
assert(xmlDoc.firstElementChild.attributes.attr.value, 'a"b"c');

// attribute values are escaped in HTML documents too (#340)
const {document: htmlDoc} = parseHTML('<!doctype html><html><body><div id="t"></div></body></html>');
const t = htmlDoc.getElementById('t');

t.setAttribute('data-x', 'A & B');
assert(t.outerHTML, '<div data-x="A &amp; B" id="t"></div>');

t.setAttribute('data-x', 'A &amp; B');
assert(t.getAttribute('data-x'), 'A &amp; B');
assert(t.outerHTML, '<div data-x="A &amp;amp; B" id="t"></div>');

t.setAttribute('data-x', 'a\u00A0b');
assert(t.getAttribute('data-x'), 'a\u00A0b');
assert(t.outerHTML, '<div data-x="a&#160;b" id="t"></div>');

t.setAttribute('data-x', 'say "hi"');
assert(t.outerHTML, '<div data-x="say &quot;hi&quot;" id="t"></div>');
4 changes: 2 additions & 2 deletions test/interface/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const parser = new DOMParser();
const htmlDoc = parser.parseFromString(`<div><span content-desc="text3&amp;more"/></div>`, 'text/html').documentElement;

assert(htmlDoc.firstChild.getAttribute('content-desc'), 'text3&more');
assert(htmlDoc.firstChild.outerHTML, '<span content-desc="text3&more"></span>');
assert(htmlDoc.innerHTML, '<span content-desc="text3&more"></span>');
assert(htmlDoc.firstChild.outerHTML, '<span content-desc="text3&amp;more"></span>');
assert(htmlDoc.innerHTML, '<span content-desc="text3&amp;more"></span>');

htmlDoc.firstChild.setAttribute('content-desc', ''); // attribute is not in emptyAttributes set is empty
assert(htmlDoc.firstChild.getAttribute('content-desc'), '');
Expand Down
4 changes: 3 additions & 1 deletion worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2998,6 +2998,8 @@ const pe = m => esca[m];
const escape = es => replace.call(es, ca, pe);

const QUOTE = /"/g;
const AMP_OR_NBSP = /[&\xA0]/g;
const escapeHTML = value => value.replace(AMP_OR_NBSP, c => c === '&' ? '&amp;' : '&#160;');

/**
* @implements globalThis.Attr
Expand Down Expand Up @@ -3032,7 +3034,7 @@ let Attr$1 = class Attr extends Node$1 {
if (emptyAttributes.has(name) && !value) {
return ignoreCase(this) ? name : `${name}=""`;
}
const escapedValue = (ignoreCase(this) ? value : escape(value)).replace(QUOTE, '&quot;');
const escapedValue = (ignoreCase(this) ? escapeHTML(value) : escape(value)).replace(QUOTE, '&quot;');
return `${name}="${escapedValue}"`;
}

Expand Down