Skip to content

Commit

Permalink
remove onload (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshuawuyts authored Jun 26, 2017
1 parent 630b8b8 commit 64c56dd
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 47 deletions.
32 changes: 3 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ var SVG_TAGS = [
'set', 'stop', 'switch', 'symbol', 'text', 'textPath', 'title', 'tref',
'tspan', 'use', 'view', 'vkern'
]
var onloadElementId = 0

module.exports = function yoYoify (file, opts) {
if (/\.json$/.test(file)) return through()
Expand Down Expand Up @@ -82,7 +81,6 @@ module.exports = function yoYoify (file, opts) {

function processNode (node) {
var args = [ node.quasis.map(cooked) ].concat(node.expressions.map(expr))
var needsOnLoad = false

var resultArgs = []
var argCount = 0
Expand All @@ -105,30 +103,6 @@ function processNode (node) {
res.push('var ' + elname + ' = document.createElement(' + JSON.stringify(tag) + ')')
}

// If adding onload events
if (props.onload || props.onunload) {
var onloadParts = getSourceParts(props.onload)
var onunloadParts = getSourceParts(props.onunload)
var onloadCode = ''
var onunloadCode = ''
var elementIdentifier = JSON.stringify('o' + onloadElementId)
onloadElementId += 1
if (onloadParts && onloadParts[0].arg !== '') {
onloadCode = 'args[' + argCount + '](' + elname + ')'
resultArgs.push(onloadParts[0].arg)
argCount++
}
if (onunloadParts && onunloadParts[0].arg !== '') {
onunloadCode = 'args[' + argCount + '](' + elname + ')'
resultArgs.push(onunloadParts[0].arg)
argCount++
}
res.push('var args = arguments\n onload(' + elname + ', function bel_onload () {\n ' + onloadCode + '\n }, function bel_onunload () {\n ' + onunloadCode + '\n }, ' + elementIdentifier + ')')
needsOnLoad = true
delete props.onload
delete props.onunload
}

function addAttr (to, key, val) {
// Normalize className
if (key.toLowerCase() === '"classname"') {
Expand Down Expand Up @@ -246,9 +220,9 @@ function processNode (node) {
var src = getSourceParts(res)
if (src && src[0].src) {
var params = resultArgs.join(',')
// TODO: This should use the on-load version of choo/yo-yo/bel
node.parent.update('(function () {\n ' + (needsOnLoad ? 'var onload = require(\'' + require.resolve('on-load').replace(/\\/g, '\\\\') + // fix Windows paths
'\')' : '') + '\n var ac = require(\'' + path.resolve(__dirname, 'lib', 'appendChild.js').replace(/\\/g, '\\\\') + // fix Windows paths

node.parent.update('(function () {\n ' +
'\n var ac = require(\'' + path.resolve(__dirname, 'lib', 'appendChild.js').replace(/\\/g, '\\\\') + // fix Windows paths
'\')\n ' + src[0].src + '\n return ' + src[0].name + '\n }(' + params + '))')
}
}
Expand Down
18 changes: 0 additions & 18 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,24 +142,6 @@ test('emits error for syntax error', function (t) {
})
})

test('onload/onunload', function (t) {
t.plan(4)
var src = 'var bel = require(\'bel\')\n var el = bel`<div onload=${function (e) {\n console.log(\'onload\', e)\n }} onunload=${function (e) {\n console.log(\'onunload\', e)\n }}>bel</div>`' // eslint-disable-line
fs.writeFileSync(FIXTURE, src)
var b = browserify(FIXTURE, {
transform: path.join(__dirname, '..')
})
b.bundle(function (err, src) {
fs.unlinkSync(FIXTURE)
t.ifError(err, 'no error')
var result = src.toString()
t.ok(result.indexOf('onload(bel0, function bel_onload () {' !== -1), 'adds onload event to element')
t.ok(result.indexOf('function bel_onunload () {' !== -1), 'adds onunload event to element')
t.ok(result.indexOf(', "o0")' !== -1), 'it identified the element')
t.end()
})
})

test('works with newer js', function (t) {
t.plan(1)
var src = 'const bel = require(\'bel\')\n async function whatever() {\n return bel`<div>yep</div>`\n }' // eslint-disable-line
Expand Down

0 comments on commit 64c56dd

Please sign in to comment.