1 /*
  2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
  3 For licensing, see LICENSE.html 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  * @constructor
 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.
 19  * @example
 20  * var nativeNode = document.createTextNode( 'Example' );
 21  * var text = CKEDITOR.dom.text( nativeNode );
 22  * @example
 23  * var text = CKEDITOR.dom.text( 'Example' );
 24  */
 25 CKEDITOR.dom.text = function( text, ownerDocument )
 26 {
 27 	if ( typeof text == 'string' )
 28 		text = ( ownerDocument ? ownerDocument.$ : document ).createTextNode( text );
 29
 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).
 34 	//
 35 	// CKEDITOR.dom.domObject.call( this, element );
 36
 37 	/**
 38 	 * The native DOM text node represented by this class instance.
 39 	 * @type Object
 40 	 * @example
 41 	 * var element = new CKEDITOR.dom.text( 'Example' );
 42 	 * alert( element.$.nodeType );  // "3"
 43 	 */
 44 	this.$ = text;
 45 };
 46
 47 CKEDITOR.dom.text.prototype = new CKEDITOR.dom.node();
 48
 49 CKEDITOR.tools.extend( CKEDITOR.dom.text.prototype,
 50 	/** @lends CKEDITOR.dom.text.prototype */
 51 	{
 52 		/**
 53 		 * The node type. This is a constant value set to
 54 		 * {@link CKEDITOR.NODE_TEXT}.
 55 		 * @type Number
 56 		 * @example
 57 		 */
 58 		type : CKEDITOR.NODE_TEXT,
 59
 60 		getLength : function()
 61 		{
 62 			return this.$.nodeValue.length;
 63 		},
 64
 65 		getText : function()
 66 		{
 67 			return this.$.nodeValue;
 68 		},
 69
 70 		/**
 71 		 * Breaks this text node into two nodes at the specified offset,
 72 		 * keeping both in the tree as siblings. This node then only contains
 73 		 * all the content up to the offset point. A new text node, which is
 74 		 * inserted as the next sibling of this node, contains all the content
 75 		 * at and after the offset point. When the offset is equal to the
 76 		 * length of this node, the new node has no data.
 77 		 * @param {Number} The position at which to split, starting from zero.
 78 		 * @returns {CKEDITOR.dom.text} The new text node.
 79 		 */
 80 		split : function( offset )
 81 		{
 82 			return new CKEDITOR.dom.text( this.$.splitText( offset ) );
 83 		},
 84
 85 		/**
 86 		 * Extracts characters from indexA up to but not including indexB.
 87 		 * @param {Number} indexA An integer between 0 and one less than the
 88 		 *		length of the text.
 89 		 * @param {Number} [indexB] An integer between 0 and the length of the
 90 		 *		string. If omitted, extracts characters to the end of the text.
 91 		 */
 92 		substring : function( indexA, indexB )
 93 		{
 94 			// We need the following check due to a Firefox bug
 95 			// https://bugzilla.mozilla.org/show_bug.cgi?id=458886
 96 			if ( typeof indexB != 'number' )
 97 				return this.$.nodeValue.substr( indexA );
 98 			else
 99 				return this.$.nodeValue.substring( indexA, indexB );
100 		}
101 	});
102