mirror of
https://github.com/danbulant/node-html-parser
synced 2026-06-17 05:31:13 +00:00
🐛 fix issue #37
This commit is contained in:
parent
16dac81e64
commit
2a6da2e01b
2 changed files with 8 additions and 2 deletions
|
|
@ -178,11 +178,11 @@ export default class HTMLElement extends Node {
|
||||||
}).join('');
|
}).join('');
|
||||||
}
|
}
|
||||||
|
|
||||||
public set_content(content: string | Node | Node[]) {
|
public set_content(content: string | Node | Node[], options = {} as Options) {
|
||||||
if (content instanceof Node) {
|
if (content instanceof Node) {
|
||||||
content = [content];
|
content = [content];
|
||||||
} else if (typeof content == 'string') {
|
} else if (typeof content == 'string') {
|
||||||
const r = parse(content);
|
const r = parse(content, options);
|
||||||
content = r.childNodes.length ? r.childNodes : [new TextNode(content)];
|
content = r.childNodes.length ? r.childNodes : [new TextNode(content)];
|
||||||
}
|
}
|
||||||
this.childNodes = content;
|
this.childNodes = content;
|
||||||
|
|
|
||||||
|
|
@ -524,6 +524,12 @@ describe('HTML Parser', function () {
|
||||||
root.childNodes[0].set_content('abc');
|
root.childNodes[0].set_content('abc');
|
||||||
root.toString().should.eql('<div>abc</div>');
|
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 () {
|
describe('encode/decode', function () {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue