Skip to content

Commit

Permalink
CharacterData implements the data and length attributes, remove the l…
Browse files Browse the repository at this point in the history
…ength() function, update tests, fixes bwrrp#6
  • Loading branch information
devatwork committed May 5, 2014
1 parent 9e5d468 commit 7025cb9
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 102 deletions.
79 changes: 58 additions & 21 deletions lib/CharacterData.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,69 @@ define(
if (!arguments.length) return;

Node.call(this, type);

/**
* nodeValue is a string representing the textual data contained in this CharacterData node.
*
* NOTE: we'll use 'nodeValue' here instead of the standard 'data' to avoid having duplicate accessors.
* Holds the data value.
*
* @property nodeValue
* @type {string}
* @property _data
* @type {String}
* @private
*/
this.nodeValue = data || '';
this._data = data;
}
CharacterData.prototype = new Node();
CharacterData.prototype.constructor = CharacterData;

/**
* Returns a number representing the size of the string contained in CharacterData.nodeValue.
* A string representing the textual data contained in this object.
*
* @method length
*
* @return {number} The length of the string used as textual data for this CharacterData node.
* @class CharacterData
* @property data
* @type {string}
*/
CharacterData.prototype.length = function() {
return this.nodeValue.length;
};
Object.defineProperty(CharacterData.prototype, 'data', {
enumerable: true,
configurable: false,
get: function() {
return this._data;
},
set: function(newValue) {
this.replaceData(0, this._data.length, newValue);
}
});

/**
* nodeValue is a string representing the textual data contained in this CharacterData node.
*
* @class CharacterData
* @property nodeValue
* @type {string}
*/
Object.defineProperty(CharacterData.prototype, 'nodeValue', {
enumerable: true,
configurable: false,
get: function() {
return this.data;
}
});

/**
* The length of the string used as textual data for this CharacterData node.
*
* @class CharacterData
* @property length
* @type {Number}
*/
Object.defineProperty(CharacterData.prototype, 'length', {
enumerable: true,
configurable: false,
get: function() {
return this._data.length;
}
});

/**
* Returns a string containing the part of CharacterData.nodeValue of the specified length and starting at the
* Returns a string containing the part of CharacterData.data of the specified length and starting at the
* specified offset.
*
* @method substringData
Expand All @@ -69,19 +106,19 @@ define(
* @return {string} The substring extracted from the textual data of this CharacterData node.
*/
CharacterData.prototype.substringData = function(offset, count) {
return this.nodeValue.substr(offset, count);
return this._data.substr(offset, count);
};

/**
* Appends the given string to the CharacterData.nodeValue string; when this method returns, data contains the
* Appends the given string to the CharacterData.data string; when this method returns, data contains the
* concatenated string.
*
* @method appendData
*
* @param {string} data Is a string representing the textual data to append to the CharacterData node.
*/
CharacterData.prototype.appendData = function(data) {
this.replaceData(this.length(), 0, data);
this.replaceData(this.length, 0, data);
};

/**
Expand Down Expand Up @@ -109,7 +146,7 @@ define(
*/
CharacterData.prototype.deleteData = function(offset, count) {
// Omitting count means 'delete from offset to end'
if (count === undefined) count = this.length() - offset;
if (count === undefined) count = this.length - offset;
this.replaceData(offset, count, '');
};

Expand All @@ -125,7 +162,7 @@ define(
* CharacterData node.
*/
CharacterData.prototype.replaceData = function(offset, count, data) {
var length = this.length();
var length = this.length;

if (offset > length)
offset = length;
Expand All @@ -135,13 +172,13 @@ define(

// Queue mutation record
var record = new MutationRecord('characterData', this);
record.oldValue = this.nodeValue;
record.oldValue = this._data;
util.queueMutationRecord(record);

// Replace data
var before = this.substringData(0, offset),
after = this.substringData(offset + count);
this.nodeValue = before + data + after;
this._data = before + data + after;

// Update ranges
var document = this.ownerDocument || this;
Expand Down
2 changes: 1 addition & 1 deletion lib/Comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ define(
* @return {Node} The clone.
*/
Comment.prototype.cloneNode = function(deep, copy) {
copy = copy || new Comment(this.nodeValue);
copy = copy || new Comment(this.data);

return CharacterData.prototype.cloneNode.call(this, deep, copy);
};
Expand Down
6 changes: 3 additions & 3 deletions lib/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ define(
var nextNode = childNode.nextSibling;
if (childNode.nodeType == Node.TEXT_NODE) {
// Delete empty text nodes
var length = childNode.length();
var length = childNode.length;
if (!length) {
childNode.parentNode.removeChild(childNode);
--index;
Expand All @@ -399,7 +399,7 @@ define(
sibling && sibling.nodeType == Node.TEXT_NODE;
sibling = sibling.nextSibling, ++siblingIndex) {

data += sibling.nodeValue;
data += sibling.data;
siblingsToRemove.push(sibling);
}
// Append concatenated data, if any
Expand All @@ -423,7 +423,7 @@ define(
range.setEnd(childNode, length);
}

length += sibling.length();
length += sibling.length;
}
// Remove contiguous text nodes (excluding current) in tree order
while (siblingsToRemove.length) {
Expand Down
2 changes: 1 addition & 1 deletion lib/ProcessingInstruction.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ define(
* @return {Node} The clone.
*/
ProcessingInstruction.prototype.cloneNode = function(deep, copy) {
copy = copy || new ProcessingInstruction(this.target, this.nodeValue);
copy = copy || new ProcessingInstruction(this.target, this.data);

return CharacterData.prototype.cloneNode.call(this, deep, copy);
};
Expand Down
10 changes: 5 additions & 5 deletions lib/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ define(
],
function(
CharacterData,
Node,
util
) {
Node,
util
) {
'use strict';

/**
Expand Down Expand Up @@ -55,7 +55,7 @@ define(
*/
Text.prototype.splitText = function(offset) {
// Check offset
var length = this.length();
var length = this.length;
if (offset < 0) offset = 0;
if (offset > length) offset = length;

Expand Down Expand Up @@ -114,7 +114,7 @@ define(
* @return {Node} The clone.
*/
Text.prototype.cloneNode = function(deep, copy) {
copy = copy || new Text(this.nodeValue);
copy = copy || new Text(this.data);

return CharacterData.prototype.cloneNode.call(this, deep, copy);
};
Expand Down
8 changes: 3 additions & 5 deletions test/specs/MutationObserver.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ define(
it('responds to text changes', function() {
// TODO: direct assignment not yet detected
//text.data = 'meep';
text.replaceData(0, text.length(), 'meep');
text.replaceData(0, text.length, 'meep');

var queue = observer.takeRecords();
expect(queue[0].type).toBe('characterData');
Expand Down Expand Up @@ -179,7 +179,7 @@ define(
element.removeChild(newElement);
observer.takeRecords();

newText.replaceData(0, text.length(), 'meep');
newText.replaceData(0, text.length, 'meep');
var queue = observer.takeRecords();
expect(queue[0].type).toBe('characterData');
expect(queue[0].oldValue).toBe('test');
Expand All @@ -195,9 +195,7 @@ define(

describe('asynchronous usage', function() {
it('responds to text changes', function() {
// TODO: direct assignment not yet detected
//text.data = 'meep';
text.replaceData(0, text.length(), 'meep');
text.data = 'meep';

jasmine.Clock.tick(100);
expect(callback).toHaveBeenCalled();
Expand Down
Loading

0 comments on commit 7025cb9

Please sign in to comment.