Skip to content
Merged
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
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,16 @@ function element(node, schema) {
function h(name, attrs) {
var values = []
var p5
var attr
var value
var key
var info
var pos

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(':')
Expand Down
75 changes: 75 additions & 0 deletions test/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
'<div>',
'<h1 id="a" class="b c" hidden height="2">',
'alpha ',
'<strong style="color:red;" foo="bar" camelCase="on off" data-123="456" data-some="yes" aria-valuenow="1">',
'bravo',
'</strong>',
' charlie',
'</h1>',
'<input checked type="file" accept=".jpg, .jpeg">',
'</div>'
].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',
Expand Down