(el: DOMElement)
| 104 | * @returns The calculated height |
| 105 | */ |
| 106 | const calculateElementHeight = (el: DOMElement) => { |
| 107 | const { lineHeight, fontSize } = window.getComputedStyle(el) |
| 108 | |
| 109 | // TODO: Handle pt, em, rem units |
| 110 | |
| 111 | let height = 0 |
| 112 | // Ends with px |
| 113 | if (lineHeight.endsWith('px')) { |
| 114 | height = parseFloat(lineHeight) |
| 115 | return height |
| 116 | } |
| 117 | // Ends with % |
| 118 | else if (lineHeight.endsWith('%')) { |
| 119 | height = parseInt(lineHeight, 10) / 100 |
| 120 | } |
| 121 | // Number |
| 122 | else if (/^\d+(\.\d+)?$/.test(lineHeight)) { |
| 123 | height = parseFloat(lineHeight) |
| 124 | } else { |
| 125 | return el.getBoundingClientRect().height |
| 126 | } |
| 127 | |
| 128 | let size = 0 |
| 129 | if (fontSize.endsWith('px')) { |
| 130 | size = parseInt(fontSize, 10) |
| 131 | } |
| 132 | |
| 133 | return height * size |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Resets the DOMRect of an element to the specified height |
no outgoing calls
no test coverage detected
searching dependent graphs…