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: 2 additions & 2 deletions src/nodes/html.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { is, selectAll, selectOne } from 'css-select';
import { decodeHTML } from 'entities';
import { decodeHTML, encode } from 'entities';
import arr_back from '../back';
import Matcher from '../matcher';
import VoidTag from '../void-tag';
Expand Down Expand Up @@ -271,7 +271,7 @@ export default class HTMLElement extends Node {
return decode(this.rawText);
}
public set textContent(val: string) {
const content = [new TextNode(val, this)];
const content = [new TextNode(encode(val), this)];
this.childNodes = content;
}
/**
Expand Down
19 changes: 19 additions & 0 deletions test/tests/textcontent-encode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { parse } = require('@test/test-target');

describe('HTMLElement#textContent setter encodes special characters', function () {
it('escapes HTML metacharacters when serialized', function () {
const root = parse('<div></div>');
const div = root.firstChild;
div.textContent = '<b>Tom & Jerry</b>';
div.innerHTML.should.eql('&lt;b&gt;Tom &amp; Jerry&lt;/b&gt;');
div.textContent.should.eql('<b>Tom & Jerry</b>');
});
it('round-trips a text value that looks like markup', function () {
const root = parse('<div></div>');
const div = root.firstChild;
div.textContent = '<script>alert(1)</script>';
const reparsed = parse(div.toString()).firstChild;
reparsed.childNodes.should.have.length(1);
reparsed.textContent.should.eql('<script>alert(1)</script>');
});
});
Loading