8
0
Просмотр исходного кода

Added stringify() method to model test utils.

Szymon Kupś 9 лет назад
Родитель
Сommit
d0dd9866d2

+ 19 - 1
packages/ckeditor5-engine/tests/_utils-tests/model.js

@@ -5,8 +5,9 @@
 
 
 'use strict';
 'use strict';
 
 
-import { getData, setData } from '/tests/engine/_utils/model.js';
+import { stringify, getData, setData } from '/tests/engine/_utils/model.js';
 import Document from '/ckeditor5/engine/treemodel/document.js';
 import Document from '/ckeditor5/engine/treemodel/document.js';
+import DocumentFragment from '/ckeditor5/engine/treemodel/documentfragment.js';
 import Element from '/ckeditor5/engine/treemodel/element.js';
 import Element from '/ckeditor5/engine/treemodel/element.js';
 import Text from '/ckeditor5/engine/treemodel/text.js';
 import Text from '/ckeditor5/engine/treemodel/text.js';
 import Range from '/ckeditor5/engine/treemodel/range.js';
 import Range from '/ckeditor5/engine/treemodel/range.js';
@@ -22,6 +23,23 @@ describe( 'model test utils', () => {
 		selection.removeAllRanges();
 		selection.removeAllRanges();
 	} );
 	} );
 
 
+	describe( 'stringify', () => {
+		it( 'should stringify text', () => {
+			const text = new Text( 'text', { underline: true, bold: true } );
+			expect( stringify( text ) ).to.equal( '<$text bold=true underline=true>text</$text>' );
+		} );
+
+		it( 'should stringify element', () => {
+			const element = new Element( 'a', null, [ new Element( 'b', null, 'btext' ), 'atext' ] );
+			expect( stringify( element ) ).to.equal( '<a><b>btext</b>atext</a>' );
+		} );
+
+		it( 'should stringify document fragment', () => {
+			const fragment = new DocumentFragment( [ new Element( 'b', null, 'btext' ), 'atext' ] );
+			expect( stringify( fragment ) ).to.equal( '<b>btext</b>atext' );
+		} );
+	} );
+
 	describe( 'getData', () => {
 	describe( 'getData', () => {
 		it( 'writes elements and texts', () => {
 		it( 'writes elements and texts', () => {
 			root.appendChildren( [
 			root.appendChildren( [

+ 46 - 17
packages/ckeditor5-engine/tests/_utils/model.js

@@ -9,45 +9,74 @@ import TreeWalker from '/ckeditor5/engine/treemodel/treewalker.js';
 import Range from '/ckeditor5/engine/treemodel/range.js';
 import Range from '/ckeditor5/engine/treemodel/range.js';
 import Position from '/ckeditor5/engine/treemodel/position.js';
 import Position from '/ckeditor5/engine/treemodel/position.js';
 import Text from '/ckeditor5/engine/treemodel/text.js';
 import Text from '/ckeditor5/engine/treemodel/text.js';
+import RootElement from '/ckeditor5/engine/treemodel/rootelement.js';
 import Element from '/ckeditor5/engine/treemodel/element.js';
 import Element from '/ckeditor5/engine/treemodel/element.js';
+import DocumentFragment from '/ckeditor5/engine/treemodel/documentfragment.js';
+import Selection from '/ckeditor5/engine/treemodel/selection.js';
+
+export function stringify( root, selectionOrPositionOrRange ) {
+	let selection;
+
+	// If root is Element or Text - wrap it with DocumentFragment.
+	if ( !( root instanceof RootElement ) && ( root instanceof Element || root instanceof Text ) ) {
+		root = new DocumentFragment( root );
+	}
 
 
-/**
- * Writes the contents of the document to an HTML-like string.
- *
- * @param {engine.treeModel.Document} document
- * @param {String} rootName
- * @param {Object} [options]
- * @param {Boolean} [options.selection] Whether to write the selection.
- * @returns {String} The stringified data.
- */
-export function getData( document, rootName, options ) {
-	const root = document.getRoot( rootName );
 	const walker = new TreeWalker( {
 	const walker = new TreeWalker( {
 		boundaries: Range.createFromElement( root )
 		boundaries: Range.createFromElement( root )
 	} );
 	} );
+
+	if ( selectionOrPositionOrRange instanceof Selection ) {
+		selection = selectionOrPositionOrRange;
+	} else if ( selectionOrPositionOrRange instanceof Range ) {
+		selection = new Selection( new Document() );
+		selection.addRange( selectionOrPositionOrRange );
+	} else if ( selectionOrPositionOrRange instanceof Position ) {
+		selection = new Selection( new Document() );
+		selection.addRange( new Range( selectionOrPositionOrRange, selectionOrPositionOrRange ) );
+	}
+
 	let ret = '';
 	let ret = '';
 	let lastPosition = Position.createFromParentAndOffset( root, 0 );
 	let lastPosition = Position.createFromParentAndOffset( root, 0 );
-	const selection = document.selection;
-
-	options = options || {};
+	const withSelection = !!selection;
 
 
 	for ( let value of walker ) {
 	for ( let value of walker ) {
-		if ( options.selection ) {
+		if ( withSelection ) {
 			ret += writeSelection( value.previousPosition, selection );
 			ret += writeSelection( value.previousPosition, selection );
 		}
 		}
 
 
-		ret += writeItem( value, selection, options );
+		ret += writeItem( value, selection, { selection: withSelection } );
 
 
 		lastPosition = value.nextPosition;
 		lastPosition = value.nextPosition;
 	}
 	}
 
 
-	if ( options.selection ) {
+	if ( withSelection ) {
 		ret += writeSelection( lastPosition, selection );
 		ret += writeSelection( lastPosition, selection );
 	}
 	}
 
 
 	return ret;
 	return ret;
 }
 }
 
 
+/**
+ * Writes the contents of the document to an HTML-like string.
+ *
+ * @param {engine.treeModel.Document} document
+ * @param {String} rootName
+ * @param {Object} [options]
+ * @param {Boolean} [options.selection] Whether to write the selection.
+ * @returns {String} The stringified data.
+ */
+export function getData( document, rootName, options ) {
+	options = options || {};
+	const root = document.getRoot( rootName );
+
+	if ( options.selection ) {
+		return stringify( root, document.selection );
+	} else {
+		return stringify( root );
+	}
+}
+
 /**
 /**
  * Sets the contents of the model and the selection in it.
  * Sets the contents of the model and the selection in it.
  *
  *