From 9400ba6bf4c7fde20975aeb960cb94fb91eca51a Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 24 Apr 2019 11:26:34 +0200 Subject: [PATCH] Fix primitive values Closes GH-5. --- index.js | 4 ++- test/element.js | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index e0fa1ad..9b775a0 100644 --- a/index.js +++ b/index.js @@ -66,6 +66,7 @@ function element(node, schema) { function h(name, attrs) { var values = [] var p5 + var attr var value var key var info @@ -73,7 +74,8 @@ function element(node, schema) { for (key in attrs) { info = find(schema, key) - value = {name: key, value: attrs[key]} + attr = attrs[key] + value = {name: key, value: attr === true ? '' : String(attr)} if (info.space && ignoredSpaces.indexOf(info.space) === -1) { pos = key.indexOf(':') diff --git a/test/element.js b/test/element.js index d0e3f01..2b8be1c 100644 --- a/test/element.js +++ b/test/element.js @@ -21,6 +21,81 @@ test('element', function(t) { st.end() }) + t.test('should transform attributes', function(st) { + var actual = toParse5({ + type: 'element', + tagName: 'div', + properties: {}, + children: [ + { + type: 'element', + tagName: 'h1', + properties: {id: 'a', className: ['b', 'c'], hidden: true, height: 2}, + children: [ + {type: 'text', value: 'alpha '}, + { + type: 'element', + tagName: 'strong', + properties: { + style: 'color:red;', + // Unknown booleans are ignored. + ignored: false, + // Falsey known booleans are ignored. + disabled: 0, + // Unknown props are left as-is. + foo: 'bar', + // Unknown lists are space-separated. + // Note: you’d expect `camelCase` here, but p5 lowercases + // attributes on HTML, so it drops the camelcase. + camelcase: ['on', 'off'], + // Numeric-start data properties. + data123: '456', + // Data properties. + dataSome: 'yes', + // ARIA props. + ariaValuenow: '1' + }, + children: [{type: 'text', value: 'bravo'}] + }, + {type: 'text', value: ' charlie'} + ] + }, + { + type: 'element', + tagName: 'input', + properties: { + checked: true, + type: 'file', + // Known comma-separated lists: + accept: ['.jpg', '.jpeg'] + }, + children: [] + } + ] + }) + + var expected = parse5.parseFragment( + [ + '
', + '

', + 'alpha ', + '', + 'bravo', + '', + ' charlie', + '

', + '', + '
' + ].join('') + ).childNodes[0] + + delete expected.parentNode + + st.deepEqual(json(actual), json(expected)) + + st.end() + }) + t.test('should transform void elements', function(st) { var actual = toParse5({ type: 'element',