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