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

Adjusted code to changes in i/6091 #92

Merged
merged 9 commits into from
Mar 3, 2020
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
5 changes: 2 additions & 3 deletions src/filters/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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 ) {
Expand Down Expand Up @@ -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 );
Expand Down
5 changes: 4 additions & 1 deletion src/filters/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;

Expand Down
10 changes: 9 additions & 1 deletion src/normalizers/googledocsnormalizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion src/pastefromoffice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 3 additions & 1 deletion tests/_utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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.
Expand Down
26 changes: 14 additions & 12 deletions tests/filters/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand All @@ -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' );
Expand All @@ -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 );
Expand All @@ -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' );
Expand All @@ -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' );
Expand All @@ -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( '' );
Expand All @@ -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', () => {
Expand Down
9 changes: 6 additions & 3 deletions tests/filters/reomoveboldwrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
4 changes: 3 additions & 1 deletion tests/pastefromoffice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down