Skip to content

Commit

Permalink
Add serializeOuter function. Resolves inikulin#230.
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldenning committed Jan 13, 2022
1 parent 8e8e936 commit 32394b9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/parse5/lib/serializer/index.js
Expand Up @@ -36,6 +36,12 @@ export class Serializer {
return this.html;
}

serializeOuter() {
this._serializeElement(this.startNode);

return this.html;
}

//Internals
_serializeChildNodes(parentNode) {
const childNodes = this.treeAdapter.getChildNodes(parentNode);
Expand Down
13 changes: 13 additions & 0 deletions packages/parse5/test/serializer.test.js
Expand Up @@ -2,6 +2,7 @@ import assert from 'node:assert';
import * as parse5 from '../lib/index.js';
import { generateSeriliazerTests } from '../../../test/utils/generate-serializer-tests.js';
import { treeAdapters } from '../../../test/utils/common.js';
import { Serializer } from '../lib/serializer/index.js';

generateSeriliazerTests('serializer', 'Serializer', parse5.serialize);

Expand All @@ -21,4 +22,16 @@ suite('serializer', () => {
parse5.serialize(document, { treeAdapter });
});
});

test('serializes outerHTML correctly', () => {
const document = parse5.parse('<div><button>Hello</button></div>');

const div = document.childNodes[0].childNodes[1].childNodes[0];

const serializer = new Serializer(div);

const html = serializer.serializeOuter();

assert.equal(html, '<div><button>Hello</button></div>');
});
});

0 comments on commit 32394b9

Please sign in to comment.