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
3 changes: 2 additions & 1 deletion cjs/html/text-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class TextElement extends HTMLElement {

toString() {
const outerHTML = toString.call(this.cloneNode());
return outerHTML.replace('><', () => `>${this.textContent}<`);
const at = outerHTML.lastIndexOf('><');
return `${outerHTML.slice(0, at + 1)}${this.textContent}${outerHTML.slice(at + 1)}`;
}
}
exports.TextElement = TextElement
7 changes: 6 additions & 1 deletion cjs/interface/attr.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ const {attributeChangedCallback: ceAttributes} = require('./custom-element-regis

const {Node} = require('./node.js');
const {escape} = require('../shared/text-escaper.js');
const {decodeHTMLAttribute} = require('entities/decode');

const QUOTE = /"/g;
const AMP = /&/g;

// HTML keeps a raw & (e.g. ?a=1&b=2) unless the parser would read a character reference.
const escapeAmpersands = value => decodeHTMLAttribute(value) === value ? value : value.replace(AMP, '&amp;');

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

Expand Down
3 changes: 2 additions & 1 deletion esm/html/text-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export class TextElement extends HTMLElement {

toString() {
const outerHTML = toString.call(this.cloneNode());
return outerHTML.replace('><', () => `>${this.textContent}<`);
const at = outerHTML.lastIndexOf('><');
return `${outerHTML.slice(0, at + 1)}${this.textContent}${outerHTML.slice(at + 1)}`;
}
}
7 changes: 6 additions & 1 deletion esm/interface/attr.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ import {attributeChangedCallback as ceAttributes} from './custom-element-registr

import {Node} from './node.js';
import {escape} from '../shared/text-escaper.js';
import {decodeHTMLAttribute} from 'entities/decode';

const QUOTE = /"/g;
const AMP = /&/g;

// HTML keeps a raw & (e.g. ?a=1&b=2) unless the parser would read a character reference.
const escapeAmpersands = value => decodeHTMLAttribute(value) === value ? value : value.replace(AMP, '&amp;');

/**
* @implements globalThis.Attr
Expand Down Expand Up @@ -45,7 +50,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, '&quot;');
const escapedValue = (ignoreCase(this) ? escapeAmpersands(value) : escape(value)).replace(QUOTE, '&quot;');
return `${name}="${escapedValue}"`;
}

Expand Down
75 changes: 17 additions & 58 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"dependencies": {
"css-select": "^7.0.0",
"cssomnom": "^0.1.11",
"entities": "^7.0.1",
"html-escaper": "^3.0.3",
"htmlparser2": "^10.1.0",
"uhyphen": "^0.2.0"
Expand Down
6 changes: 6 additions & 0 deletions test/html/style-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ assert(style.sheet.cssRules[0].style.color, 'blue');
assert(style.sheet.cssRules[0].cssRules.length, 1);
assert(style.sheet.cssRules[0].cssRules[0].selectorText, '& .child');
assert(style.sheet.cssRules[0].cssRules[0].style.color, 'red');

// https://github.com/WebReflection/linkedom/issues/341
style = parseHTML('<!doctype html><html><head></head><body></body></html>').document.createElement('style');
style.setAttribute('data-src', 'a><b.css');
style.textContent = '.caption{color:red}';
assert(style.toString(), '<style data-src="a><b.css">.caption{color:red}</style>', 'style with >< in an attribute value');
12 changes: 12 additions & 0 deletions test/interface/attr.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,15 @@ 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');

// https://github.com/WebReflection/linkedom/issues/340
const {document: htmlDoc} = parseHTML('<!doctype html><html><body><div id="t"></div></body></html>');
const div = htmlDoc.getElementById('t');
div.setAttribute('data-x', 'A &amp; B');
assert(div.outerHTML, '<div data-x="A &amp;amp; B" id="t"></div>', 'HTML attribute & is escaped');
assert(parseHTML(`<!doctype html><html><body>${div.outerHTML}</body></html>`).document.getElementById('t').getAttribute('data-x'), 'A &amp; B', 'HTML attribute round trip');
div.setAttribute('data-x', '?a=1&b=2 & d&copy=1');
assert(div.outerHTML, '<div data-x="?a=1&b=2 & d&copy=1" id="t"></div>', 'HTML attribute & kept when it is not a character reference');
div.setAttribute('data-x', 'x&copy y&#38 z&#x26;');
assert(div.outerHTML, '<div data-x="x&amp;copy y&amp;#38 z&amp;#x26;" id="t"></div>', 'HTML attribute character references are escaped');
assert(parseHTML(`<!doctype html><html><body>${div.outerHTML}</body></html>`).document.getElementById('t').getAttribute('data-x'), 'x&copy y&#38 z&#x26;', 'HTML attribute character reference round trip');
1 change: 0 additions & 1 deletion types/esm/html/text-element.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export class TextElement extends HTMLElement {
toString(): any;
}
import { HTMLElement } from './element.js';
Loading