Skip to content

Commit 628e765

Browse files
committed
ensure options are passed to nested calls
1 parent 4d63e85 commit 628e765

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

test/getOpts.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
document.write('<script src="http://noah/code/writeCapture/test/options.js" />')

test/options.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
document.write("<iframe id=abcd />");
2+
if(document.getElementById("abcd")) {
3+
document.write('<div id="foo">FooBaZ</div>');
4+
}

test/pluginTests.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@
1212
$('#foo').writeCapture('replaceWith','<div id="foo">Foo<script type="text/javascript">document.write("BaZ");</script></div>');
1313
equals($('#foo').html(),'FooBaZ');
1414
});
15+
16+
module("options");
17+
test("writeOnGetElementById",function() {
18+
expect(1);
19+
$.writeCapture.writeOnGetElementById = false;
20+
$.writeCapture.proxyGetElementById = false;
21+
$('#foo').writeCapture().replaceWith('<script src="http://noah/code/writeCapture/test/getOpts.js"></script>',{
22+
writeOnGetElementById: true,
23+
done: function() {
24+
start();
25+
equals($('#foo').html(),'FooBaZ');
26+
}
27+
});
28+
stop();
29+
});
1530

1631
module("proxying");
1732
test('find',function(){

writeCapture.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@
377377
}
378378

379379
var SCRIPT_TAGS = /(<script[\s\S]*?>)([\s\S]*?)<\/script>/ig,
380+
SCRIPT_2 = /<script[\s\S]*?\/>/ig,
380381
SRC_REGEX = attrPattern('src'),
381382
SRC_ATTR = matchAttr('src'),
382383
TYPE_ATTR = matchAttr('type'),
@@ -475,7 +476,11 @@
475476
}
476477
// for each tag, generate a function to load and eval the code and queue
477478
// themselves
478-
return html.replace(SCRIPT_TAGS,proxyTag) + doneHtml;
479+
return html.replace(SCRIPT_TAGS,proxyTag).replace(SCRIPT_2,proxyBodyless) + doneHtml;
480+
function proxyBodyless(tag) {
481+
// hack in a bodyless tag...
482+
return proxyTag(tag,tag.substring(0,tag.length-2)+'>','');
483+
}
479484
function proxyTag(element,openTag,code) {
480485
var src = SRC_ATTR(openTag),
481486
type = TYPE_ATTR(openTag) || '',
@@ -599,8 +604,18 @@
599604
cb = newCallbackTag(state.finish) + (cb || '');
600605
html(state.out,cb);
601606
}
607+
function safeOpts(options) {
608+
var copy = {};
609+
for(var i in options) {
610+
if(options.hasOwnProperty(i)) {
611+
copy[i] = options[i];
612+
}
613+
}
614+
delete copy.done;
615+
return copy;
616+
}
602617
function html(markup,cb) {
603-
$.replaceWith(context.target,sanitize(markup,null,queue,context) + (cb || ''));
618+
$.replaceWith(context.target,sanitize(markup,safeOpts(options),queue,context) + (cb || ''));
604619
}
605620
return '<div style="display: none" id="'+divId+'"></div>' + openTag +
606621
TEMPLATE.replace(/%d/,id) + '</script>';

0 commit comments

Comments
 (0)