Skip to content

Commit

Permalink
Merge pull request #7313 from ckeditor/i/7275
Browse files Browse the repository at this point in the history
Fix (paste-from-office): The Paste from Office feature should use the same instance of `StylesProcessor` as the view document. Closes #7275.
  • Loading branch information
oleq authored and pomek committed May 29, 2020
1 parent ff5ba1d commit 67a469a
Show file tree
Hide file tree
Showing 11 changed files with 979 additions and 11 deletions.
11 changes: 6 additions & 5 deletions packages/ckeditor5-paste-from-office/src/filters/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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 );
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const googleDocsMatch = /id=("|')docs-internal-guid-[-0-9a-f]+("|')/i;
* @implements module:paste-from-office/normalizer~Normalizer
*/
export default class GoogleDocsNormalizer {
/**
* Creates a new `GoogleDocsNormalizer` instance.
*
* @param {module:engine/view/document~Document} document View document.
*/
constructor( document ) {
/**
* @readonly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ const msWordMatch2 = /xmlns:o="urn:schemas-microsoft-com/i;
* @implements module:paste-from-office/normalizer~Normalizer
*/
export default class MSWordNormalizer {
/**
* Creates a new `MSWordNormalizer` instance.
*
* @param {module:engine/view/document~Document} document View document.
*/
constructor( document ) {
/**
* @readonly
* @type {module:engine/view/document~Document}
*/
this.document = document;
}

/**
* @inheritDoc
*/
Expand All @@ -31,7 +44,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' ) );
Expand Down
5 changes: 3 additions & 2 deletions packages/ckeditor5-paste-from-office/src/pastefromoffice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
16 changes: 16 additions & 0 deletions packages/ckeditor5-paste-from-office/tests/_data/table/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

import tableCellProperties from './tablecellproperties/input.html';
import tableCellPropertiesModel from './tablecellproperties/model.html';

export const fixtures = {
input: {
tableCellProperties
},
model: {
tableCellProperties: tableCellPropertiesModel
}
};
Loading

0 comments on commit 67a469a

Please sign in to comment.