Skip to content
This repository was archived by the owner on Aug 9, 2023. It is now read-only.

Accept root nodes #10

Closed
wants to merge 3 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var spaces = require('space-separated-tokens');
var commas = require('comma-separated-tokens');
var nan = require('is-nan');
var is = require('unist-util-is');
var u = require('unist-builder');

module.exports = wrapper;

Expand All @@ -18,17 +19,26 @@ function wrapper(h, node, prefix) {
throw new Error('h is not a function');
}

if (!is('element', node)) {
throw new Error('Expected element, not `' + node + '`');
}

r = react(h);
v = vdom(h);

if (prefix === null || prefix === undefined) {
prefix = r === true || v === true ? 'h-' : false;
}

if (is('root', node)) {
if (!Array.isArray(node.children) || node.children.length === 0) {
throw new Error('Expected children in root');
} else if (node.children.length === 1) {
node = node.children[0];
} else {
node = u('element', {tagName: 'div'}, node.children);
}
} else if (!is('element', node)) {
var name = (node && node.type) || node;
throw new Error('Expected root or element, not `' + name + '`');
}

return toH(h, node, {
prefix: prefix,
key: 0,
Expand Down
48 changes: 45 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,23 @@ test('hast-to-hyperscript', function (t) {
t.test('should throw if not given a node', function (st) {
t.throws(function () {
toH(h);
}, /Expected element, not `undefined`/);
}, /Expected root or element, not `undefined`/);

t.throws(function () {
toH(h, 'text');
}, /Error: Expected element, not `text`/);
}, /Error: Expected root or element, not `text`/);

t.throws(function () {
toH(h, u('text', 'value'));
}, /Expected element/);
}, /Expected root or element/);

st.end();
});

t.test('should throw if the root node has 0 children', function (st) {
t.throws(function () {
toH(h, u('root', {}, []));
}, /Expected children in root/);

st.end();
});
Expand Down Expand Up @@ -274,6 +282,40 @@ test('hast-to-hyperscript', function (t) {
st.end();
});

t.test('flattens a `root` element with exactly 1 child', function (st) {
const uTree = u('root', {}, [
u('element', {
tagName: 'h1',
properties: {id: 'a'}
})
]);

const vTree = toH(v, uTree);
st.equal(vTree.tagName, 'H1');
st.ok(vTree.properties);
st.equal(vTree.properties.id, 'a');

st.end();
});

t.test('uses a `div` for a `root` element with >1 children', function (st) {
const uTree = u('root', {}, [
u('element', {
tagName: 'h1',
properties: {id: 'a'}
}),
u('element', {
tagName: 'p',
properties: {id: 'b'}
})
]);

const vTree = toH(v, uTree);
st.equal(vTree.tagName, 'DIV');

st.end();
});

t.end();
});

Expand Down