Skip to content

Add support for passing a document #4

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

Merged
merged 1 commit into from
May 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ Transform a [**hast**][hast] [*tree*][tree] to a DOM tree.

Whether a DOM fragment should be returned (default: `false`).

###### `options.document`

Document interface to use (default: `global.document`).

###### `options.namespace`

`namespace` to use to create [*elements*][element].
Expand Down
28 changes: 14 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function transform(node, options) {

// Create a document.
function root(node, options) {
const { fragment, namespace: optionsNamespace } = options;
const { doc, fragment, namespace: optionsNamespace } = options;
const { children = [] } = node;
const { length: childrenLength } = children;

Expand All @@ -45,43 +45,43 @@ function root(node, options) {
let el;

if (rootIsDocument) {
el = document.implementation.createDocument(namespace, '', null);
el = doc.implementation.createDocument(namespace, '', null);
} else if (fragment) {
el = document.createDocumentFragment();
el = doc.createDocumentFragment();
} else {
el = document.createElement('html');
el = doc.createElement('html');
}

return appendAll(el, children, Object.assign({ fragment, namespace }, options));
}

// Create a `doctype`.
function doctype(node) {
return document.implementation.createDocumentType(
function doctype(node, { doc }) {
return doc.implementation.createDocumentType(
node.name || 'html',
node.public || '',
node.system || '',
);
}

// Create a `text`.
function text(node) {
return document.createTextNode(node.value);
function text(node, { doc }) {
return doc.createTextNode(node.value);
}

// Create a `comment`.
function comment(node) {
return document.createComment(node.value);
function comment(node, { doc }) {
return doc.createComment(node.value);
}

// Create an `element`.
function element(node, options) {
const { namespace } = options;
const { namespace, doc } = options;
// TODO: use `g` in SVG space.
const { tagName = 'div', properties = {}, children = [] } = node;
const el = typeof namespace !== 'undefined'
? document.createElementNS(namespace, tagName)
: document.createElement(tagName);
? doc.createElementNS(namespace, tagName)
: doc.createElement(tagName);

// Add HTML attributes.
const props = Object.keys(properties);
Expand Down Expand Up @@ -146,5 +146,5 @@ function appendAll(node, children, options) {


export default function toDOM(hast, options = {}) {
return transform(hast, options);
return transform(hast, { ...options, doc: options.document || document });
}
31 changes: 31 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,37 @@ describe('hast-util-to-dom', () => {
expect(actual).toEqual('<html xmlns="http://example.com"/>');
});

it('should support a given document', () => {
const doc = {
createElementNS(namespace, tagName) {
const name = tagName === 'h1' ? 'h2' : tagName;
return document.createElementNS(namespace, name);
},
createTextNode(value) {
return document.createTextNode(value.toUpperCase());
},
implementation: {
createDocument(namespace, qualifiedName, documentType) {
return document.implementation.createDocument(namespace, qualifiedName, documentType);
},
},
};

const actual = serializeNodeToHtmlString(
toDOM({
type: 'root',
children: [
h('html', [
h('title', 'foo'),
h('h1', 'bar'),
]),
],
}, { document: doc }),
);

expect(actual).toEqual('<html><title>FOO</title><h2>BAR</h2></html>');
});

describe('booleanish property', () => {
it('handles booleanish attribute with `true` value correctly', () => {
const actual = serializeNodeToHtmlString(toDOM(h('div', {
Expand Down