Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Implement Text.prototype.splitText
Browse files Browse the repository at this point in the history
  • Loading branch information
arv committed Jan 16, 2014
1 parent 6cada7b commit c779975
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 2 deletions.
1 change: 1 addition & 0 deletions build.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions shadowdom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
42 changes: 42 additions & 0 deletions src/wrappers/Text.js
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 0 additions & 2 deletions src/wrappers/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
45 changes: 45 additions & 0 deletions test/js/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<b></b>';
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);
});

});

0 comments on commit c779975

Please sign in to comment.