From aefbddf6e4143457e7c2c22926d077acf56f212d Mon Sep 17 00:00:00 2001 From: Junyoung Choi Date: Fri, 24 Feb 2017 05:41:52 +0900 Subject: [PATCH 1/2] Transfrom unkown node with `data.h*` into element #11 --- lib/one.js | 21 ++++++++++++++++++++- test/core.js | 12 ++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) 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..91bc205 100644 --- a/test/core.js +++ b/test/core.js @@ -90,5 +90,17 @@ test('toHAST()', function (t) { 'should transform unknown nodes to `div`' ); + t.deepEqual( + to(u('foo', {data: {hName: 'code', hProperties: {className: 'tango'}, hChildren: [u('text', 'tango')]}}, 'tango')), + u('element', {tagName: 'code', properties: {className: 'tango'}}, [u('text', 'tango')]), + 'should transform unknown nodes with `data.h*` properties to `element` node' + ); + + 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.h*` except `data.hName` to `element` node with `div` as a `tagName`' + ); + t.end(); }); From 2353c57e557d3742018493645256611f44622b6b Mon Sep 17 00:00:00 2001 From: Junyoung Choi Date: Fri, 24 Feb 2017 05:57:26 +0900 Subject: [PATCH 2/2] Cover all `if` statements --- test/core.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/test/core.js b/test/core.js index 91bc205..1f1ac8a 100644 --- a/test/core.js +++ b/test/core.js @@ -91,15 +91,21 @@ test('toHAST()', function (t) { ); t.deepEqual( - to(u('foo', {data: {hName: 'code', hProperties: {className: 'tango'}, hChildren: [u('text', 'tango')]}}, 'tango')), - u('element', {tagName: 'code', properties: {className: 'tango'}}, [u('text', 'tango')]), - 'should transform unknown nodes with `data.h*` properties to `element` node' + 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.h*` except `data.hName` to `element` node with `div` as a `tagName`' + '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();