Skip to content

Commit

Permalink
Merge pull request #10620 from ckeditor/ck/10619-dataprocessor-public…
Browse files Browse the repository at this point in the history
…-properties

Other (engine): Made properties of `XmlDataProcessor` and `HtmlDataProcessor` public to allow overriding e.g. the HTML writer. Closes #10619.
  • Loading branch information
Reinmar authored Sep 30, 2021
2 parents 7274202 + 916ca89 commit b097349
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 32 deletions.
23 changes: 10 additions & 13 deletions packages/ckeditor5-engine/src/dataprocessor/htmldataprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,23 @@ export default class HtmlDataProcessor {
/**
* A DOM parser instance used to parse an HTML string to an HTML document.
*
* @private
* @member {DOMParser}
*/
this._domParser = new DOMParser();
this.domParser = new DOMParser();

/**
* A DOM converter used to convert DOM elements to view elements.
*
* @private
* @member {module:engine/view/domconverter~DomConverter}
*/
this._domConverter = new DomConverter( document, { renderingMode: 'data' } );
this.domConverter = new DomConverter( document, { renderingMode: 'data' } );

/**
* A basic HTML writer instance used to convert DOM elements to an HTML string.
*
* @private
* @member {module:engine/dataprocessor/basichtmlwriter~BasicHtmlWriter}
* @member {module:engine/dataprocessor/htmlwriter~HtmlWriter}
*/
this._htmlWriter = new BasicHtmlWriter();
this.htmlWriter = new BasicHtmlWriter();
}

/**
Expand All @@ -59,10 +56,10 @@ export default class HtmlDataProcessor {
*/
toData( viewFragment ) {
// Convert view DocumentFragment to DOM DocumentFragment.
const domFragment = this._domConverter.viewToDom( viewFragment, document );
const domFragment = this.domConverter.viewToDom( viewFragment, document );

// Convert DOM DocumentFragment to HTML output.
return this._htmlWriter.getHtml( domFragment );
return this.htmlWriter.getHtml( domFragment );
}

/**
Expand All @@ -76,7 +73,7 @@ export default class HtmlDataProcessor {
const domFragment = this._toDom( data );

// Convert DOM DocumentFragment to view DocumentFragment.
return this._domConverter.domToView( domFragment );
return this.domConverter.domToView( domFragment );
}

/**
Expand All @@ -90,7 +87,7 @@ export default class HtmlDataProcessor {
* be treated as raw data.
*/
registerRawContentMatcher( pattern ) {
this._domConverter.registerRawContentMatcher( pattern );
this.domConverter.registerRawContentMatcher( pattern );
}

/**
Expand All @@ -105,7 +102,7 @@ export default class HtmlDataProcessor {
* @param {'default'|'marked'} type Whether to use the default or the marked ` ` block fillers.
*/
useFillerType( type ) {
this._domConverter.blockFillerMode = type == 'marked' ? 'markedNbsp' : 'nbsp';
this.domConverter.blockFillerMode = type == 'marked' ? 'markedNbsp' : 'nbsp';
}

/**
Expand All @@ -117,7 +114,7 @@ export default class HtmlDataProcessor {
* @returns {DocumentFragment}
*/
_toDom( data ) {
const document = this._domParser.parseFromString( data, 'text/html' );
const document = this.domParser.parseFromString( data, 'text/html' );
const fragment = document.createDocumentFragment();

// The rules for parsing an HTML string can be read on https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inhtml.
Expand Down
28 changes: 12 additions & 16 deletions packages/ckeditor5-engine/src/dataprocessor/xmldataprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class XmlDataProcessor {
*
* @param {module:engine/view/document~Document} document The view document instance.
* @param {Object} options Configuration options.
* @param {Array<String>} [options.namespaces=[]] A list of namespaces allowed to use in the XML input.
* @param {Array.<String>} [options.namespaces=[]] A list of namespaces allowed to use in the XML input.
*/
constructor( document, options = {} ) {
/**
Expand All @@ -35,35 +35,31 @@ export default class XmlDataProcessor {
* For example, registering namespaces [ 'attribute', 'container' ] allows to use `<attirbute:tagName></attribute:tagName>`
* and `<container:tagName></container:tagName>` input. It is mainly for debugging.
*
* @public
* @member {DOMParser}
* @member {Array.<String>}
*/
this.namespaces = options.namespaces || [];

/**
* DOM parser instance used to parse an XML string to an XML document.
*
* @private
* @member {DOMParser}
*/
this._domParser = new DOMParser();
this.domParser = new DOMParser();

/**
* DOM converter used to convert DOM elements to view elements.
*
* @private
* @member {module:engine/view/domconverter~DomConverter}
*/
this._domConverter = new DomConverter( document, { renderingMode: 'data' } );
this.domConverter = new DomConverter( document, { renderingMode: 'data' } );

/**
* A basic HTML writer instance used to convert DOM elements to an XML string.
* There is no need to use a dedicated XML writer because the basic HTML writer works well in this case.
*
* @private
* @member {module:engine/dataprocessor/basichtmlwriter~BasicHtmlWriter}
* @member {module:engine/dataprocessor/htmlwriter~HtmlWriter}
*/
this._htmlWriter = new BasicHtmlWriter();
this.htmlWriter = new BasicHtmlWriter();
}

/**
Expand All @@ -75,11 +71,11 @@ export default class XmlDataProcessor {
*/
toData( viewFragment ) {
// Convert view DocumentFragment to DOM DocumentFragment.
const domFragment = this._domConverter.viewToDom( viewFragment, document );
const domFragment = this.domConverter.viewToDom( viewFragment, document );

// Convert DOM DocumentFragment to XML output.
// There is no need to use dedicated for XML serializing method because BasicHtmlWriter works well in this case.
return this._htmlWriter.getHtml( domFragment );
return this.htmlWriter.getHtml( domFragment );
}

/**
Expand All @@ -93,7 +89,7 @@ export default class XmlDataProcessor {
const domFragment = this._toDom( data );

// Convert DOM DocumentFragment to view DocumentFragment.
return this._domConverter.domToView( domFragment, { keepOriginalCase: true } );
return this.domConverter.domToView( domFragment, { keepOriginalCase: true } );
}

/**
Expand All @@ -107,7 +103,7 @@ export default class XmlDataProcessor {
* be treated as raw data.
*/
registerRawContentMatcher( pattern ) {
this._domConverter.registerRawContentMatcher( pattern );
this.domConverter.registerRawContentMatcher( pattern );
}

/**
Expand All @@ -122,7 +118,7 @@ export default class XmlDataProcessor {
* @param {'default'|'marked'} type Whether to use the default or the marked `&nbsp;` block fillers.
*/
useFillerType( type ) {
this._domConverter.blockFillerMode = type == 'marked' ? 'markedNbsp' : 'nbsp';
this.domConverter.blockFillerMode = type == 'marked' ? 'markedNbsp' : 'nbsp';
}

/**
Expand All @@ -140,7 +136,7 @@ export default class XmlDataProcessor {
// Wrap data into root element with optional namespace definitions.
data = `<xml ${ namespaces }>${ data }</xml>`;

const parsedDocument = this._domParser.parseFromString( data, 'text/xml' );
const parsedDocument = this.domParser.parseFromString( data, 'text/xml' );

// Parse validation.
const parserError = parsedDocument.querySelector( 'parsererror' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

/* globals setTimeout, window, Node */
/* globals setTimeout, window, Node, DOMParser */

import HtmlDataProcessor from '../../src/dataprocessor/htmldataprocessor';
import BasicHtmlWriter from '../../src/dataprocessor/basichtmlwriter';
import DomConverter from '../../src//view/domconverter';
import xssTemplates from '../../tests/dataprocessor/_utils/xsstemplates';
import ViewDocumentFragment from '../../src/view/documentfragment';
import { stringify, parse } from '../../src/dev-utils/view';
Expand All @@ -20,6 +22,18 @@ describe( 'HtmlDataProcessor', () => {
dataProcessor = new HtmlDataProcessor( viewDocument );
} );

describe( 'constructor', () => {
it( 'should set public properties', () => {
expect( dataProcessor ).to.have.property( 'domParser' );
expect( dataProcessor ).to.have.property( 'domConverter' );
expect( dataProcessor ).to.have.property( 'htmlWriter' );

expect( dataProcessor.domParser ).to.be.an.instanceOf( DOMParser );
expect( dataProcessor.domConverter ).to.be.an.instanceOf( DomConverter );
expect( dataProcessor.htmlWriter ).to.be.an.instanceOf( BasicHtmlWriter );
} );
} );

describe( 'toView()', () => {
it( 'should return empty DocumentFragment when empty string is passed', () => {
const fragment = dataProcessor.toView( '' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

/* globals window */
/* globals window, DOMParser */

import XmlDataProcessor from '../../src/dataprocessor/xmldataprocessor';
import BasicHtmlWriter from '../../src/dataprocessor/basichtmlwriter';
import DomConverter from '../../src//view/domconverter';
import xssTemplates from '../../tests/dataprocessor/_utils/xsstemplates';
import ViewDocumentFragment from '../../src/view/documentfragment';
import ViewDocument from '../../src/view/document';
Expand All @@ -20,6 +22,20 @@ describe( 'XmlDataProcessor', () => {
dataProcessor = new XmlDataProcessor( viewDocument );
} );

describe( 'constructor', () => {
it( 'should set public properties', () => {
expect( dataProcessor ).to.have.property( 'namespaces' );
expect( dataProcessor ).to.have.property( 'domParser' );
expect( dataProcessor ).to.have.property( 'domConverter' );
expect( dataProcessor ).to.have.property( 'htmlWriter' );

expect( dataProcessor.namespaces ).to.be.an.instanceOf( Array );
expect( dataProcessor.domParser ).to.be.an.instanceOf( DOMParser );
expect( dataProcessor.domConverter ).to.be.an.instanceOf( DomConverter );
expect( dataProcessor.htmlWriter ).to.be.an.instanceOf( BasicHtmlWriter );
} );
} );

describe( 'toView', () => {
it( 'should return empty DocumentFragment when empty string is passed', () => {
const fragment = dataProcessor.toView( '' );
Expand Down
2 changes: 1 addition & 1 deletion packages/ckeditor5-utils/tests/_utils/normalizehtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Document from '@ckeditor/ckeditor5-engine/src/view/document';
export default function normalizeHtml( html, options = {} ) {
const processor = new HtmlDataProcessor( new Document( new StylesProcessor() ) );
const domFragment = processor._toDom( html );
const viewFragment = processor._domConverter.domToView( domFragment, options );
const viewFragment = processor.domConverter.domToView( domFragment, options );

return stringify( viewFragment );
}

0 comments on commit b097349

Please sign in to comment.