]> git.proxmox.com Git - mirror_xterm.js.git/blame - src/utils/CharMeasure.ts
Convert CharMeasure phantom test to jsdom
[mirror_xterm.js.git] / src / utils / CharMeasure.ts
CommitLineData
4f18d842
DI
1/**
2 * @module xterm/utils/CharMeasure
3 * @license MIT
4 */
5
6import { EventEmitter } from '../EventEmitter.js';
7
8/**
9 * Utility class that measures the size of a character.
10 */
11export class CharMeasure extends EventEmitter {
202b54af 12 private _document: Document;
4f18d842
DI
13 private _parentElement: HTMLElement;
14 private _measureElement: HTMLElement;
15 private _width: number;
16 private _height: number;
17
202b54af 18 constructor(document: Document, parentElement: HTMLElement) {
4f18d842 19 super();
202b54af 20 this._document = document;
4f18d842
DI
21 this._parentElement = parentElement;
22 }
23
24 public get width(): number {
25 return this._width;
26 }
27
28 public get height(): number {
29 return this._height;
30 }
31
32 public measure(): void {
4f18d842 33 if (!this._measureElement) {
202b54af 34 this._measureElement = this._document.createElement('span');
4f18d842
DI
35 this._measureElement.style.position = 'absolute';
36 this._measureElement.style.top = '0';
37 this._measureElement.style.left = '-9999em';
38 this._measureElement.textContent = 'W';
d06c920a 39 this._measureElement.setAttribute('aria-hidden', 'true');
d9682bd6
DI
40 this._parentElement.appendChild(this._measureElement);
41 // Perform _doMeasure async if the element was just attached as sometimes
42 // getBoundingClientRect does not return accurate values without this.
43 setTimeout(() => this._doMeasure(), 0);
44 } else {
45 this._doMeasure();
4f18d842 46 }
d9682bd6 47 }
4f18d842 48
d9682bd6 49 private _doMeasure(): void {
4f18d842 50 const geometry = this._measureElement.getBoundingClientRect();
9626b1e3
DI
51 // The element is likely currently display:none, we should retain the
52 // previous value.
53 if (geometry.width === 0 || geometry.height === 0) {
54 return;
55 }
d9682bd6
DI
56 if (this._width !== geometry.width || this._height !== geometry.height) {
57 this._width = geometry.width;
58 this._height = geometry.height;
4f18d842
DI
59 this.emit('charsizechanged');
60 }
61 }
62}