mirror of
https://github.com/danbulant/node-html-parser
synced 2026-05-19 04:18:52 +00:00
add method set_content
This commit is contained in:
parent
9527f945e8
commit
ca90e798e3
3 changed files with 51 additions and 7 deletions
16
README.md
16
README.md
|
|
@ -33,10 +33,10 @@ Tested with [htmlparser-benchmark](https://github.com/AndreasMadsen/htmlparser-b
|
|||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
var HTMLParser = require('node-html-parser');
|
||||
```ts
|
||||
import { parse } from 'node-html-parser';
|
||||
|
||||
var root = HTMLParser.parse('<ul id="list"><li>Hello World</li></ul>');
|
||||
const root = parse('<ul id="list"><li>Hello World</li></ul>');
|
||||
|
||||
console.log(root.firstChild.structure);
|
||||
// ul#list
|
||||
|
|
@ -55,6 +55,14 @@ console.log(root.querySelector('#list'));
|
|||
// classNames: [] }
|
||||
console.log(root.toString());
|
||||
// <ul id="list"><li>Hello World</li></ul>
|
||||
root.set_content('<li>Hello World</li>');
|
||||
root.toString(); // <li>Hello World</li>
|
||||
```
|
||||
|
||||
```js
|
||||
var HTMLParser = require('node-html-parser');
|
||||
|
||||
var root = HTMLParser.parse('<ul id="list"><li>Hello World</li></ul>');
|
||||
```
|
||||
|
||||
## API
|
||||
|
|
@ -141,3 +149,5 @@ Get innerHTML.
|
|||
|
||||
### HTMLElement#outerHTML
|
||||
Get outerHTML.
|
||||
### HTMLElement#set_content(content: string | Node | Node[])
|
||||
Set content. **Notice**: Do not set content of the **root** node.
|
||||
|
|
|
|||
16
src/index.ts
16
src/index.ts
|
|
@ -208,6 +208,16 @@ export class HTMLElement extends Node {
|
|||
}).join('');
|
||||
}
|
||||
|
||||
set_content(content: string | Node | Node[]) {
|
||||
if (content instanceof Node) {
|
||||
content = [content];
|
||||
} else if (typeof content == 'string') {
|
||||
const r = parse(content);
|
||||
content = r.childNodes.length ? r.childNodes : [new TextNode(content)];
|
||||
}
|
||||
this.childNodes = content;
|
||||
}
|
||||
|
||||
get outerHTML() {
|
||||
return this.toString();
|
||||
}
|
||||
|
|
@ -237,9 +247,9 @@ export class HTMLElement extends Node {
|
|||
return this;
|
||||
}
|
||||
/**
|
||||
* Get DOM structure
|
||||
* @return {string} strucutre
|
||||
*/
|
||||
* Get DOM structure
|
||||
* @return {string} strucutre
|
||||
*/
|
||||
get structure() {
|
||||
const res = [] as string[];
|
||||
let indention = 0;
|
||||
|
|
|
|||
26
test/html.js
26
test/html.js
|
|
@ -249,11 +249,35 @@ describe('HTML Parser', function () {
|
|||
});
|
||||
|
||||
});
|
||||
describe('#set_content', function () {
|
||||
|
||||
it('set content string', function () {
|
||||
var root = parseHTML('<div></div>');
|
||||
root.childNodes[0].set_content('<span><div>abc</div>bla</span>');
|
||||
root.toString().should.eql('<div><span><div>abc</div>bla</span></div>');
|
||||
});
|
||||
it('set content nodes', function () {
|
||||
var root = parseHTML('<div></div>');
|
||||
root.childNodes[0].set_content(parseHTML('<span><div>abc</div>bla</span>').childNodes);
|
||||
root.toString().should.eql('<div><span><div>abc</div>bla</span></div>');
|
||||
});
|
||||
it('set content node', function () {
|
||||
var root = parseHTML('<div></div>');
|
||||
root.childNodes[0].set_content(parseHTML('<span><div>abc</div>bla</span>').childNodes[0]);
|
||||
root.toString().should.eql('<div><span><div>abc</div>bla</span></div>');
|
||||
});
|
||||
it('set content text', function () {
|
||||
var root = parseHTML('<div></div>');
|
||||
root.childNodes[0].set_content('abc');
|
||||
root.toString().should.eql('<div>abc</div>');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('stringify', function () {
|
||||
describe('toString', function () {
|
||||
describe('#toString()', function () {
|
||||
const html = '<p id="id" data-feidao-actions="ssss"><a class=\'cls\'>Hello</a><ul><li>aaaaa</li></ul><span>bbb</span></p>';
|
||||
const root = parseHTML(html);
|
||||
root.toString().should.eql(html)
|
||||
|
|
|
|||
Loading…
Reference in a new issue