From 3bd1db400848ad022688f7bd63a58ed672d6c6bf Mon Sep 17 00:00:00 2001 From: Ryan Thomson Date: Thu, 9 May 2024 19:05:17 -0400 Subject: [PATCH] feat: [#1431] Implement HTMLTimeElement --- .../happy-dom/src/config/HTMLElementConfig.ts | 2 +- .../src/config/IHTMLElementTagNameMap.ts | 3 +- packages/happy-dom/src/index.ts | 3 +- .../html-time-element/HTMLTimeElement.ts | 27 +++++++++++++++ .../happy-dom/src/window/BrowserWindow.ts | 3 +- .../html-time-element/HTMLTimeElement.test.ts | 34 +++++++++++++++++++ 6 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 packages/happy-dom/src/nodes/html-time-element/HTMLTimeElement.ts create mode 100644 packages/happy-dom/test/nodes/html-time-element/HTMLTimeElement.test.ts diff --git a/packages/happy-dom/src/config/HTMLElementConfig.ts b/packages/happy-dom/src/config/HTMLElementConfig.ts index 2bf98409..8ee9e534 100644 --- a/packages/happy-dom/src/config/HTMLElementConfig.ts +++ b/packages/happy-dom/src/config/HTMLElementConfig.ts @@ -654,7 +654,7 @@ export default <{ [key: string]: IHTMLElementConfigEntity }>{ contentModel: HTMLElementConfigContentModelEnum.anyDescendants }, time: { - className: 'HTMLElement', + className: 'HTMLTimeElement', localName: 'time', tagName: 'TIME', contentModel: HTMLElementConfigContentModelEnum.anyDescendants diff --git a/packages/happy-dom/src/config/IHTMLElementTagNameMap.ts b/packages/happy-dom/src/config/IHTMLElementTagNameMap.ts index 6c3ab496..39337a22 100644 --- a/packages/happy-dom/src/config/IHTMLElementTagNameMap.ts +++ b/packages/happy-dom/src/config/IHTMLElementTagNameMap.ts @@ -19,6 +19,7 @@ import HTMLIFrameElement from '../nodes/html-iframe-element/HTMLIFrameElement.js import HTMLOptGroupElement from '../nodes/html-opt-group-element/HTMLOptGroupElement.js'; import HTMLOptionElement from '../nodes/html-option-element/HTMLOptionElement.js'; import HTMLSelectElement from '../nodes/html-select-element/HTMLSelectElement.js'; +import HTMLTimeElement from '../nodes/html-time-element/HTMLTimeElement.js'; import HTMLVideoElement from '../nodes/html-video-element/HTMLVideoElement.js'; // Makes it work with custom elements when they declare their own interface. @@ -139,7 +140,7 @@ export default interface IHTMLElementTagNameMap extends HTMLElementTagNameMap { tfoot: HTMLElement; th: HTMLElement; thead: HTMLElement; - time: HTMLElement; + time: HTMLTimeElement; title: HTMLElement; tr: HTMLElement; track: HTMLElement; diff --git a/packages/happy-dom/src/index.ts b/packages/happy-dom/src/index.ts index 8f984ea3..d1a329bf 100644 --- a/packages/happy-dom/src/index.ts +++ b/packages/happy-dom/src/index.ts @@ -99,6 +99,7 @@ import HTMLSlotElement from './nodes/html-slot-element/HTMLSlotElement.js'; import HTMLStyleElement from './nodes/html-style-element/HTMLStyleElement.js'; import HTMLTemplateElement from './nodes/html-template-element/HTMLTemplateElement.js'; import HTMLTextAreaElement from './nodes/html-text-area-element/HTMLTextAreaElement.js'; +import HTMLTimeElement from './nodes/html-time-element/HTMLTimeElement.js'; import HTMLUnknownElement from './nodes/html-unknown-element/HTMLUnknownElement.js'; import HTMLVideoElement from './nodes/html-video-element/HTMLVideoElement.js'; import Node from './nodes/node/Node.js'; @@ -303,7 +304,7 @@ export { HTMLElement as HTMLTableSectionElement, HTMLTemplateElement, HTMLTextAreaElement, - HTMLElement as HTMLTimeElement, + HTMLTimeElement, HTMLElement as HTMLTitleElement, HTMLElement as HTMLTrackElement, HTMLElement as HTMLUListElement, diff --git a/packages/happy-dom/src/nodes/html-time-element/HTMLTimeElement.ts b/packages/happy-dom/src/nodes/html-time-element/HTMLTimeElement.ts new file mode 100644 index 00000000..3adb3fbc --- /dev/null +++ b/packages/happy-dom/src/nodes/html-time-element/HTMLTimeElement.ts @@ -0,0 +1,27 @@ +import HTMLElement from '../html-element/HTMLElement.js'; + +/** + * HTML Time Element. + * + * Reference: + * https://developer.mozilla.org/en-US/docs/Web/API/HTMLTimeElement + */ +export default class HTMLTimeElement extends HTMLElement { + /** + * Returns dateTime. + * + * @returns dateTime. + */ + public get dateTime(): string { + return this.getAttribute('dateTime') || ''; + } + + /** + * Sets dateTime. + * + * @param dateTime dateTime. + */ + public set dateTime(dateTime: string) { + this.setAttribute('dateTime', dateTime); + } +} diff --git a/packages/happy-dom/src/window/BrowserWindow.ts b/packages/happy-dom/src/window/BrowserWindow.ts index 70a6e827..6213c76d 100644 --- a/packages/happy-dom/src/window/BrowserWindow.ts +++ b/packages/happy-dom/src/window/BrowserWindow.ts @@ -134,6 +134,7 @@ import HTMLAnchorElement from '../nodes/html-anchor-element/HTMLAnchorElement.js import HTMLButtonElement from '../nodes/html-button-element/HTMLButtonElement.js'; import HTMLOptionElement from '../nodes/html-option-element/HTMLOptionElement.js'; import HTMLOptGroupElement from '../nodes/html-opt-group-element/HTMLOptGroupElement.js'; +import HTMLTimeElement from '../nodes/html-time-element/HTMLTimeElement.js'; import WindowPageOpenUtility from './WindowPageOpenUtility.js'; import IResponseBody from '../fetch/types/IResponseBody.js'; import IResponseInit from '../fetch/types/IResponseInit.js'; @@ -214,6 +215,7 @@ export default class BrowserWindow extends EventTarget implements INodeJSGlobal public readonly HTMLLinkElement: typeof HTMLLinkElementImplementation; public readonly HTMLIFrameElement: typeof HTMLIFrameElementImplementation; public readonly HTMLFormElement: typeof HTMLFormElementImplementation; + public readonly HTMLTimeElement: typeof HTMLTimeElement = HTMLTimeElement; // Non-implemented element classes public readonly HTMLHeadElement: typeof HTMLElement = HTMLElement; @@ -254,7 +256,6 @@ export default class BrowserWindow extends EventTarget implements INodeJSGlobal public readonly HTMLTableCellElement: typeof HTMLElement = HTMLElement; public readonly HTMLTableColElement: typeof HTMLElement = HTMLElement; public readonly HTMLTableElement: typeof HTMLElement = HTMLElement; - public readonly HTMLTimeElement: typeof HTMLElement = HTMLElement; public readonly HTMLTableRowElement: typeof HTMLElement = HTMLElement; public readonly HTMLTableSectionElement: typeof HTMLElement = HTMLElement; public readonly HTMLFrameElement: typeof HTMLElement = HTMLElement; diff --git a/packages/happy-dom/test/nodes/html-time-element/HTMLTimeElement.test.ts b/packages/happy-dom/test/nodes/html-time-element/HTMLTimeElement.test.ts new file mode 100644 index 00000000..caf5bb30 --- /dev/null +++ b/packages/happy-dom/test/nodes/html-time-element/HTMLTimeElement.test.ts @@ -0,0 +1,34 @@ +import { beforeEach, describe, expect, it } from 'vitest'; +import Document from '../../../src/nodes/document/Document.js'; +import HTMLTimeElement from '../../../src/nodes/html-time-element/HTMLTimeElement.js'; +import Window from '../../../src/window/Window.js'; + +describe('HTMLTimeElement', () => { + let window: Window; + let document: Document; + let element: HTMLTimeElement; + + beforeEach(() => { + window = new Window(); + document = window.document; + element = document.createElement('time'); + }); + + describe('get dateTime()', () => { + it('Gets the attribute value "datetime".', () => { + element.setAttribute('datetime', '1969-07-20'); + expect(element.dateTime).toBe('1969-07-20'); + }); + + it('Returns "" if the "datetime" attribute is not set.', () => { + expect(element.dateTime).toBe(''); + }); + }); + + describe('set dateTime()', () => { + it('Sets the attribute value "datetime".', () => { + element.dateTime = '1969-07-20'; + expect(element.getAttribute('datetime')).toBe('1969-07-20'); + }); + }); +});