Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for custom-self-closing elements #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
41 changes: 15 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var ATTR_KEY = 5, ATTR_KEY_W = 6
var ATTR_VALUE_W = 7, ATTR_VALUE = 8
var ATTR_VALUE_SQ = 9, ATTR_VALUE_DQ = 10
var ATTR_EQ = 11, ATTR_BREAK = 12
var COMMENT = 13
var COMMENT = 13, SELF_CLOSE = 14

module.exports = function (h, opts) {
if (!opts) opts = {}
Expand Down Expand Up @@ -98,13 +98,12 @@ module.exports = function (h, opts) {
} else if (s === VAR && p[1] === ATTR_KEY) {
cur[1][p[2]] = true
} else if (s === CLOSE) {
if (selfClosing(cur[0]) && stack.length) {
var ix = stack[stack.length-1][1]
stack.pop()
stack[stack.length-1][0][2][ix] = h(
cur[0], cur[1], cur[2].length ? cur[2] : undefined
)
}
} else if (s === SELF_CLOSE) {
var ix = stack[stack.length-1][1]
stack.pop()
stack[stack.length-1][0][2][ix] = h(
cur[0], cur[1], cur[2].length ? cur[2] : undefined
)
} else if (s === VAR && p[1] === TEXT) {
if (p[2] === undefined || p[2] === null) p[2] = ''
else if (!p[2]) p[2] = concat('', p[2])
Expand Down Expand Up @@ -147,6 +146,14 @@ module.exports = function (h, opts) {
if (reg.length) res.push([TEXT, reg])
reg = ''
state = OPEN
} else if (c === '>' && str.charAt(i - 1) === '/') {
res.push([SELF_CLOSE])
reg = ''
state = TEXT
} else if (c === '>' && str.charAt(i - 1) === '-' && str.charAt(i - 2) === '-') {
res.push([SELF_CLOSE])
reg = ''
state = TEXT
} else if (c === '>' && !quot(state) && state !== COMMENT) {
if (state === OPEN) {
res.push([OPEN,reg])
Expand Down Expand Up @@ -261,21 +268,3 @@ function quot (state) {

var hasOwn = Object.prototype.hasOwnProperty
function has (obj, key) { return hasOwn.call(obj, key) }

var closeRE = RegExp('^(' + [
'area', 'base', 'basefont', 'bgsound', 'br', 'col', 'command', 'embed',
'frame', 'hr', 'img', 'input', 'isindex', 'keygen', 'link', 'meta', 'param',
'source', 'track', 'wbr', '!--',
// SVG TAGS
'animate', 'animateTransform', 'circle', 'cursor', 'desc', 'ellipse',
'feBlend', 'feColorMatrix', 'feComposite',
'feConvolveMatrix', 'feDiffuseLighting', 'feDisplacementMap',
'feDistantLight', 'feFlood', 'feFuncA', 'feFuncB', 'feFuncG', 'feFuncR',
'feGaussianBlur', 'feImage', 'feMergeNode', 'feMorphology',
'feOffset', 'fePointLight', 'feSpecularLighting', 'feSpotLight', 'feTile',
'feTurbulence', 'font-face-format', 'font-face-name', 'font-face-uri',
'glyph', 'glyphRef', 'hkern', 'image', 'line', 'missing-glyph', 'mpath',
'path', 'polygon', 'polyline', 'rect', 'set', 'stop', 'tref', 'use', 'view',
'vkern'
].join('|') + ')(?:[\.#][a-zA-Z0-9\u007F-\uFFFF_:-]+)*$')
function selfClosing (tag) { return closeRE.test(tag) }
16 changes: 16 additions & 0 deletions test/empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var test = require('tape')
var vdom = require('virtual-dom')
var hyperx = require('../')
var hx = hyperx(vdom.h)

test('self closing tag should close', function (t) {
var tree = hx`<div>Hello <br /> World</div>`
t.equal(vdom.create(tree).toString(), '<div>Hello <br /> World</div>')
t.end()
})

test('custom self closing tag should close', function (t) {
var tree = hx`<div><self-closing-tag />Hello World</div>`
t.equal(vdom.create(tree).toString(), '<div><self-closing-tag></self-closing-tag>Hello World</div>')
t.end()
})
7 changes: 0 additions & 7 deletions test/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,3 @@ test('multiple keys dont overwrite existing ones', function (t) {
t.equal(vdom.create(tree).toString(), '<input type="date" />')
t.end()
})

// https://github.com/choojs/hyperx/issues/55
test('unquoted key does not make void element eat adjacent elements', function (t) {
var tree = hx`<span><input type=text>sometext</span>`
t.equal(vdom.create(tree).toString(), '<span><input type="text" />sometext</span>')
t.end()
})