Przeglądaj źródła

Changed: Allowed to pass `id` in `view.Writer#createAttributeElement`.

Szymon Cofalik 8 lat temu
rodzic
commit
fbbd144409

+ 17 - 4
packages/ckeditor5-engine/src/view/writer.js

@@ -135,15 +135,28 @@ export default class Writer {
 	 *		writer.createAttributeElement( 'strong' );
 	 *		writer.createAttributeElement( 'strong', { 'alignment': 'center' } );
 	 *
+	 *		// Make `<a>` element contain other attributes element so the `<a>` element is not broken.
+	 *		writer.createAttributeElement( 'a', { href: 'foo.bar' }, { priority: 5 } );
+	 *
+	 *		// Set `id` of a marker element so it is not joined or merged with "normal" elements.
+	 *		writer.createAttributeElement( 'span', { class: 'myMarker' }, { id: 'marker:my' } );
+	 *
 	 * @param {String} name Name of the element.
-	 * @param {Object} [attributes] Elements attributes.
+	 * @param {Object} [attributes] Element's attributes.
+	 * @param {Object} [options] Element's options.
+	 * @param {Number} [options.priority] Element's {@link module:engine/view/attributeelement~AttributeElement#priority priority}.
+	 * @param {Number|String} [options.id] Element's {@link module:engine/view/attributeelement~AttributeElement#id id}.
 	 * @returns {module:engine/view/attributeelement~AttributeElement} Created element.
 	 */
-	createAttributeElement( name, attributes, priority ) {
+	createAttributeElement( name, attributes, options = {} ) {
 		const attributeElement = new AttributeElement( name, attributes );
 
-		if ( priority ) {
-			attributeElement._priority = priority;
+		if ( options.priority ) {
+			attributeElement._priority = options.priority;
+		}
+
+		if ( options.id ) {
+			attributeElement._id = options.id;
 		}
 
 		return attributeElement;

+ 3 - 2
packages/ckeditor5-engine/tests/view/writer/writer.js

@@ -68,12 +68,13 @@ describe( 'Writer', () => {
 			assertElementAttributes( element, attributes );
 		} );
 
-		it( 'should allow to pass priority', () => {
-			const element = writer.createAttributeElement( 'foo', attributes, 99 );
+		it( 'should allow to pass additional options', () => {
+			const element = writer.createAttributeElement( 'foo', attributes, { priority: 99, id: 'bar' } );
 
 			expect( element.is( 'attributeElement' ) ).to.be.true;
 			expect( element.name ).to.equal( 'foo' );
 			expect( element.priority ).to.equal( 99 );
+			expect( element.id ).to.equal( 'bar' );
 			assertElementAttributes( element, attributes );
 		} );
 	} );