diff --git a/README.md b/README.md index dc51c01..b0ee5f3 100644 --- a/README.md +++ b/README.md @@ -29,3 +29,19 @@ interface Node { attrs?: Record; } ``` + +You can manipulate the tree and serialize it back to HTML: + +```ts +import {parse, stringify} from 'fast-xml-parser'; + +const root = parse('hello'); + +// change text node +root.children[1].children[0].name = 'hello, world!' + +// hello, world! +console.log(stringify(root)); +``` + +> TBD: DOM API to manipulate the tree in a handy way diff --git a/e2e/serialize.test.js b/e2e/serialize.test.js new file mode 100644 index 0000000..907e119 --- /dev/null +++ b/e2e/serialize.test.js @@ -0,0 +1,12 @@ +const {parse, stringify, Node} = require('../dst/index'); + +describe('serialize', () => { + it('readme example', () => { + const root = parse('hello'); + + // change text node + root.children[1].children[0].name = 'hello, world!' + + expect(stringify(root)).toEqual('hello, world!'); + }); +}); diff --git a/src/index.ts b/src/index.ts index c5bfbf1..a3a0e93 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import {Node, TYPES, VOID_ELEMENTS} from './node'; +import {Node, TYPES} from './node'; import {Tokenizer} from './tokenizer'; import {stringifyAttrs} from './utils';