瀏覽代碼

Introduces Text class.

Piotr Jasiun 10 年之前
父節點
當前提交
7a88313802

+ 12 - 1
packages/ckeditor5-utils/src/document/nodelist.js

@@ -5,7 +5,12 @@
 
 'use strict';
 
-CKEDITOR.define( [ 'document/character', 'document/node', 'utils' ], function( Character, Node, utils ) {
+CKEDITOR.define( [
+	'document/character',
+	'document/text',
+	'document/node',
+	'utils'
+], function( Character, Text, Node, utils ) {
 	/**
 	 * @class document.NodeList
 	 */
@@ -16,6 +21,8 @@ CKEDITOR.define( [ 'document/character', 'document/node', 'utils' ], function( C
 				return nodes;
 			}
 
+			// debugger;
+
 			this._nodes = [];
 
 			if ( nodes ) {
@@ -30,6 +37,10 @@ CKEDITOR.define( [ 'document/character', 'document/node', 'utils' ], function( C
 
 					if ( node instanceof Node ) {
 						this._nodes.push( node );
+					} else if ( node instanceof Text ) {
+						for ( j = 0, nodeLen = node.text.length; j < nodeLen; j++ ) {
+							this._nodes.push( new Character( node.text[ j ], utils.clone( node.attrs ) ) );
+						}
 					} else {
 						for ( j = 0, nodeLen = node.length; j < nodeLen; j++ ) {
 							this._nodes.push( new Character( node[ j ] ) );

+ 40 - 0
packages/ckeditor5-utils/src/document/text.js

@@ -0,0 +1,40 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+CKEDITOR.define( [], function() {
+	/**
+	 * Data structure for text with attributes. Note that Text is not a node, because it will never be part of document
+	 * tree, {@link document.Character is a node}.
+	 *
+	 * @class document.Text
+	 */
+	class Text {
+		/**
+		 * Creates text with attributes.
+		 *
+		 * @param {String} text Described character.
+		 */
+		constructor( text, attrs ) {
+			/**
+			 * Text.
+			 *
+			 * @readonly
+			 * @property {String} text
+			 */
+			this.text = text;
+
+			/**
+			 * Array of attributes.
+			 *
+			 * @property {Array} attr
+			 */
+			this.attrs = attrs;
+		}
+	}
+
+	return Text;
+} );

+ 11 - 25
packages/ckeditor5-utils/tests/document/changeoperation.js

@@ -15,6 +15,7 @@ var modules = bender.amd.require(
 	'document/character',
 	'document/attribute',
 	'document/nodelist',
+	'document/text',
 	'ckeditorerror' );
 
 describe( 'ChangeOperation', function() {
@@ -108,7 +109,7 @@ describe( 'ChangeOperation', function() {
 		var ChangeOperation = modules[ 'document/changeoperation' ];
 		var Position = modules[ 'document/position' ];
 		var Range = modules[ 'document/range' ];
-		var Character = modules[ 'document/character' ];
+		var Text = modules[ 'document/text' ];
 		var Attribute = modules[ 'document/attribute' ];
 
 		var doc = new Document();
@@ -116,10 +117,7 @@ describe( 'ChangeOperation', function() {
 		var oldAttr = new Attribute( 'isNew', false );
 		var newAttr = new Attribute( 'isNew', true );
 
-		doc.root.insertChildren( 0, [
-			new Character( 'b', [ oldAttr ] ),
-			new Character( 'a', [ oldAttr ] ),
-			new Character( 'r', [ oldAttr ] ) ] );
+		doc.root.insertChildren( 0, new Text( 'bar', [ oldAttr ] ) );
 
 		doc.applyOperation( new ChangeOperation(
 			[
@@ -145,7 +143,7 @@ describe( 'ChangeOperation', function() {
 		var ChangeOperation = modules[ 'document/changeoperation' ];
 		var Position = modules[ 'document/position' ];
 		var Range = modules[ 'document/range' ];
-		var Character = modules[ 'document/character' ];
+		var Text = modules[ 'document/text' ];
 		var Attribute = modules[ 'document/attribute' ];
 
 		var doc = new Document();
@@ -153,10 +151,7 @@ describe( 'ChangeOperation', function() {
 		var oldAttr = new Attribute( 'isNew', false );
 		var newAttr = new Attribute( 'isNew', true );
 
-		doc.root.insertChildren( 0, [
-			new Character( 'b', [ oldAttr ] ),
-			new Character( 'a', [ oldAttr ] ),
-			new Character( 'r', [ oldAttr ] ) ] );
+		doc.root.insertChildren( 0, new Text( 'bar', [ oldAttr ] ) );
 
 		doc.applyOperation( new ChangeOperation(
 			new Range( new Position( [ 0 ], doc.root ), new Position( [ 2 ], doc.root ) ),
@@ -239,17 +234,14 @@ describe( 'ChangeOperation', function() {
 		var ChangeOperation = modules[ 'document/changeoperation' ];
 		var Position = modules[ 'document/position' ];
 		var Range = modules[ 'document/range' ];
-		var Character = modules[ 'document/character' ];
+		var Text = modules[ 'document/text' ];
 		var Attribute = modules[ 'document/attribute' ];
 
 		var doc = new Document();
 
 		var fooAttr = new Attribute( 'foo', true );
 
-		doc.root.insertChildren( 0, [
-			new Character( 'b', [ fooAttr ] ),
-			new Character( 'a', [ fooAttr ] ),
-			new Character( 'r', [ fooAttr ] ) ] );
+		doc.root.insertChildren( 0, new Text( 'bar', [ fooAttr ] ) );
 
 		doc.applyOperation( new ChangeOperation(
 			[
@@ -329,7 +321,7 @@ describe( 'ChangeOperation', function() {
 		var ChangeOperation = modules[ 'document/changeoperation' ];
 		var Position = modules[ 'document/position' ];
 		var Range = modules[ 'document/range' ];
-		var Character = modules[ 'document/character' ];
+		var Text = modules[ 'document/text' ];
 		var Attribute = modules[ 'document/attribute' ];
 
 		var doc = new Document();
@@ -337,10 +329,7 @@ describe( 'ChangeOperation', function() {
 		var oldAttr = new Attribute( 'isNew', false );
 		var newAttr = new Attribute( 'isNew', true );
 
-		doc.root.insertChildren( 0, [
-			new Character( 'b', [ oldAttr ] ),
-			new Character( 'a', [ oldAttr ] ),
-			new Character( 'r', [ oldAttr ] ) ] );
+		doc.root.insertChildren( 0, new Text( 'bar', [ oldAttr ] ) );
 
 		var oppertaion = new ChangeOperation(
 			new Range( new Position( [ 0 ], doc.root ), new Position( [ 3 ], doc.root ) ),
@@ -369,17 +358,14 @@ describe( 'ChangeOperation', function() {
 		var ChangeOperation = modules[ 'document/changeoperation' ];
 		var Position = modules[ 'document/position' ];
 		var Range = modules[ 'document/range' ];
-		var Character = modules[ 'document/character' ];
+		var Text = modules[ 'document/text' ];
 		var Attribute = modules[ 'document/attribute' ];
 
 		var doc = new Document();
 
 		var fooAttr = new Attribute( 'foo', false );
 
-		doc.root.insertChildren( 0, [
-			new Character( 'b', [ fooAttr ] ),
-			new Character( 'a', [ fooAttr ] ),
-			new Character( 'r', [ fooAttr ] ) ] );
+		doc.root.insertChildren( 0, new Text( 'bar', [ fooAttr ] ) );
 
 		var oppertaion = new ChangeOperation(
 			new Range( new Position( [ 0 ], doc.root ), new Position( [ 3 ], doc.root ) ),

+ 24 - 1
packages/ckeditor5-utils/tests/document/nodelist.js

@@ -9,7 +9,9 @@
 
 var modules = bender.amd.require(
 	'document/nodelist',
-	'document/character' );
+	'document/character',
+	'document/text',
+	'document/attribute' );
 
 describe( 'constructor', function() {
 	it( 'should change array of strings into a set of nodes', function() {
@@ -48,6 +50,27 @@ describe( 'constructor', function() {
 		expect( nodeList.length ).to.be.equal( 1 );
 		expect( nodeList.get( 0 ).character ).to.be.equal( 'x' );
 	} );
+
+	it( 'should change text with attribute into a set of nodes', function() {
+		var NodeList = modules[ 'document/nodelist' ];
+		var Text = modules[ 'document/text' ];
+		var Attribute = modules[ 'document/attribute' ];
+
+		var attrs = [ new Attribute( 'bold', true ) ];
+
+		var nodeList = new NodeList( new Text( 'foo', attrs ) );
+
+		expect( nodeList.length ).to.be.equal( 3 );
+		expect( nodeList.get( 0 ).character ).to.be.equal( 'f' );
+		expect( nodeList.get( 0 ).attrs ).to.be.deep.equal( attrs );
+		expect( nodeList.get( 0 ).attrs ).not.to.be.equal( attrs );
+		expect( nodeList.get( 1 ).character ).to.be.equal( 'o' );
+		expect( nodeList.get( 1 ).attrs ).to.be.deep.equal( attrs );
+		expect( nodeList.get( 1 ).attrs ).not.to.be.equal( attrs );
+		expect( nodeList.get( 2 ).character ).to.be.equal( 'o' );
+		expect( nodeList.get( 2 ).attrs ).to.be.deep.equal( attrs );
+		expect( nodeList.get( 2 ).attrs ).not.to.be.equal( attrs );
+	} );
 } );
 
 describe( 'insert', function() {

+ 28 - 0
packages/ckeditor5-utils/tests/document/text.js

@@ -0,0 +1,28 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* jshint expr: true */
+
+/* bender-tags: document */
+
+'use strict';
+
+var modules = bender.amd.require(
+	'document/text',
+	'document/attribute' );
+
+describe( 'constructor', function() {
+	it( 'should create character without attributes', function() {
+		var Text = modules[ 'document/text' ];
+		var Attribute = modules[ 'document/attribute' ];
+
+		var attrs = [ new Attribute( 'bold', true ) ];
+		var text = new Text( 'bar', attrs );
+
+		expect( text ).to.have.property( 'text' ).that.equals( 'bar' );
+		expect( text ).to.have.property( 'attrs' ).that.is.an( 'array' );
+		expect( text.attrs ).to.be.deep.equals( attrs );
+	} );
+} );