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

t/ckeditor5-media-embed/1: Made the view stringify util output the content of the UI element #1506

Merged
merged 5 commits into from
Aug 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/dev-utils/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @module engine/dev-utils/view
*/

/* globals document */

/**
* Collection of methods for manipulating the {@link module:engine/view/view view} for testing purposes.
*/
Expand Down Expand Up @@ -49,6 +51,8 @@ const allowedTypes = {
* (`<span view-priority="12">`, `<b view-priority="10">`).
* @param {Boolean} [options.showAttributeElementId=false] When set to `true`, attribute element's id will be printed
* (`<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.
* @returns {String} The stringified data.
*/
export function getData( view, options = {} ) {
Expand All @@ -63,6 +67,7 @@ export function getData( view, options = {} ) {
const stringifyOptions = {
showType: options.showType,
showPriority: options.showPriority,
renderUIElements: options.renderUIElements,
ignoreRoot: true
};

Expand Down Expand Up @@ -222,6 +227,8 @@ setData._parse = parse;
* Mainly used by the `getData` function to ignore the {@link module:engine/view/document~Document document's} root element.
* @param {Boolean} [options.sameSelectionCharacters=false] When set to `true`, the selection inside the text will be marked as
* `{` 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.
* @returns {String} An HTML-like string representing the view.
*/
export function stringify( node, selectionOrPositionOrRange = null, options = {} ) {
Expand Down Expand Up @@ -605,6 +612,8 @@ class ViewStringify {
* be outputted.
* @param {Boolean} [options.sameSelectionCharacters=false] When set to `true`, the selection inside the text is marked as
* `{` 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.
*/
constructor( root, selection, options ) {
this.root = root;
Expand All @@ -620,6 +629,7 @@ class ViewStringify {
this.showAttributeElementId = !!options.showAttributeElementId;
this.ignoreRoot = !!options.ignoreRoot;
this.sameSelectionCharacters = !!options.sameSelectionCharacters;
this.renderUIElements = !!options.renderUIElements;
}

/**
Expand Down Expand Up @@ -652,13 +662,17 @@ class ViewStringify {
callback( this._stringifyElementOpen( root ) );
}

let offset = 0;
callback( this._stringifyElementRanges( root, offset ) );

for ( const child of root.getChildren() ) {
this._walkView( child, callback );
offset++;
if ( this.renderUIElements && root.is( 'uiElement' ) ) {
callback( root.render( document ).innerHTML );
} else {
let offset = 0;
callback( this._stringifyElementRanges( root, offset ) );

for ( const child of root.getChildren() ) {
this._walkView( child, callback );
offset++;
callback( this._stringifyElementRanges( root, offset ) );
}
}

if ( root.is( 'element' ) && !ignore ) {
Expand Down
40 changes: 39 additions & 1 deletion tests/dev-utils/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ describe( 'view test utils', () => {
const stringifySpy = sandbox.spy( getData, '_stringify' );
const view = new View();
const viewDocument = view.document;
const options = { showType: false, showPriority: false, withoutSelection: true };
const options = {
showType: false,
showPriority: false,
withoutSelection: true,
renderUIElements: false
};
const root = createAttachedRoot( viewDocument, element );
root._appendChild( new Element( 'p' ) );

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

view.destroy();
} );
Expand Down Expand Up @@ -364,6 +370,38 @@ describe( 'view test utils', () => {
.to.equal( '<container:p><ui:span></ui:span></container:p>' );
} );

it( 'should not stringify inner UIElement content (renderUIElements=false)', () => {
const span = new UIElement( 'span' );

span.render = function( domDocument ) {
const domElement = this.toDomElement( domDocument );

domElement.innerHTML = '<b>foo</b>';

return domElement;
};

const p = new ContainerElement( 'p', null, span );
expect( stringify( p, null, { showType: true } ) )
.to.equal( '<container:p><ui:span></ui:span></container:p>' );
} );

it( 'should stringify UIElement, (renderUIElements=true)', () => {
const span = new UIElement( 'span' );

span.render = function( domDocument ) {
const domElement = this.toDomElement( domDocument );

domElement.innerHTML = '<b>foo</b>';

return domElement;
};

const p = new ContainerElement( 'p', null, span );
expect( stringify( p, null, { showType: true, renderUIElements: true } ) )
.to.equal( '<container:p><ui:span><b>foo</b></ui:span></container:p>' );
} );

it( 'should sort classes in specified element', () => {
const text = new Text( 'foobar' );
const b = new Element( 'b', {
Expand Down