Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

We've included and extended your changes in jquery.docwrite.js #1

Merged
merged 5 commits into from

2 participants

@andrew-sayers--slando

Hey, thanks for improving jquery.docwrite.js!

We've made several other changes to this plugin since we launched it, so we had to tweak your code a bit to merge it in. We've also added a test for your issue - please let us know if we've missed anything.

We plan to put this up on jquery.com next week.

@m8rge m8rge commented on the diff
tests.html
@@ -0,0 +1,162 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <script src="http://code.jquery.com/jquery-latest.js"></script>
+ <link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css" type="text/css" media="screen" />
+ <script type="text/javascript" src="http://code.jquery.com/qunit/git/qunit.js"></script>
+ <script type="text/javascript" src="jquery.docwrite.1.2.0.js"></script>
@m8rge Owner
m8rge added a note

why you included jquery.docwrite.1.2.0.js, but whole docwrite code locate in jquery.docwrite.js?
BTW, what's need of jquery.docwrite.1.2.0.js file with one line "jquery.docwrite.js"?

@m8rge Owner
m8rge added a note

Oh, I got it. It was symlink. I work in windows, so I havn't got symlinks. Git clone works normally with symlinks, so I think there is no need in files rearrage. I'll pull your changes.

PS jquery tests passed successfully only in firefox. Chrome13, ie9 failed some tests.

@m8rge Owner
m8rge added a note

Good catch - external scripts aren't being loaded. I'll have a dig through
jQuery and get back to you in a few days.

I think, that origin of this problem in browser, not in jquery

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@m8rge m8rge merged commit c16f1a7 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
12 Makefile
@@ -0,0 +1,12 @@
+.PHONY: clean
+VERSION=1.2.0
+
+docwrite-${VERSION}.zip: jquery.docwrite.js
+ -mkdir docwrite
+ cp -a $^ docwrite/jquery.docwrite.${VERSION}.js
+ cp -a *.txt *.html nested_docwrite_test.js docwrite/
+ java -jar ${CLOSURE_DIR}/compiler.jar --js $^ --js_output_file=docwrite/jquery.docwrite.${VERSION}.min.js
+ zip -r $@ docwrite/
+
+clean:
+ rm -rf docwrite-${VERSION}.zip docwrite
View
17 changelog.txt
@@ -0,0 +1,17 @@
+Docwrite
+
+Version 1.2.0
+=============
+* Reformatted m8rge's changes slightly
+* Added unit tests
+* Fixed several parsing issues
+
+Version 1.1.0
+=============
+* Modified by m8rge
+* Added ability to process splitted nesteds scripts
+* See https://github.com/m8rge/docwrite
+
+Version 1.0.0
+=============
+* Initial release from the Slando (http://www.slando.com) Development team.
View
9 example.html
@@ -3,7 +3,7 @@
<title>Docwrite example</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
- <script type="text/javascript" src="jquery.docwrite.1.1.0.js"></script>
+ <script type="text/javascript" src="jquery.docwrite.1.2.0.min.js"></script>
</head>
<body>
@@ -15,8 +15,6 @@
<div id="example4_element"></div>
<script type="text/javascript"><!--
-var element = document.getElementById('example1_element');
-
$(document).bind('beforedocwrite', function(event, data) {
// redirect the write:
data.target = $('#my_element');
@@ -38,15 +36,16 @@
<script><!--
+var element = document.getElementById('example1_element');
$(element).docwrite( function(arg1, arg2) {
- document.write("my <b>html</b> will be appended to $(element1)");
+ document.write("my <b>html</b> will be appended to $(element)");
}, "passed to function as arg1",
"passed to function as arg2"
);
element = document.getElementById('example2_element');
$(element).docwrite(
- "<div>Appended to element2</div>\n" +
+ "<div>Appended to element</div>\n" +
"<script type='text/javascript'>" +
"document.write('nested document.write()s are handled correctly');" +
"</script>"
View
1  jquery.docwrite.1.2.0.js
View
103 jquery.docwrite.1.1.0.js → jquery.docwrite.js
@@ -1,29 +1,11 @@
/*!
- * docwrite jQuery plugin v1.0.0
+ * docwrite jQuery plugin v1.2.0
*
* Copyright 2011, Slando Ltd. <dev@slando.com>
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
- * Date: Thu Feb 24 11:32:37 GMT 2011
- */
-
-/*!
- * docwrite jQuery plugin v1.1.0
- *
- * Modified by m8rge
- * Added ability to process splitted nesteds scripts
- *
- * Examples:
- * $(element).docwrite("<script type='text/javascript' ");
- * $(element).docwrite("src='alert.js'></scr"+"ipt>");
- *
- * $(element).docwrite("<div>Appended to element</div>" + "<script type='text/javascript'>" + "m='<div>nested '; document.write(m+'");
- * $(element).docwrite("document.write()s are handled");
- * $(element).docwrite(" correctly</div>');" + "</scr"+"ipt>");
- * $(element).docwrite('nezhdanchik');
- *
- * Date: May 04 18:00 GMT+5 2011
+ * Date: Mon May 9 11:29:29 BST 2011
*/
/**
@@ -56,12 +38,12 @@
*/
(function($) {
- var match_script = /<script /i,
- match_script_src = /^<script ([^>]* )?src=/i,
- match_script_end = /<\/script>/i,
+ var match_script = /<script[ \t\n]([^>]*)>/i,
+ match_script_src = /^<script[ \t\n]([^>]*[ \t\n])?src[ \t\n]*=[ \t\n]*/i,
+ match_script_end = /<\/script[ \t\n]*>/i,
document_write = document.write,
write_target = undefined,
- script_buffer = [];
+ script_buffer = "";
// document.write() implementation before the ready event has fired:
function write(html) {
@@ -74,9 +56,17 @@
if ( !event.isDefaultPrevented() ) {
if ( null === data.target )
- document_write.call( document, data.html );
+ if ( 'call' in document_write ) { // sane browsers
+ document_write.call( document, data.html );
+ } else { // IE
+ // make sure document.write() will be reset, even if we throw an exception:
+ var timeout = setTimeout(function() { document.write = write; }, 0);
+ document.write = document_write;
+ document.write( data.html );
+ document.write = write;
+ }
else if ( data.target.docwrite )
- data.target .docwrite( data.html );
+ data.target .docwrite( data.html );
else
$(data.target).docwrite( data.html );
@@ -140,58 +130,47 @@
*
*/
document.write = function write_inner(text) {
-// console.log("have text:",text);
+
+// console.log("have text:",text);
var start_pos,
end_pos; // needs to be a local variable because this is called recursively
+ text = script_buffer + text;
+ script_buffer = "";
+
while ( -1 != ( start_pos = text.search(match_script) ) ) {
// <script...>
html.push( text.substr(0, start_pos) );
+ text = text.substr( start_pos );
+
if ( match_script_src.test( text ) ) {
// <script src="...">: remove the "<script" and continue parsing:
- script_buffer.push( text.substr(0, start_pos + 7) );
- text = text.substr( start_pos + 7);
+ html.push( text.substr(0, 7) );
+ text = text.substr(7 );
} else if ( -1 == ( end_pos = text.search(match_script_end) ) ) {
// <script> with no </script>:
- var tmp = text.substr(start_pos);
- start_pos = tmp.indexOf( '>', start_pos ) + 1;
- script_buffer.push(tmp.substr(start_pos));
-// console.log(html, script_buffer);
+ script_buffer = text;
+// console.log(html, script_buffer);
return;
} else {
// <script>...</script>, may do a document.write() of its own:
- start_pos = text.indexOf( '>', start_pos ) + 1;
+ start_pos = text.indexOf( '>' ) + 1;
$.globalEval( text.substr(start_pos, end_pos - start_pos) );
- text = text.substr(end_pos + 9);
- }
- }
- if ( script_buffer.length != 0 &&
- -1 != ( end_pos = text.search(match_script_end) )) {
- // script buffer have full script
-// console.log('have script');
-
- script_buffer.push(text.substr(0, end_pos)); // append last segment
- if (match_script_src.test( script_buffer.join('')+text.substr(end_pos, end_pos+9) )) {
- // we have <script src=, so just insert as text
- script_buffer.push(text.substr(end_pos, end_pos+9)); // append </script> tag
- html = html.concat(script_buffer);
- script_buffer = [];
- } else {
- // we have <script tag, eval it
- var local_script = script_buffer.join('');
- script_buffer = [];
- $.globalEval(local_script);
- }
- text = text.substr(end_pos + 9);
- }
-
- if (script_buffer.length != 0)
- script_buffer.push(text);
- else
- html.push( text );
-// console.log(html, script_buffer);
+ text = text.substr(text.indexOf( ">", end_pos )+1);
+ };
+ };
+
+ end_pos = text.search( /<[^>]*$/ );
+ if ( -1 == end_pos ) {
+ html.push( text );
+ } else {
+ script_buffer = text.substr( end_pos );
+ html.push( text.substr( 0, end_pos ) );
+ };
+// console.log(html, script_buffer);
+
};
document.writeln = function(html) { write_inner(html + "\n"); };
View
1  nested_docwrite_test.js
@@ -0,0 +1 @@
+document.write("(nested document.write() loaded from file)");
View
162 tests.html
@@ -0,0 +1,162 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <script src="http://code.jquery.com/jquery-latest.js"></script>
+ <link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css" type="text/css" media="screen" />
+ <script type="text/javascript" src="http://code.jquery.com/qunit/git/qunit.js"></script>
+ <script type="text/javascript" src="jquery.docwrite.1.2.0.js"></script>
@m8rge Owner
m8rge added a note

why you included jquery.docwrite.1.2.0.js, but whole docwrite code locate in jquery.docwrite.js?
BTW, what's need of jquery.docwrite.1.2.0.js file with one line "jquery.docwrite.js"?

@m8rge Owner
m8rge added a note

Oh, I got it. It was symlink. I work in windows, so I havn't got symlinks. Git clone works normally with symlinks, so I think there is no need in files rearrage. I'll pull your changes.

PS jquery tests passed successfully only in firefox. Chrome13, ie9 failed some tests.

@m8rge Owner
m8rge added a note

Good catch - external scripts aren't being loaded. I'll have a dig through
jQuery and get back to you in a few days.

I think, that origin of this problem in browser, not in jquery

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+ <script>
+ $(document).bind('beforedocwrite', function(event, data) {
+ data.target = $('#event-interface-target');
+ switch ( data.html ) {
+ case 'change target': break;
+ case 'edit text' : data.html = "edited text"; break;
+ case 'cancel' : event.preventDefault(); break;
+ };
+ });
+
+ var last_afterdocwrite_data;
+ $(document).bind('afterdocwrite', function(event, data) {
+ last_afterdocwrite_data = data;
+ });
+
+ </script>
+
+ </head>
+ <body>
+ <h1 id="qunit-header">jQuery.docwrite.js</h1>
+ <h2 id="qunit-banner"></h2>
+ <div id="qunit-testrunner-toolbar"></div>
+ <h2 id="qunit-userAgent"></h2>
+ <ol id="qunit-tests"></ol>
+ <div id="qunit-fixture">
+
+ <div id="event-interface-target"></div>
+ <div id="functional-interface-target"></div>
+
+ </div>
+
+ <script>
+
+ module("Event-based interface");
+
+ test( "change target", function() {
+ var result_text = "change target";
+ document.write(result_text);
+ equals( $('#event-interface-target').text(), result_text, "document.write() target changed" );
+ notStrictEqual( last_afterdocwrite_data, undefined, "afterdocwrite() is called" );
+ equals( last_afterdocwrite_data.html, result_text, "afterdocwrite() gets the right HTML" );
+ last_afterdocwrite_data = undefined;
+ });
+
+ test( "edit text", function() {
+ var result_text = "edited text";
+ document.write( "edit text");
+ equals( $('#event-interface-target').text(), result_text, "document.write() text edited" );
+ notStrictEqual( last_afterdocwrite_data, undefined, "afterdocwrite() is called" );
+ equals( last_afterdocwrite_data.html, result_text, "afterdocwrite() gets the right HTML" );
+ last_afterdocwrite_data = undefined;
+ });
+
+ test( "cancel", function() {
+ var result_text = "";
+ document.write( "cancel");
+ equals( $('#event-interface-target').text(), result_text, "document.write() cancelled" );
+ equals( last_afterdocwrite_data, undefined, "afterdocwrite() was not called" );
+ last_afterdocwrite_data = undefined;
+ });
+
+
+
+ module("Functional interface");
+
+ test( "change target", function() {
+ var result_text = "change target";
+ $('#functional-interface-target').docwrite(result_text);
+ equals( $('#functional-interface-target').text(), result_text, "document.write() target not changed" );
+ strictEqual( last_afterdocwrite_data, undefined, "afterdocwrite() was not called" );
+ last_afterdocwrite_data = undefined;
+ });
+
+ test( "edit text", function() {
+ var result_text = "edit text";
+ $('#functional-interface-target').docwrite("edit text");
+ equals( $('#functional-interface-target').text(), result_text, "document.write() text not edited" );
+ strictEqual( last_afterdocwrite_data, undefined, "afterdocwrite() was not called" );
+ last_afterdocwrite_data = undefined;
+ });
+
+ test( "cancel", function() {
+ var result_text = "cancel";
+ $('#functional-interface-target').docwrite("cancel");
+ equals( $('#functional-interface-target').text(), result_text, "document.write() not cancelled" );
+ strictEqual( last_afterdocwrite_data, undefined, "afterdocwrite() was not called" );
+ last_afterdocwrite_data = undefined;
+ });
+
+ test( "pass arguments", function() {
+ $('#functional-interface-target').docwrite(function(arg) { document.write(arg); }, "argument passed" );
+ equals( $('#functional-interface-target').text(), "argument passed", "argument passed to function" );
+ strictEqual( last_afterdocwrite_data, undefined, "afterdocwrite() was not called" );
+ last_afterdocwrite_data = undefined;
+ });
+
+
+
+ module("Complex document.write() examples");
+
+ test( "nested document.write", function() {
+ $('#functional-interface-target').docwrite(function(arg) {
+ document.write("this is a ");
+ document.write('<script type="text/javascript">document.write("nested ");</scr'+'ipt>');
+ document.write("document.write()");
+ });
+ equals( $('#functional-interface-target').text(), "this is a nested document.write()", "correct output" );
+ strictEqual( last_afterdocwrite_data, undefined, "afterdocwrite() was not called" );
+ last_afterdocwrite_data = undefined;
+ });
+
+ test( "silly formatting issues", function() {
+ $('#functional-interface-target').docwrite(function(arg) {
+ document.write("this is a ");
+ document.write('<script\t\n type\t\n =\t\n "text/javascript"\t\n >document.write("nested");</scr'+'ipt\t\n >');
+ document.write(" document.write()");
+ });
+ equals( $('#functional-interface-target').text(), "this is a nested document.write()", "correct output" );
+ strictEqual( last_afterdocwrite_data, undefined, "afterdocwrite() was not called" );
+ last_afterdocwrite_data = undefined;
+ });
+
+ test( "document.write() loading a file", function() {
+ $('#functional-interface-target').docwrite(function(arg) {
+ document.write('<script type="text/javascript" src="nested_docwrite_test.js"></scr'+'ipt>');
+ });
+ equals( $('#functional-interface-target').text(), "", "correct output" );
+ notStrictEqual( last_afterdocwrite_data, undefined, "afterdocwrite() is called" );
+ equals( last_afterdocwrite_data.html, "(nested document.write() loaded from file)", "afterdocwrite() gets the right HTML" );
+ last_afterdocwrite_data = undefined;
+ });
+
+ test( "split document.write", function() {
+ $('#functional-interface-target').docwrite(function(arg) {
+
+ document.write( "<script type='text/javascript' " );
+ document.write( "src='nested_docwrite_test.js'></scr"+"ipt>");
+ document.write( "(inline text) " +
+ "<script type='text/javascript'>" + "m='(nested document'; document.write(m+'");
+ document.write( ".write()");
+ document.write( ") ');" + "</scr"+"ipt>(");
+ document.write('more inline text)');
+
+ });
+ equals( $('#functional-interface-target').text(), "(inline text) (nested document.write()) (more inline text)", "correct output" );
+ notStrictEqual( last_afterdocwrite_data, undefined, "afterdocwrite() is called" );
+ equals( last_afterdocwrite_data.html, "(nested document.write() loaded from file)", "afterdocwrite() gets the right HTML" );
+ last_afterdocwrite_data = undefined;
+ });
+
+ </script>
+
+ </body>
+</html>
Something went wrong with that request. Please try again.