Skip to content
Myr

records.ts

This file

Path

apps/web/test/records.ts

52 lines · 1.4 KiB · TypeScript

Last changed by

Merge redesign into mainoscabrielbefore adoptioncaedd21

History of this file

Take it

Merge redesign into mainoscabrielbefore adoptioncaedd21

line 1why, one step back
1// A page's facts sit in the page above its content, so a test reads them apart from the rest of the main region.

Merge build/1222-project-recordsoscabrielbefore adoption2e6b5a5

lines 2–50why, one step back
2
3const balancedAt = (markup: string, start: number) => {4 const tag = /^<(?<name>[a-z0-9]+)/u.exec(markup.slice(start))?.groups?.name;56 if (tag === undefined) {7 return "";8 }910 const tags = new RegExp(`<(?<close>/?)${tag}\\b[^>]*>`, "gu");11 tags.lastIndex = start;12 let depth = 0;1314 for (const found of markup.matchAll(tags)) {15 if (found.index < start) {16 continue;17 }1819 depth += found.groups?.close === "/" ? -1 : 1;2021 if (depth === 0) {22 return markup.slice(start, found.index + found[0].length);23 }24 }2526 return markup.slice(start);27};2829export const elementWith = (markup: string, attribute: string) => {30 const found = new RegExp(`<[a-z0-9]+\\b[^>]*${attribute}[^>]*>`, "u").exec(31 markup32 );3334 return found === null ? "" : balancedAt(markup, found.index);35};3637export const elementsWith = (markup: string, attribute: string) =>38 [39 ...markup.matchAll(40 new RegExp(`<[a-z0-9]+\\b[^>]*${attribute}[^>]*>`, "gu")41 ),42 ].map((found) => balancedAt(markup, found.index));4344export const mainOf = (markup: string) => {45 const main = markup.slice(markup.indexOf("<main"), markup.indexOf("</main>"));46 const inline = elementWith(main, 'data-facts="inline"');4748 return inline === "" ? main : main.replace(inline, "");49};50

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 51–52why, one step back
51export const factsOf = (markup: string) =>
52 elementWith(markup, 'data-facts="inline"');