Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit 49cd795

Browse files
authored
Merge pull request #1506 from ckeditor/t/ckeditor5-media-embed/1
Other: Made the view's `stringify()` dev util output the content of the `UIElement` (see ckeditor/ckeditor5-media-embed#1).
2 parents 2f1d8dd + 7801f21 commit 49cd795

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

src/dev-utils/view.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* @module engine/dev-utils/view
88
*/
99

10+
/* globals document */
11+
1012
/**
1113
* Collection of methods for manipulating the {@link module:engine/view/view view} for testing purposes.
1214
*/
@@ -49,6 +51,8 @@ const allowedTypes = {
4951
* (`<span view-priority="12">`, `<b view-priority="10">`).
5052
* @param {Boolean} [options.showAttributeElementId=false] When set to `true`, attribute element's id will be printed
5153
* (`<span id="marker:foo">`).
54+
* @param {Boolean} [options.renderUIElements=false] When set to `true`, the inner content of each
55+
* {@link module:engine/view/uielement~UIElement} will be printed.
5256
* @returns {String} The stringified data.
5357
*/
5458
export function getData( view, options = {} ) {
@@ -63,6 +67,7 @@ export function getData( view, options = {} ) {
6367
const stringifyOptions = {
6468
showType: options.showType,
6569
showPriority: options.showPriority,
70+
renderUIElements: options.renderUIElements,
6671
ignoreRoot: true
6772
};
6873

@@ -222,6 +227,8 @@ setData._parse = parse;
222227
* Mainly used by the `getData` function to ignore the {@link module:engine/view/document~Document document's} root element.
223228
* @param {Boolean} [options.sameSelectionCharacters=false] When set to `true`, the selection inside the text will be marked as
224229
* `{` and `}` and the selection outside the text as `[` and `]`. When set to `false`, both will be marked as `[` and `]` only.
230+
* @param {Boolean} [options.renderUIElements=false] When set to `true`, the inner content of each
231+
* {@link module:engine/view/uielement~UIElement} will be printed.
225232
* @returns {String} An HTML-like string representing the view.
226233
*/
227234
export function stringify( node, selectionOrPositionOrRange = null, options = {} ) {
@@ -605,6 +612,8 @@ class ViewStringify {
605612
* be outputted.
606613
* @param {Boolean} [options.sameSelectionCharacters=false] When set to `true`, the selection inside the text is marked as
607614
* `{` and `}` and the selection outside the text as `[` and `]`. When set to `false`, both are marked as `[` and `]`.
615+
* @param {Boolean} [options.renderUIElements=false] When set to `true`, the inner content of each
616+
* {@link module:engine/view/uielement~UIElement} will be printed.
608617
*/
609618
constructor( root, selection, options ) {
610619
this.root = root;
@@ -620,6 +629,7 @@ class ViewStringify {
620629
this.showAttributeElementId = !!options.showAttributeElementId;
621630
this.ignoreRoot = !!options.ignoreRoot;
622631
this.sameSelectionCharacters = !!options.sameSelectionCharacters;
632+
this.renderUIElements = !!options.renderUIElements;
623633
}
624634

625635
/**
@@ -652,13 +662,17 @@ class ViewStringify {
652662
callback( this._stringifyElementOpen( root ) );
653663
}
654664

655-
let offset = 0;
656-
callback( this._stringifyElementRanges( root, offset ) );
657-
658-
for ( const child of root.getChildren() ) {
659-
this._walkView( child, callback );
660-
offset++;
665+
if ( this.renderUIElements && root.is( 'uiElement' ) ) {
666+
callback( root.render( document ).innerHTML );
667+
} else {
668+
let offset = 0;
661669
callback( this._stringifyElementRanges( root, offset ) );
670+
671+
for ( const child of root.getChildren() ) {
672+
this._walkView( child, callback );
673+
offset++;
674+
callback( this._stringifyElementRanges( root, offset ) );
675+
}
662676
}
663677

664678
if ( root.is( 'element' ) && !ignore ) {

tests/dev-utils/view.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ describe( 'view test utils', () => {
3838
const stringifySpy = sandbox.spy( getData, '_stringify' );
3939
const view = new View();
4040
const viewDocument = view.document;
41-
const options = { showType: false, showPriority: false, withoutSelection: true };
41+
const options = {
42+
showType: false,
43+
showPriority: false,
44+
withoutSelection: true,
45+
renderUIElements: false
46+
};
4247
const root = createAttachedRoot( viewDocument, element );
4348
root._appendChild( new Element( 'p' ) );
4449

@@ -50,6 +55,7 @@ describe( 'view test utils', () => {
5055
expect( stringifyOptions ).to.have.property( 'showType' ).that.equals( false );
5156
expect( stringifyOptions ).to.have.property( 'showPriority' ).that.equals( false );
5257
expect( stringifyOptions ).to.have.property( 'ignoreRoot' ).that.equals( true );
58+
expect( stringifyOptions ).to.have.property( 'renderUIElements' ).that.equals( false );
5359

5460
view.destroy();
5561
} );
@@ -364,6 +370,38 @@ describe( 'view test utils', () => {
364370
.to.equal( '<container:p><ui:span></ui:span></container:p>' );
365371
} );
366372

373+
it( 'should not stringify inner UIElement content (renderUIElements=false)', () => {
374+
const span = new UIElement( 'span' );
375+
376+
span.render = function( domDocument ) {
377+
const domElement = this.toDomElement( domDocument );
378+
379+
domElement.innerHTML = '<b>foo</b>';
380+
381+
return domElement;
382+
};
383+
384+
const p = new ContainerElement( 'p', null, span );
385+
expect( stringify( p, null, { showType: true } ) )
386+
.to.equal( '<container:p><ui:span></ui:span></container:p>' );
387+
} );
388+
389+
it( 'should stringify UIElement, (renderUIElements=true)', () => {
390+
const span = new UIElement( 'span' );
391+
392+
span.render = function( domDocument ) {
393+
const domElement = this.toDomElement( domDocument );
394+
395+
domElement.innerHTML = '<b>foo</b>';
396+
397+
return domElement;
398+
};
399+
400+
const p = new ContainerElement( 'p', null, span );
401+
expect( stringify( p, null, { showType: true, renderUIElements: true } ) )
402+
.to.equal( '<container:p><ui:span><b>foo</b></ui:span></container:p>' );
403+
} );
404+
367405
it( 'should sort classes in specified element', () => {
368406
const text = new Text( 'foobar' );
369407
const b = new Element( 'b', {

0 commit comments

Comments
 (0)