Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Commit

Permalink
Fix: Remove the usage of ShadowRoot (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
bigtimebuddy authored Jan 3, 2023
1 parent ccede45 commit 65f7c29
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 50 deletions.
21 changes: 3 additions & 18 deletions src/HTMLText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ export class HTMLText extends Sprite
private _style: HTMLTextStyle | null = null;
private _autoResolution = true;
private _loading = false;
private _shadow: HTMLElement;
private _shadowRoot: ShadowRoot;
private localStyleID = -1;
private dirty = false;

Expand Down Expand Up @@ -88,7 +86,6 @@ export class HTMLText extends Sprite

const nssvg = 'http://www.w3.org/2000/svg';
const nsxhtml = 'http://www.w3.org/1999/xhtml';
const shadow = document.createElement('div');
const svgRoot = document.createElementNS(nssvg, 'svg');
const foreignObject = document.createElementNS(nssvg, 'foreignObject');
const domElement = document.createElementNS(nsxhtml, 'div');
Expand All @@ -102,7 +99,6 @@ export class HTMLText extends Sprite

this.maxWidth = HTMLText.defaultMaxWidth;
this.maxHeight = HTMLText.defaultMaxHeight;
this._shadow = shadow;
this._domElement = domElement;
this._styleElement = styleElement;
this._svgRoot = svgRoot;
Expand All @@ -111,17 +107,6 @@ export class HTMLText extends Sprite
this._foreignObject.appendChild(domElement);
this._image = image;
this._autoResolution = HTMLText.defaultAutoResolution;
this._shadowRoot = shadow.attachShadow({ mode: 'open' });
this._shadowRoot.appendChild(svgRoot);
shadow.setAttribute('data-pixi-html-text', '1');
Object.assign(shadow.style, {
position: 'absolute',
top: '0',
left: '-1px',
width: '1px',
height: '1px',
});
document.body.appendChild(shadow);
this._resolution = HTMLText.defaultResolution ?? settings.RESOLUTION;
this.text = text;
this.style = style;
Expand Down Expand Up @@ -152,8 +137,11 @@ export class HTMLText extends Sprite
this._styleElement.textContent = style.toGlobalCSS();

// Measure the contents using the shadow DOM
document.body.appendChild(this._svgRoot);
const contentBounds = this._domElement.getBoundingClientRect();

this._svgRoot.remove();

const contentWidth = Math.min(this.maxWidth, Math.ceil(contentBounds.width));
const contentHeight = Math.min(this.maxHeight, Math.ceil(contentBounds.height));

Expand Down Expand Up @@ -369,9 +357,6 @@ export class HTMLText extends Sprite
this._foreignObject = forceClear;
this._styleElement?.remove();
this._styleElement = forceClear;
this._shadow?.remove();
this._shadow = forceClear;
this._shadowRoot = forceClear;
this._image.onload = null;
this._image.src = '';
this._image = forceClear;
Expand Down
32 changes: 0 additions & 32 deletions test/HTMLText.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,6 @@ describe('HTMLText', () =>
text.destroy();
});

it('should clean up the shadow element', () =>
{
const query = '[data-pixi-html-text]';

expect(document.querySelector(query)).toBeFalsy();

const text = new HTMLText('Hello world!');

expect(document.querySelector(query)).toBeTruthy();

text.destroy();

expect(document.querySelector(query)).toBeFalsy();
});

it('should clean up the shadow element multiples', () =>
{
const query = '[data-pixi-html-text]';

expect(document.querySelector(query)).toBeFalsy();

const text = new HTMLText('Hello world!');
const text2 = new HTMLText('Hello world2!');

expect(document.querySelector(query)).toBeTruthy();

text.destroy();
text2.destroy();

expect(document.querySelector(query)).toBeFalsy();
});

describe('measureText', () =>
{
it('should measure default text', () =>
Expand Down

0 comments on commit 65f7c29

Please sign in to comment.