@abide/harness 0.7.1 → 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abide/harness",
3
- "version": "0.7.1",
3
+ "version": "0.7.2",
4
4
  "type": "module",
5
5
  "description": "Ratios against hand-written code, counts of the work an op did, and gates whose revert can be run — three measurement lanes for anything that renders, on bun",
6
6
  "license": "MIT",
@@ -56,7 +56,7 @@
56
56
  },
57
57
  "peerDependencies": {
58
58
  "@playwright/test": "^1.64.0",
59
- "@abide/abide": ">=0.144.0"
59
+ "@abide/abide": ">=0.145.0"
60
60
  },
61
61
  "peerDependenciesMeta": {
62
62
  "@playwright/test": {
@@ -0,0 +1,15 @@
1
+ // 43.48 — happy-dom's form reset puts back every field but a file input, which keeps the files picked:
2
+ // the HTML reset algorithm empties a file input's list of selected files. A form that starts over
3
+ // after a call (56.34) left the picked file standing under `bun test`, where a browser clears it.
4
+ // Setting `value` to `''` is the one write a file input takes, and happy-dom empties its files on it.
5
+
6
+ export function installFormReset(): void {
7
+ const prototype = HTMLFormElement.prototype
8
+ const reset = prototype.reset
9
+ prototype.reset = function (this: HTMLFormElement): void {
10
+ for (const element of this.elements)
11
+ if (element instanceof HTMLInputElement && element.type === 'file')
12
+ element.value = ''
13
+ reset.call(this)
14
+ }
15
+ }
@@ -0,0 +1,47 @@
1
+ // 43.49 — the HTML parser drops the one newline that follows a `<textarea>`, `<pre>` or `<listing>`
2
+ // start tag, so a writer leads such an element's text with a newline wherever that text may itself
3
+ // open with one. happy-dom keeps it, and a textarea abide wrote read `"\n"` for its empty value —
4
+ // what a form reset to, and what a test read back. This drops it where the spec does: the first text
5
+ // the parser hands such an element, before anything else is in it.
6
+ //
7
+ // happy-dom's parser is no export of the global registrator, so it is resolved beside the copy of
8
+ // happy-dom the registrator itself loads, and patched there.
9
+
10
+ const LEADING_NEWLINE_ELEMENTS = new Set(['TEXTAREA', 'PRE', 'LISTING'])
11
+
12
+ type Parser = {
13
+ currentNode:
14
+ { tagName?: string | undefined; firstChild: unknown } | undefined
15
+ parsePlainText(text: string): void
16
+ }
17
+
18
+ export async function installLeadingNewline(): Promise<void> {
19
+ const registrator = Bun.resolveSync(
20
+ '@happy-dom/global-registrator',
21
+ import.meta.dir,
22
+ )
23
+ const parserPath = Bun.resolveSync(
24
+ 'happy-dom/lib/html-parser/HTMLParser.js',
25
+ registrator.slice(0, registrator.lastIndexOf('/')),
26
+ )
27
+ const { default: HTMLParser } = (await import(parserPath)) as {
28
+ default: { prototype: Parser }
29
+ }
30
+ const prototype = HTMLParser.prototype
31
+ const parsePlainText = prototype.parsePlainText
32
+ if (typeof parsePlainText !== 'function')
33
+ throw new Error(
34
+ "@abide/harness: happy-dom's parser no longer reads text where installLeadingNewline patches it. RULEBOOK 43.49.",
35
+ )
36
+ prototype.parsePlainText = function (this: Parser, text: string): void {
37
+ const node = this.currentNode
38
+ parsePlainText.call(
39
+ this,
40
+ node !== undefined &&
41
+ node.firstChild === null &&
42
+ LEADING_NEWLINE_ELEMENTS.has(node.tagName ?? '')
43
+ ? text.replace(/^\r?\n/, '')
44
+ : text,
45
+ )
46
+ }
47
+ }
@@ -0,0 +1,88 @@
1
+ // 43.47 — happy-dom's `<option>` has no `defaultSelected`, and its select lands on the wrong option
2
+ // when one is built from markup: its parser connects the first option before the marked one, the
3
+ // first is chosen as the default, and of the two then selected it keeps the one AT THE INDEX of the
4
+ // count less one rather than the last of them (a mark on the third option of four lands on the
5
+ // second). A select cloned out of a `<template>` keeps the index it never computed, -1. A form reads
6
+ // its baseline off `defaultSelected` (56.14), so under it every select read as edited at the first
7
+ // keystroke. This is the selectedness algorithm as the spec writes it, run where happy-dom runs its
8
+ // own, and `selectedIndex` read off the options rather than off a copy that can go stale.
9
+ //
10
+ // happy-dom keeps selectedness under its own symbols, which no export names; they are found by their
11
+ // descriptions, as `bridgeFiles` finds the one a `Blob` keeps its bytes under.
12
+
13
+ export function installSelect(): void {
14
+ const optionPrototype = HTMLOptionElement.prototype
15
+ if (Object.getOwnPropertyDescriptor(optionPrototype, 'defaultSelected'))
16
+ return
17
+ Object.defineProperty(optionPrototype, 'defaultSelected', {
18
+ configurable: true,
19
+ get(this: HTMLOptionElement) {
20
+ return this.hasAttribute('selected')
21
+ },
22
+ set(this: HTMLOptionElement, selected: boolean) {
23
+ this.toggleAttribute('selected', Boolean(selected))
24
+ },
25
+ })
26
+ const symbolNamed = (holder: object, description: string): symbol => {
27
+ const found = Object.getOwnPropertySymbols(holder).find(
28
+ (symbol) => symbol.description === description,
29
+ )
30
+ if (found === undefined)
31
+ throw new Error(
32
+ `@abide/harness: happy-dom no longer keeps a select's '${description}' where installSelect reads it. RULEBOOK 43.47.`,
33
+ )
34
+ return found
35
+ }
36
+ const selectPrototype = HTMLSelectElement.prototype
37
+ const UPDATE = symbolNamed(selectPrototype, 'updateSelectedness')
38
+ const SELECTEDNESS = symbolNamed(
39
+ document.createElement('option'),
40
+ 'selectedness',
41
+ )
42
+ type Held = HTMLOptionElement & Record<symbol, boolean>
43
+ const optionsOf = (select: HTMLSelectElement): Held[] =>
44
+ [...select.querySelectorAll('option')] as Held[]
45
+ const disabled = (option: HTMLOptionElement): boolean =>
46
+ option.disabled ||
47
+ (option.parentElement instanceof HTMLOptGroupElement &&
48
+ option.parentElement.disabled)
49
+ // https://html.spec.whatwg.org/multipage/form-elements.html#selectedness-setting-algorithm
50
+ Object.defineProperty(selectPrototype, UPDATE, {
51
+ configurable: true,
52
+ value(this: HTMLSelectElement, chosen?: HTMLOptionElement | null) {
53
+ // A multiple select keeps every option picked: one picked leaves the others be.
54
+ if (this.multiple) return
55
+ const options = optionsOf(this)
56
+ if (chosen)
57
+ for (const option of options)
58
+ option[SELECTEDNESS] = option === chosen
59
+ let last: Held | undefined
60
+ for (const option of options)
61
+ if (option[SELECTEDNESS]) last = option
62
+ if (last !== undefined) {
63
+ for (const option of options)
64
+ option[SELECTEDNESS] = option === last
65
+ return
66
+ }
67
+ if (this.size > 1) return
68
+ for (const option of options)
69
+ if (!disabled(option)) {
70
+ option[SELECTEDNESS] = true
71
+ return
72
+ }
73
+ },
74
+ })
75
+ const selectedIndex = Object.getOwnPropertyDescriptor(
76
+ selectPrototype,
77
+ 'selectedIndex',
78
+ )
79
+ Object.defineProperty(selectPrototype, 'selectedIndex', {
80
+ ...selectedIndex,
81
+ get(this: HTMLSelectElement) {
82
+ const options = optionsOf(this)
83
+ for (let index = 0; index < options.length; index++)
84
+ if (options[index]?.[SELECTEDNESS]) return index
85
+ return -1
86
+ },
87
+ })
88
+ }
@@ -4,7 +4,10 @@
4
4
  import { GlobalRegistrator } from '@happy-dom/global-registrator'
5
5
 
6
6
  import { install } from './install.ts'
7
+ import { installFormReset } from './installFormReset.ts'
8
+ import { installLeadingNewline } from './installLeadingNewline.ts'
7
9
  import { installPopover } from './installPopover.ts'
10
+ import { installSelect } from './installSelect.ts'
8
11
 
9
12
  // A child spawned with this env would paint its output in ANSI off a pipe (bun's console, and
10
13
  // abide's own log under RULEBOOK 36.9), and the suites compare that output as text. A terminal
@@ -57,6 +60,9 @@ for (const name of WIRE) Object.assign(globalThis, { [name]: native[name] })
57
60
  bridgeFiles()
58
61
  install()
59
62
  installPopover()
63
+ installSelect()
64
+ installFormReset()
65
+ await installLeadingNewline()
60
66
 
61
67
  /**
62
68
  * 43.37, 43.38 — ONE `File` FOR BOTH HALVES. The wire's are bun's (43.28), and happy-dom takes only