|
@@ -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.
|
|
|
*
|
|
*
|