8
0
Pārlūkot izejas kodu

New way of setting and printing attribute priority.

Oskar Wrobel 9 gadi atpakaļ
vecāks
revīzija
6ef6308901

+ 4 - 4
packages/ckeditor5-engine/tests/_utils-tests/view.js

@@ -240,7 +240,7 @@ describe( 'view test utils', () => {
 			const p = new ContainerElement( 'p', null, b );
 
 			expect( stringify( p, null, { showPriority: true } ) )
-				.to.equal( '<p><b:10>foobar</b:10></p>' );
+				.to.equal( '<p><b view-priority="10">foobar</b></p>' );
 		} );
 
 		it( 'should parse DocumentFragment as root', () => {
@@ -412,10 +412,10 @@ describe( 'view test utils', () => {
 		} );
 
 		it( 'should parse element priority', () => {
-			const parsed1 = parse( '<b:12></b:12>' );
+			const parsed1 = parse( '<b view-priority="12"></b>' );
 			const attribute1 = new AttributeElement( 'b' );
 			attribute1.priority = 12;
-			const parsed2 = parse( '<attribute:b:44></attribute:b:44>' );
+			const parsed2 = parse( '<attribute:b view-priority="44"></attribute:b>' );
 			const attribute2 = new AttributeElement( 'b' );
 			attribute2.priority = 44;
 
@@ -425,7 +425,7 @@ describe( 'view test utils', () => {
 		} );
 
 		it( 'should paste nested elements and texts', () => {
-			const parsed = parse( '<container:p>foo<b:12>bar<i:25>qux</i:25></b:12></container:p>' );
+			const parsed = parse( '<container:p>foo<b view-priority="12">bar<i view-priority="25">qux</i></b></container:p>' );
 			expect( parsed.isSimilar( new ContainerElement( 'p' ) ) ).to.be.true;
 			expect( parsed.childCount ).to.equal( 2 );
 			expect( parsed.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'foo' );

+ 22 - 44
packages/ckeditor5-engine/tests/_utils/view.js

@@ -18,6 +18,7 @@ const ELEMENT_RANGE_START_TOKEN = '[';
 const ELEMENT_RANGE_END_TOKEN = ']';
 const TEXT_RANGE_START_TOKEN = '{';
 const TEXT_RANGE_END_TOKEN = '}';
+const VIEW_PRIORITY_ATTRIBUTE = 'view-priority';
 
 /**
  * Writes the contents of the {@link engine.view.Document Document} to an HTML-like string.
@@ -31,7 +32,7 @@ const TEXT_RANGE_END_TOKEN = '}';
  * @param {Boolean} [options.showType=false] When set to `true` type of elements will be printed (`<container:p>`
  * instead of `<p>` and `<attribute:b>` instead of `<b>`).
  * @param {Boolean} [options.showPriority=false] When set to `true` AttributeElement's priority will be printed
- * (`<span:12>`, `<b:10>`).
+ * (`<span view-priority="12">`, `<b view-priority="10">`).
  * @returns {String} The stringified data.
  */
 export function getData( document, options = {} ) {
@@ -166,7 +167,7 @@ setData._parse = parse;
  *
  *		const attribute = new AttributeElement( 'b' );
  *		attribute.priority = 20;
- *		getData( attribute, null, { showPriority: true } ); // <b:20></b:20>
+ *		getData( attribute, null, { showPriority: true } ); // <b view-priority="20"></b>
  *
  * @param {engine.view.Text|engine.view.Element|engine.view.DocumentFragment} node Node to stringify.
  * @param {engine.view.Selection|engine.view.Position|engine.view.Range} [selectionOrPositionOrRange = null ]
@@ -177,7 +178,7 @@ setData._parse = parse;
  * @param {Boolean} [options.showType=false] When set to `true` type of elements will be printed (`<container:p>`
  * instead of `<p>` and `<attribute:b>` instead of `<b>`).
  * @param {Boolean} [options.showPriority=false] When set to `true` AttributeElement's priority will be printed
- * (`<span:12>`, `<b:10>`).
+ * (`<span view-priority="12">`, `<b view-priority="10">`).
  * @param {Boolean} [options.ignoreRoot=false] When set to `true` root's element opening and closing will not be printed.
  * Mainly used by `getData` function to ignore {@link engine.view.Document Document's} root element.
  * @param {Array<String>} [options.characterForSelectionInText=['{','}']] At default range placed inside text is
@@ -677,7 +678,7 @@ class ViewStringify {
 	/**
 	 * Converts passed {@link engine.view.Element Element} to opening tag.
 	 * Depending on current configuration opening tag can be simple (`<a>`), contain type prefix (`<container:p>` or
-	 * `<attribute:a>`), contain priority information ( `<attribute:a priority=20>` ). Element's attributes also
+	 * `<attribute:a>`), contain priority information ( `<attribute:a view-priority="20">` ). Element's attributes also
 	 * will be included (`<a href="http://ckeditor.com" name="foobar">`).
 	 *
 	 * @private
@@ -686,17 +687,18 @@ class ViewStringify {
 	 */
 	_stringifyElementOpen( element ) {
 		const priority = this._stringifyElementPriority( element );
+
 		const type = this._stringifyElementType( element );
-		const name = [ type, element.name, priority ].filter( i=> i !== '' ).join( ':' );
+		const name = [ type, element.name ].filter( i=> i !== '' ).join( ':' );
 		const attributes = this._stringifyElementAttributes( element );
-		const parts = [ name, attributes ];
+		const parts = [ name, priority, attributes ];
 
 		return `<${ parts.filter( i => i !== '' ).join( ' ' ) }>`;
 	}
 
 	/**
 	 * Converts passed {@link engine.view.Element Element} to closing tag.
-	 * Depending on current configuration opening tag can be simple (`</a>`) or contain type prefix (`</container:p>` or
+	 * Depending on current configuration closing tag can be simple (`</a>`) or contain type prefix (`</container:p>` or
 	 * `</attribute:a>`).
 	 *
 	 * @private
@@ -704,9 +706,8 @@ class ViewStringify {
 	 * @returns {String}
 	 */
 	_stringifyElementClose( element ) {
-		const priority = this._stringifyElementPriority( element );
 		const type = this._stringifyElementType( element );
-		const name = [ type, element.name, priority ].filter( i=> i !== '' ).join( ':' );
+		const name = [ type, element.name ].filter( i=> i !== '' ).join( ':' );
 
 		return `</${ name }>`;
 	}
@@ -747,7 +748,7 @@ class ViewStringify {
 	 */
 	_stringifyElementPriority( element ) {
 		if ( this.showPriority && element instanceof AttributeElement ) {
-			return element.priority;
+			return `${ VIEW_PRIORITY_ATTRIBUTE }="${ element.priority }"`;
 		}
 
 		return '';
@@ -800,19 +801,19 @@ function _convertViewElements( rootNode ) {
 
 // Converts {@link engine.view.Element Element} to {@link engine.view.AttributeElement AttributeElement} or
 // {@link engine.view.ContainerElement ContainerElement}.
-// If element's name is in format `attribute:b:1` or `b:11` it will be converted to
+// If element's name is in format `attribute:b` with `view-priority="11"` attribute it will be converted to
 // {@link engine.view.AttributeElement AttributeElement} with priority 11.
 // If element's name is in format `container:p` - it will be converted to
 // {@link engine.view.ContainerElement ContainerElement}.
-// If element's name will not contain any additional information - {@link engine.view.Element view Element}will be
+// If element's name will not contain any additional information - {@link engine.view.Element view Element} will be
 // returned.
 //
 // @param {engine.view.Element} viewElement View element to convert.
 // @returns {engine.view.Element|engine.view.AttributeElement|engine.view.ContainerElement} Tree view
 // element converted according to it's name.
 function _convertElement( viewElement ) {
-	const info = _convertElementName( viewElement );
 	let newElement;
+	const info = _convertElementNameAndPriority( viewElement );
 
 	if ( info.type == 'attribute' ) {
 		newElement = new AttributeElement( info.name );
@@ -834,24 +835,26 @@ function _convertElement( viewElement ) {
 	return newElement;
 }
 
-// Converts {@link engine.view.Element#name Element's name} information needed for creating
+// Converts `view-priority` attribute and {@link engine.view.Element#name Element's name} information needed for creating
 // {@link engine.view.AttributeElement AttributeElement} or {@link engine.view.ContainerElement ContainerElement} instance.
-// Name can be provided in couple formats: as a simple element's name (`div`), as a type and name (`container:div`,
-// `attribute:span`), as a name and priority (`span:12`) and as a type, priority, name trio (`attribute:span:12`);
+// Name can be provided in two formats: as a simple element's name (`div`), or as a type and name (`container:div`,
+// `attribute:span`);
 //
 // @param {engine.view.Element} element Element which name should be converted.
 // @returns {Object} info Object with parsed information.
 // @returns {String} info.name Parsed name of the element.
 // @returns {String|null} info.type Parsed type of the element, can be `attribute` or `container`.
 // returns {Number|null} info.priority Parsed priority of the element.
-function _convertElementName( viewElement ) {
+function _convertElementNameAndPriority( viewElement ) {
 	const parts = viewElement.name.split( ':' );
+	const priority = _convertPriority( viewElement.getAttribute( VIEW_PRIORITY_ATTRIBUTE ) );
+	viewElement.removeAttribute( VIEW_PRIORITY_ATTRIBUTE );
 
 	if ( parts.length == 1 ) {
 		return {
 			name: parts[ 0 ],
-			type: null,
-			priority: null
+			type: priority !== null ? 'attribute' : null,
+			priority: priority
 		};
 	}
 
@@ -863,17 +866,6 @@ function _convertElementName( viewElement ) {
 			return {
 				name: parts[ 1 ],
 				type: type,
-				priority: null
-			};
-		}
-
-		// Check if name and priority: span:10.
-		const priority = _convertPriority( parts[ 1 ] );
-
-		if ( priority !== null ) {
-			return {
-				name: parts[ 0 ],
-				type: 'attribute',
 				priority: priority
 			};
 		}
@@ -881,20 +873,6 @@ function _convertElementName( viewElement ) {
 		throw new Error( `Parse error - cannot parse element's name: ${ viewElement.name }.` );
 	}
 
-	// Check if name is in format type:name:priority.
-	if ( parts.length === 3 ) {
-		const type = _convertType( parts[ 0 ] );
-		const priority = _convertPriority( parts[ 2 ] );
-
-		if ( type && priority !== null ) {
-			return {
-				name: parts[ 1 ],
-				type: type,
-				priority: priority
-			};
-		}
-	}
-
 	throw new Error( `Parse error - cannot parse element's tag name: ${ viewElement.name }.` );
 }