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

Added support for RawElement in getData(), stringify(), and parse() view utils.

Aleksander Nowodzinski 5 лет назад
Родитель
Сommit
2deb00b721

+ 15 - 5
packages/ckeditor5-engine/src/dev-utils/view.js

@@ -25,6 +25,7 @@ import AttributeElement from '../view/attributeelement';
 import ContainerElement from '../view/containerelement';
 import EmptyElement from '../view/emptyelement';
 import UIElement from '../view/uielement';
+import RawElement from '../view/rawelement';
 import { StylesProcessor } from '../view/stylesmap';
 
 const ELEMENT_RANGE_START_TOKEN = '[';
@@ -35,7 +36,8 @@ const allowedTypes = {
 	'container': ContainerElement,
 	'attribute': AttributeElement,
 	'empty': EmptyElement,
-	'ui': UIElement
+	'ui': UIElement,
+	'raw': RawElement
 };
 
 /**
@@ -55,6 +57,8 @@ const allowedTypes = {
  * (`<span id="marker:foo">`).
  * @param {Boolean} [options.renderUIElements=false] When set to `true`, the inner content of each
  * {@link module:engine/view/uielement~UIElement} will be printed.
+ * @param {Boolean} [options.renderRawElements=false] When set to `true`, the inner content of each
+ * {@link module:engine/view/rawelement~RawElement} will be printed.
  * @returns {String} The stringified data.
  */
 export function getData( view, options = {} ) {
@@ -70,6 +74,7 @@ export function getData( view, options = {} ) {
 		showType: options.showType,
 		showPriority: options.showPriority,
 		renderUIElements: options.renderUIElements,
+		renderRawElements: options.renderRawElements,
 		ignoreRoot: true
 	};
 
@@ -234,6 +239,8 @@ setData._parse = parse;
  *  `{` and `}` and the selection outside the text as `[` and `]`. When set to `false`, both will be marked as `[` and `]` only.
  * @param {Boolean} [options.renderUIElements=false] When set to `true`, the inner content of each
  * {@link module:engine/view/uielement~UIElement} will be printed.
+ * @param {Boolean} [options.renderRawElements=false] When set to `true`, the inner content of each
+ * {@link module:engine/view/rawelement~RawElement} will be printed.
  * @returns {String} An HTML-like string representing the view.
  */
 export function stringify( node, selectionOrPositionOrRange = null, options = {} ) {
@@ -622,6 +629,8 @@ class ViewStringify {
 	 * `{` and `}` and the selection outside the text as `[` and `]`. When set to `false`, both are marked as `[` and `]`.
 	 * @param {Boolean} [options.renderUIElements=false] When set to `true`, the inner content of each
 	 * {@link module:engine/view/uielement~UIElement} will be printed.
+	 * @param {Boolean} [options.renderRawElements=false] When set to `true`, the inner content of each
+	 * {@link module:engine/view/rawelement~RawElement} will be printed.
 	 */
 	constructor( root, selection, options ) {
 		this.root = root;
@@ -638,6 +647,7 @@ class ViewStringify {
 		this.ignoreRoot = !!options.ignoreRoot;
 		this.sameSelectionCharacters = !!options.sameSelectionCharacters;
 		this.renderUIElements = !!options.renderUIElements;
+		this.renderRawElements = !!options.renderRawElements;
 	}
 
 	/**
@@ -670,7 +680,7 @@ class ViewStringify {
 				callback( this._stringifyElementOpen( root ) );
 			}
 
-			if ( this.renderUIElements && root.is( 'uiElement' ) ) {
+			if ( ( this.renderUIElements && root.is( 'uiElement' ) ) || ( this.renderRawElements && root.is( 'rawElement' ) ) ) {
 				callback( root.render( document ).innerHTML );
 			} else {
 				let offset = 0;
@@ -943,10 +953,10 @@ function _convertViewElements( rootNode ) {
 		for ( const child of [ ...rootNode.getChildren() ] ) {
 			if ( convertedElement.is( 'emptyElement' ) ) {
 				throw new Error( 'Parse error - cannot parse inside EmptyElement.' );
-			}
-
-			if ( convertedElement.is( 'uiElement' ) ) {
+			} else if ( convertedElement.is( 'uiElement' ) ) {
 				throw new Error( 'Parse error - cannot parse inside UIElement.' );
+			} else if ( convertedElement.is( 'rawElement' ) ) {
+				throw new Error( 'Parse error - cannot parse inside RawElement.' );
 			}
 
 			convertedElement._appendChild( _convertViewElements( child ) );

+ 55 - 3
packages/ckeditor5-engine/tests/dev-utils/view.js

@@ -14,6 +14,7 @@ import AttributeElement from '../../src/view/attributeelement';
 import ContainerElement from '../../src/view/containerelement';
 import EmptyElement from '../../src/view/emptyelement';
 import UIElement from '../../src/view/uielement';
+import RawElement from '../../src/view/rawelement';
 import Text from '../../src/view/text';
 import DocumentSelection from '../../src/view/documentselection';
 import Range from '../../src/view/range';
@@ -404,6 +405,45 @@ describe( 'view test utils', () => {
 				.to.equal( '<container:p><ui:span><b>foo</b></ui:span></container:p>' );
 		} );
 
+		it( 'should stringify a RawElement', () => {
+			const span = new RawElement( viewDocument, 'span' );
+			const p = new ContainerElement( viewDocument, 'p', null, span );
+			expect( stringify( p, null, { showType: true } ) )
+				.to.equal( '<container:p><raw:span></raw:span></container:p>' );
+		} );
+
+		it( 'should not stringify the inner RawElement content (renderRawElements=false)', () => {
+			const span = new RawElement( viewDocument, 'span' );
+
+			span.render = function( domDocument ) {
+				const domElement = this.toDomElement( domDocument );
+
+				domElement.innerHTML = '<b>foo</b>';
+
+				return domElement;
+			};
+
+			const p = new ContainerElement( viewDocument, 'p', null, span );
+			expect( stringify( p, null, { showType: true } ) )
+				.to.equal( '<container:p><raw:span></raw:span></container:p>' );
+		} );
+
+		it( 'should stringify a RawElement, (renderRawElements=true)', () => {
+			const span = new RawElement( viewDocument, 'span' );
+
+			span.render = function( domDocument ) {
+				const domElement = this.toDomElement( domDocument );
+
+				domElement.innerHTML = '<b>foo</b>';
+
+				return domElement;
+			};
+
+			const p = new ContainerElement( viewDocument, 'p', null, span );
+			expect( stringify( p, null, { showType: true, renderRawElements: true } ) )
+				.to.equal( '<container:p><raw:span><b>foo</b></raw:span></container:p>' );
+		} );
+
 		it( 'should sort classes in specified element', () => {
 			const text = new Text( viewDocument, 'foobar' );
 			const b = new Element( viewDocument, 'b', {
@@ -714,29 +754,41 @@ describe( 'view test utils', () => {
 			expect( stringify( data ) ).to.equal( '<p><span>text</span><b>test</b></p>' );
 		} );
 
-		it( 'should parse EmptyElement', () => {
+		it( 'should parse an EmptyElement', () => {
 			const parsed = parse( '<empty:img></empty:img>' );
 
 			expect( parsed ).to.be.instanceof( EmptyElement );
 		} );
 
-		it( 'should parse UIElement', () => {
+		it( 'should parse a UIElement', () => {
 			const parsed = parse( '<ui:span></ui:span>' );
 
 			expect( parsed ).to.be.instanceof( UIElement );
 		} );
 
+		it( 'should parse a RawElement', () => {
+			const parsed = parse( '<raw:span></raw:span>' );
+
+			expect( parsed ).to.be.instanceof( RawElement );
+		} );
+
 		it( 'should throw an error if EmptyElement is not empty', () => {
 			expect( () => {
 				parse( '<empty:img>foo bar</empty:img>' );
 			} ).to.throw( Error, 'Parse error - cannot parse inside EmptyElement.' );
 		} );
 
-		it( 'should throw an error if UIElement is not empty', () => {
+		it( 'should throw an error if a UIElement is not empty', () => {
 			expect( () => {
 				parse( '<ui:span>foo bar</ui:span>' );
 			} ).to.throw( Error, 'Parse error - cannot parse inside UIElement.' );
 		} );
+
+		it( 'should throw an error if a RawElement is not empty', () => {
+			expect( () => {
+				parse( '<raw:span>foo bar</raw:span>' );
+			} ).to.throw( Error, 'Parse error - cannot parse inside RawElement.' );
+		} );
 	} );
 } );