]>
git.immae.eu Git - perso/Immae/Projets/packagist/connexionswing-ckeditor-component.git/blob - sources/core/dom/text.js
2 * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
3 * For licensing, see LICENSE.md or http://ckeditor.com/license
7 * @fileOverview Defines the {@link CKEDITOR.dom.text} class, which represents
12 * Represents a DOM text node.
14 * var nativeNode = document.createTextNode( 'Example' );
15 * var text = new CKEDITOR.dom.text( nativeNode );
17 * var text = new CKEDITOR.dom.text( 'Example' );
20 * @extends CKEDITOR.dom.node
21 * @constructor Creates a text class instance.
22 * @param {Object/String} text A native DOM text node or a string containing
23 * the text to use to create a new text node.
24 * @param {CKEDITOR.dom.document} [ownerDocument] The document that will contain
25 * the node in case of new node creation. Defaults to the current document.
27 CKEDITOR
.dom
.text = function( text
, ownerDocument
) {
28 if ( typeof text
== 'string' )
29 text
= ( ownerDocument
? ownerDocument
.$ : document
).createTextNode( text
);
31 // Theoretically, we should call the base constructor here
32 // (not CKEDITOR.dom.node though). But, IE doesn't support expando
33 // properties on text node, so the features provided by domObject will not
34 // work for text nodes (which is not a big issue for us).
36 // CKEDITOR.dom.domObject.call( this, element );
41 CKEDITOR
.dom
.text
.prototype = new CKEDITOR
.dom
.node();
43 CKEDITOR
.tools
.extend( CKEDITOR
.dom
.text
.prototype, {
45 * The node type. This is a constant value set to {@link CKEDITOR#NODE_TEXT}.
48 * @property {Number} [=CKEDITOR.NODE_TEXT]
50 type: CKEDITOR
.NODE_TEXT
,
53 * Gets length of node's value.
57 getLength: function() {
58 return this.$.nodeValue
.length
;
67 return this.$.nodeValue
;
73 * @param {String} text
75 setText: function( text
) {
76 this.$.nodeValue
= text
;
80 * Breaks this text node into two nodes at the specified offset,
81 * keeping both in the tree as siblings. This node then only contains
82 * all the content up to the offset point. A new text node, which is
83 * inserted as the next sibling of this node, contains all the content
84 * at and after the offset point. When the offset is equal to the
85 * length of this node, the new node has no data.
87 * @param {Number} The position at which to split, starting from zero.
88 * @returns {CKEDITOR.dom.text} The new text node.
90 split: function( offset
) {
92 // Saved the children count and text length beforehand.
93 var parent
= this.$.parentNode
,
94 count
= parent
.childNodes
.length
,
95 length
= this.getLength();
97 var doc
= this.getDocument();
98 var retval
= new CKEDITOR
.dom
.text( this.$.splitText( offset
), doc
);
100 if ( parent
.childNodes
.length
== count
) {
101 // If the offset is after the last char, IE creates the text node
102 // on split, but don't include it into the DOM. So, we have to do
103 // that manually here.
104 if ( offset
>= length
) {
105 retval
= doc
.createText( '' );
106 retval
.insertAfter( this );
108 // IE BUG: IE8+ does not update the childNodes array in DOM after splitText(),
109 // we need to make some DOM changes to make it update. (#3436)
110 var workaround
= doc
.createText( '' );
111 workaround
.insertAfter( retval
);
120 * Extracts characters from indexA up to but not including `indexB`.
122 * @param {Number} indexA An integer between `0` and one less than the
123 * length of the text.
124 * @param {Number} [indexB] An integer between `0` and the length of the
125 * string. If omitted, extracts characters to the end of the text.
127 substring: function( indexA
, indexB
) {
128 // We need the following check due to a Firefox bug
129 // https://bugzilla.mozilla.org/show_bug.cgi?id=458886
130 if ( typeof indexB
!= 'number' )
131 return this.$.nodeValue
.substr( indexA
);
133 return this.$.nodeValue
.substring( indexA
, indexB
);