diff --git a/lib/one.js b/lib/one.js index 4a01282..3c1f899 100644 --- a/lib/one.js +++ b/lib/one.js @@ -6,9 +6,28 @@ var u = require('unist-builder'); var has = require('has'); var all = require('./all'); +/* Check if the node should be renderered a text node. */ +function isTextNode(node) { + if ('data' in node) { + if ('hName' in node.data) { + return false; + } + if ('hProperties' in node.data) { + return false; + } + if ('hChildren' in node.data) { + return false; + } + } + if ('value' in node) { + return true; + } + return false; +} + /* Transform an unknown node. */ function unknown(h, node) { - if ('value' in node) { + if (isTextNode(node)) { return h.augment(node, u('text', node.value)); } diff --git a/test/core.js b/test/core.js index 4c6421e..1f1ac8a 100644 --- a/test/core.js +++ b/test/core.js @@ -90,5 +90,23 @@ test('toHAST()', function (t) { 'should transform unknown nodes to `div`' ); + t.deepEqual( + to(u('foo', {data: {hName: 'code', hProperties: {className: 'charlie'}, hChildren: [u('text', 'tango')]}}, 'tango')), + u('element', {tagName: 'code', properties: {className: 'charlie'}}, [u('text', 'tango')]), + 'should transform unknown nodes with `data.h*` properties' + ); + + t.deepEqual( + to(u('foo', {data: {hChildren: [u('text', 'tango')]}}, 'tango')), + u('element', {tagName: 'div', properties: {}}, [u('text', 'tango')]), + 'should transform unknown nodes with `data.hChildren` only to `div`' + ); + + t.deepEqual( + to(u('foo', {data: {hProperties: {className: 'charlie'}}}, 'tango')), + u('element', {tagName: 'div', properties: {className: 'charlie'}}, []), + 'should transform unknown nodes with `data.hProperties` only to a `element` node' + ); + t.end(); });