Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/page-controller/src/dom/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { describe, expect, it } from 'vitest'

import { getElementTextMap } from './index'

describe('getElementTextMap', () => {
it('maps existing (non-new) element lines to their description', () => {
const simplifiedHTML = ['[0]<a aria-label=Home />', '[1]<button >Submit />'].join('\n')

const map = getElementTextMap(simplifiedHTML)

expect(map.get(0)).toBe('[0]<a aria-label=Home />')
expect(map.get(1)).toBe('[1]<button >Submit />')
})

it('maps new element lines marked with a leading "*"', () => {
const simplifiedHTML = ['[0]<a aria-label=Home />', '*[5]<a role=button>Get started />'].join(
'\n'
)

const map = getElementTextMap(simplifiedHTML)

// New elements are prefixed with '*' and must still be indexed.
expect(map.get(5)).toBe('*[5]<a role=button>Get started />')
})

it('indents do not prevent matching', () => {
const simplifiedHTML = '\t\t*[3]<div >Option />'

const map = getElementTextMap(simplifiedHTML)

expect(map.get(3)).toBe('*[3]<div >Option />')
})
})
3 changes: 2 additions & 1 deletion packages/page-controller/src/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,8 @@ export function getElementTextMap(simplifiedHTML: string) {
.filter((line) => line.length > 0)
const elementTextMap = new Map<number, string>()
for (const line of lines) {
const regex = /^\[(\d+)\]<[^>]+>([^<]*)/
// New elements are prefixed with '*' (e.g. `*[5]<a ...`), so allow an optional leading '*'.
const regex = /^\*?\[(\d+)\]<[^>]+>([^<]*)/
const match = regex.exec(line)
if (match) {
const index = parseInt(match[1], 10)
Expand Down