Skip to content

Commit

Permalink
Add support for write/writeln called with multiple arguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
bnkuhn committed Aug 9, 2013
1 parent 3737e3c commit df940eb
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 20 deletions.
9 changes: 8 additions & 1 deletion postscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,14 @@
options.afterWrite(str);
}

set(doc, { write: write, writeln: function(str) { write(str + '\n'); } });
set(doc, {
write: function(){
return write(toArray(arguments).join(''));
},
writeln: function(str) {
return write(toArray(arguments).join('') + '\n');
}
});

// Override window.onerror
var oldOnError = active.win.onerror || doNothing;
Expand Down
48 changes: 29 additions & 19 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ var IFrame = function(id) {
ifr.doc._write = ifr.doc.write;
ifr.doc._writeln = ifr.doc.writeln;

ifr.doc.write = function(str) {
ifr.doc._write(str);
ifr.doc.write = function() {
ifr.doc._write.apply(ifr.doc, [].slice.call(arguments));
}

ifr.doc.writeln = function(str) {
ifr.doc.write(str+'\n');
ifr.doc.writeln = function() {
var args = [].slice.call(arguments);
args.push('\n');
ifr.doc.write.apply(ifr.doc, args);
}

ifr.doc.writeInline = function(js) {
Expand Down Expand Up @@ -197,8 +199,8 @@ var execute = function(name, tags, options) {
}, 'Rendering Complete');
},

compareInnerHtml: function(str) {
self.eqPrefix(innerHtml(self.div), tag.id+':'+str);
compareInnerHtml: function() {
self.eqPrefix(innerHtml(self.div), tag.id+':'+[].slice.call(arguments).join(''));
}

};
Expand Down Expand Up @@ -238,28 +240,36 @@ var execute = function(name, tags, options) {
autoFix: true
});

self.doc.write = function(str) {
console.log('native docwrite', str);
self.doc.write = function() {
var args = [].slice.call(arguments);
console.log('native docwrite', args);

if(parser) {
parser.append(str);
str = '';
for(var tok; tok = parser.readToken();) {
str += tok.text;
}
$.each(args, function(index, value) {
parser.append(value);
});
args = (function() {
var str = '';
for(var tok; tok = parser.readToken();) {
str += tok.text;
}
return [str];
})();
}


//TODO(dbrans): Add comment explaining why this is commented out.
//str = str.replace(/\.js/g, '.js?'+Math.random());

self.written = self.written + str;
$.each(args, function(index, value) {
self.written = self.written + value;
});

if(options.useInnerHtml) {
self.div.innerHTML = self.written;
} else {
self.doc._write(str);
self.doc._write.apply(self.doc, args);
}
self.compareInnerHtml(str);
self.compareInnerHtml.apply(self, args);
};

return self;
Expand Down Expand Up @@ -407,8 +417,8 @@ var execute = function(name, tags, options) {

ifr = IFrame('[ACTUAL]'+name);

ifr.doc.write = function(str) {
ok(false, ifr.doc.currentTag.id + ' - document.write outside: ' + str);
ifr.doc.write = function() {
ok(false, ifr.doc.currentTag.id + ' - document.write outside: ' + [].slice.call(arguments).join(''));
};

for(i = 0; tag = tags[i]; i++) {
Expand Down
83 changes: 83 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,5 +447,88 @@ $(document).ready(function(){
}


module('write with multiple arguments');
setOptions({});

testWrite('wma: split mid-element', function(ctx) {
ctx.write('<i', 'mg alt="foo">');
ctx.eq($('img', ctx.doc).attr('alt'));
});

testWrite('wma: split mid-attribute', function(ctx) {
ctx.write('<img a', 'lt="foo">');
ctx.eq($('img', ctx.doc).attr('alt'));
});

testWrite('wma: split mid-attribute-value', function(ctx) {
ctx.write('<img alt="f', 'oo">');
ctx.eq($('img', ctx.doc).attr('alt'));
});

testWrite('wma: empty strings', function(ctx) {
ctx.write('', '<im', '', 'g ', '', 'al', '', 't="f', '', 'oo">', '');
ctx.eq($('img', ctx.doc).attr('alt'));
});

testWrite('wma: docwrite outside parent of script', function(ctx) {
ctx.write('<div>A<script type="', 'text/javascript">\n',
'doc', 'ument.write("B</div>C");\n</script>D');
});

testWrite('wma: SW9', function(ctx) {
ctx.write('<div><i></i></div>', 'foo', '<div>bar', '<i></i>');
});

testWrite('wma: SW10', function(ctx) {
ctx.write('<div><b><i></i></b></div>', 'foo', '<div>bar<i>', '</i>bla');
});

testWrite("wma: TS2", function(ctx) {
ctx.write('<div><i>', '<div>foo', '<div><i>');
});



module('writeln with multiple arguments');
setOptions({});

testWrite('wlma: split mid-element', function(ctx) {
ctx.writeln('<i', 'mg alt="foo">');
ctx.eq($('img', ctx.doc).attr('alt'));
});

testWrite('wlma: split mid-attribute', function(ctx) {
ctx.writeln('<img a', 'lt="foo">');
ctx.eq($('img', ctx.doc).attr('alt'));
});

testWrite('wlma: split mid-attribute-value', function(ctx) {
ctx.writeln('<img alt="f', 'oo">');
ctx.eq($('img', ctx.doc).attr('alt'));
});

testWrite('wlma: empty strings', function(ctx) {
ctx.writeln('', '<im', '', 'g ', '', 'al', '', 't="f', '', 'oo">', '');
ctx.eq($('img', ctx.doc).attr('alt'));
});

testWrite('wlma: docwrite outside parent of script', function(ctx) {
ctx.writeln('<div>A<script type="', 'text/javascript">\n',
'doc', 'ument.write("B</div>C");\n</script>D');
});

testWrite('wlma: SW9', function(ctx) {
ctx.writeln('<div><i></i></div>', 'foo', '<div>bar', '<i></i>');
});

testWrite('wlma: SW10', function(ctx) {
ctx.writeln('<div><b><i></i></b></div>', 'foo', '<div>bar<i>', '</i>bla');
});

testWrite("wlma: TS2", function(ctx) {
ctx.writeln('<div><i>', '<div>foo', '<div><i>');
});


});

0 comments on commit df940eb

Please sign in to comment.