浏览代码

Added DocumentSelection#getSelectedElement().

Maciej Bukowski 7 年之前
父节点
当前提交
2d3166569d

+ 11 - 0
packages/ckeditor5-engine/src/model/documentselection.js

@@ -244,6 +244,17 @@ export default class DocumentSelection {
 	}
 
 	/**
+	 * Returns the selected element. {@link module:engine/model/element~Element Element} is considered as selected if there is only
+	 * one range in the selection, and that range contains exactly one element.
+	 * Returns `null` if there is no selected element.
+	 *
+	 * @returns {module:engine/model/element~Element|null}
+	 */
+	getSelectedElement() {
+		return this._selection.getSelectedElement();
+	}
+
+	/**
 	 * Checks whether the selection contains the entire content of the given element. This means that selection must start
 	 * at a position {@link module:engine/model/position~Position#isTouching touching} the element's start and ends at position
 	 * touching the element's end.

+ 9 - 0
packages/ckeditor5-engine/tests/model/documentselection.js

@@ -392,6 +392,15 @@ describe( 'DocumentSelection', () => {
 		} );
 	} );
 
+	describe( 'getSelectedElement()', () => {
+		it( 'should return selected element', () => {
+			selection._setTo( liveRange );
+			const p = root.getChild( 0 );
+
+			expect( selection.getSelectedElement() ).to.equal( p );
+		} );
+	} );
+
 	// DocumentSelection uses LiveRanges so here are only simple test to see if integration is
 	// working well, without getting into complicated corner cases.
 	describe( 'after applying an operation should get updated and fire events', () => {