🐛 fix issue #37

This commit is contained in:
taoqf 2020-03-31 12:28:15 +08:00
parent 16dac81e64
commit 2a6da2e01b
2 changed files with 8 additions and 2 deletions

View file

@ -178,11 +178,11 @@ export default class HTMLElement extends Node {
}).join('');
}
public set_content(content: string | Node | Node[]) {
public set_content(content: string | Node | Node[], options = {} as Options) {
if (content instanceof Node) {
content = [content];
} else if (typeof content == 'string') {
const r = parse(content);
const r = parse(content, options);
content = r.childNodes.length ? r.childNodes : [new TextNode(content)];
}
this.childNodes = content;

View file

@ -524,6 +524,12 @@ describe('HTML Parser', function () {
root.childNodes[0].set_content('abc');
root.toString().should.eql('<div>abc</div>');
});
it('set content pre', function () {
const root = parseHTML(`<html><head></head><body></body></html>`);
const body = root.querySelector("body");
body.set_content(`<pre>this is some preformatted text</pre>`, { pre: true });
root.toString().should.eql('<html><head></head><body><pre>this is some preformatted text</pre></body></html>')
});
});
describe('encode/decode', function () {