mirror of
https://github.com/danbulant/node-html-parser
synced 2026-06-16 13:11:04 +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
|
## Usage
|
||||||
|
|
||||||
```js
|
```ts
|
||||||
var HTMLParser = require('node-html-parser');
|
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);
|
console.log(root.firstChild.structure);
|
||||||
// ul#list
|
// ul#list
|
||||||
|
|
@ -55,6 +55,14 @@ console.log(root.querySelector('#list'));
|
||||||
// classNames: [] }
|
// classNames: [] }
|
||||||
console.log(root.toString());
|
console.log(root.toString());
|
||||||
// <ul id="list"><li>Hello World</li></ul>
|
// <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
|
## API
|
||||||
|
|
@ -141,3 +149,5 @@ Get innerHTML.
|
||||||
|
|
||||||
### HTMLElement#outerHTML
|
### HTMLElement#outerHTML
|
||||||
Get 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('');
|
}).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() {
|
get outerHTML() {
|
||||||
return this.toString();
|
return this.toString();
|
||||||
}
|
}
|
||||||
|
|
@ -237,9 +247,9 @@ export class HTMLElement extends Node {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Get DOM structure
|
* Get DOM structure
|
||||||
* @return {string} strucutre
|
* @return {string} strucutre
|
||||||
*/
|
*/
|
||||||
get structure() {
|
get structure() {
|
||||||
const res = [] as string[];
|
const res = [] as string[];
|
||||||
let indention = 0;
|
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('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 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);
|
const root = parseHTML(html);
|
||||||
root.toString().should.eql(html)
|
root.toString().should.eql(html)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue