All files / roosterjs-content-model-dom/lib/domUtils normalizeRect.ts

100% Statements 7/7
87.5% Branches 7/8
100% Functions 1/1
100% Lines 4/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20              1x 176x 176x 176x                  
import type { Rect } from 'roosterjs-content-model-types';
 
/**
 * A ClientRect of all 0 is possible. i.e. chrome returns a ClientRect of 0 when the cursor is on an empty p
 * We validate that and only return a rect when the passed in ClientRect is valid
 * @param clientRect Client rect object normally retrieved from getBoundingClientRect function
 */
export function normalizeRect(clientRect: DOMRect): Rect | null {
    const { left, right, top, bottom } =
        clientRect || <DOMRect>{ left: 0, right: 0, top: 0, bottom: 0 };
    return left === 0 && right === 0 && top === 0 && bottom === 0
        ? null
        : {
              left: Math.round(left),
              right: Math.round(right),
              top: Math.round(top),
              bottom: Math.round(bottom),
          };
}