Skip to content

Commit d753ed9

Browse files
authored
Fix primitive values
Closes GH-5. Closes GH-6. Reviewed-by: Merlijn Vos <[email protected]> Reviewed-by: Christian Murphy <[email protected]>
1 parent 7721053 commit d753ed9

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

Diff for: index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,16 @@ function element(node, schema) {
6666
function h(name, attrs) {
6767
var values = []
6868
var p5
69+
var attr
6970
var value
7071
var key
7172
var info
7273
var pos
7374

7475
for (key in attrs) {
7576
info = find(schema, key)
76-
value = {name: key, value: attrs[key]}
77+
attr = attrs[key]
78+
value = {name: key, value: attr === true ? '' : String(attr)}
7779

7880
if (info.space && ignoredSpaces.indexOf(info.space) === -1) {
7981
pos = key.indexOf(':')

Diff for: test/element.js

+75
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,81 @@ test('element', function(t) {
2121
st.end()
2222
})
2323

24+
t.test('should transform attributes', function(st) {
25+
var actual = toParse5({
26+
type: 'element',
27+
tagName: 'div',
28+
properties: {},
29+
children: [
30+
{
31+
type: 'element',
32+
tagName: 'h1',
33+
properties: {id: 'a', className: ['b', 'c'], hidden: true, height: 2},
34+
children: [
35+
{type: 'text', value: 'alpha '},
36+
{
37+
type: 'element',
38+
tagName: 'strong',
39+
properties: {
40+
style: 'color:red;',
41+
// Unknown booleans are ignored.
42+
ignored: false,
43+
// Falsey known booleans are ignored.
44+
disabled: 0,
45+
// Unknown props are left as-is.
46+
foo: 'bar',
47+
// Unknown lists are space-separated.
48+
// Note: you’d expect `camelCase` here, but p5 lowercases
49+
// attributes on HTML, so it drops the camelcase.
50+
camelcase: ['on', 'off'],
51+
// Numeric-start data properties.
52+
data123: '456',
53+
// Data properties.
54+
dataSome: 'yes',
55+
// ARIA props.
56+
ariaValuenow: '1'
57+
},
58+
children: [{type: 'text', value: 'bravo'}]
59+
},
60+
{type: 'text', value: ' charlie'}
61+
]
62+
},
63+
{
64+
type: 'element',
65+
tagName: 'input',
66+
properties: {
67+
checked: true,
68+
type: 'file',
69+
// Known comma-separated lists:
70+
accept: ['.jpg', '.jpeg']
71+
},
72+
children: []
73+
}
74+
]
75+
})
76+
77+
var expected = parse5.parseFragment(
78+
[
79+
'<div>',
80+
'<h1 id="a" class="b c" hidden height="2">',
81+
'alpha ',
82+
'<strong style="color:red;" foo="bar" camelCase="on off" data-123="456" data-some="yes" aria-valuenow="1">',
83+
'bravo',
84+
'</strong>',
85+
' charlie',
86+
'</h1>',
87+
'<input checked type="file" accept=".jpg, .jpeg">',
88+
'</div>'
89+
].join('')
90+
).childNodes[0]
91+
92+
delete expected.parentNode
93+
94+
st.deepEqual(json(actual), json(expected))
95+
96+
st.end()
97+
})
98+
2499
t.test('should transform void elements', function(st) {
25100
var actual = toParse5({
26101
type: 'element',

0 commit comments

Comments
 (0)