| 1 |
"use strict"; |
| 2 |
module.exports = Text; |
| 3 |
|
| 4 |
var utils = require('./utils'); |
| 5 |
var Node = require('./Node'); |
| 6 |
var CharacterData = require('./CharacterData'); |
| 7 |
|
| 8 |
function Text(doc, data) { |
| 9 |
CharacterData.call(this); |
| 10 |
this.nodeType = Node.TEXT_NODE; |
| 11 |
this.ownerDocument = doc; |
| 12 |
this._data = data; |
| 13 |
this._index = undefined; |
| 14 |
} |
| 15 |
|
| 16 |
var nodeValue = { |
| 17 |
get: function() { return this._data; }, |
| 18 |
set: function(v) { |
| 19 |
if (v === null || v === undefined) { v = ''; } else { v = String(v); } |
| 20 |
if (v === this._data) return; |
| 21 |
this._data = v; |
| 22 |
if (this.rooted) |
| 23 |
this.ownerDocument.mutateValue(this); |
| 24 |
if (this.parentNode && |
| 25 |
this.parentNode._textchangehook) |
| 26 |
this.parentNode._textchangehook(this); |
| 27 |
} |
| 28 |
}; |
| 29 |
|
| 30 |
Text.prototype = Object.create(CharacterData.prototype, { |
| 31 |
nodeName: { value: "#text" }, |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
nodeValue: nodeValue, |
| 36 |
textContent: nodeValue, |
| 37 |
innerText: nodeValue, |
| 38 |
data: { |
| 39 |
get: nodeValue.get, |
| 40 |
set: function(v) { |
| 41 |
nodeValue.set.call(this, v===null ? '' : String(v)); |
| 42 |
}, |
| 43 |
}, |
| 44 |
|
| 45 |
splitText: { value: function splitText(offset) { |
| 46 |
if (offset > this._data.length || offset < 0) utils.IndexSizeError(); |
| 47 |
|
| 48 |
var newdata = this._data.substring(offset), |
| 49 |
newnode = this.ownerDocument.createTextNode(newdata); |
| 50 |
this.data = this.data.substring(0, offset); |
| 51 |
|
| 52 |
var parent = this.parentNode; |
| 53 |
if (parent !== null) |
| 54 |
parent.insertBefore(newnode, this.nextSibling); |
| 55 |
|
| 56 |
return newnode; |
| 57 |
}}, |
| 58 |
|
| 59 |
wholeText: { get: function wholeText() { |
| 60 |
var result = this.textContent; |
| 61 |
for (var next = this.nextSibling; next; next = next.nextSibling) { |
| 62 |
if (next.nodeType !== Node.TEXT_NODE) { break; } |
| 63 |
result += next.textContent; |
| 64 |
} |
| 65 |
return result; |
| 66 |
}}, |
| 67 |
|
| 68 |
replaceWholeText: { value: utils.nyi }, |
| 69 |
|
| 70 |
|
| 71 |
clone: { value: function clone() { |
| 72 |
return new Text(this.ownerDocument, this._data); |
| 73 |
}}, |
| 74 |
|
| 75 |
}); |