8
0
Эх сурвалжийг харах

Added changed view's getData/setData.

Szymon Kupś 9 жил өмнө
parent
commit
1c5c65a4f8

+ 5 - 4
packages/ckeditor5-engine/tests/_utils/model.js

@@ -16,7 +16,7 @@ import Selection from '/ckeditor5/engine/treemodel/selection.js';
 import Document from '/ckeditor5/engine/treemodel/document.js';
 import Document from '/ckeditor5/engine/treemodel/document.js';
 
 
 /**
 /**
- * Writes the contents of the document to an HTML-like string.
+ * Writes the contents of the {@link engine.treeModel.Document Document} to an HTML-like string.
  *
  *
  * @param {engine.treeModel.Document} document
  * @param {engine.treeModel.Document} document
  * @param {Object} [options]
  * @param {Object} [options]
@@ -34,10 +34,10 @@ export function getData( document, options = {} ) {
 }
 }
 
 
 /**
 /**
- * Sets the contents of the document model provided as HTML-like string.
+ * Sets the contents of the {@link engine.treeModel.Document Document} provided as HTML-like string.
  *
  *
  * @param {engine.treeModel.Document} document
  * @param {engine.treeModel.Document} document
- * @param {String} data HTML-like string to write into document.
+ * @param {String} data HTML-like string to write into Document.
  * @param {Object} options
  * @param {Object} options
  * @param {String} [rootName] Root name where parsed data will be stored. If not provided, default `main` name will be
  * @param {String} [rootName] Root name where parsed data will be stored. If not provided, default `main` name will be
  * used.
  * used.
@@ -51,6 +51,7 @@ export function setData( document, data, options = {} ) {
 
 
 /**
 /**
  * Converts model nodes to HTML-like string representation.
  * Converts model nodes to HTML-like string representation.
+ *
  * @param {engine.treeModel.RootElement|engine.treeModel.Element|engine.treeModel.Text|
  * @param {engine.treeModel.RootElement|engine.treeModel.Element|engine.treeModel.Text|
  * engine.treeModel.DocumentFragment} node Node to stringify.
  * engine.treeModel.DocumentFragment} node Node to stringify.
  * @param {engine.treeModel.Selection|engine.treeModel.Position|engine.treeModel.Range} [selectionOrPositionOrRange = null ]
  * @param {engine.treeModel.Selection|engine.treeModel.Position|engine.treeModel.Range} [selectionOrPositionOrRange = null ]
@@ -108,7 +109,7 @@ export function stringify( node, selectionOrPositionOrRange = null ) {
 }
 }
 
 
 /**
 /**
- * Parses HTML-like string and returns model {engine.treeModel.RootElement rootElement}.
+ * Parses HTML-like string and returns model {@link engine.treeModel.RootElement rootElement}.
  *
  *
  * @param {String} data HTML-like string to be parsed.
  * @param {String} data HTML-like string to be parsed.
  * @param {Object} options
  * @param {Object} options

+ 52 - 25
packages/ckeditor5-engine/tests/_utils/view.js

@@ -23,6 +23,58 @@ const ELEMENT_RANGE_END_TOKEN = ']';
 const TEXT_RANGE_START_TOKEN = '{';
 const TEXT_RANGE_START_TOKEN = '{';
 const TEXT_RANGE_END_TOKEN = '}';
 const TEXT_RANGE_END_TOKEN = '}';
 
 
+/**
+ * Writes the contents of the {@link engine.treeView.TreeView TreeView} to an HTML-like string.
+ *
+ * @param {engine.treeView.TreeView} treeView
+ * @param {Object} [options]
+ * @param {Boolean} [options.withSelection] Whether to write the selection.
+ * @param {Boolean} [options.rootName='main'] Name of the root from which data should be stringified. If not provided
+ * default `main` name will be used.
+ * @param {Boolean} [options.showType=false] When set to `true` type of elements will be printed (`<container:p>`
+ * instead of `<p>` and `<attribute:b>` instead of `<b>`).
+ * @param {Boolean} [options.showPriority=false] When set to `true` AttributeElement's priority will be printed
+ * (`<span:12>`, `<b:10>`).
+ * @returns {String} The stringified data.
+ */
+export function getData( treeView, options ) {
+	const withSelection = !!options.withSelection;
+	const rootName = options.rootName || 'main';
+	const root = treeView.getRoot( rootName );
+
+	return withSelection ? stringify( root, treeView.selection, options ) : stringify( root, null, options );
+}
+
+/**
+ * Sets the contents of the {@link engine.treeView.TreeView TreeView} provided as HTML-like string.
+ *
+ * @param {engine.treeView.TreeView} treeView
+ * @param {String} data HTML-like string to write into TreeView.
+ * @param {Object} options
+ * @param {String} [rootName] Root name where parsed data will be stored. If not provided, default `main` name will be
+ * used.
+ */
+export function setData( treeView, data, options = {} ) {
+	let view, selection;
+	const rootName = options.rootName || 'main';
+	const result = parse( data );
+
+	if ( result.view && result.selection ) {
+		selection = result.selection;
+		view = result.view;
+	} else {
+		view = result;
+	}
+
+	const root = treeView.getRoot( rootName );
+	root.removeChildren( 0, root.getChildCount() );
+	root.appendChildren( view instanceof DocumentFragment ? view.getChildren() : view );
+
+	if ( selection ) {
+		treeView.selection.setTo( selection );
+	}
+}
+
 /**
 /**
  * Converts view elements to HTML-like string representation.
  * Converts view elements to HTML-like string representation.
  * Root element can be provided as {@link engine.treeView.Text Text}:
  * Root element can be provided as {@link engine.treeView.Text Text}:
@@ -844,28 +896,3 @@ class ViewStringify {
 		return attributes.join( ' ' );
 		return attributes.join( ' ' );
 	}
 	}
 }
 }
-
-export function setData( treeView, data ) {
-	let view, selection;
-
-	const result = parse( data );
-
-	if ( result.view && result.selection ) {
-		selection = result.selection;
-		view = result.view;
-	} else {
-		view = result;
-	}
-
-	const root = treeView.getRoot();
-	root.removeChildren( 0, root.getChildCount() );
-	root.appendChildren( view );
-
-	if ( selection ) {
-		treeView.selection.setTo( selection );
-	}
-}
-
-export function getData( treeView ) {
-	return stringify( treeView.getRoot(), treeView.selection );
-}