Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serializeOuter function. Resolves #230. #378

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/parse5/lib/serializer/index.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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>');
});
});