浏览代码

New params order tests and code adjustments.

Krzysztof Krztoń 7 年之前
父节点
当前提交
969c99329c
共有 2 个文件被更改,包括 43 次插入39 次删除
  1. 7 3
      packages/ckeditor5-engine/src/view/upcastwriter.js
  2. 36 36
      packages/ckeditor5-engine/tests/view/upcastwriter.js

+ 7 - 3
packages/ckeditor5-engine/src/view/upcastwriter.js

@@ -8,6 +8,7 @@
  */
 
 import Element from './element';
+import { isPlainObject } from 'lodash-es';
 
 /**
  * View upcast writer class. Provides set of methods used to properly manipulate nodes attached to
@@ -85,7 +86,7 @@ export default class UpcastWriter {
 		const parent = element.parent;
 
 		if ( parent ) {
-			return this.removeChildren( parent, parent.getChildIndex( element ) );
+			return this.removeChildren( parent.getChildIndex( element ), 1, parent );
 		}
 
 		return [];
@@ -104,8 +105,8 @@ export default class UpcastWriter {
 		if ( parent ) {
 			const index = parent.getChildIndex( oldElement );
 
-			this.removeChildren( parent, index );
-			this.insertChild( parent, index, newElement );
+			this.removeChildren( index, 1, parent );
+			this.insertChild( index, newElement, parent );
 
 			return true;
 		}
@@ -200,6 +201,9 @@ export default class UpcastWriter {
 	 * @param {module:engine/view/element~Element} element Element for which style will be added.
 	 */
 	setStyle( property, value, element ) {
+		if ( isPlainObject( property ) && element === undefined ) {
+			element = value;
+		}
 		element._setStyle( property, value );
 	}
 

+ 36 - 36
packages/ckeditor5-engine/tests/view/upcastwriter.js

@@ -63,7 +63,7 @@ describe( 'UpcastWriter', () => {
 			const el = view.getChild( 2 );
 			const newChild = new Element( 'span' );
 
-			const appended = writer.appendChild( el, newChild );
+			const appended = writer.appendChild( newChild, el );
 
 			expect( appended ).to.equal( 1 );
 			expect( newChild.parent ).to.equal( el );
@@ -75,7 +75,7 @@ describe( 'UpcastWriter', () => {
 			const newChild1 = new Element( 'p' );
 			const newChild2 = new Element( 'h2' );
 
-			const appended = writer.appendChild( el, [ newChild1, newChild2 ] );
+			const appended = writer.appendChild( [ newChild1, newChild2 ], el );
 
 			expect( appended ).to.equal( 2 );
 			expect( newChild1.parent ).to.equal( el );
@@ -87,7 +87,7 @@ describe( 'UpcastWriter', () => {
 			const el = view.getChild( 3 );
 			const newChild = new Element( 'li' );
 
-			const appended = writer.appendChild( el, newChild );
+			const appended = writer.appendChild( newChild, el );
 
 			expect( appended ).to.equal( 1 );
 			expect( newChild.parent ).to.equal( el );
@@ -97,7 +97,7 @@ describe( 'UpcastWriter', () => {
 		it( 'should append element to DocumentFragment element', () => {
 			const newChild = new Element( 'p' );
 
-			const appended = writer.appendChild( view, newChild );
+			const appended = writer.appendChild( newChild, view );
 
 			expect( appended ).to.equal( 1 );
 			expect( newChild.parent ).to.equal( view );
@@ -110,7 +110,7 @@ describe( 'UpcastWriter', () => {
 			const el = view.getChild( 2 );
 			const newChild = new Element( 'span' );
 
-			const inserted = writer.insertChild( el, 0, newChild );
+			const inserted = writer.insertChild( 0, newChild, el );
 
 			expect( inserted ).to.equal( 1 );
 			expect( newChild.parent ).to.equal( el );
@@ -123,7 +123,7 @@ describe( 'UpcastWriter', () => {
 			const newChild1 = new Element( 'blockquote' );
 			const newChild2 = new Element( 'h2' );
 
-			const inserted = writer.insertChild( el, 2, [ newChild1, newChild2 ] );
+			const inserted = writer.insertChild( 2, [ newChild1, newChild2 ], el );
 
 			expect( inserted ).to.equal( 2 );
 			expect( newChild1.parent ).to.equal( el );
@@ -137,7 +137,7 @@ describe( 'UpcastWriter', () => {
 			const el = view.getChild( 3 );
 			const newChild = new Element( 'li' );
 
-			const inserted = writer.insertChild( el, 1, newChild );
+			const inserted = writer.insertChild( 1, newChild, el );
 
 			expect( inserted ).to.equal( 1 );
 			expect( newChild.parent ).to.equal( el );
@@ -148,7 +148,7 @@ describe( 'UpcastWriter', () => {
 		it( 'should insert element to DocumentFragment element', () => {
 			const newChild = new Element( 'p' );
 
-			const inserted = writer.insertChild( view, 4, newChild );
+			const inserted = writer.insertChild( 4, newChild, view );
 
 			expect( inserted ).to.equal( 1 );
 			expect( newChild.parent ).to.equal( view );
@@ -162,7 +162,7 @@ describe( 'UpcastWriter', () => {
 			const el = view.getChild( 1 );
 			const toRemove = el.getChild( 0 );
 
-			const removed = writer.removeChildren( el, 0 );
+			const removed = writer.removeChildren( 0, 1, el );
 
 			expect( removed.length ).to.equal( 1 );
 			expect( removed[ 0 ] ).to.equal( toRemove );
@@ -174,7 +174,7 @@ describe( 'UpcastWriter', () => {
 			const toRemove1 = el.getChild( 1 );
 			const toRemove2 = el.getChild( 2 );
 
-			const removed = writer.removeChildren( el, 1, 2 );
+			const removed = writer.removeChildren( 1, 2, el );
 
 			expect( removed.length ).to.equal( 2 );
 			expect( removed[ 0 ] ).to.equal( toRemove1 );
@@ -185,7 +185,7 @@ describe( 'UpcastWriter', () => {
 		it( 'should remove child from DocumentFragment element', () => {
 			const toRemove = view.getChild( 2 );
 
-			const removed = writer.removeChildren( view, 2 );
+			const removed = writer.removeChildren( 2, 1, view );
 
 			expect( removed.length ).to.equal( 1 );
 			expect( removed[ 0 ] ).to.equal( toRemove );
@@ -262,7 +262,7 @@ describe( 'UpcastWriter', () => {
 		it( 'should rename simple element', () => {
 			const el = view.getChild( 0 ).getChild( 1 );
 
-			const renamed = writer.rename( el, 'i' );
+			const renamed = writer.rename( 'i', el );
 
 			expect( renamed ).to.not.equal( el );
 			expect( renamed ).to.equal( view.getChild( 0 ).getChild( 1 ) );
@@ -273,7 +273,7 @@ describe( 'UpcastWriter', () => {
 		it( 'should rename direct root (DocumentFragment) child element', () => {
 			const el = view.getChild( 1 );
 
-			const renamed = writer.rename( el, 'h3' );
+			const renamed = writer.rename( 'h3', el );
 
 			expect( renamed ).to.not.equal( el );
 			expect( renamed ).to.equal( view.getChild( 1 ) );
@@ -282,9 +282,9 @@ describe( 'UpcastWriter', () => {
 		} );
 
 		it( 'should have no effect on detached element', () => {
-			const element = new Element( 'h2' );
+			const el = new Element( 'h2' );
 
-			const renamed = writer.rename( element, 'h3' );
+			const renamed = writer.rename( 'h3', el );
 
 			expect( renamed ).to.null;
 			expect( view.childCount ).to.equal( 4 );
@@ -295,7 +295,7 @@ describe( 'UpcastWriter', () => {
 		it( 'should add new attribute', () => {
 			const el = view.getChild( 0 );
 
-			writer.setAttribute( el, 'testAttr', 'testVal' );
+			writer.setAttribute( 'testAttr', 'testVal', el );
 
 			expect( el.getAttribute( 'testAttr' ) ).to.equal( 'testVal' );
 		} );
@@ -303,7 +303,7 @@ describe( 'UpcastWriter', () => {
 		it( 'should update existing attribute', () => {
 			const el = view.getChild( 1 );
 
-			writer.setAttribute( el, 'data-attr', 'foo' );
+			writer.setAttribute( 'data-attr', 'foo', el );
 
 			expect( el.getAttribute( 'data-attr' ) ).to.equal( 'foo' );
 		} );
@@ -313,7 +313,7 @@ describe( 'UpcastWriter', () => {
 		it( 'should remove existing attribute', () => {
 			const el = view.getChild( 1 );
 
-			writer.removeAttribute( el, 'data-attr' );
+			writer.removeAttribute( 'data-attr', el );
 
 			expect( el.hasAttribute( 'data-attr' ) ).to.false;
 		} );
@@ -321,7 +321,7 @@ describe( 'UpcastWriter', () => {
 		it( 'should have no effect if attribute does not exists', () => {
 			const el = view.getChild( 0 );
 
-			writer.removeAttribute( el, 'non-existent' );
+			writer.removeAttribute( 'non-existent', el );
 
 			expect( el.hasAttribute( 'non-existent' ) ).to.false;
 		} );
@@ -331,7 +331,7 @@ describe( 'UpcastWriter', () => {
 		it( 'should add new classes if no classes', () => {
 			const el = view.getChild( 2 );
 
-			writer.addClass( el, [ 'foo', 'bar' ] );
+			writer.addClass( [ 'foo', 'bar' ], el );
 
 			expect( el.hasClass( 'foo' ) ).to.true;
 			expect( el.hasClass( 'bar' ) ).to.true;
@@ -341,7 +341,7 @@ describe( 'UpcastWriter', () => {
 		it( 'should add new class to existing classes', () => {
 			const el = view.getChild( 1 );
 
-			writer.addClass( el, 'newClass' );
+			writer.addClass( 'newClass', el );
 
 			expect( el.hasClass( 'newClass' ) ).to.true;
 			expect( Array.from( el.getClassNames() ).length ).to.equal( 3 );
@@ -352,7 +352,7 @@ describe( 'UpcastWriter', () => {
 		it( 'should remove existing class', () => {
 			const el = view.getChild( 3 ).getChild( 0 );
 
-			writer.removeClass( el, 'single' );
+			writer.removeClass( 'single', el );
 
 			expect( el.hasClass( 'single' ) ).to.false;
 			expect( Array.from( el.getClassNames() ).length ).to.equal( 0 );
@@ -361,7 +361,7 @@ describe( 'UpcastWriter', () => {
 		it( 'should remove existing class from many classes', () => {
 			const el = view.getChild( 1 );
 
-			writer.removeClass( el, 'foo1' );
+			writer.removeClass( 'foo1', el );
 
 			expect( el.hasClass( 'foo1' ) ).to.false;
 			expect( el.hasClass( 'bar2' ) ).to.true;
@@ -371,7 +371,7 @@ describe( 'UpcastWriter', () => {
 		it( 'should have no effect if there are no classes', () => {
 			const el = view.getChild( 0 );
 
-			writer.removeClass( el, 'non-existent' );
+			writer.removeClass( 'non-existent', el );
 
 			expect( el.hasClass( 'non-existent' ) ).to.false;
 			expect( Array.from( el.getClassNames() ).length ).to.equal( 0 );
@@ -382,10 +382,10 @@ describe( 'UpcastWriter', () => {
 		it( 'should add new style', () => {
 			const el = view.getChild( 2 );
 
-			writer.setStyle( el, {
+			writer.setStyle( {
 				color: 'red',
 				position: 'fixed'
-			} );
+			}, el );
 
 			expect( el.getStyle( 'color' ) ).to.equal( 'red' );
 			expect( el.getStyle( 'position' ) ).to.equal( 'fixed' );
@@ -395,7 +395,7 @@ describe( 'UpcastWriter', () => {
 		it( 'should update existing styles', () => {
 			const el = view.getChild( 1 );
 
-			writer.setStyle( el, 'text-align', 'center' );
+			writer.setStyle( 'text-align', 'center', el );
 
 			expect( el.getStyle( 'text-align' ) ).to.equal( 'center' );
 			expect( Array.from( el.getStyleNames() ).length ).to.equal( 1 );
@@ -406,7 +406,7 @@ describe( 'UpcastWriter', () => {
 		it( 'should remove existing style', () => {
 			const el = view.getChild( 0 );
 
-			writer.removeStyle( el, [ 'color', 'position' ] );
+			writer.removeStyle( [ 'color', 'position' ], el );
 
 			expect( el.hasStyle( 'color' ) ).to.false;
 			expect( el.hasStyle( 'position' ) ).to.false;
@@ -416,7 +416,7 @@ describe( 'UpcastWriter', () => {
 		it( 'should remove value from existing styles', () => {
 			const el = view.getChild( 0 );
 
-			writer.removeStyle( el, 'position' );
+			writer.removeStyle( 'position', el );
 
 			expect( el.hasStyle( 'color' ) ).to.true;
 			expect( el.hasStyle( 'position' ) ).to.false;
@@ -426,7 +426,7 @@ describe( 'UpcastWriter', () => {
 		it( 'should have no effect if styles does not exists', () => {
 			const el = view.getChild( 2 );
 
-			writer.removeStyle( el, [ 'color', 'position' ] );
+			writer.removeStyle( [ 'color', 'position' ], el );
 
 			expect( el.hasStyle( 'color' ) ).to.false;
 			expect( el.hasStyle( 'position' ) ).to.false;
@@ -438,15 +438,15 @@ describe( 'UpcastWriter', () => {
 		it( 'should add or update custom property', () => {
 			const el = new Element( 'span' );
 
-			writer.setCustomProperty( el, 'prop1', 'foo' );
-			writer.setCustomProperty( el, 'prop2', 'bar' );
+			writer.setCustomProperty( 'prop1', 'foo', el );
+			writer.setCustomProperty( 'prop2', 'bar', el );
 
 			expect( el.getCustomProperty( 'prop1' ) ).to.equal( 'foo' );
 			expect( el.getCustomProperty( 'prop2' ) ).to.equal( 'bar' );
 			expect( Array.from( el.getCustomProperties() ).length ).to.equal( 2 );
 
 			const objectProperty = { foo: 'bar' };
-			writer.setCustomProperty( el, 'prop2', objectProperty );
+			writer.setCustomProperty( 'prop2', objectProperty, el );
 
 			expect( el.getCustomProperty( 'prop1' ) ).to.equal( 'foo' );
 			expect( el.getCustomProperty( 'prop2' ) ).to.equal( objectProperty );
@@ -458,12 +458,12 @@ describe( 'UpcastWriter', () => {
 		it( 'should remove existing custom property', () => {
 			const el = new Element( 'p' );
 
-			writer.setCustomProperty( el, 'prop1', 'foo' );
+			writer.setCustomProperty( 'prop1', 'foo', el );
 
 			expect( el.getCustomProperty( 'prop1' ) ).to.equal( 'foo' );
 			expect( Array.from( el.getCustomProperties() ).length ).to.equal( 1 );
 
-			writer.removeCustomProperty( el, 'prop1' );
+			writer.removeCustomProperty( 'prop1', el );
 
 			expect( el.getCustomProperty( 'prop1' ) ).to.undefined;
 			expect( Array.from( el.getCustomProperties() ).length ).to.equal( 0 );
@@ -472,7 +472,7 @@ describe( 'UpcastWriter', () => {
 		it( 'should have no effect if custom property does not exists', () => {
 			const el = new Element( 'h1' );
 
-			writer.removeCustomProperty( el, 'prop1' );
+			writer.removeCustomProperty( 'prop1', el );
 
 			expect( el.getCustomProperty( 'prop1' ) ).to.undefined;
 			expect( Array.from( el.getCustomProperties() ).length ).to.equal( 0 );