-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.docwrite.js
194 lines (161 loc) · 6.54 KB
/
jquery.docwrite.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*!
* docwrite jQuery plugin v1.2.0
*
* Copyright 2011, Slando Ltd. <[email protected]>
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
* Date: Mon May 9 11:29:29 BST 2011
*/
/**
* @fileOverview
* Make document.write() a little bit safer
*
* // Redirect document.write() to an element:
* $(element).docwrite( function(arg1, arg2) {
* document.write("appended to element");
* document.write("<script>document.write('also appended to element')</script>");
* }, "passed to function as arg1", "passed to function as arg2" );
*
* $(document).load(function() {
* document.write("will be appended to an invisible <DIV>");
* });
*
* $(document).bind( 'beforedocwrite', function(event, data) {
* data.html.replace("whatever was going to be written",
* "what you would rather write");
* data.target = $(destination);
* data.unused = "pass data to the afterdocwrite event";
* event.preventDefault(); // cancel writing
* });
*
* $(document).bind( 'afterdocwrite', function(event, data) {
* if ( data.unused ) {
* # ...
* };
* });
*/
(function($) {
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 = "";
// document.write() implementation before the ready event has fired:
function write(html) {
var data = { target: null, html: html },
event = $.Event("beforedocwrite");
$(document).trigger( event, data );
if ( !event.isDefaultPrevented() ) {
if ( null === data.target )
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 );
else
$(data.target).docwrite( data.html );
$(document).trigger( 'afterdocwrite', data );
}
};
function writeln(html) { write( html + "\n" ); };
// document.write() implementation after the ready event has fired:
function write_late(html) {
if ( write_target === undefined ) {
write_target = document.createElement('DIV');
write_target.style.display = "none";
document.body.appendChild(write_target);
write_target = $(write_target);
};
var data = { target: write_target, html: html },
event = $.Event("beforedocwrite");
$(document).trigger( event, data );
if ( !event.isDefaultPrevented() ) {
data.target.docwrite( data.html );
$(document).trigger( 'afterdocwrite', data );
};
};
function writeln_late(html) { write_late( html + "\n" ); };
document.write = write;
document.writeln = writeln;
$(document).ready(function() {
document.write = write_late;
document.writeln = writeln_late;
});
$.fn.docwrite = function(arg) {
var html = [],
// we're about to override the system document.write (again)
// but first we make sure it will be reset, even if we throw an exception:
document_write = document.write,
document_writeln = document.writeln,
timeout = setTimeout(function() {
document.write = document_write;
document.writeln = document_writeln;
}, 0),
i=0, args=[];
/*
* Protect against nested document.write's:
*
* document.write('<script type="text/javascript">document.write("...")</script>');
*
* Browsers ignore <script>s inserted with innerHTML,
* so we have to parse out and eval() the nested <script>s.
*
*/
document.write = function write_inner(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:
html.push( text.substr(0, 7) );
text = text.substr(7 );
} else if ( -1 == ( end_pos = text.search(match_script_end) ) ) {
// <script> with no </script>:
script_buffer = text;
// console.log(html, script_buffer);
return;
} else {
// <script>...</script>, may do a document.write() of its own:
start_pos = text.indexOf( '>' ) + 1;
$.globalEval( text.substr(start_pos, end_pos - start_pos) );
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"); };
if ( arg.apply ) {
while ( ++i < arguments.length ) args[i-1] = arguments[i];
arg.apply( this, args );
} else {
document.write( arg );
};
document.write = document_write;
clearTimeout( timeout );
if ( '' != ( html = html.join('') ) ) {
this.append( html );
};
return this;
};
})(jQuery);