-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
utils.ts
58 lines (52 loc) · 1.27 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module paste-from-office/filters/utils
*/
/**
* Normalizes CSS length value to 'px'.
*
* @internal
*/
export function convertCssLengthToPx( value: string ): string {
const numericValue = parseFloat( value );
if ( value.endsWith( 'pt' ) ) {
// 1pt = 1in / 72
return toPx( numericValue * 96 / 72 );
}
else if ( value.endsWith( 'pc' ) ) {
// 1pc = 12pt = 1in / 6.
return toPx( numericValue * 12 * 96 / 72 );
}
else if ( value.endsWith( 'in' ) ) {
// 1in = 2.54cm = 96px
return toPx( numericValue * 96 );
}
else if ( value.endsWith( 'cm' ) ) {
// 1cm = 96px / 2.54
return toPx( numericValue * 96 / 2.54 );
}
else if ( value.endsWith( 'mm' ) ) {
// 1mm = 1cm / 10
return toPx( numericValue / 10 * 96 / 2.54 );
}
return value;
}
/**
* Returns true for value with 'px' unit.
*
* @internal
*/
export function isPx( value?: string ): value is string {
return value !== undefined && value.endsWith( 'px' );
}
/**
* Returns a rounded 'px' value.
*
* @internal
*/
export function toPx( value: number ): string {
return value.toFixed( 2 ).replace( /\.?0+$/, '' ) + 'px';
}