2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
7 * @fileOverview Defines the {@link CKEDITOR.dom.text} class, which represents
12 * Represents a DOM text node.
14 * @augments CKEDITOR.dom.node
15 * @param {Object|String} text A native DOM text node or a string containing
16 * the text to use to create a new text node.
17 * @param {CKEDITOR.dom.document} [ownerDocument] The document that will contain
18 * the node in case of new node creation. Defaults to the current document.
20 * var nativeNode = document.createTextNode( 'Example' );
21 * var text = CKEDITOR.dom.text( nativeNode );
23 * var text = CKEDITOR.dom.text( 'Example' );
25 CKEDITOR
.dom
.text = function( text
, ownerDocument
)
27 if ( typeof text
== 'string' )
28 text
= ( ownerDocument
? ownerDocument
.$ : document
).createTextNode( text
);
30 // Theoretically, we should call the base constructor here
31 // (not CKEDITOR.dom.node though). But, IE doesn't support expando
32 // properties on text node, so the features provided by domObject will not
33 // work for text nodes (which is not a big issue for us).
35 // CKEDITOR.dom.domObject.call( this, element );
38 * The native DOM text node represented by this class instance.
41 * var element = new CKEDITOR.dom.text( 'Example' );
42 * alert( element.$.nodeType ); // "3"
47 CKEDITOR
.dom
.text
.prototype = new CKEDITOR
.dom
.node();
49 CKEDITOR
.tools
.extend( CKEDITOR
.dom
.text
.prototype,
50 /** @lends CKEDITOR.dom.text.prototype */
53 * The node type. This is a constant value set to
54 * {@link CKEDITOR.NODE_TEXT}.
58 type
: CKEDITOR
.NODE_TEXT
,
60 getLength : function()
62 return this.$.nodeValue
.length
;
67 return this.$.nodeValue
;
70 setText : function( text
)
72 this.$.nodeValue
= text
;
76 * Breaks this text node into two nodes at the specified offset,
77 * keeping both in the tree as siblings. This node then only contains
78 * all the content up to the offset point. A new text node, which is
79 * inserted as the next sibling of this node, contains all the content
80 * at and after the offset point. When the offset is equal to the
81 * length of this node, the new node has no data.
82 * @param {Number} The position at which to split, starting from zero.
83 * @returns {CKEDITOR.dom.text} The new text node.
85 split : function( offset
)
87 // If the offset is after the last char, IE creates the text node
88 // on split, but don't include it into the DOM. So, we have to do
89 // that manually here.
90 if ( CKEDITOR
.env
.ie
&& offset
== this.getLength() )
92 var next
= this.getDocument().createText( '' );
93 next
.insertAfter( this );
97 var doc
= this.getDocument();
98 var retval
= new CKEDITOR
.dom
.text( this.$.splitText( offset
), doc
);
100 // IE BUG: IE8 does not update the childNodes array in DOM after splitText(),
101 // we need to make some DOM changes to make it update. (#3436)
102 if ( CKEDITOR
.env
.ie8
)
104 var workaround
= new CKEDITOR
.dom
.text( '', doc
);
105 workaround
.insertAfter( retval
);
113 * Extracts characters from indexA up to but not including indexB.
114 * @param {Number} indexA An integer between 0 and one less than the
115 * length of the text.
116 * @param {Number} [indexB] An integer between 0 and the length of the
117 * string. If omitted, extracts characters to the end of the text.
119 substring : function( indexA
, indexB
)
121 // We need the following check due to a Firefox bug
122 // https://bugzilla.mozilla.org/show_bug.cgi?id=458886
123 if ( typeof indexB
!= 'number' )
124 return this.$.nodeValue
.substr( indexA
);
126 return this.$.nodeValue
.substring( indexA
, indexB
);