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

Merge pull request #92 from ckeditor/i/6091

Internal: Adjusted the code to changes required for replacing the `StylesProcessor` singleton with an instance of the class. See ckeditor/ckeditor5#6091.
Piotrek Koszuliński 5 лет назад
Родитель
Сommit
e7868f921e

+ 2 - 3
packages/ckeditor5-paste-from-office/src/filters/list.js

@@ -7,7 +7,6 @@
  * @module paste-from-office/filters/list
  */
 
-import Element from '@ckeditor/ckeditor5-engine/src/view/element';
 import Matcher from '@ckeditor/ckeditor5-engine/src/view/matcher';
 import UpcastWriter from '@ckeditor/ckeditor5-engine/src/view/upcastwriter';
 
@@ -27,7 +26,7 @@ export function transformListItemLikeElementsIntoLists( documentFragment, styles
 		return;
 	}
 
-	const writer = new UpcastWriter();
+	const writer = new UpcastWriter( documentFragment.document );
 	const itemLikeElements = findAllItemLikeElements( documentFragment, writer );
 
 	if ( !itemLikeElements.length ) {
@@ -160,7 +159,7 @@ function detectListStyle( listLikeItem, stylesString ) {
 // @param {module:engine/view/upcastwriter~UpcastWriter} writer
 // @returns {module:engine/view/element~Element} Newly created list element.
 function insertNewEmptyList( listStyle, element, writer ) {
-	const list = new Element( listStyle.type );
+	const list = writer.createElement( listStyle.type );
 	const position = element.parent.getChildIndex( element );
 
 	writer.insertChild( position, list, element.parent );

+ 4 - 1
packages/ckeditor5-paste-from-office/src/filters/parse.js

@@ -10,8 +10,10 @@
 /* globals DOMParser */
 
 import DomConverter from '@ckeditor/ckeditor5-engine/src/view/domconverter';
+import ViewDocument from '@ckeditor/ckeditor5-engine/src/view/document';
 
 import { normalizeSpacing, normalizeSpacerunSpans } from './space';
+import { StylesProcessor } from '@ckeditor/ckeditor5-engine/src/view/stylesmap';
 
 /**
  * Parses provided HTML extracting contents of `<body>` and `<style>` tags.
@@ -60,7 +62,8 @@ export function parseHtml( htmlString ) {
 // @param {Document} htmlDocument Native `Document` object to be transformed.
 // @returns {module:engine/view/documentfragment~DocumentFragment}
 function documentToView( htmlDocument ) {
-	const domConverter = new DomConverter( { blockFillerMode: 'nbsp' } );
+	const viewDocument = new ViewDocument( new StylesProcessor() );
+	const domConverter = new DomConverter( viewDocument, { blockFillerMode: 'nbsp' } );
 	const fragment = htmlDocument.createDocumentFragment();
 	const nodes = htmlDocument.body.childNodes;
 

+ 9 - 1
packages/ckeditor5-paste-from-office/src/normalizers/googledocsnormalizer.js

@@ -19,6 +19,14 @@ const googleDocsMatch = /id=("|')docs-internal-guid-[-0-9a-f]+("|')/i;
  * @implements module:paste-from-office/normalizer~Normalizer
  */
 export default class GoogleDocsNormalizer {
+	constructor( document ) {
+		/**
+		 * @readonly
+		 * @type {module:engine/view/document~Document}
+		 */
+		this.document = document;
+	}
+
 	/**
 	 * @inheritDoc
 	 */
@@ -30,7 +38,7 @@ export default class GoogleDocsNormalizer {
 	 * @inheritDoc
 	 */
 	execute( data ) {
-		const writer = new UpcastWriter();
+		const writer = new UpcastWriter( this.document );
 
 		removeBoldWrapper( data.content, writer );
 		unwrapParagraphInListItem( data.content, writer );

+ 1 - 1
packages/ckeditor5-paste-from-office/src/pastefromoffice.js

@@ -51,7 +51,7 @@ export default class PasteFromOffice extends Plugin {
 		const normalizers = [];
 
 		normalizers.push( new MSWordNormalizer() );
-		normalizers.push( new GoogleDocsNormalizer() );
+		normalizers.push( new GoogleDocsNormalizer( editor.editing.view.document ) );
 
 		editor.plugins.get( 'Clipboard' ).on(
 			'inputTransformation',

+ 3 - 1
packages/ckeditor5-paste-from-office/tests/_utils/utils.js

@@ -7,6 +7,7 @@
 
 import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
 import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
+import ViewDocument from '@ckeditor/ckeditor5-engine/src/view/document';
 
 import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
 import normalizeClipboardData from '@ckeditor/ckeditor5-clipboard/src/utils/normalizeclipboarddata';
@@ -16,8 +17,9 @@ import { stringify as stringifyView } from '@ckeditor/ckeditor5-engine/src/dev-u
 import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 
 import { fixtures, browserFixtures } from './fixtures';
+import { StylesProcessor } from '@ckeditor/ckeditor5-engine/src/view/stylesmap';
 
-const htmlDataProcessor = new HtmlDataProcessor();
+const htmlDataProcessor = new HtmlDataProcessor( new ViewDocument( new StylesProcessor() ) );
 
 /**
  * Mocks dataTransfer object which can be used for simulating paste.

+ 14 - 12
packages/ckeditor5-paste-from-office/tests/filters/list.js

@@ -5,24 +5,25 @@
 
 import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
 import { stringify } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
-import View from '@ckeditor/ckeditor5-engine/src/view/view';
+import Document from '@ckeditor/ckeditor5-engine/src/view/document';
 import UpcastWriter from '@ckeditor/ckeditor5-engine/src/view/upcastwriter';
 
 import {
 	transformListItemLikeElementsIntoLists,
 	unwrapParagraphInListItem
 } from '../../src/filters/list';
+import { StylesProcessor } from '@ckeditor/ckeditor5-engine/src/view/stylesmap';
 
 describe( 'PasteFromOffice - filters', () => {
 	describe( 'list - paste from MS Word', () => {
-		const htmlDataProcessor = new HtmlDataProcessor();
+		const htmlDataProcessor = new HtmlDataProcessor( new Document( new StylesProcessor() ) );
 
 		describe( 'transformListItemLikeElementsIntoLists()', () => {
 			it( 'replaces list-like elements with semantic lists', () => {
 				const html = '<p style="mso-list:l0 level1 lfo0"><span style="mso-list:Ignore">1.</span>Item 1</p>';
 				const view = htmlDataProcessor.toView( html );
 
-				transformListItemLikeElementsIntoLists( view, '', new View() );
+				transformListItemLikeElementsIntoLists( view, '' );
 
 				expect( view.childCount ).to.equal( 1 );
 				expect( view.getChild( 0 ).name ).to.equal( 'ol' );
@@ -33,7 +34,7 @@ describe( 'PasteFromOffice - filters', () => {
 				const html = '<p style="mso-list:l0 level1 lfo0"><span style="mso-list:Ignore">1.</span>Item 1</p>';
 				const view = htmlDataProcessor.toView( html );
 
-				transformListItemLikeElementsIntoLists( view, '@list l0:level1 { mso-level-number-format: bullet; }', new View() );
+				transformListItemLikeElementsIntoLists( view, '@list l0:level1 { mso-level-number-format: bullet; }' );
 
 				expect( view.childCount ).to.equal( 1 );
 				expect( view.getChild( 0 ).name ).to.equal( 'ul' );
@@ -44,7 +45,7 @@ describe( 'PasteFromOffice - filters', () => {
 				const html = '<h1>H1</h1><p>Foo Bar</p>';
 				const view = htmlDataProcessor.toView( html );
 
-				transformListItemLikeElementsIntoLists( view, '', new View() );
+				transformListItemLikeElementsIntoLists( view, '' );
 
 				expect( view.childCount ).to.equal( 2 );
 				expect( stringify( view ) ).to.equal( html );
@@ -54,7 +55,7 @@ describe( 'PasteFromOffice - filters', () => {
 				const html = '<p style="mso-list:"><span style="mso-list:Ignore">1.</span>Item 1</p>';
 				const view = htmlDataProcessor.toView( html );
 
-				transformListItemLikeElementsIntoLists( view, '', new View() );
+				transformListItemLikeElementsIntoLists( view, '' );
 
 				expect( view.childCount ).to.equal( 1 );
 				expect( view.getChild( 0 ).name ).to.equal( 'ol' );
@@ -65,7 +66,7 @@ describe( 'PasteFromOffice - filters', () => {
 				const html = '<p style="mso-list:none">not numbered<o:p></o:p></p>';
 				const view = htmlDataProcessor.toView( html );
 
-				transformListItemLikeElementsIntoLists( view, '', new View() );
+				transformListItemLikeElementsIntoLists( view, '' );
 
 				expect( view.childCount ).to.equal( 1 );
 				expect( view.getChild( 0 ).name ).to.equal( 'ol' );
@@ -75,7 +76,7 @@ describe( 'PasteFromOffice - filters', () => {
 			it( 'handles empty body correctly', () => {
 				const view = htmlDataProcessor.toView( '' );
 
-				transformListItemLikeElementsIntoLists( view, '', new View() );
+				transformListItemLikeElementsIntoLists( view, '' );
 
 				expect( view.childCount ).to.equal( 0 );
 				expect( stringify( view ) ).to.equal( '' );
@@ -84,11 +85,12 @@ describe( 'PasteFromOffice - filters', () => {
 	} );
 
 	describe( 'list - paste from google docs', () => {
-		const htmlDataProcessor = new HtmlDataProcessor();
-		let writer;
+		let writer, viewDocument, htmlDataProcessor;
 
-		before( () => {
-			writer = new UpcastWriter();
+		beforeEach( () => {
+			viewDocument = new Document( new StylesProcessor() );
+			writer = new UpcastWriter( viewDocument );
+			htmlDataProcessor = new HtmlDataProcessor( viewDocument );
 		} );
 
 		describe( 'unwrapParagraphInListItem', () => {

+ 6 - 3
packages/ckeditor5-paste-from-office/tests/filters/reomoveboldwrapper.js

@@ -6,14 +6,17 @@
 import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
 import removeBoldWrapper from '../../src/filters/removeboldwrapper';
 import UpcastWriter from '@ckeditor/ckeditor5-engine/src/view/upcastwriter';
+import Document from '@ckeditor/ckeditor5-engine/src/view/document';
+import { StylesProcessor } from '@ckeditor/ckeditor5-engine/src/view/stylesmap';
 
 describe( 'PasteFromOffice - filters', () => {
-	const htmlDataProcessor = new HtmlDataProcessor();
+	const htmlDataProcessor = new HtmlDataProcessor( new Document( new StylesProcessor() ) );
 	describe( 'removeBoldWrapper', () => {
-		let writer;
+		let writer, viewDocument;
 
 		before( () => {
-			writer = new UpcastWriter();
+			viewDocument = new Document();
+			writer = new UpcastWriter( viewDocument );
 		} );
 
 		it( 'should remove bold wrapper added by google docs', () => {

+ 3 - 1
packages/ckeditor5-paste-from-office/tests/pastefromoffice.js

@@ -10,9 +10,11 @@ import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/html
 import { createDataTransfer } from './_utils/utils';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
+import { StylesProcessor } from '@ckeditor/ckeditor5-engine/src/view/stylesmap';
+import ViewDocument from '@ckeditor/ckeditor5-engine/src/view/document';
 
 describe( 'PasteFromOffice', () => {
-	const htmlDataProcessor = new HtmlDataProcessor();
+	const htmlDataProcessor = new HtmlDataProcessor( new ViewDocument( new StylesProcessor() ) );
 	let editor, pasteFromOffice, clipboard;
 
 	testUtils.createSinonSandbox();