| 1 | /* jshint bitwise: false */ |
| 2 | "use strict"; |
| 3 | module.exports = CharacterData; |
| 4 | |
| 5 | var Leaf = require('./Leaf'); |
| 6 | var utils = require('./utils'); |
| 7 | var ChildNode = require('./ChildNode'); |
| 8 | var NonDocumentTypeChildNode = require('./NonDocumentTypeChildNode'); |
| 9 | |
| 10 | function CharacterData() { |
| 11 | Leaf.call(this); |
| 12 | } |
| 13 | |
| 14 | CharacterData.prototype = Object.create(Leaf.prototype, { |
| 15 | // DOMString substringData(unsigned long offset, |
| 16 | // unsigned long count); |
| 17 | // The substringData(offset, count) method must run these steps: |
| 18 | // |
| 19 | // If offset is greater than the context object's |
| 20 | // length, throw an INDEX_SIZE_ERR exception and |
| 21 | // terminate these steps. |
| 22 | // |
| 23 | // If offset+count is greater than the context |
| 24 | // object's length, return a DOMString whose value is |
| 25 | // the UTF-16 code units from the offsetth UTF-16 code |
| 26 | // unit to the end of data. |
| 27 | // |
| 28 | // Return a DOMString whose value is the UTF-16 code |
| 29 | // units from the offsetth UTF-16 code unit to the |
| 30 | // offset+countth UTF-16 code unit in data. |
| 31 | substringData: { value: function substringData(offset, count) { |
| 32 | if (arguments.length < 2) { throw new TypeError("Not enough arguments"); } |
| 33 | // Convert arguments to WebIDL "unsigned long" |
| 34 | offset = offset >>> 0; |
| 35 | count = count >>> 0; |
| 36 | if (offset > this.data.length || offset < 0 || count < 0) { |
| 37 | utils.IndexSizeError(); |
| 38 | } |
| 39 | return this.data.substring(offset, offset+count); |
| 40 | }}, |
| 41 | |
| 42 | // void appendData(DOMString data); |
| 43 | // The appendData(data) method must append data to the context |
| 44 | // object's data. |
| 45 | appendData: { value: function appendData(data) { |
| 46 | if (arguments.length < 1) { throw new TypeError("Not enough arguments"); } |
| 47 | this.data += String(data); |
| 48 | }}, |
| 49 | |
| 50 | // void insertData(unsigned long offset, DOMString data); |
| 51 | // The insertData(offset, data) method must run these steps: |
| 52 | // |
| 53 | // If offset is greater than the context object's |
| 54 | // length, throw an INDEX_SIZE_ERR exception and |
| 55 | // terminate these steps. |
| 56 | // |
| 57 | // Insert data into the context object's data after |
| 58 | // offset UTF-16 code units. |
| 59 | // |
| 60 | insertData: { value: function insertData(offset, data) { |
| 61 | return this.replaceData(offset, 0, data); |
| 62 | }}, |
| 63 | |
| 64 | |
| 65 | // void deleteData(unsigned long offset, unsigned long count); |
| 66 | // The deleteData(offset, count) method must run these steps: |
| 67 | // |
| 68 | // If offset is greater than the context object's |
| 69 | // length, throw an INDEX_SIZE_ERR exception and |
| 70 | // terminate these steps. |
| 71 | // |
| 72 | // If offset+count is greater than the context |
| 73 | // object's length var count be length-offset. |
| 74 | // |
| 75 | // Starting from offset UTF-16 code units remove count |
| 76 | // UTF-16 code units from the context object's data. |
| 77 | deleteData: { value: function deleteData(offset, count) { |
| 78 | return this.replaceData(offset, count, ''); |
| 79 | }}, |
| 80 | |
| 81 | |
| 82 | // void replaceData(unsigned long offset, unsigned long count, |
| 83 | // DOMString data); |
| 84 | // |
| 85 | // The replaceData(offset, count, data) method must act as |
| 86 | // if the deleteData() method is invoked with offset and |
| 87 | // count as arguments followed by the insertData() method |
| 88 | // with offset and data as arguments and re-throw any |
| 89 | // exceptions these methods might have thrown. |
| 90 | replaceData: { value: function replaceData(offset, count, data) { |
| 91 | var curtext = this.data, len = curtext.length; |
| 92 | // Convert arguments to correct WebIDL type |
| 93 | offset = offset >>> 0; |
| 94 | count = count >>> 0; |
| 95 | data = String(data); |
| 96 | |
| 97 | if (offset > len || offset < 0) utils.IndexSizeError(); |
| 98 | |
| 99 | if (offset+count > len) |
| 100 | count = len - offset; |
| 101 | |
| 102 | var prefix = curtext.substring(0, offset), |
| 103 | suffix = curtext.substring(offset+count); |
| 104 | |
| 105 | this.data = prefix + data + suffix; |
| 106 | }}, |
| 107 | |
| 108 | // Utility method that Node.isEqualNode() calls to test Text and |
| 109 | // Comment nodes for equality. It is okay to put it here, since |
| 110 | // Node will have already verified that nodeType is equal |
| 111 | isEqual: { value: function isEqual(n) { |
| 112 | return this._data === n._data; |
| 113 | }}, |
| 114 | |
| 115 | length: { get: function() { return this.data.length; }} |
| 116 | |
| 117 | }); |
| 118 | |
| 119 | Object.defineProperties(CharacterData.prototype, ChildNode); |
| 120 | Object.defineProperties(CharacterData.prototype, NonDocumentTypeChildNode); |