JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.0
[ckeditor.git] / _source / core / dom / text.js
diff --git a/_source/core/dom/text.js b/_source/core/dom/text.js
new file mode 100644 (file)
index 0000000..2cedd9b
--- /dev/null
@@ -0,0 +1,123 @@
+/*\r
+Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
+For licensing, see LICENSE.html or http://ckeditor.com/license\r
+*/\r
+\r
+/**\r
+ * @fileOverview Defines the {@link CKEDITOR.dom.text} class, which represents\r
+ *             a DOM text node.\r
+ */\r
+\r
+/**\r
+ * Represents a DOM text node.\r
+ * @constructor\r
+ * @augments CKEDITOR.dom.node\r
+ * @param {Object|String} text A native DOM text node or a string containing\r
+ *             the text to use to create a new text node.\r
+ * @param {CKEDITOR.dom.document} [ownerDocument] The document that will contain\r
+ *             the node in case of new node creation. Defaults to the current document.\r
+ * @example\r
+ * var nativeNode = document.createTextNode( 'Example' );\r
+ * var text = CKEDITOR.dom.text( nativeNode );\r
+ * @example\r
+ * var text = CKEDITOR.dom.text( 'Example' );\r
+ */\r
+CKEDITOR.dom.text = function( text, ownerDocument )\r
+{\r
+       if ( typeof text == 'string' )\r
+               text = ( ownerDocument ? ownerDocument.$ : document ).createTextNode( text );\r
+\r
+       // Theoretically, we should call the base constructor here\r
+       // (not CKEDITOR.dom.node though). But, IE doesn't support expando\r
+       // properties on text node, so the features provided by domObject will not\r
+       // work for text nodes (which is not a big issue for us).\r
+       //\r
+       // CKEDITOR.dom.domObject.call( this, element );\r
+\r
+       /**\r
+        * The native DOM text node represented by this class instance.\r
+        * @type Object\r
+        * @example\r
+        * var element = new CKEDITOR.dom.text( 'Example' );\r
+        * alert( element.$.nodeType );  // "3"\r
+        */\r
+       this.$ = text;\r
+};\r
+\r
+CKEDITOR.dom.text.prototype = new CKEDITOR.dom.node();\r
+\r
+CKEDITOR.tools.extend( CKEDITOR.dom.text.prototype,\r
+       /** @lends CKEDITOR.dom.text.prototype */\r
+       {\r
+               /**\r
+                * The node type. This is a constant value set to\r
+                * {@link CKEDITOR.NODE_TEXT}.\r
+                * @type Number\r
+                * @example\r
+                */\r
+               type : CKEDITOR.NODE_TEXT,\r
+\r
+               getLength : function()\r
+               {\r
+                       return this.$.nodeValue.length;\r
+               },\r
+\r
+               getText : function()\r
+               {\r
+                       return this.$.nodeValue;\r
+               },\r
+\r
+               /**\r
+                * Breaks this text node into two nodes at the specified offset,\r
+                * keeping both in the tree as siblings. This node then only contains\r
+                * all the content up to the offset point. A new text node, which is\r
+                * inserted as the next sibling of this node, contains all the content\r
+                * at and after the offset point. When the offset is equal to the\r
+                * length of this node, the new node has no data.\r
+                * @param {Number} The position at which to split, starting from zero.\r
+                * @returns {CKEDITOR.dom.text} The new text node.\r
+                */\r
+               split : function( offset )\r
+               {\r
+                       // If the offset is after the last char, IE creates the text node\r
+                       // on split, but don't include it into the DOM. So, we have to do\r
+                       // that manually here.\r
+                       if ( CKEDITOR.env.ie && offset == this.getLength() )\r
+                       {\r
+                               var next = this.getDocument().createText( '' );\r
+                               next.insertAfter( this );\r
+                               return next;\r
+                       }\r
+\r
+                       var doc = this.getDocument();\r
+                       var retval = new CKEDITOR.dom.text( this.$.splitText( offset ), doc );\r
+\r
+                       // IE BUG: IE8 does not update the childNodes array in DOM after splitText(),\r
+                       // we need to make some DOM changes to make it update. (#3436)\r
+                       if ( CKEDITOR.env.ie8 )\r
+                       {\r
+                               var workaround = new CKEDITOR.dom.text( '', doc );\r
+                               workaround.insertAfter( retval );\r
+                               workaround.remove();\r
+                       }\r
+\r
+                       return retval;\r
+               },\r
+\r
+               /**\r
+                * Extracts characters from indexA up to but not including indexB.\r
+                * @param {Number} indexA An integer between 0 and one less than the\r
+                *              length of the text.\r
+                * @param {Number} [indexB] An integer between 0 and the length of the\r
+                *              string. If omitted, extracts characters to the end of the text.\r
+                */\r
+               substring : function( indexA, indexB )\r
+               {\r
+                       // We need the following check due to a Firefox bug\r
+                       // https://bugzilla.mozilla.org/show_bug.cgi?id=458886\r
+                       if ( typeof indexB != 'number' )\r
+                               return this.$.nodeValue.substr( indexA );\r
+                       else\r
+                               return this.$.nodeValue.substring( indexA, indexB );\r
+               }\r
+       });\r