├── .gitattributes ├── .gitignore ├── GPL-LICENSE.txt ├── LICENSE.txt ├── MIT-LICENSE.txt ├── README.md ├── build.gradle ├── build └── yuicompressor-2.4.2.jar ├── cjs.writeCapture.js ├── lib └── jquery-1.3.2.js ├── plugin ├── README.md └── jquery.writeCapture.js ├── support ├── debug-support.js └── nolib-support.js ├── test ├── autoCapture-noLib.html ├── autoCapture.html ├── autoCapture.js ├── bar.js ├── baz.js ├── bs-serving.js ├── cjs.html ├── extsrc-tests.js ├── extsrc.html ├── foo.js ├── getById.js ├── getOpts.js ├── getParent.js ├── index.html ├── noLib.html ├── options.js ├── plugin.html ├── pluginTests.js ├── qunit │ ├── qunit.css │ └── qunit.js ├── testLoad.html ├── testLoadFilter.html ├── testLoadInline.html └── tests.js └── writeCapture.js /.gitattributes: -------------------------------------------------------------------------------- 1 | README.md merge=keepmine 2 | plugin/README.md merge=keepmine 3 | build.gradle merge=keepmine 4 | .gitignore merge=keepmine 5 | package.json merge=keepmine 6 | build merge=keepmine -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .gradle 3 | writeCapture-*-min.js 4 | jquery.writeCapture-*-min.js 5 | -------------------------------------------------------------------------------- /GPL-LICENSE.txt: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | You may use this code under the terms of the GPLv2 or the MIT license, whichever you prefer. 2 | The text of both licenses is included in this repository. 3 | 4 | Both licenses apply retroactively to all versions of this software available from 5 | http://github.com/iamnoah/writeCapture 6 | -------------------------------------------------------------------------------- /MIT-LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010 Noah Sloan, http://github.com/iamnoah/writeCapture 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | NEWER! Checkout [PostScribe](https://github.com/krux/postscribe/)! It's [awesome](https://groups.google.com/d/topic/writecapturejs-users/XV_09Fpfc94/discussion). 2 | 3 | NEW! Checkout [this branch](https://github.com/iamnoah/writeCapture/tree/writeCapture2) for a completely rewritten writeCapture that should be much more compatible with a greater variety of scripts. 4 | 5 | # Usage Summary # 6 | 7 | $(something).writeCapture().html('some html with document.write scripts'); 8 | 9 | or if you just want to force your ads to load async: 10 | 11 | $.writeCapture.autoAsync(); 12 | 13 | writeCapture can be used [with][plugin] or [without][nolib] jQuery and can be loaded asynchronously. For async loading, [[ControlJS]] is a good option, but any async loading library or technique should work. See the [wiki](/iamnoah/writeCapture/wiki/Usage) for more detailed usage. 14 | 15 | ***Need help? See "Help!" below.*** 16 | 17 | # Dependencies # 18 | 19 | writeCapture.js was developed using jQuery 1.3.2, but should work with most 20 | versions. We recommend you use jQuery, because it's pretty great, however, 21 | if that's not possible, there is support/nolib-support.js, which provides 22 | a bare bones implementation of the support functions jQuery would otherwise 23 | provide and only adds about 800 bytes when Gzipped. 24 | 25 | To use nolib-support, just grab the latest writeCapture-x.x.x.-nolib.min.js 26 | from the downloads section. It already includes writeCapture.js, so it's the 27 | only file you need. If you'd like the unminified source for debugging, simply 28 | include nolib-support.js before writeCapture.js. 29 | 30 | Note that nolib does not implement `onLoad`, which is required for `autoAsync`. 31 | 32 | If you already have another Ajax library you are using like Prototype or dojo, 33 | you can [implement write capture support yourself](/iamnoah/writeCapture/wiki/WriteCaptureSupport). 34 | 35 | # Why should I care about writeCapture.js? # 36 | 37 | Sometimes we are forced to use a third party script or markup that we simply 38 | cannot change (usually an Ad server). While our websites are sleek and snappy 39 | with a nearly pristine codebase and progressive enhancement and Ajax 40 | everywhere, many 3rd party libraries are still using tables for layout and 41 | the dreaded, *evil* `document.write` method. 42 | 43 | What makes `document.write` so evil is that it is only "useful" for scripts inside 44 | the body tag that are processed while the page is loading. If document.write is 45 | called anytime after page load, it wipes out all the existing page content. 46 | That makes it very difficult to dynamically refresh content containing 47 | `document.write` calls. 48 | 49 | Fortunately for you, difficult is not impossible, and writeCapture.js has 50 | already written (and extensively tested) the difficult part for you. All 51 | you have to do is give it the offending evil HTML and it will return nice 52 | clean HTML, safe for injection into the document. 53 | 54 | # Satisfied Customers 55 | 56 | * [TheDailyBeast.com](http://TheDailyBeast.com) [uses][jesus] writeCapture.js. 57 | 58 | * [8tracks.com](http://8tracks.com) "We were having horrible latency with our ad providers and your plugin saved the day! Very impressive work!" 59 | 60 | If you use writeCapture and want some free advertising, let us know! 61 | 62 | # Help! 63 | 64 | First, please read the wiki, particularly the [Options](https://github.com/iamnoah/writeCapture/wiki/Options) page. There 65 | are a lot of workarounds to try before giving up. 66 | 67 | Next, ask your question on the [mailing list](http://groups.google.com/group/writecapturejs-users). Please 68 | ask your question [the smart way](http://catb.org/~esr/faqs/smart-questions.html#before). That includes creating 69 | a stripped down and *working* example at http://jsfiddle.net/ so that everyone can play with it. A private page on your site 70 | demonstrating the problem is rarely helpful because we can't go and edit the source. Please take the time to 71 | reproduce it on JS fiddle so we can help you. 72 | 73 | Please **do not** email the author privately or open an issue in the issue tracker. The author does not actively 74 | use write capture, so he probably does not have any idea what is causing your problem. 75 | 76 | You may think that it is quicker to go straight to the mailing list or the author without taking the time to 77 | isolate the problem and fully read and comprehend the documentation and go over the source code, but anyone 78 | who can help you has already done all of those things. Please respect our time, and you will get enthusiastic help. 79 | 80 | # Caveats/Limitations # 81 | 82 | * AdSense - AdSense code using `document.write` will not work. However AdSense provides 83 | [asynchronous tracking support][adsense], so you don't even need writeCapture to load 84 | AdSense asynchronously. 85 | 86 | * If any of the included scripts are on another domain, they will have to be 87 | loaded asynchronously via script tag injection. Subsequent scripts in the 88 | HTML will be blocked, so order will be preserved, but this means that not 89 | all scripts may have yet run after the HTML is injected. This means that if 90 | your script depends on all the scripts in the HTML having run, it should 91 | utilize the done() callback. 92 | 93 | * Scripts that assume that they are the last element in the document will 94 | probably not function properly. This is rare, but if a script is uncouth 95 | enough to use document.write, it's a possibility. 96 | 97 | # Control JS Support # 98 | 99 | The built in `document.write` support in [Control JS](http://stevesouders.com/controljs/) 100 | is not great. It adds extra `span` tags and is not nearly as robust as a full library like writeCapture. That's why writeCapture provides a [plugin](/iamnoah/writeCapture/blob/master/cjs.writeCapture.js) to replace Control JS's `document.write` support with writeCapture.js. 101 | 102 | For usage, see the [wiki](/iamnoah/writeCapture/wiki/ControlJS). 103 | 104 | # extsrc Support # 105 | 106 | [extsrc](http://code.google.com/p/extsrcjs/) support: 107 | 108 | // call from $.ready() or your document ready of choice 109 | writeCapture.extsrc(doneCallback); 110 | 111 | You can use the same markup (`extsrc="..."`,`asyncsrc="..."`) but with a few 112 | advantages: 113 | 114 | * All the power of writeCapture.js, so more `document.write` edge cases will work. 115 | * No extra span tags! 116 | * All scripts with `extsrc` will be run in order, so you don't have to worry about dependencies. 117 | 118 | Note that you *do not* need extsrc.js, we are simply supporting the same syntax in writeCapture. 119 | 120 | 121 | # Version History # 122 | 123 | ## 1.0.5 ## 124 | 125 | * Hacks: 126 | 127 | * Updated `proxyGetElementById` will attempt to copy attributes from the proxy 128 | when jQuery is present. Should improve compatibility in some cases. 129 | 130 | * `writeOnGetElementById` for when proxying isn't enough. 131 | 132 | * New Experimental Feature: `autoAsync`. Inspired by 133 | [eligrey](http://github.com/eligrey)'s 134 | [async-document-write](http://github.com/eligrey/async-document-write). 135 | 136 | * All options and hacks can now be set globally and/or per call. 137 | 138 | * No more eval! Fixes a problem in IE where `var` variables were not visible 139 | to other scripts. 140 | 141 | ## 1.0 ## 142 | 143 | * If it's good enough for [newsweek.com](http://newsweek.com) , it's good 144 | enough to be version 1.0. 145 | 146 | * Fixed bug in pause/resume logic when using asyncAll that was preventing 147 | execution from resuming. 148 | 149 | * Fixed error in IE when using `proxyGetElementById`. 150 | 151 | ## 0.9.5 ## 152 | 153 | * Big overhaul of the internal queuing system. It should be more robust and 154 | 100% deterministic from this point forward. Known to help with deeply nested 155 | cross domain scripts. 156 | 157 | * Fixed issue where scripts that include newlines in their src attribute were 158 | being truncated. 159 | 160 | * Added debugging support. See support/debug-support.js. 161 | 162 | ## 0.9.0 ## 163 | 164 | * writeCapture.js has matured enough to be close to a 1.0 release, after a 165 | little seasoning. If you have a problem with your Ad server or other code, 166 | submit a bug report! 167 | 168 | * Added hacks to support scripts that combine `document.write` with DOM 169 | manipulation and iframes. Thanks to [jcugno](http://github.com/jcugno) 170 | for the bug reports and example code. Enabling `proxyGetElementById` 171 | should help with some Ad servers. 172 | 173 | ## 0.3.3 ## 174 | 175 | * Fixed a number of issues with 'marginal' scripts (but if all scripts were in 176 | good shape, you wouldn't need writeCapture). Should help with some ad servers. 177 | Hat tip to [neskire](http://github.com/neskire) for raising the issues. 178 | 179 | ## 0.3.2 ## 180 | 181 | * Bugfix - jQuery 1.4.1 has a bug in replaceWith that mishandles strings. We 182 | work around it by not using replaceWith. 183 | 184 | ## 0.3.1 ## 185 | 186 | * Added fixUrls hack to deal with encoded URLs. 187 | 188 | * Added MIT and GPL licenses, applied retroactively to all versions. 189 | 190 | * Optimized script loading (start loading in parallel immediately) if asyncAll is enabled. 191 | 192 | ## 0.3.0 ## 193 | 194 | * Replaced `sanitizeAll` with `sanitizeSerial`. 195 | 196 | * Created jQuery plugin. 197 | 198 | * Run all sanitized scripts through a global queue to prevent problems when 199 | multiple sanitizes with async loads are run in sequence. 200 | 201 | ## 0.2.1 ## 202 | 203 | * Added `html` and `replaceWith` convenience methods. 204 | 205 | * Added `load` convenience method. 206 | 207 | ## 0.2.0 ## 208 | 209 | * Only tasks that were running async (read: async script 210 | loading with pause/resume) will result in a call to defer. 211 | 212 | * Allowed replacement of support functions by defining writeCaptureSupport 213 | so writeCapture can be used without jQuery. 214 | 215 | * Added nolib-support.js for using writeCapture without jQuery. 216 | 217 | ## 0.1 ## 218 | 219 | * first release. Any content but a single script tag would result in 220 | partially async execution thanks to an overuse of defer. 221 | 222 | [adsense]: https://code.google.com/apis/analytics/docs/tracking/asyncUsageGuide.html 223 | 224 | [jesus]: http://iamnoah.blogspot.com/2009/12/github-taming-documentwrite-and-nodejs.html?showComment=1275069903219#c7207630670857732321 "It will save your site from hacks." 225 | 226 | [nolib]: https://github.com/iamnoah/writeCapture/blob/master/support/nolib-support.js 227 | [plugin]: https://github.com/iamnoah/writeCapture/tree/master/plugin 228 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | defaultTasks 'minify' 2 | 3 | def VERSION_LINE = / \* writeCapture\.js v((\d+)\.(\d+)\.(\d+)(-SNAPSHOT)?)/ 4 | 5 | task upVersion << { 6 | def script = new File('writeCapture.js') 7 | def text = script.text.replaceAll(VERSION_LINE) { line, v, major, minor, fix, snapshot -> 8 | if(!snapshot) { 9 | // if snapshot wasn't in there, increment the version number 10 | fix = fix.toInteger() + 1 11 | } 12 | " * writeCapture.js v${major}.${minor}.${fix}" 13 | } 14 | script.write(text) 15 | } 16 | 17 | def getVersion = { 18 | (new File('writeCapture.js').filterLine { 19 | it =~ VERSION_LINE 20 | } =~ VERSION_LINE)[0][1] 21 | } 22 | 23 | def doMinify = { 24 | ant.java(jar:'build/yuicompressor-2.4.2.jar',fork:true) { 25 | arg(value:'-o') 26 | arg(value:"writeCapture-${getVersion()}-min.js") 27 | arg(value:'writeCapture.js') 28 | } 29 | } 30 | 31 | def doConcat = { 32 | ant.concat(destfile:'build-concat.tmp.js') { 33 | fileset(dir:'support',includes:'nolib-support.js') 34 | fileset(dir:'.',includes:'writeCapture.js') 35 | } 36 | } 37 | 38 | def doConcatPlugin = { 39 | ant.concat(destfile:'plugin-concat.tmp.js') { 40 | fileset(dir:'.',includes:'writeCapture.js') 41 | fileset(dir:'plugin',includes:'jquery.writeCapture.js') 42 | } 43 | } 44 | 45 | def doConcatCjs = { 46 | ant.concat(destfile:'cjs-concat.tmp.js') { 47 | fileset(dir:'support',includes:'nolib-support.js') 48 | fileset(dir:'.',includes:'writeCapture.js') 49 | fileset(dir:'.',includes:'cjs.writeCapture.js') 50 | } 51 | } 52 | 53 | task concat << doConcat 54 | task concatPlugin << doConcatPlugin 55 | task concatCjs << doConcatCjs 56 | 57 | def doMinifyNolib = { 58 | ant.java(jar:'build/yuicompressor-2.4.2.jar',fork:true) { 59 | arg(value:'-o') 60 | arg(value:"writeCapture-${getVersion()}-nolib-min.js") 61 | arg(value:'build-concat.tmp.js') 62 | } 63 | ant.delete(file:'build-concat.tmp.js') 64 | } 65 | 66 | def doMinifyCjs = { 67 | ant.java(jar:'build/yuicompressor-2.4.2.jar',fork:true) { 68 | arg(value:'-o') 69 | arg(value:"writeCapture-${getVersion()}-forCjs-min.js") 70 | arg(value:'cjs-concat.tmp.js') 71 | } 72 | ant.delete(file:'cjs-concat.tmp.js') 73 | } 74 | 75 | def doMinifyPlugin = { 76 | ant.java(jar:'build/yuicompressor-2.4.2.jar',fork:true) { 77 | arg(value:'-o') 78 | arg(value:"jquery.writeCapture-${getVersion()}-min.js") 79 | arg(value:'plugin-concat.tmp.js') 80 | } 81 | ant.delete(file:'plugin-concat.tmp.js') 82 | } 83 | 84 | task minifyNolib(dependsOn:'concat') << doMinifyNolib 85 | 86 | task minifyPlugin(dependsOn:'concatPlugin') << doMinifyPlugin 87 | 88 | task minifyCjs(dependsOn:'concatCjs') << doMinifyCjs 89 | 90 | task minify << doMinify 91 | 92 | task release(dependsOn:['upVersion']) << { 93 | // putting minify in dependsOn causes it to run before upVersion... WTF? 94 | doMinify() 95 | doConcat() 96 | doMinifyNolib() 97 | doConcatPlugin() 98 | doMinifyPlugin() 99 | doConcatCjs() 100 | doMinifyCjs() 101 | def version = getVersion() 102 | println "Releasing v${version}" 103 | println "You should:" 104 | println " git commit -am 'releasing version ${version}'" 105 | println " git tag -a v${version} -m 'tagging release v${version}'" 106 | } 107 | -------------------------------------------------------------------------------- /build/yuicompressor-2.4.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iamnoah/writeCapture/1de879b8892ef574bc4d75d4513e0132eefd4090/build/yuicompressor-2.4.2.jar -------------------------------------------------------------------------------- /cjs.writeCapture.js: -------------------------------------------------------------------------------- 1 | (function(global,wc) { 2 | function wcEvil(code) { 3 | wc.replaceWith(global.CJS.curScript,''); 4 | } 5 | 6 | function wcExt(src,cb) { 7 | // copied from control.js. be sure we support callbacks properly 8 | var fonload = ( function(ponload) { 9 | switch ( typeof(ponload) ) { 10 | case "string": 11 | ponload = new Function(ponload); 12 | break; 13 | case "function": 14 | // ponload is already a function 15 | break; 16 | default: 17 | ponload = new Function(); 18 | } 19 | return ponload; 20 | })(cb); 21 | wc.replaceWith(global.CJS.curScript,'',fonload); 22 | } 23 | 24 | global.__writeCapture_cjsSupport_setup = function() { 25 | // 2. replace CJS's eval and exec with our eval and exec 26 | global.CJS.eval = wcEvil; 27 | global.CJS.execScript = wcExt; 28 | }; 29 | 30 | // 1. add an inline CJS script to the top that calls setup 31 | // this should be the first script CJS evals 32 | var dummy = document.createElement('script'); 33 | var sib = document.getElementsByTagName('script')[0]; 34 | sib.parentNode.insertBefore(dummy, sib); 35 | wc.replaceWith(dummy,''); 36 | 37 | if(global.writeCapture_cjsSrc) { 38 | var se = document.createElement('script'); 39 | se.src = global.writeCapture_cjsSrc; 40 | sib.parentNode.insertBefore(se, sib); 41 | } 42 | })(this,this.writeCapture); -------------------------------------------------------------------------------- /plugin/README.md: -------------------------------------------------------------------------------- 1 | # jquery.writeCapture.js # 2 | 3 | The writeCapture.js jQuery plugin provides an easy way to utilize 4 | [writeCapture.js](http://github.com/iamnoah/writeCapture) the jQuery way. e.g., 5 | 6 | $('#foo').writeCapture().load('someUrl.php',function() { 7 | alert("loaded safely!"); 8 | }).html('Safe! - Loading').endCapture().html('Unsafe!'); 9 | 10 | All HTML manipulations are proxied by the plugin and filtered through 11 | writeCapture before injection, so all calls chained off of the call to 12 | `writeCapture()` will be "safe". i.e., scripts containing `document.write` 13 | calls will be captured and the content injected in the appropriate place. 14 | 15 | The method `endCapture()` only needs to be called if you intend to chain 16 | further HTML manipulation methods and do not want the HTML to be sanitized 17 | by writeCapture. 18 | 19 | `load` is not the only function that has a callback. Due to the fact that 20 | scripts on another domain must be loaded asynchronously, all HTML 21 | manipulation method calls chained off of writeCapture() take a callback 22 | parameter: 23 | 24 | $('#foo').writeCapture().html(someHtmlWithXDomainScripts,function() { 25 | this.addClass('loaded'); // this is jQuery i.e., $('#foo') 26 | }).doSomethingElse(); 27 | 28 | Note that if any script in the HTML (or resulting HTML) is on another 29 | domain, `doSomethingElse()` will execute before the callback, otherwise the 30 | callback will execute immediately, followed by the remainder of the chain. 31 | If you are not sure whether or not the HTML contains cross domain scripts, you 32 | should assume that it does and chain dependent actions in the callback. 33 | 34 | If you only need to call a single method, you can pass the method name and 35 | arguments to `writeCapture()` and not bother with chaining: 36 | 37 | $('#foo').writeCapture('html,theHtml,callback).doSomethingNormal(); 38 | 39 | Here `doSomethingNormal()` is meant to indicate that any chained methods will 40 | not be proxied and/or sanitized as they would when chaining off of a 41 | no-argument call to `writeCapture()`. 42 | 43 | ## Hacks ## 44 | 45 | See [Hacks](http://github.com/iamnoah/writeCapture) in the core documentation. 46 | 47 | ## Low Level Support ## 48 | 49 | When using the plugin, the global `writeCapture` object will not be present. If 50 | you need to invoke `sanitize`, tweak an option, or or something else, you can 51 | access the core off the jQuery object. e.g., 52 | 53 | var safeHtml = $.writeCapture.sanitize(unsafeHtml); 54 | 55 | If you come across a use case where the plugin is failing, this may help you 56 | out, but also please consider filling an issue. 57 | -------------------------------------------------------------------------------- /plugin/jquery.writeCapture.js: -------------------------------------------------------------------------------- 1 | /** 2 | * jquery.writeCapture.js 3 | * 4 | * Note that this file only provides the jQuery plugin functionality, you still 5 | * need writeCapture.js. The compressed version will contain both as as single 6 | * file. 7 | * 8 | * @author noah 9 | * 10 | */ 11 | (function($,wc,noop) { 12 | // methods that take HTML content (according to API) 13 | var methods = { 14 | html: html 15 | }; 16 | // TODO wrap domManip instead? 17 | $.each(['append', 'prepend', 'after', 'before', 'wrap', 'wrapAll', 'replaceWith', 18 | 'wrapInner'],function() { methods[this] = makeMethod(this); }); 19 | 20 | function isString(s) { 21 | return Object.prototype.toString.call(s) == "[object String]"; 22 | } 23 | 24 | function executeMethod(method,content,options,cb) { 25 | if(arguments.length == 0) return proxyMethods.call(this); 26 | 27 | var m = methods[method]; 28 | if(method == 'load') { 29 | return load.call(this,content,options,cb); 30 | } 31 | if(!m) error(method); 32 | return doEach.call(this,content,options,m); 33 | } 34 | 35 | $.fn.writeCapture = executeMethod; 36 | 37 | var PROXIED = '__writeCaptureJsProxied-fghebd__'; 38 | // inherit from the jQuery instance, proxying the HTML injection methods 39 | // so that the HTML is sanitized 40 | function proxyMethods() { 41 | if(this[PROXIED]) return this; 42 | 43 | var jq = this; 44 | function F() { 45 | var _this = this, sanitizing = false; 46 | this[PROXIED] = true; 47 | $.each(methods,function(method) { 48 | var _super = jq[method]; 49 | if(!_super) return; 50 | _this[method] = function(content,options,cb) { 51 | // if it's unsanitized HTML, proxy it 52 | if(!sanitizing && isString(content)) { 53 | try { 54 | sanitizing = true; 55 | return executeMethod.call(_this,method,content, 56 | options,cb); 57 | } finally { 58 | sanitizing = false; 59 | } 60 | } 61 | return _super.apply(_this,arguments); // else delegate 62 | }; 63 | }); 64 | // wrap pushStack so that the new jQuery instance is also wrapped 65 | this.pushStack = function() { 66 | return proxyMethods.call(jq.pushStack.apply(_this,arguments)); 67 | }; 68 | this.endCapture = function() { return jq; }; 69 | } 70 | F.prototype = jq; 71 | return new F(); 72 | } 73 | 74 | function doEach(content,options,action) { 75 | var done, self = this; 76 | if(options && options.done) { 77 | done = options.done; 78 | delete options.done; 79 | } else if($.isFunction(options)) { 80 | done = options; 81 | options = null; 82 | } 83 | wc.sanitizeSerial($.map(this,function(el) { 84 | return { 85 | html: content, 86 | options: options, 87 | action: function(text) { 88 | action.call(el,text); 89 | } 90 | }; 91 | }),done && function() { done.call(self); } || done); 92 | return this; 93 | } 94 | 95 | 96 | function html(safe) { 97 | $(this).html(safe); 98 | } 99 | 100 | function makeMethod(method) { 101 | return function(safe) { 102 | $(this)[method](safe); 103 | }; 104 | } 105 | 106 | function load(url,options,callback) { 107 | var self = this, selector, off = url.indexOf(' '); 108 | if ( off >= 0 ) { 109 | selector = url.slice(off, url.length); 110 | url = url.slice(0, off); 111 | } 112 | if($.isFunction(callback)) { 113 | options = options || {}; 114 | options.done = callback; 115 | } 116 | return $.ajax({ 117 | url: url, 118 | type: options && options.type || "GET", 119 | dataType: "html", 120 | data: options && options.params, 121 | complete: loadCallback(self,options,selector) 122 | }); 123 | } 124 | 125 | function loadCallback(self,options,selector) { 126 | return function(res,status) { 127 | if ( status == "success" || status == "notmodified" ) { 128 | var text = getText(res.responseText,selector); 129 | doEach.call(self,text,options,html); 130 | } 131 | }; 132 | } 133 | 134 | var PLACEHOLDER = /jquery-writeCapture-script-placeholder-(\d+)-wc/g; 135 | function getText(text,selector) { 136 | if(!selector || !text) return text; 137 | 138 | var id = 0, scripts = {}; 139 | return $('
').append( 140 | text.replace(//g, function(s) { 141 | scripts[id] = s; 142 | return "jquery-writeCapture-script-placeholder-"+(id++)+'-wc'; 143 | }) 144 | ).find(selector).html().replace(PLACEHOLDER,function(all,id) { 145 | return scripts[id]; 146 | }); 147 | } 148 | 149 | function error(method) { 150 | throw "invalid method parameter "+method; 151 | } 152 | 153 | // expose core 154 | $.writeCapture = wc; 155 | })(jQuery,writeCapture.noConflict()); -------------------------------------------------------------------------------- /support/debug-support.js: -------------------------------------------------------------------------------- 1 | (function($) { 2 | function renderDebug() { 3 | var debug = (window.writeCapture || $.writeCapture).debug; 4 | var root = $('
    '), cur = root, srcs = {}; 5 | var extra = $('
    '); 6 | $.each(debug,function() { 7 | if(this.length && (this.join || this.callee)) { 8 | extra.append('

    '+Array.prototype.join.call(this,' ')+'

    '); 9 | return; 10 | } 11 | var it = srcs[this.src] || (srcs[this.src] = {src:this.src, el: $('

    '+this.src+'

      '), parent: cur}); 12 | switch(this.type) { 13 | case 'replace': 14 | $('
    1. ').appendTo(cur).append(it.el); 15 | break; 16 | case 'pause': 17 | cur = $(it.el).find('ol'); 18 | break; 19 | case 'resume': 20 | cur = it.parent; 21 | break; 22 | case 'out': 23 | it.el.find('> h3').after('

      out

      '+this.data.replace(/'); 24 | break; 25 | } 26 | 27 | var found = false; 28 | it.el.parentsUntil(root).siblings().each(function() { 29 | found = found || this.innerHTML.indexOf(it.src) !== -1; 30 | return !found; 31 | }); 32 | if(found) { 33 | it.el.addClass('outOfOrder'); 34 | } 35 | }); 36 | 37 | $(document.body).append(root).append(extra); 38 | } 39 | 40 | this.debugWriteCapture = true; 41 | $(function() { 42 | $('').click(renderDebug).appendTo(document.body); 43 | }); 44 | })(jQuery); -------------------------------------------------------------------------------- /support/nolib-support.js: -------------------------------------------------------------------------------- 1 | (function(global) { 2 | var sup = global.writeCaptureSupport = global.writeCaptureSupport || {}; 3 | // scriptEval & globalEval copied almost verbatim from jQuery 1.3.2 4 | var scriptEval = (function() { 5 | var script = document.createElement("script"); 6 | var id = "script" + (new Date).getTime(); 7 | var root = document.documentElement; 8 | 9 | script.type = "text/javascript"; 10 | try { 11 | script.appendChild( document.createTextNode( "window." + id + "=1;" ) ); 12 | } catch(e){} 13 | 14 | root.insertBefore( script, root.firstChild ); 15 | 16 | // Make sure that the execution of code works by injecting a script 17 | // tag with appendChild/createTextNode 18 | // (IE doesn't support this, fails, and uses .text instead) 19 | if ( window[ id ] ) { 20 | delete window[ id ]; 21 | return true; 22 | } 23 | return false; 24 | })(); 25 | 26 | function attrPattern(name) { 27 | return new RegExp(name+'=(?:(["\'])([\\s\\S]*?)\\1|([^\\s>]+))','i'); 28 | } 29 | 30 | function matchAttr(name) { 31 | var regex = attrPattern(name); 32 | return function(tag) { 33 | var match = regex.exec(tag) || []; 34 | return match[2] || match[3]; 35 | }; 36 | } 37 | 38 | var TYPE_ATTR = matchAttr('type'), 39 | LANG_ATTR = matchAttr('language'); 40 | 41 | function isJs(scriptTag) { 42 | var type = TYPE_ATTR(scriptTag) || '', 43 | lang = LANG_ATTR(scriptTag) || ''; 44 | return (!type && !lang) || // no type or lang assumes JS 45 | type.toLowerCase().indexOf('javascript') !== -1 || 46 | lang.toLowerCase().indexOf('javascript') !== -1 47 | } 48 | 49 | function globalEval(data) { 50 | if ( data && /\S/.test(data) ) { 51 | // Inspired by code by Andrea Giammarchi 52 | // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html 53 | var head = document.getElementsByTagName("head")[0] || document.documentElement, 54 | script = document.createElement("script"); 55 | 56 | script.type = "text/javascript"; 57 | if ( scriptEval ) 58 | script.appendChild( document.createTextNode( data ) ); 59 | else 60 | script.text = data; 61 | 62 | // Use insertBefore instead of appendChild to circumvent an IE6 bug. 63 | // This arises when a base node is used (#2709). 64 | head.insertBefore( script, head.firstChild ); 65 | head.removeChild( script ); 66 | } 67 | } 68 | global.writeCaptureSupport = { 69 | _original: global.writeCaptureSupport, 70 | noConflict: function() { 71 | global.writeCaptureSupport = this._original; 72 | return this; 73 | }, 74 | // the code in this function is based on code from jQuery 1.3.2 75 | ajax: function(options) { 76 | if(options.dataType === 'script') { 77 | loadXDomain(options.url,options.success,options.error); 78 | return; 79 | } 80 | 81 | var xhr = newXhr(), requestDone = false, checkTimer; 82 | 83 | xhr.open("GET", options.url, options.async); 84 | xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); 85 | xhr.setRequestHeader("Accept","text/javascript, application/javascript, */*"); 86 | 87 | function checkXhr(){ 88 | if ( !requestDone && xhr && (xhr.readyState == 4) ) { 89 | requestDone = true; 90 | 91 | if (checkTimer) { 92 | clearInterval(checkTimer); 93 | checkTimer = null; 94 | } 95 | 96 | var suc = false; 97 | try { 98 | // IE error sometimes returns 1223 when it should be 204 so treat it as success, see #1450 99 | suc = !xhr.status && location.protocol == "file:" || 100 | ( xhr.status >= 200 && xhr.status < 300 ) || 101 | xhr.status == 304 || xhr.status == 1223; 102 | } catch(e){} 103 | 104 | if ( suc ) { 105 | options.success(xhr.responseText); 106 | } else { 107 | options.error(xhr,"error","xhr.status="+ xhr.status); 108 | } 109 | 110 | 111 | // Stop memory leaks 112 | if ( options.async ) 113 | xhr = null; 114 | } 115 | } 116 | 117 | if ( options.async ) { 118 | // poll for changes 119 | checkTimer = setInterval(checkXhr, 20); 120 | } 121 | 122 | try { 123 | xhr.send(); 124 | } catch(e) { 125 | options.error(xhr, null, e); 126 | } 127 | 128 | if(!options.async) { 129 | checkXhr(); 130 | } 131 | }, 132 | $: $, 133 | replaceWith: function(selector,content) { 134 | var i, len, el = $(selector), 135 | parent = el.parentNode || el.ownerDocument, 136 | work = document.createElement('div'), 137 | scripts = [], 138 | clearHTML = content.replace(/([\S\s]*?)<\/script>/gi,function(all,attrs,code) { 139 | if(isJs(attrs)) { 140 | scripts.push(code); 141 | return ""; 142 | } else { 143 | return all; 144 | } 145 | }); 146 | work.innerHTML = clearHTML; 147 | while (work.firstChild) { 148 | parent.insertBefore(work.removeChild(work.firstChild),el); 149 | } 150 | parent.removeChild(el); 151 | for(i = 0, len = scripts.length; i < len; i++) { 152 | globalEval(scripts[i]); 153 | } 154 | } 155 | }; 156 | 157 | function isElement(o) { 158 | return o && o.nodeType == 1; 159 | } 160 | 161 | function $(s) { 162 | if(isElement(s)) return s; 163 | 164 | // trim the selector 165 | s = s && s.replace(/^\s*/,'').replace(/\s*$/,''); 166 | 167 | if(!/^#[a-zA-Z0-9_:\.\-]+$/.test(s)) 168 | throw "nolib-support only allows id based selectors. selector=" + s; 169 | 170 | return document.getElementById(s.substring(1)); 171 | } 172 | 173 | var newXhr = global.ActiveXObject ? function() { 174 | return new ActiveXObject("Microsoft.XMLHTTP"); 175 | } : function () { 176 | return new XMLHttpRequest(); 177 | }; 178 | 179 | // the code in this function is copied and slightly modified from jQuery 1.3.2 180 | function loadXDomain(url,success) { 181 | // TODO what about scripts that fail to load? bad url, etc.? 182 | var head = document.getElementsByTagName("head")[0]; 183 | var script = document.createElement("script"); 184 | script.src = url; 185 | 186 | var done = false; 187 | 188 | // Attach handlers for all browsers 189 | script.onload = script.onreadystatechange = function(){ 190 | if ( !done && (!this.readyState || 191 | this.readyState == "loaded" || this.readyState == "complete") ) { 192 | done = true; 193 | success(); 194 | 195 | // Handle memory leak in IE 196 | script.onload = script.onreadystatechange = null; 197 | head.removeChild( script ); 198 | } 199 | }; 200 | 201 | head.appendChild(script); 202 | } 203 | 204 | })(this); -------------------------------------------------------------------------------- /test/autoCapture-noLib.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | Auto Capture Tests 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |

      QUnit

      18 |

      19 |

      20 |
        21 | 22 |
        23 |
        24 |
        25 | 26 | 27 | -------------------------------------------------------------------------------- /test/autoCapture.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | Auto Capture Tests 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |

        QUnit

        17 |

        18 |

        19 |
          20 | 21 |
          22 |
          23 |
          24 | 25 | 26 | -------------------------------------------------------------------------------- /test/autoCapture.js: -------------------------------------------------------------------------------- 1 | (function($,dwa) { 2 | document.write = document.writeln = function(s) { 3 | ok(false,"'real' document.write(ln) called!"); 4 | if(window.console) { 5 | console.warn("wrote: ",s); 6 | } 7 | }; 8 | function h(html) { 9 | return $('
          ').html(html).html().replace(/FooBarBaz'); 28 | document.write('QuxQuxxQuxx
          '); 29 | 30 | stop(); 31 | }); 32 | test('multiple',function() { 33 | expect(2); 34 | dwa.autoAsync(function() { 35 | equals(h('FooBarBaz\nQuxQuxxQuxx'),h($('#auto-test').html())); 36 | equals(h('Easy as ABC\n123 Do Re Mi'),h($('#auto-test2').html())); 37 | reset(); start(); 38 | }); 39 | 40 | document.writeln('
          FooBarBaz'); 41 | document.write('QuxQuxxQuxx
          '); 42 | 43 | $('#test-content').append('
          '); 44 | 45 | document.writeln('
          Easy as ABC'); 46 | document.write('123 Do Re Mi
          '); 47 | 48 | stop(); 49 | }); 50 | test('inline',function() { 51 | expect(1); 52 | dwa.autoAsync(function() { 53 | equals(h('FooBarBaz'),h($('#auto-test').html())); 54 | reset(); start(); 55 | }); 56 | 57 | document.write('
          FooBaz
          '); 58 | stop(); 59 | }); 60 | test('external',function() { 61 | expect(1); 62 | dwa.autoAsync(function() { 63 | equals(h('FooBarBaz'),h($('#auto-test').html())); 64 | reset(); start(); 65 | }); 66 | 67 | document.write('
          BarBaz
          '); 68 | stop(); 69 | }); 70 | test('xdomain',function() { 71 | expect(1); 72 | dwa.autoAsync(function() { 73 | equals(h($('#auto-test').html()),h('Fooexternal barBaz')); 74 | reset(); start(); 75 | }); 76 | 77 | document.write('
          FooBaz
          '); 78 | stop(); 79 | }); 80 | test('nested',function() { 81 | expect(1); 82 | dwa.autoAsync(function() { 83 | equals(h($('#auto-test').html()),h('FooBarbArexternal barbArBarBaz')); 84 | reset(); start(); 85 | }); 86 | 87 | document.write('
          FooBaz
          '); 88 | stop(); 89 | }); 90 | })(jQuery,writeCapture); -------------------------------------------------------------------------------- /test/bar.js: -------------------------------------------------------------------------------- 1 | document.write('bAr'); 2 | document.write(' '); 3 | document.write('bAr'); -------------------------------------------------------------------------------- /test/baz.js: -------------------------------------------------------------------------------- 1 | document.write('bAr'); 2 | document.write(' '); 3 | document.write('bAr'); -------------------------------------------------------------------------------- /test/bs-serving.js: -------------------------------------------------------------------------------- 1 | document.write("\n"); 2 | document.write("\n"); 3 | document.write("\n"); 6 | -------------------------------------------------------------------------------- /test/cjs.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | CJS Plugin Tests 7 | 12 | 13 | 14 | 15 | 16 | 17 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /test/extsrc-tests.js: -------------------------------------------------------------------------------- 1 | function h(html) { 2 | return html.replace(/ 2 | 4 | 5 | 6 | Write Capture Tests 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |

          QUnit

          18 |

          19 |

          20 |
            21 |
            22 | 23 | 24 | -------------------------------------------------------------------------------- /test/foo.js: -------------------------------------------------------------------------------- 1 | document.write('Foo'); -------------------------------------------------------------------------------- /test/getById.js: -------------------------------------------------------------------------------- 1 | var divid='testDiv'; 2 | document.write('
            '); 3 | var span = document.createElement('span'); 4 | span.innerHTML = 'Hello World'; 5 | document.getElementById(divid).appendChild(span); 6 | -------------------------------------------------------------------------------- /test/getOpts.js: -------------------------------------------------------------------------------- 1 | document.write(' 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |

            Other Suites

            18 | 23 |

            QUnit

            24 |

            25 |

            26 |
              27 |
              28 |
              29 |
              30 |
              31 | 32 | 33 | -------------------------------------------------------------------------------- /test/noLib.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | Write Capture Tests 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |

              QUnit

              19 |

              20 |

              21 |
                22 |
                23 |
                24 |
                25 |
                26 | 27 | 28 | -------------------------------------------------------------------------------- /test/options.js: -------------------------------------------------------------------------------- 1 | document.write("