JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.1
[ckeditor.git] / _source / core / dom / text.js
1 /*\r
2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 /**\r
7  * @fileOverview Defines the {@link CKEDITOR.dom.text} class, which represents\r
8  *              a DOM text node.\r
9  */\r
10 \r
11 /**\r
12  * Represents a DOM text node.\r
13  * @constructor\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
19  * @example\r
20  * var nativeNode = document.createTextNode( 'Example' );\r
21  * var text = CKEDITOR.dom.text( nativeNode );\r
22  * @example\r
23  * var text = CKEDITOR.dom.text( 'Example' );\r
24  */\r
25 CKEDITOR.dom.text = function( text, ownerDocument )\r
26 {\r
27         if ( typeof text == 'string' )\r
28                 text = ( ownerDocument ? ownerDocument.$ : document ).createTextNode( text );\r
29 \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
34         //\r
35         // CKEDITOR.dom.domObject.call( this, element );\r
36 \r
37         /**\r
38          * The native DOM text node represented by this class instance.\r
39          * @type Object\r
40          * @example\r
41          * var element = new CKEDITOR.dom.text( 'Example' );\r
42          * alert( element.$.nodeType );  // "3"\r
43          */\r
44         this.$ = text;\r
45 };\r
46 \r
47 CKEDITOR.dom.text.prototype = new CKEDITOR.dom.node();\r
48 \r
49 CKEDITOR.tools.extend( CKEDITOR.dom.text.prototype,\r
50         /** @lends CKEDITOR.dom.text.prototype */\r
51         {\r
52                 /**\r
53                  * The node type. This is a constant value set to\r
54                  * {@link CKEDITOR.NODE_TEXT}.\r
55                  * @type Number\r
56                  * @example\r
57                  */\r
58                 type : CKEDITOR.NODE_TEXT,\r
59 \r
60                 getLength : function()\r
61                 {\r
62                         return this.$.nodeValue.length;\r
63                 },\r
64 \r
65                 getText : function()\r
66                 {\r
67                         return this.$.nodeValue;\r
68                 },\r
69 \r
70                 /**\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
79                  */\r
80                 split : function( offset )\r
81                 {\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
86                         {\r
87                                 var next = this.getDocument().createText( '' );\r
88                                 next.insertAfter( this );\r
89                                 return next;\r
90                         }\r
91 \r
92                         var doc = this.getDocument();\r
93                         var retval = new CKEDITOR.dom.text( this.$.splitText( offset ), doc );\r
94 \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
98                         {\r
99                                 var workaround = new CKEDITOR.dom.text( '', doc );\r
100                                 workaround.insertAfter( retval );\r
101                                 workaround.remove();\r
102                         }\r
103 \r
104                         return retval;\r
105                 },\r
106 \r
107                 /**\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
113                  */\r
114                 substring : function( indexA, indexB )\r
115                 {\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
120                         else\r
121                                 return this.$.nodeValue.substring( indexA, indexB );\r
122                 }\r
123         });\r