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

Upcast conversion should use proper instance of StylesProcessor.

Kuba Niegowski 5 лет назад
Родитель
Сommit
a5cefe546c

+ 6 - 5
packages/ckeditor5-paste-from-office/src/filters/parse.js

@@ -13,12 +13,12 @@ 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.
  *
  * @param {String} htmlString HTML string to be parsed.
+ * @param {module:engine/view/stylesmap~StylesProcessor} stylesProcessor
  * @returns {Object} result
  * @returns {module:engine/view/documentfragment~DocumentFragment} result.body Parsed body
  * content as a traversable structure.
@@ -27,7 +27,7 @@ import { StylesProcessor } from '@ckeditor/ckeditor5-engine/src/view/stylesmap';
  * separate `style` tag from the source HTML.
  * @returns {String} result.stylesString All `style` tags contents combined in the order of occurrence into one string.
  */
-export function parseHtml( htmlString ) {
+export function parseHtml( htmlString, stylesProcessor ) {
 	const domParser = new DOMParser();
 
 	// Remove Word specific "if comments" so content inside is not omitted by the parser.
@@ -44,7 +44,7 @@ export function parseHtml( htmlString ) {
 	const bodyString = htmlDocument.body.innerHTML;
 
 	// Transform document.body to View.
-	const bodyView = documentToView( htmlDocument );
+	const bodyView = documentToView( htmlDocument, stylesProcessor );
 
 	// Extract stylesheets.
 	const stylesObject = extractStyles( htmlDocument );
@@ -60,9 +60,10 @@ export function parseHtml( htmlString ) {
 // Transforms native `Document` object into {@link module:engine/view/documentfragment~DocumentFragment}.
 //
 // @param {Document} htmlDocument Native `Document` object to be transformed.
+// @param {module:engine/view/stylesmap~StylesProcessor} stylesProcessor
 // @returns {module:engine/view/documentfragment~DocumentFragment}
-function documentToView( htmlDocument ) {
-	const viewDocument = new ViewDocument( new StylesProcessor() );
+function documentToView( htmlDocument, stylesProcessor ) {
+	const viewDocument = new ViewDocument( 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/mswordnormalizer.js

@@ -20,6 +20,14 @@ const msWordMatch2 = /xmlns:o="urn:schemas-microsoft-com/i;
  * @implements module:paste-from-office/normalizer~Normalizer
  */
 export default class MSWordNormalizer {
+	constructor( document ) {
+		/**
+		 * @readonly
+		 * @type {module:engine/view/document~Document}
+		 */
+		this.document = document;
+	}
+
 	/**
 	 * @inheritDoc
 	 */
@@ -31,7 +39,7 @@ export default class MSWordNormalizer {
 	 * @inheritDoc
 	 */
 	execute( data ) {
-		const { body, stylesString } = parseHtml( data.dataTransfer.getData( 'text/html' ) );
+		const { body, stylesString } = parseHtml( data.dataTransfer.getData( 'text/html' ), this.document.stylesProcessor );
 
 		transformListItemLikeElementsIntoLists( body, stylesString );
 		replaceImagesSourceWithBase64( body, data.dataTransfer.getData( 'text/rtf' ) );

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

@@ -48,10 +48,11 @@ export default class PasteFromOffice extends Plugin {
 	 */
 	init() {
 		const editor = this.editor;
+		const viewDocument = editor.editing.view.document;
 		const normalizers = [];
 
-		normalizers.push( new MSWordNormalizer() );
-		normalizers.push( new GoogleDocsNormalizer( editor.editing.view.document ) );
+		normalizers.push( new MSWordNormalizer( viewDocument ) );
+		normalizers.push( new GoogleDocsNormalizer( viewDocument ) );
 
 		editor.plugins.get( 'Clipboard' ).on(
 			'inputTransformation',

+ 2 - 1
packages/ckeditor5-paste-from-office/tests/manual/integration.js

@@ -19,6 +19,7 @@ import { stringify as stringifyView } from '@ckeditor/ckeditor5-engine/src/dev-u
 
 import { CS_CONFIG } from '@ckeditor/ckeditor5-cloud-services/tests/_utils/cloud-services-config';
 import TableProperties from '@ckeditor/ckeditor5-table/src/tableproperties';
+import TableCellProperties from '@ckeditor/ckeditor5-table/src/tablecellproperties';
 
 const htmlDiv = document.querySelector( '#html' );
 const textDiv = document.querySelector( '#text' );
@@ -27,7 +28,7 @@ const dataDiv = document.querySelector( '#data' );
 ClassicEditor
 	.create( document.querySelector( '#editor' ), {
 		plugins: [ ArticlePluginSet, Strikethrough, Underline, Table, TableToolbar,
-			TableProperties, TableProperties, EasyImage, PasteFromOffice ],
+			TableProperties, TableCellProperties, EasyImage, PasteFromOffice ],
 		toolbar: [ 'heading', '|', 'bold', 'italic', 'strikethrough', 'underline', 'link',
 			'bulletedList', 'numberedList', 'blockQuote', 'insertTable', 'undo', 'redo' ],
 		table: {