Skip to content

Commit ca90e79

Browse files
author
taoqiufeng
committed
add method set_content
1 parent 9527f94 commit ca90e79

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ Tested with [htmlparser-benchmark](https://github.com/AndreasMadsen/htmlparser-b
3333

3434
## Usage
3535

36-
```js
37-
var HTMLParser = require('node-html-parser');
36+
```ts
37+
import { parse } from 'node-html-parser';
3838

39-
var root = HTMLParser.parse('<ul id="list"><li>Hello World</li></ul>');
39+
const root = parse('<ul id="list"><li>Hello World</li></ul>');
4040

4141
console.log(root.firstChild.structure);
4242
// ul#list
@@ -55,6 +55,14 @@ console.log(root.querySelector('#list'));
5555
// classNames: [] }
5656
console.log(root.toString());
5757
// <ul id="list"><li>Hello World</li></ul>
58+
root.set_content('<li>Hello World</li>');
59+
root.toString(); // <li>Hello World</li>
60+
```
61+
62+
```js
63+
var HTMLParser = require('node-html-parser');
64+
65+
var root = HTMLParser.parse('<ul id="list"><li>Hello World</li></ul>');
5866
```
5967

6068
## API
@@ -141,3 +149,5 @@ Get innerHTML.
141149

142150
### HTMLElement#outerHTML
143151
Get outerHTML.
152+
### HTMLElement#set_content(content: string | Node | Node[])
153+
Set content. **Notice**: Do not set content of the **root** node.

src/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,16 @@ export class HTMLElement extends Node {
208208
}).join('');
209209
}
210210

211+
set_content(content: string | Node | Node[]) {
212+
if (content instanceof Node) {
213+
content = [content];
214+
} else if (typeof content == 'string') {
215+
const r = parse(content);
216+
content = r.childNodes.length ? r.childNodes : [new TextNode(content)];
217+
}
218+
this.childNodes = content;
219+
}
220+
211221
get outerHTML() {
212222
return this.toString();
213223
}
@@ -237,9 +247,9 @@ export class HTMLElement extends Node {
237247
return this;
238248
}
239249
/**
240-
* Get DOM structure
241-
* @return {string} strucutre
242-
*/
250+
* Get DOM structure
251+
* @return {string} strucutre
252+
*/
243253
get structure() {
244254
const res = [] as string[];
245255
let indention = 0;

test/html.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,35 @@ describe('HTML Parser', function () {
249249
});
250250

251251
});
252+
describe('#set_content', function () {
253+
254+
it('set content string', function () {
255+
var root = parseHTML('<div></div>');
256+
root.childNodes[0].set_content('<span><div>abc</div>bla</span>');
257+
root.toString().should.eql('<div><span><div>abc</div>bla</span></div>');
258+
});
259+
it('set content nodes', function () {
260+
var root = parseHTML('<div></div>');
261+
root.childNodes[0].set_content(parseHTML('<span><div>abc</div>bla</span>').childNodes);
262+
root.toString().should.eql('<div><span><div>abc</div>bla</span></div>');
263+
});
264+
it('set content node', function () {
265+
var root = parseHTML('<div></div>');
266+
root.childNodes[0].set_content(parseHTML('<span><div>abc</div>bla</span>').childNodes[0]);
267+
root.toString().should.eql('<div><span><div>abc</div>bla</span></div>');
268+
});
269+
it('set content text', function () {
270+
var root = parseHTML('<div></div>');
271+
root.childNodes[0].set_content('abc');
272+
root.toString().should.eql('<div>abc</div>');
273+
});
274+
275+
});
252276

253277
});
254278

255279
describe('stringify', function () {
256-
describe('toString', function () {
280+
describe('#toString()', function () {
257281
const html = '<p id="id" data-feidao-actions="ssss"><a class=\'cls\'>Hello</a><ul><li>aaaaa</li></ul><span>bbb</span></p>';
258282
const root = parseHTML(html);
259283
root.toString().should.eql(html)

0 commit comments

Comments
 (0)