Browse Source

Added validation for document object in set/getData utils methods.

Oskar Wrobel 9 years ago
parent
commit
a0e5b7a875

+ 8 - 3
packages/ckeditor5-engine/tests/_utils-tests/model.js

@@ -12,7 +12,6 @@ import Element from '/ckeditor5/engine/model/element.js';
 import Text from '/ckeditor5/engine/model/text.js';
 import Range from '/ckeditor5/engine/model/range.js';
 import Position from '/ckeditor5/engine/model/position.js';
-import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
 
 describe( 'model test utils', () => {
 	let document, root, selection, sandbox;
@@ -51,8 +50,8 @@ describe( 'model test utils', () => {
 
 		it( 'should throw an error when passing invalid document', () => {
 			expect( () => {
-				getData( { foo: 'bar' } );
-			} ).to.throw( CKEditorError, /model-getData-invalid-document/ );
+				getData( { invalid: 'document' } );
+			} ).to.throw( TypeError, /Document needs to be an instance of engine.model.Document/ );
 		} );
 	} );
 
@@ -82,6 +81,12 @@ describe( 'model test utils', () => {
 			const args = parseSpy.firstCall.args;
 			expect( args[ 0 ] ).to.equal( data );
 		} );
+
+		it( 'should throw an error when passing invalid document', () => {
+			expect( () => {
+				getData( { invalid: 'document' } );
+			} ).to.throw( TypeError, /Document needs to be an instance of engine.model.Document/ );
+		} );
 	} );
 
 	describe( 'stringify', () => {

+ 12 - 0
packages/ckeditor5-engine/tests/_utils-tests/view.js

@@ -68,6 +68,12 @@ describe( 'view test utils', () => {
 				expect( stringifyOptions ).to.have.property( 'showPriority' ).that.equals( false );
 				expect( stringifyOptions ).to.have.property( 'ignoreRoot' ).that.equals( true );
 			} );
+
+			it( 'should throw an error when passing invalid document', () => {
+				expect( () => {
+					getData( { invalid: 'document' } );
+				} ).to.throw( TypeError, /Document needs to be an instance of engine.view.Document/ );
+			} );
 		} );
 
 		describe( 'setData', () => {
@@ -101,6 +107,12 @@ describe( 'view test utils', () => {
 				expect( args[ 1 ] ).to.be.an( 'object' );
 				expect( args[ 1 ].rootElement ).to.equal( viewDocument.getRoot() );
 			} );
+
+			it( 'should throw an error when passing invalid document', () => {
+				expect( () => {
+					getData( { invalid: 'document' } );
+				} ).to.throw( TypeError, /Document needs to be an instance of engine.view.Document/ );
+			} );
 		} );
 	} );
 

+ 5 - 5
packages/ckeditor5-engine/tests/_utils/model.js

@@ -14,7 +14,6 @@ import Element from '/ckeditor5/engine/model/element.js';
 import DocumentFragment from '/ckeditor5/engine/model/documentfragment.js';
 import Selection from '/ckeditor5/engine/model/selection.js';
 import Document from '/ckeditor5/engine/model/document.js';
-import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
 
 /**
  * Writes the contents of the {@link engine.model.Document Document} to an HTML-like string.
@@ -29,10 +28,7 @@ import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  */
 export function getData( document, options = {} ) {
 	if ( !( document instanceof Document ) ) {
-		throw new CKEditorError(
-			'model-getData-invalid-document: document needs to be an instance of Document class',
-			{ document }
-		);
+		throw new TypeError( 'Document needs to be an instance of engine.model.Document' );
 	}
 
 	const withoutSelection = !!options.withoutSelection;
@@ -55,6 +51,10 @@ getData._stringify = stringify;
  * used.
  */
 export function setData( document, data, options = {} ) {
+	if ( !( document instanceof Document ) ) {
+		throw new TypeError( 'Document needs to be an instance of engine.model.Document' );
+	}
+
 	setData._parse( data, {
 		document: document,
 		rootName: options.rootName

+ 9 - 0
packages/ckeditor5-engine/tests/_utils/view.js

@@ -5,6 +5,7 @@
 
 'use strict';
 
+import Document from '/ckeditor5/engine/view/document.js';
 import ViewDocumentFragment from '/ckeditor5/engine/view/documentfragment.js';
 import HtmlDataProcessor from '/ckeditor5/engine/dataprocessor/htmldataprocessor.js';
 import ViewElement from '/ckeditor5/engine/view/element.js';
@@ -36,6 +37,10 @@ const TEXT_RANGE_END_TOKEN = '}';
  * @returns {String} The stringified data.
  */
 export function getData( document, options = {} ) {
+	if ( !( document instanceof Document ) ) {
+		throw new TypeError( 'Document needs to be an instance of engine.view.Document' );
+	}
+
 	const withoutSelection = !!options.withoutSelection;
 	const rootName = options.rootName || 'main';
 	const root = document.getRoot( rootName );
@@ -63,6 +68,10 @@ getData._stringify = stringify;
  * used.
  */
 export function setData( document, data, options = {} ) {
+	if ( !( document instanceof Document ) ) {
+		throw new TypeError( 'Document needs to be an instance of engine.view.Document' );
+	}
+
 	const rootName = options.rootName || 'main';
 	const root = document.getRoot( rootName );
 	const result = setData._parse( data, { rootElement: root } );