@abide/harness 0.7.1 → 0.8.0

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.8.0",
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.152.0"
60
60
  },
61
61
  "peerDependenciesMeta": {
62
62
  "@playwright/test": {
@@ -35,10 +35,19 @@ export type AbideWork = Work & {
35
35
  /**
36
36
  * Runs `body` inside an armed case and returns the work it did, abide's counters included.
37
37
  *
38
+ * A body answering a thenable is counted until it settles, as the lane's own `measure` is (43.50).
38
39
  * Throws where abide's record is published without one of its fields as a number (43.20).
39
40
  */
40
- export function measure(body: () => void): AbideWork {
41
- const work = measureWork(body)
41
+ export function measure(body: () => PromiseLike<unknown>): Promise<AbideWork>
42
+ export function measure(body: () => void): AbideWork
43
+ export function measure(body: () => unknown): AbideWork | Promise<AbideWork> {
44
+ const work: Work | Promise<Work> = measureWork(body as () => void)
45
+ return work instanceof Promise
46
+ ? work.then(withAbideCounters)
47
+ : withAbideCounters(work)
48
+ }
49
+
50
+ function withAbideCounters(work: Work): AbideWork {
42
51
  const counted = publishedBy(work, 'abide', FIELDS) ?? {
43
52
  wakes: null,
44
53
  bindingRuns: null,
@@ -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,81 @@
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
+ import { symbolNamed } from './symbolNamed.ts'
14
+
15
+ export function installSelect(): void {
16
+ const optionPrototype = HTMLOptionElement.prototype
17
+ if (Object.getOwnPropertyDescriptor(optionPrototype, 'defaultSelected'))
18
+ return
19
+ Object.defineProperty(optionPrototype, 'defaultSelected', {
20
+ configurable: true,
21
+ get(this: HTMLOptionElement) {
22
+ return this.hasAttribute('selected')
23
+ },
24
+ set(this: HTMLOptionElement, selected: boolean) {
25
+ this.toggleAttribute('selected', Boolean(selected))
26
+ },
27
+ })
28
+ const selectPrototype = HTMLSelectElement.prototype
29
+ const UPDATE = symbolNamed(selectPrototype, 'updateSelectedness', '43.47')
30
+ const SELECTEDNESS = symbolNamed(
31
+ document.createElement('option'),
32
+ 'selectedness',
33
+ '43.47',
34
+ )
35
+ type Held = HTMLOptionElement & Record<symbol, boolean>
36
+ const optionsOf = (select: HTMLSelectElement): Held[] =>
37
+ [...select.querySelectorAll('option')] as Held[]
38
+ const disabled = (option: HTMLOptionElement): boolean =>
39
+ option.disabled ||
40
+ (option.parentElement instanceof HTMLOptGroupElement &&
41
+ option.parentElement.disabled)
42
+ // https://html.spec.whatwg.org/multipage/form-elements.html#selectedness-setting-algorithm
43
+ Object.defineProperty(selectPrototype, UPDATE, {
44
+ configurable: true,
45
+ value(this: HTMLSelectElement, chosen?: HTMLOptionElement | null) {
46
+ // A multiple select keeps every option picked: one picked leaves the others be.
47
+ if (this.multiple) return
48
+ const options = optionsOf(this)
49
+ if (chosen)
50
+ for (const option of options)
51
+ option[SELECTEDNESS] = option === chosen
52
+ let last: Held | undefined
53
+ for (const option of options)
54
+ if (option[SELECTEDNESS]) last = option
55
+ if (last !== undefined) {
56
+ for (const option of options)
57
+ option[SELECTEDNESS] = option === last
58
+ return
59
+ }
60
+ if (this.size > 1) return
61
+ for (const option of options)
62
+ if (!disabled(option)) {
63
+ option[SELECTEDNESS] = true
64
+ return
65
+ }
66
+ },
67
+ })
68
+ const selectedIndex = Object.getOwnPropertyDescriptor(
69
+ selectPrototype,
70
+ 'selectedIndex',
71
+ )
72
+ Object.defineProperty(selectPrototype, 'selectedIndex', {
73
+ ...selectedIndex,
74
+ get(this: HTMLSelectElement) {
75
+ const options = optionsOf(this)
76
+ for (let index = 0; index < options.length; index++)
77
+ if (options[index]?.[SELECTEDNESS]) return index
78
+ return -1
79
+ },
80
+ })
81
+ }
@@ -53,15 +53,47 @@ function copyPublished(record: Work): void {
53
53
  }
54
54
  }
55
55
 
56
- /** Runs `body` inside an armed case and returns the `Work` it did. */
57
- export function measure(body: () => void): Work {
56
+ /**
57
+ * Runs `body` inside an armed case and returns the `Work` it did.
58
+ *
59
+ * A body answering a thenable keeps the case armed until it settles, so the answer is a promise of
60
+ * the record, and every counted call in that window lands in it, the body's or not (43.50).
61
+ */
62
+ export function measure(body: () => PromiseLike<unknown>): Promise<Work>
63
+ export function measure(body: () => void): Work
64
+ export function measure(body: () => unknown): Work | Promise<Work> {
58
65
  const record = blankWork()
59
66
  zeroPublished()
60
67
  arm(record)
68
+ let answer: unknown
61
69
  // The case is closed before a throw leaves, or the next `measure()` reports that one is
62
70
  // already armed and the real failure is two frames down.
63
71
  try {
64
- body()
72
+ answer = body()
73
+ } catch (error) {
74
+ disarm()
75
+ throw error
76
+ }
77
+ if (isThenable(answer)) return closeAfter(answer, record)
78
+ disarm()
79
+ copyPublished(record)
80
+ return record
81
+ }
82
+
83
+ function isThenable(value: unknown): value is PromiseLike<unknown> {
84
+ return (
85
+ value !== null &&
86
+ (typeof value === 'object' || typeof value === 'function') &&
87
+ typeof (value as { then?: unknown }).then === 'function'
88
+ )
89
+ }
90
+
91
+ async function closeAfter(
92
+ pending: PromiseLike<unknown>,
93
+ record: Work,
94
+ ): Promise<Work> {
95
+ try {
96
+ await pending
65
97
  } finally {
66
98
  disarm()
67
99
  }
@@ -4,7 +4,11 @@
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'
11
+ import { symbolNamed } from './symbolNamed.ts'
8
12
 
9
13
  // A child spawned with this env would paint its output in ANSI off a pipe (bun's console, and
10
14
  // abide's own log under RULEBOOK 36.9), and the suites compare that output as text. A terminal
@@ -57,6 +61,9 @@ for (const name of WIRE) Object.assign(globalThis, { [name]: native[name] })
57
61
  bridgeFiles()
58
62
  install()
59
63
  installPopover()
64
+ installSelect()
65
+ installFormReset()
66
+ await installLeadingNewline()
60
67
 
61
68
  /**
62
69
  * 43.37, 43.38 — ONE `File` FOR BOTH HALVES. The wire's are bun's (43.28), and happy-dom takes only
@@ -71,9 +78,7 @@ function bridgeFiles(): void {
71
78
  const RuntimeBlob = native.Blob as typeof Blob
72
79
  const RuntimeFile = native.File as typeof File
73
80
  const RuntimeFormData = native.FormData as typeof FormData
74
- const BYTES = Object.getOwnPropertySymbols(
75
- Reflect.construct(DomBlob, []),
76
- ).find((symbol) => symbol.description === 'buffer') as symbol
81
+ const BYTES = symbolNamed(Reflect.construct(DomBlob, []), 'buffer', '43.37')
77
82
  const bytesOf = (parts: readonly unknown[]): Buffer | undefined => {
78
83
  const buffers: Buffer[] = []
79
84
  for (const part of parts) {
@@ -0,0 +1,19 @@
1
+ /**
2
+ * The symbol `holder` keeps under `description`, a private one of happy-dom's the preload reaches
3
+ * for — thrown for by `clause` where happy-dom no longer keeps it, rather than read as `undefined`
4
+ * and degrading the bridge it was found for in silence.
5
+ */
6
+ export function symbolNamed(
7
+ holder: object,
8
+ description: string,
9
+ clause: string,
10
+ ): symbol {
11
+ const found = Object.getOwnPropertySymbols(holder).find(
12
+ (symbol) => symbol.description === description,
13
+ )
14
+ if (found === undefined)
15
+ throw new Error(
16
+ `@abide/harness: happy-dom no longer keeps '${description}' where the preload reads it. RULEBOOK ${clause}.`,
17
+ )
18
+ return found
19
+ }