Permalink
Browse files

Wrap document.write

When we do a document.write we reset the lastChild_ and nextSibling_ pointers
so that next time we get lastChild or nextSibling we recompute them.

This also adds a dependency on tooltkitchen/tools to reuse the htmlSuite and
htmlTest.
  • Loading branch information...
arv committed Apr 26, 2013
1 parent de8e8ce commit 07be1f387f9a099bd9a13a0edf193acbf0f5c522
Showing with 37 additions and 1 deletion.
  1. +3 −0 .gitmodules
  2. +12 −0 src/wrappers/Document.js
  3. +2 −1 test/Document.js
  4. +18 −0 test/document-write.html
  5. +1 −0 test/index.html
  6. +1 −0 tools
View
@@ -1,3 +1,6 @@
[submodule "buildbot"]
path = buildbot
url = https://github.com/toolkitchen/buildbot.git
+[submodule "tools"]
+ path = tools
+ url = git@github.com:toolkitchen/tools.git
View
@@ -56,6 +56,7 @@
].forEach(wrapMethod);
var originalAdoptNode = document.adoptNode;
+ var originalWrite = document.write;
mixin(Document.prototype, {
adoptNode: function(node) {
@@ -64,6 +65,17 @@
},
elementFromPoint: function(x, y) {
return elementFromPoint(this, this, x, y);
+ },
+ write: function(s) {
+ var all = this.querySelectorAll('*');
+ var last = all[all.length - 1];
+ while (last.nextSibling) {
+ last = last.nextSibling;
+ }
+ var p = last.parentNode;
+ p.lastChild_ = undefined;
+ last.nextSibling_ = undefined;
+ originalWrite.call(this.impl, s);
}
});
View
@@ -4,7 +4,7 @@
* license that can be found in the LICENSE file.
*/
-suite('Document', function() {
+htmlSuite('Document', function() {
var wrap = ShadowDOMPolyfill.wrap;
@@ -188,4 +188,5 @@ suite('Document', function() {
assert.equal(doc.elementFromPoint(5, 5), div);
});
+ htmlTest('document-write.html');
});
View
@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<script src="../node_modules/chai/chai.js"></script>
+<script src="../tools/test/htmltest.js"></script>
+<script src="../shadowdom.js"></script>
+<a></a><script>
+var assert = chai.assert;
+
+var a = document.querySelector('a');
+document.write('<b></b>');
+assert.equal(a.nextSibling.localName, 'script');
+assert.equal(a.nextSibling.nextSibling.localName, 'b');
+
+var all = document.querySelectorAll('*');
+var last = all[all.length - 1];
+assert.equal(last.localName, 'b');
+
+done();
+</script>
View
@@ -8,6 +8,7 @@
<link rel="stylesheet" href="../node_modules/mocha/mocha.css">
<script src="../node_modules/chai/chai.js"></script>
<script src="../node_modules/mocha/mocha.js"></script>
+<script src="../tools/test/mocha-htmltest.js"></script>
<script src="../shadowdom.js"></script>
<script src="test.main.js"></script>
<div id="mocha"></div>
1 tools
Submodule tools added at a588ab

0 comments on commit 07be1f3

Please sign in to comment.