Skip to content

Commit

Permalink
addresses #96
Browse files Browse the repository at this point in the history
  • Loading branch information
ded committed Nov 30, 2012
1 parent 9191478 commit 9e8440e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
11 changes: 10 additions & 1 deletion bonzo.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
, query = null // used for setting a selector engine host
, specialAttributes = /^(checked|value|selected|disabled)$/i
, specialTags = /^(select|fieldset|table|tbody|tfoot|td|tr|colgroup)$/i // tags that we have trouble inserting *into*
, simpleScriptTagRe = /\s*<script +src=['"]([^'"]+)['"]>/
, table = ['<table>', '</table>', 1]
, td = ['<table><tbody><tr>', '</tr></tbody></table>', 3]
, option = ['<select>', '</select>', 1]
Expand Down Expand Up @@ -1036,6 +1037,13 @@
return { x: win.pageXOffset || html.scrollLeft, y: win.pageYOffset || html.scrollTop }
}

function createScriptFromHtml(html) {
var scriptEl = document.createElement('script')
, matches = html.match(simpleScriptTagRe)
scriptEl.src = matches[1]
return scriptEl
}

/**
* @param {Array.<Element>|Element|Node|string} els
* @return {Bonzo}
Expand All @@ -1060,7 +1068,8 @@
// hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
return typeof node == 'string' && node !== '' ?
function () {
var tag = /^\s*<([^\s>]+)/.exec(node)
if (simpleScriptTagRe.test(node)) return [createScriptFromHtml(node)]
var tag = node.match(/^\s*<([^\s>]+)/)
, el = doc.createElement('div')
, els = []
, p = tag ? tagMap[tag[1].toLowerCase()] : null
Expand Down
2 changes: 1 addition & 1 deletion bonzo.min.js

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion src/bonzo.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
, parentNode = 'parentNode'
, specialAttributes = /^(checked|value|selected|disabled)$/i
, specialTags = /^(select|fieldset|table|tbody|tfoot|td|tr|colgroup)$/i // tags that we have trouble inserting *into*
, simpleScriptTagRe = /\s*<script +src=['"]([^'"]+)['"]>/
, table = ['<table>', '</table>', 1]
, td = ['<table><tbody><tr>', '</tr></tbody></table>', 3]
, option = ['<select>', '</select>', 1]
Expand Down Expand Up @@ -1031,6 +1032,13 @@
return { x: win.pageXOffset || html.scrollLeft, y: win.pageYOffset || html.scrollTop }
}

function createScriptFromHtml(html) {
var scriptEl = document.createElement('script')
, matches = html.match(simpleScriptTagRe)
scriptEl.src = matches[1]
return scriptEl
}

/**
* @param {Array.<Element>|Element|Node|string} els
* @return {Bonzo}
Expand All @@ -1055,7 +1063,8 @@
// hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
return typeof node == 'string' && node !== '' ?
function () {
var tag = /^\s*<([^\s>]+)/.exec(node)
if (simpleScriptTagRe.test(node)) return [createScriptFromHtml(node)]
var tag = node.match(/^\s*<([^\s>]+)/)
, el = doc.createElement('div')
, els = []
, p = tag ? tagMap[tag[1].toLowerCase()] : null
Expand Down
18 changes: 17 additions & 1 deletion tests/dommanip_insertions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2265,4 +2265,20 @@ sink('DOM Manipulation - insertions', function(test, ok, before, after, assert)
, verify : [ verifyExistingElementSourceEmpty, verifySingleToMultiPrepended, verifyReturnType('#insertiontastic > p') ]
})

})

/*********************************
* $.create script tags
*/

test('$.create with script tags will execute scripts', function (complete) {
$($.create('<script src="./sample-fixture.js?' + (+new Date) + '"></script>')).appendTo(document.body)
var intervalTimer = setInterval(function () {
if (typeof window.externalFunction == 'function') {
clearInterval(intervalTimer)
ok(true, 'loaded external script file')
complete()
}
}, 50)
})

})
3 changes: 3 additions & 0 deletions tests/sample-fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
window.externalFunction = function () {

}

0 comments on commit 9e8440e

Please sign in to comment.