]>
Commit | Line | Data |
---|---|---|
1 | /** | |
2 | * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. | |
3 | * For licensing, see LICENSE.md or http://ckeditor.com/license | |
4 | */ | |
5 | ||
6 | /** | |
7 | * @fileOverview Defines the {@link CKEDITOR.dom.text} class, which represents | |
8 | * a DOM text node. | |
9 | */ | |
10 | ||
11 | /** | |
12 | * Represents a DOM text node. | |
13 | * | |
14 | * var nativeNode = document.createTextNode( 'Example' ); | |
15 | * var text = new CKEDITOR.dom.text( nativeNode ); | |
16 | * | |
17 | * var text = new CKEDITOR.dom.text( 'Example' ); | |
18 | * | |
19 | * @class | |
20 | * @extends CKEDITOR.dom.node | |
21 | * @constructor Creates a text class instance. | |
22 | * @param {Object/String} text A native DOM text node or a string containing | |
23 | * the text to use to create a new text node. | |
24 | * @param {CKEDITOR.dom.document} [ownerDocument] The document that will contain | |
25 | * the node in case of new node creation. Defaults to the current document. | |
26 | */ | |
27 | CKEDITOR.dom.text = function( text, ownerDocument ) { | |
28 | if ( typeof text == 'string' ) | |
29 | text = ( ownerDocument ? ownerDocument.$ : document ).createTextNode( text ); | |
30 | ||
31 | // Theoretically, we should call the base constructor here | |
32 | // (not CKEDITOR.dom.node though). But, IE doesn't support expando | |
33 | // properties on text node, so the features provided by domObject will not | |
34 | // work for text nodes (which is not a big issue for us). | |
35 | // | |
36 | // CKEDITOR.dom.domObject.call( this, element ); | |
37 | ||
38 | this.$ = text; | |
39 | }; | |
40 | ||
41 | CKEDITOR.dom.text.prototype = new CKEDITOR.dom.node(); | |
42 | ||
43 | CKEDITOR.tools.extend( CKEDITOR.dom.text.prototype, { | |
44 | /** | |
45 | * The node type. This is a constant value set to {@link CKEDITOR#NODE_TEXT}. | |
46 | * | |
47 | * @readonly | |
48 | * @property {Number} [=CKEDITOR.NODE_TEXT] | |
49 | */ | |
50 | type: CKEDITOR.NODE_TEXT, | |
51 | ||
52 | /** | |
53 | * Gets length of node's value. | |
54 | * | |
55 | * @returns {Number} | |
56 | */ | |
57 | getLength: function() { | |
58 | return this.$.nodeValue.length; | |
59 | }, | |
60 | ||
61 | /** | |
62 | * Gets node's value. | |
63 | * | |
64 | * @returns {String} | |
65 | */ | |
66 | getText: function() { | |
67 | return this.$.nodeValue; | |
68 | }, | |
69 | ||
70 | /** | |
71 | * Sets node's value. | |
72 | * | |
73 | * @param {String} text | |
74 | */ | |
75 | setText: function( text ) { | |
76 | this.$.nodeValue = text; | |
77 | }, | |
78 | ||
79 | /** | |
80 | * Breaks this text node into two nodes at the specified offset, | |
81 | * keeping both in the tree as siblings. This node then only contains | |
82 | * all the content up to the offset point. A new text node, which is | |
83 | * inserted as the next sibling of this node, contains all the content | |
84 | * at and after the offset point. When the offset is equal to the | |
85 | * length of this node, the new node has no data. | |
86 | * | |
87 | * @param {Number} The position at which to split, starting from zero. | |
88 | * @returns {CKEDITOR.dom.text} The new text node. | |
89 | */ | |
90 | split: function( offset ) { | |
91 | ||
92 | // Saved the children count and text length beforehand. | |
93 | var parent = this.$.parentNode, | |
94 | count = parent.childNodes.length, | |
95 | length = this.getLength(); | |
96 | ||
97 | var doc = this.getDocument(); | |
98 | var retval = new CKEDITOR.dom.text( this.$.splitText( offset ), doc ); | |
99 | ||
100 | if ( parent.childNodes.length == count ) { | |
101 | // If the offset is after the last char, IE creates the text node | |
102 | // on split, but don't include it into the DOM. So, we have to do | |
103 | // that manually here. | |
104 | if ( offset >= length ) { | |
105 | retval = doc.createText( '' ); | |
106 | retval.insertAfter( this ); | |
107 | } else { | |
108 | // IE BUG: IE8+ does not update the childNodes array in DOM after splitText(), | |
109 | // we need to make some DOM changes to make it update. (http://dev.ckeditor.com/ticket/3436) | |
110 | var workaround = doc.createText( '' ); | |
111 | workaround.insertAfter( retval ); | |
112 | workaround.remove(); | |
113 | } | |
114 | } | |
115 | ||
116 | return retval; | |
117 | }, | |
118 | ||
119 | /** | |
120 | * Extracts characters from indexA up to but not including `indexB`. | |
121 | * | |
122 | * @param {Number} indexA An integer between `0` and one less than the | |
123 | * length of the text. | |
124 | * @param {Number} [indexB] An integer between `0` and the length of the | |
125 | * string. If omitted, extracts characters to the end of the text. | |
126 | */ | |
127 | substring: function( indexA, indexB ) { | |
128 | // We need the following check due to a Firefox bug | |
129 | // https://bugzilla.mozilla.org/show_bug.cgi?id=458886 | |
130 | if ( typeof indexB != 'number' ) | |
131 | return this.$.nodeValue.substr( indexA ); | |
132 | else | |
133 | return this.$.nodeValue.substring( indexA, indexB ); | |
134 | } | |
135 | } ); |