From 7914b54c4849b621545aa65ce3055de7feb4268e Mon Sep 17 00:00:00 2001 From: Benjamin Koltes Date: Thu, 14 Nov 2024 01:51:41 +0100 Subject: [PATCH] fix: [#1611] Implement window.TextEncoder & window.TextDecoder Co-authored-by: David Ortner --- packages/happy-dom/src/window/BrowserWindow.ts | 3 +++ .../test/window/BrowserWindow.test.ts | 18 +++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/happy-dom/src/window/BrowserWindow.ts b/packages/happy-dom/src/window/BrowserWindow.ts index 455d5494..3f4aec22 100644 --- a/packages/happy-dom/src/window/BrowserWindow.ts +++ b/packages/happy-dom/src/window/BrowserWindow.ts @@ -1,5 +1,6 @@ import { Buffer } from 'buffer'; import { webcrypto } from 'crypto'; +import { TextEncoder, TextDecoder } from 'util'; import Stream from 'stream'; import { ReadableStream } from 'stream/web'; import { URLSearchParams } from 'url'; @@ -707,6 +708,8 @@ export default class BrowserWindow extends EventTarget implements INodeJSGlobal public readonly screenX: number = 0; public readonly screenY: number = 0; public readonly crypto: typeof webcrypto = webcrypto; + public readonly TextEncoder: typeof TextEncoder = TextEncoder; + public readonly TextDecoder: typeof TextDecoder = TextDecoder; public readonly closed = false; public console: Console; public name = ''; diff --git a/packages/happy-dom/test/window/BrowserWindow.test.ts b/packages/happy-dom/test/window/BrowserWindow.test.ts index afb297c8..100572ce 100644 --- a/packages/happy-dom/test/window/BrowserWindow.test.ts +++ b/packages/happy-dom/test/window/BrowserWindow.test.ts @@ -200,6 +200,18 @@ describe('BrowserWindow', () => { }); }); + describe('get TextEncoder()', () => { + it('Returns an instance of TextEncoder.', () => { + expect(window.TextEncoder).toBe(TextEncoder); + }); + }); + + describe('get TextDecoder()', () => { + it('Returns an instance of TextDecoder.', () => { + expect(window.TextDecoder).toBe(TextDecoder); + }); + }); + describe('get location()', () => { it('Returns an instance of Location', () => { expect(window.location).toBeInstanceOf(Location); @@ -489,7 +501,7 @@ describe('BrowserWindow', () => { elementStyle.innerHTML = ` span { border: 1px solid #000; - border-radius: 2px; + border-radius: 2px; color: green; cursor: default; direction: ltr; @@ -664,7 +676,7 @@ describe('BrowserWindow', () => { --result1: var(--color1, var(--unknown, var(--unknown, green))); --result2: var(--unknown, var(--color1, var(--unknown, green))); --result3: var(--unknown, var(--unknown, var(--color1, green))); - + --result4: var(--unknown, var(--unknown, var(--unknown, var(--unknown, white)))); --result5: var(--color1, var(--color2)); @@ -920,7 +932,7 @@ describe('BrowserWindow', () => { span { color: green; } - + :not { color: red; }