UNPKG

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