From ca90e798e3849fdf55a55ec050dac169cfb866c8 Mon Sep 17 00:00:00 2001 From: taoqiufeng Date: Thu, 15 Jun 2017 18:27:57 +0800 Subject: [PATCH] add method set_content --- README.md | 16 +++++++++++++--- src/index.ts | 16 +++++++++++++--- test/html.js | 26 +++++++++++++++++++++++++- 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index bfda2f7..836db5c 100644 --- a/README.md +++ b/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(''); +const root = parse(''); console.log(root.firstChild.structure); // ul#list @@ -55,6 +55,14 @@ console.log(root.querySelector('#list')); // classNames: [] } console.log(root.toString()); // +root.set_content('
  • Hello World
  • '); +root.toString(); //
  • Hello World
  • +``` + +```js +var HTMLParser = require('node-html-parser'); + +var root = HTMLParser.parse(''); ``` ## 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. diff --git a/src/index.ts b/src/index.ts index c433416..f4b2420 100644 --- a/src/index.ts +++ b/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; diff --git a/test/html.js b/test/html.js index dcfff91..d88394d 100644 --- a/test/html.js +++ b/test/html.js @@ -249,11 +249,35 @@ describe('HTML Parser', function () { }); }); + describe('#set_content', function () { + + it('set content string', function () { + var root = parseHTML('
    '); + root.childNodes[0].set_content('
    abc
    bla
    '); + root.toString().should.eql('
    abc
    bla
    '); + }); + it('set content nodes', function () { + var root = parseHTML('
    '); + root.childNodes[0].set_content(parseHTML('
    abc
    bla
    ').childNodes); + root.toString().should.eql('
    abc
    bla
    '); + }); + it('set content node', function () { + var root = parseHTML('
    '); + root.childNodes[0].set_content(parseHTML('
    abc
    bla
    ').childNodes[0]); + root.toString().should.eql('
    abc
    bla
    '); + }); + it('set content text', function () { + var root = parseHTML('
    '); + root.childNodes[0].set_content('abc'); + root.toString().should.eql('
    abc
    '); + }); + + }); }); describe('stringify', function () { - describe('toString', function () { + describe('#toString()', function () { const html = '

    Hello

    bbb

    '; const root = parseHTML(html); root.toString().should.eql(html)