8
0
Эх сурвалжийг харах

Implemented the getBorderWidths() DOM helper.

Aleksander Nowodzinski 8 жил өмнө
parent
commit
dbaad47df1

+ 33 - 0
packages/ckeditor5-utils/src/dom/getborderwidths.js

@@ -0,0 +1,33 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/**
+ * @module utils/dom/getborderwidths
+ */
+
+import global from './global';
+
+/**
+ * Returns an object containing CSS border withs of a specified `HTMLElement`.
+ *
+ * @param {HTMLElement} element An element which has CSS borders.
+ * @param {Object} An object containing `top`, `left`, `right` and `bottom` properties
+ * with numerical values of the `border-[top,left,right,bottom]-width` CSS styles.
+ */
+export default function getBorderWidths( element ) {
+	const computedStyles = global.window.getComputedStyle( element );
+	const borderWidths = {
+		top: computedStyles.borderTopWidth,
+		right: computedStyles.borderRightWidth,
+		bottom: computedStyles.borderBottomWidth,
+		left: computedStyles.borderLeftWidth
+	};
+
+	for ( const width in borderWidths ) {
+		borderWidths[ width ] = parseInt( borderWidths[ width ], 10 );
+	}
+
+	return borderWidths;
+}

+ 30 - 0
packages/ckeditor5-utils/tests/dom/getborderwidths.js

@@ -0,0 +1,30 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import global from '../../src/dom/global';
+import getBorderWidths from '../../src/dom/getborderwidths';
+import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
+
+testUtils.createSinonSandbox();
+
+describe( 'getBorderWidths()', () => {
+	it( 'returns CSS border widths', () => {
+		testUtils.sinon.stub( global.window, 'getComputedStyle' ).returns( {
+			borderTopWidth: '10px',
+			borderRightWidth: '20px',
+			borderLeftWidth: '30px',
+			borderBottomWidth: '40px'
+		} );
+
+		const elementMock = {};
+
+		expect( getBorderWidths( elementMock ) ).to.deep.equal( {
+			top: 10,
+			right: 20,
+			bottom: 30,
+			left: 40
+		} );
+	} );
+} );