Преглед изворни кода

Tests: adjusted tests to new filters structure.

Krzysztof Krztoń пре 7 година
родитељ
комит
d87fae2d9d

+ 0 - 1
packages/ckeditor5-paste-from-office/tests/_data/list/simple/input.word2016.html

@@ -904,4 +904,3 @@ PL'><span style='mso-list:Ignore'>2.<span style='font:7.0pt "Times New Roman"'>&
 </body>
 
 </html>
-y

+ 0 - 3
packages/ckeditor5-paste-from-office/tests/data/normalization/list.js

@@ -4,7 +4,6 @@
  */
 
 import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
-import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
 import PasteFromOffice from '../../../src/pastefromoffice';
 
@@ -32,8 +31,6 @@ import heading7Normalized from '../../_data/list/heading7/normalized.word2016.ht
 describe( 'List – normalization', () => {
 	let editor, pasteFromOfficePlugin;
 
-	testUtils.createSinonSandbox();
-
 	beforeEach( () => {
 		return VirtualTestEditor
 			.create( {

+ 0 - 180
packages/ckeditor5-paste-from-office/tests/filters/common.js

@@ -1,180 +0,0 @@
-/**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
-
-/* globals document, CSSStyleSheet */
-
-import DocumentFragment from '@ckeditor/ckeditor5-engine/src/view/documentfragment';
-import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
-
-import { extractBody, bodyToView, extractStyles, stylesToStylesheet } from '../../src/filters/common';
-
-describe( 'Filters – common', () => {
-	describe( 'extractBody', () => {
-		it( 'correctly extracts body tag contents from the given string', () => {
-			const html = '<html><head></head><body><p>FooBar</p></body></html>';
-			const result = extractBody( { html } );
-
-			expect( result.body ).to.equal( '<p>FooBar</p>' );
-		} );
-
-		it( 'returns null if body tag present but empty', () => {
-			const html = '<html><head></head><body></body></html>';
-			const result = extractBody( { html } );
-
-			expect( result.body ).to.null;
-		} );
-
-		it( 'returns null if no body tag present', () => {
-			const html = '<html><head></head></html>';
-			const result = extractBody( { html } );
-
-			expect( result.body ).to.null;
-		} );
-	} );
-
-	describe( 'bodyToView', () => {
-		it( 'transforms body string to DocumentFragment instance', () => {
-			const body = '<p>FooBar</p>';
-			const result = bodyToView( { body } );
-
-			expect( result.view ).to.instanceof( DocumentFragment );
-			expect( result.view.childCount ).to.equal( 1 );
-		} );
-
-		it( 'does nothing when body string not present', () => {
-			const result = bodyToView( {} );
-
-			expect( result.view ).to.null;
-		} );
-	} );
-
-	describe( 'extractStyles', () => {
-		it( 'correctly extracts style tag contents from the given string', () => {
-			const html = '<html><head><style>p { color: red; }</style></head><body></body></html>';
-			const result = extractStyles( { html } );
-
-			expect( result.styles ).to.equal( 'p { color: red; }' );
-		} );
-
-		it( 'correctly extracts contents from many style tags in a the given string', () => {
-			const html = '<html><head>' +
-				'<style>p { color: red; }</style><meta tag="12345">' +
-				'<style>h1 { font-size: 10px; }</style></head><body><p>FooBar</p>' +
-				'<style>.cls1 { width: 10em; }</style></body></html>';
-
-			const result = extractStyles( { html } );
-
-			expect( result.styles ).to.equal( 'p { color: red; }h1 { font-size: 10px; }.cls1 { width: 10em; }' );
-		} );
-
-		it( 'returns null if style tag present but empty', () => {
-			const html = '<html><head><style></style></head><body></body></html>';
-			const result = extractStyles( { html } );
-
-			expect( result.styles ).to.null;
-		} );
-
-		it( 'returns null if no style tag present', () => {
-			const html = '<html><head></head><body></body></html>';
-			const result = extractStyles( { html } );
-
-			expect( result.styles ).to.null;
-		} );
-	} );
-
-	describe( 'stylesToStylesheet', () => {
-		testUtils.createSinonSandbox();
-
-		let spy = null;
-
-		const shadowDomSupported = document.head && document.head.attachShadow;
-		const styles = 'p { color: red; } h1 { font-size: 20px; }';
-
-		afterEach( () => {
-			if ( shadowDomSupported ) {
-				// Restore native `attachSahdow()` function;
-				document.head.attachShadow = shadowDomSupported;
-			}
-
-			if ( spy ) {
-				// Restore spy.
-				spy.restore();
-				spy = null;
-			}
-		} );
-
-		( shadowDomSupported ? it : it.skip )( 'parses styles using shadow DOM if available', () => {
-			spy = sinon.spy( document, 'createElement' );
-
-			const result = stylesToStylesheet( { styles }, document );
-
-			// We cannot directly spy `attachShadow` as it is called on temporary element, so we check if `div`
-			// was created which is a wrapper for shadow DOM. For `iframe` method only `iframe` element is created.
-			expect( spy.calledWithExactly( 'div' ) ).to.true;
-			expect( spy.neverCalledWith( 'iframe' ) ).to.true;
-
-			expect( result.stylesheet ).to.instanceof( CSSStyleSheet );
-			expect( result.stylesheet.cssRules.length ).to.equal( 2 );
-			expect( result.stylesheet.cssRules[ 0 ].style.color ).to.equal( 'red' );
-			expect( result.stylesheet.cssRules[ 1 ].style[ 'font-size' ] ).to.equal( '20px' );
-		} );
-
-		it( 'parses styles using iframe if shadow DOM not supported', () => {
-			if ( shadowDomSupported ) {
-				// Make native `attachShadow()` function `null` to force styles via iframe parsing.
-				document.head.attachShadow = null;
-			}
-
-			spy = sinon.spy( document, 'createElement' );
-
-			const result = stylesToStylesheet( { styles }, document );
-
-			// Check if only `iframe` was used.
-			expect( spy.calledWithExactly( 'iframe' ) ).to.true;
-			expect( spy.neverCalledWith( 'div' ) ).to.true;
-
-			expect( result.stylesheet ).to.instanceof( CSSStyleSheet );
-			expect( result.stylesheet.cssRules.length ).to.equal( 2 );
-			expect( result.stylesheet.cssRules[ 0 ].style.color ).to.equal( 'red' );
-			expect( result.stylesheet.cssRules[ 1 ].style[ 'font-size' ] ).to.equal( '20px' );
-		} );
-
-		it( 'does nothing and returns null if styles are empty', () => {
-			spy = sinon.spy( document, 'createElement' );
-
-			const styles = '';
-			const result = stylesToStylesheet( { styles }, document );
-
-			expect( spy.neverCalledWith( 'iframe' ) ).to.true;
-			expect( spy.neverCalledWith( 'div' ) ).to.true;
-			expect( result.stylesheet ).to.null;
-		} );
-
-		it( 'does nothing and returns null if document not provided', () => {
-			spy = sinon.spy( document, 'createElement' );
-
-			const result = stylesToStylesheet( { styles } );
-
-			expect( spy.neverCalledWith( 'iframe' ) ).to.true;
-			expect( spy.neverCalledWith( 'div' ) ).to.true;
-			expect( result.stylesheet ).to.null;
-		} );
-
-		it( 'replaces @list selector so it is parsed correctly', () => {
-			const styles = '@list { color: red; } p { color: blue; } @list l11:level1234 { color: green; }';
-
-			const result = stylesToStylesheet( { styles }, document );
-
-			expect( result.stylesheet ).to.instanceof( CSSStyleSheet );
-			expect( result.stylesheet.cssRules.length ).to.equal( 3 );
-			expect( result.stylesheet.cssRules[ 0 ].selectorText ).to.equal( 'at_list' );
-			expect( result.stylesheet.cssRules[ 1 ].selectorText ).to.equal( 'p' );
-			expect( result.stylesheet.cssRules[ 2 ].selectorText ).to.equal( 'at_list l11_level1234' );
-			expect( result.stylesheet.cssRules[ 0 ].style.color ).to.equal( 'red' );
-			expect( result.stylesheet.cssRules[ 1 ].style.color ).to.equal( 'blue' );
-			expect( result.stylesheet.cssRules[ 2 ].style.color ).to.equal( 'green' );
-		} );
-	} );
-} );

+ 28 - 23
packages/ckeditor5-paste-from-office/tests/filters/list.js

@@ -4,8 +4,6 @@
  */
 
 import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
-import DocumentFragment from '@ckeditor/ckeditor5-engine/src/view/documentfragment';
-
 import { stringify } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
 
 import { paragraphsToLists } from '../../src/filters/list';
@@ -18,45 +16,52 @@ describe( 'Filters – list', () => {
 			const html = '<p style="mso-list:l0 level1 lfo0"><span style="mso-list:Ignore">1.</span>Item 1</p>';
 			const view = htmlDataProcessor.toView( html );
 
-			const result = paragraphsToLists( { view } );
+			const result = paragraphsToLists( view, '' );
 
-			expect( result.view.childCount ).to.equal( 1 );
-			expect( result.view.getChild( 0 ).name ).to.equal( 'ol' );
-			expect( stringify( result.view ) ).to.equal( '<ol><li style="mso-list:l0 level1 lfo0">Item 1</li></ol>' );
+			expect( result.childCount ).to.equal( 1 );
+			expect( result.getChild( 0 ).name ).to.equal( 'ol' );
+			expect( stringify( result ) ).to.equal( '<ol><li style="mso-list:l0 level1 lfo0">Item 1</li></ol>' );
 		} );
 
-		it( 'has no effect if `data.view` is not set', () => {
-			const result = paragraphsToLists( {} );
-
-			expect( result ).to.empty;
-		} );
+		it( 'replaces list-like elements with semantic lists with proper bullet type based on styles', () => {
+			const html = '<p style="mso-list:l0 level1 lfo0"><span style="mso-list:Ignore">1.</span>Item 1</p>';
+			const view = htmlDataProcessor.toView( html );
 
-		it( 'has no effect if `data.view` has no children', () => {
-			const view = new DocumentFragment();
-			const result = paragraphsToLists( { view } );
+			const result = paragraphsToLists( view, '@list l0:level1 { mso-level-number-format: bullet; }' );
 
-			expect( result.view.childCount ).to.equal( 0 );
+			expect( result.childCount ).to.equal( 1 );
+			expect( result.getChild( 0 ).name ).to.equal( 'ul' );
+			expect( stringify( result ) ).to.equal( '<ul><li style="mso-list:l0 level1 lfo0">Item 1</li></ul>' );
 		} );
 
 		it( 'does not modify the view if there are no list-like elements', () => {
 			const html = '<h1>H1</h1><p>Foo Bar</p>';
 			const view = htmlDataProcessor.toView( html );
 
-			const result = paragraphsToLists( { view } );
+			const result = paragraphsToLists( view, '' );
 
-			expect( result.view.childCount ).to.equal( 2 );
-			expect( stringify( result.view ) ).to.equal( html );
+			expect( result.childCount ).to.equal( 2 );
+			expect( stringify( result ) ).to.equal( html );
 		} );
 
-		it( 'can handle empty `mso-list` style', () => {
+		it( 'handles empty `mso-list` style correctly', () => {
 			const html = '<p style="mso-list:"><span style="mso-list:Ignore">1.</span>Item 1</p>';
 			const view = htmlDataProcessor.toView( html );
 
-			const result = paragraphsToLists( { view } );
+			const result = paragraphsToLists( view, '' );
+
+			expect( result.childCount ).to.equal( 1 );
+			expect( result.getChild( 0 ).name ).to.equal( 'ol' );
+			expect( stringify( result ) ).to.equal( '<ol><li style="mso-list:">Item 1</li></ol>' );
+		} );
+
+		it( 'handles empty body correctly', () => {
+			const view = htmlDataProcessor.toView( '' );
+
+			const result = paragraphsToLists( view, '' );
 
-			expect( result.view.childCount ).to.equal( 1 );
-			expect( result.view.getChild( 0 ).name ).to.equal( 'ol' );
-			expect( stringify( result.view ) ).to.equal( '<ol><li style="mso-list:">Item 1</li></ol>' );
+			expect( result.childCount ).to.equal( 0 );
+			expect( stringify( result ) ).to.equal( '' );
 		} );
 	} );
 } );

+ 80 - 0
packages/ckeditor5-paste-from-office/tests/filters/utils.js

@@ -0,0 +1,80 @@
+/**
+ * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* globals CSSStyleSheet */
+
+import DocumentFragment from '@ckeditor/ckeditor5-engine/src/view/documentfragment';
+
+import { parseHtml } from '../../src/filters/utils';
+
+describe( 'Filters – utils', () => {
+	describe( 'parseHtml', () => {
+		it( 'correctly parses HTML with body and one style tag', () => {
+			const html = '<head><style>p { color: red; } a { border: none; }</style></head><body><p>Foo Bar</p></body>';
+			const { body, bodyString, styles, stylesString } = parseHtml( html );
+
+			expect( body ).to.instanceof( DocumentFragment );
+			expect( body.childCount ).to.equal( 1 );
+
+			expect( bodyString ).to.equal( '<p>Foo Bar</p>' );
+
+			expect( styles.length ).to.equal( 1 );
+			expect( styles[ 0 ] ).to.instanceof( CSSStyleSheet );
+			expect( styles[ 0 ].cssRules.length ).to.equal( 2 );
+			expect( styles[ 0 ].cssRules[ 0 ].style.color ).to.equal( 'red' );
+			expect( styles[ 0 ].cssRules[ 1 ].style.border ).to.equal( 'none' );
+
+			expect( stylesString ).to.equal( 'p { color: red; } a { border: none; }' );
+		} );
+
+		it( 'correctly parses HTML with body contents only', () => {
+			const html = '<p>Foo Bar</p>';
+			const { body, bodyString, styles, stylesString } = parseHtml( html );
+
+			expect( body ).to.instanceof( DocumentFragment );
+			expect( body.childCount ).to.equal( 1 );
+
+			expect( bodyString ).to.equal( '<p>Foo Bar</p>' );
+
+			expect( styles.length ).to.equal( 0 );
+
+			expect( stylesString ).to.equal( '' );
+		} );
+
+		it( 'correctly parses HTML with no body and multiple style tags', () => {
+			const html = '<html><head><style>p { color: blue; }</style><style>a { color: green; }</style></head></html>';
+			const { body, bodyString, styles, stylesString } = parseHtml( html );
+
+			expect( body ).to.instanceof( DocumentFragment );
+			expect( body.childCount ).to.equal( 0 );
+
+			expect( bodyString ).to.equal( '' );
+
+			expect( styles.length ).to.equal( 2 );
+			expect( styles[ 0 ] ).to.instanceof( CSSStyleSheet );
+			expect( styles[ 1 ] ).to.instanceof( CSSStyleSheet );
+			expect( styles[ 0 ].cssRules.length ).to.equal( 1 );
+			expect( styles[ 1 ].cssRules.length ).to.equal( 1 );
+			expect( styles[ 0 ].cssRules[ 0 ].style.color ).to.equal( 'blue' );
+			expect( styles[ 1 ].cssRules[ 0 ].style.color ).to.equal( 'green' );
+
+			expect( stylesString ).to.equal( 'p { color: blue; } a { color: green; }' );
+		} );
+
+		it( 'correctly parses HTML with no body and no style tags', () => {
+			const html = '<html><head><meta name="Foo" content="Bar"></head></html>';
+			const { body, bodyString, styles, stylesString } = parseHtml( html );
+
+			expect( body ).to.instanceof( DocumentFragment );
+			expect( body.childCount ).to.equal( 0 );
+
+			expect( bodyString ).to.equal( '' );
+
+			expect( styles.length ).to.equal( 0 );
+
+			expect( stylesString ).to.equal( '' );
+		} );
+	} );
+} );

+ 1 - 5
packages/ckeditor5-paste-from-office/tests/pastefromword.js → packages/ckeditor5-paste-from-office/tests/pastefromoffice.js

@@ -22,14 +22,10 @@ describe( 'Paste from Office plugin', () => {
 			} )
 			.then( newEditor => {
 				editor = newEditor;
-				normalizeSpy = sinon.spy( editor.plugins.get( 'PasteFromOffice' ), '_normalizeWordInput' );
+				normalizeSpy = testUtils.sinon.spy( editor.plugins.get( 'PasteFromOffice' ), '_normalizeWordInput' );
 			} );
 	} );
 
-	afterEach( () => {
-		normalizeSpy.restore();
-	} );
-
 	it( 'runs normalizations if Word meta tag detected #1', () => {
 		const dataTransfer = createDataTransfer( {
 			'text/html': '<meta name=Generator content="Microsoft Word 15">'