diff --git a/build.json b/build.json index 922db75..a9c08d0 100644 --- a/build.json +++ b/build.json @@ -10,6 +10,7 @@ "src/querySelector.js", "src/wrappers/node-interfaces.js", "src/wrappers/CharacterData.js", + "src/wrappers/Text.js", "src/wrappers/Element.js", "src/wrappers/HTMLElement.js", "src/wrappers/HTMLCanvasElement.js", diff --git a/shadowdom.js b/shadowdom.js index 3ccb75a..cce3f91 100644 --- a/shadowdom.js +++ b/shadowdom.js @@ -26,6 +26,7 @@ 'src/querySelector.js', 'src/wrappers/node-interfaces.js', 'src/wrappers/CharacterData.js', + 'src/wrappers/Text.js', 'src/wrappers/Element.js', 'src/wrappers/HTMLElement.js', 'src/wrappers/HTMLCanvasElement.js', diff --git a/src/wrappers/Text.js b/src/wrappers/Text.js new file mode 100644 index 0000000..fefc501 --- /dev/null +++ b/src/wrappers/Text.js @@ -0,0 +1,42 @@ +// Copyright 2014 The Polymer Authors. All rights reserved. +// Use of this source code is goverened by a BSD-style +// license that can be found in the LICENSE file. + +(function(scope) { + 'use strict'; + + var CharacterData = scope.wrappers.CharacterData; + var enqueueMutation = scope.enqueueMutation; + var mixin = scope.mixin; + var registerWrapper = scope.registerWrapper; + + function toUInt32(x) { + return x >>> 0; + } + + var OriginalText = window.Text; + + function Text(node) { + CharacterData.call(this, node); + } + Text.prototype = Object.create(CharacterData.prototype); + mixin(Text.prototype, { + splitText: function(offset) { + offset = toUInt32(offset); + var s = this.data; + if (offset > s.length) + throw new Error('IndexSizeError'); + var head = s.slice(0, offset); + var tail = s.slice(offset); + this.data = head; + var newTextNode = this.ownerDocument.createTextNode(tail); + if (this.parentNode) + this.parentNode.insertBefore(newTextNode, this.nextSibling); + return newTextNode; + } + }); + + registerWrapper(OriginalText, Text, document.createTextNode('')); + + scope.wrappers.Text = Text; +})(window.ShadowDOMPolyfill); diff --git a/src/wrappers/generic.js b/src/wrappers/generic.js index bb0b3c6..9d7a35a 100644 --- a/src/wrappers/generic.js +++ b/src/wrappers/generic.js @@ -16,11 +16,9 @@ mixin(DocumentFragment.prototype, SelectorsInterface); mixin(DocumentFragment.prototype, GetElementsByInterface); - var Text = registerObject(document.createTextNode('')); var Comment = registerObject(document.createComment('')); scope.wrappers.Comment = Comment; scope.wrappers.DocumentFragment = DocumentFragment; - scope.wrappers.Text = Text; })(window.ShadowDOMPolyfill); diff --git a/test/js/Text.js b/test/js/Text.js index 273f85e..cc933ff 100644 --- a/test/js/Text.js +++ b/test/js/Text.js @@ -12,4 +12,49 @@ suite('Text', function() { assert.instanceOf(div.firstChild, Text); }); + test('splitText', function() { + var t = document.createTextNode('abcd'); + var t2 = t.splitText(3); + assert.equal(t.data, 'abc'); + assert.equal(t2.data, 'd'); + + t = document.createTextNode('abcd'); + t2 = t.splitText(0); + assert.equal(t.data, ''); + assert.equal(t2.data, 'abcd'); + + t = document.createTextNode('abcd'); + t2 = t.splitText(4); + assert.equal(t.data, 'abcd'); + assert.equal(t2.data, ''); + }); + + test('splitText with too large offset', function() { + var t = document.createTextNode('abcd'); + assert.throws(function() { + t.splitText(5); + }); + }); + + test('splitText negative offset', function() { + var t = document.createTextNode('abcd'); + assert.throws(function() { + t.splitText(-1); + }); + }); + + test('splitText siblings', function() { + var div = document.createElement('div'); + div.innerHTML = 'abcd'; + var t = div.firstChild; + var b = div.lastChild; + + var t2 = t.splitText(3); + assert.equal(t.data, 'abc'); + assert.equal(t2.data, 'd'); + + assert.equal(t.nextSibling, t2); + assert.equal(t2.nextSibling, b); + }); + });