@@ -9,11 +9,6 @@ import * as Schema from "effect/Schema";
99import * as Stream from "effect/Stream" ;
1010import * as ChildProcess from "effect/process/ChildProcess" ;
1111import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner" ;
12- import {
13- encodeHtmlRenderOutline ,
14- HTML_RENDER_COLUMN_WIDTH ,
15- HTML_RENDER_OUTLINE_MAX_BOXES ,
16- } from "@t3tools/shared/htmlRender" ;
1712
1813import { publicProxy } from "./publicProxy.ts" ;
1914
@@ -164,84 +159,6 @@ const SETTLE_EXPRESSION =
164159const MEASURE_EXPRESSION =
165160 "(() => { const root = document.documentElement; return root.scrollHeight > root.clientHeight ? root.scrollHeight : root.getBoundingClientRect().height; })()" ;
166161
167- const OUTLINE_MAX_VISITS = 2_000 ;
168- const OUTLINE_EXPRESSION = `(() => {
169- const boxes = [], detail = ${ HTML_RENDER_OUTLINE_MAX_BOXES - 12 } , max = ${ HTML_RENDER_OUTLINE_MAX_BOXES } ;
170- const media = "canvas,svg,img,video,picture,iframe,object,embed,input,select,textarea,button";
171- const pageBackground = getComputedStyle(document.documentElement).backgroundColor;
172- const filled = (s) => (s.backgroundColor !== "rgba(0, 0, 0, 0)" && s.backgroundColor !== pageBackground) || s.backgroundImage !== "none" || parseFloat(s.borderTopWidth) + parseFloat(s.borderBottomWidth) + parseFloat(s.borderLeftWidth) > 0;
173- const add = (kind, r, lines) => boxes.push([kind, r.left + scrollX, r.top + scrollY, r.width, r.height, lines]);
174- const lineCount = (s, r) => Math.max(1, Math.round(r.height / (parseFloat(s.lineHeight) || parseFloat(s.fontSize) * 1.5 || 20)));
175- // Text's own extent, so a short heading in a wide block stays short.
176- const addText = (el, s, r) => {
177- const range = document.createRange();
178- range.selectNodeContents(el);
179- const t = range.getBoundingClientRect();
180- const box = t.width >= 2 && t.height >= 2 ? t : r;
181- add("text", box, lineCount(s, box));
182- };
183- // Visits are capped too, so a huge generated DOM costs no more than a page.
184- let full = false, visits = 0;
185- // Whether an element holds text of its own, looking at its first few children only.
186- const hasOwnText = (el) => {
187- let n = el.firstChild;
188- for (let i = 0; n && i < 64; n = n.nextSibling, i++) if (n.nodeType === 3 && n.data.trim() !== "") return true;
189- return false;
190- };
191- const walk = (parent, ps) => {
192- for (const el of parent.childNodes) {
193- // The last slot is kept for whatever the outline has no room left for.
194- if (boxes.length >= max - 1 || ++visits > ${ OUTLINE_MAX_VISITS } ) { full = true; return; }
195- // Bare text only reaches here under body or a display: contents element.
196- if (el.nodeType === 3) {
197- if (el.data.trim() === "") continue;
198- const range = document.createRange();
199- range.selectNodeContents(el);
200- const t = range.getBoundingClientRect();
201- if (t.width >= 2 && t.height >= 2) add("text", t, lineCount(ps, t));
202- continue;
203- }
204- if (el.nodeType !== 1) continue;
205- const s = getComputedStyle(el);
206- if (s.display === "none" || s.visibility === "hidden" || s.opacity === "0") continue;
207- if (s.display === "contents") { walk(el, s); continue; }
208- const r = el.getBoundingClientRect();
209- // A zero-size box can still show positioned children, unless it clips them.
210- if (r.width < 2 || r.height < 2) { if (s.overflow === "visible") walk(el, s); continue; }
211- const tag = el.localName;
212- if (el.matches(media)) { add("media", r, 1); continue; }
213- if (tag === "table") { add("table", r, el.rows.length); continue; }
214- const text = hasOwnText(el);
215- if (boxes.length >= detail) { if (el.querySelector(media) || (!text && !el.firstElementChild)) add("media", r, 1); else addText(el, s, r); continue; }
216- if (text) { addText(el, s, r); continue; }
217- if (el.children.length === 0) { if (filled(s)) add("media", r, 1); continue; }
218- walk(el, s);
219- }
220- };
221- walk(document.body, getComputedStyle(document.body));
222- if (full) {
223- const top = Math.max(0, ...boxes.map((b) => b[2] + b[4]));
224- const root = document.documentElement;
225- const rest = (root.scrollHeight > root.clientHeight ? root.scrollHeight : root.getBoundingClientRect().height) - top;
226- if (rest >= 8) boxes.push(["text", 0, top, document.documentElement.clientWidth, rest, Math.max(1, Math.round(rest / 28))]);
227- }
228- return boxes;
229- })()` ;
230- const Outlined = Schema . Struct ( {
231- result : Schema . Struct ( {
232- value : Schema . Array (
233- Schema . Tuple ( [
234- Schema . Literals ( [ "text" , "media" , "table" ] ) ,
235- Schema . Finite ,
236- Schema . Finite ,
237- Schema . Finite ,
238- Schema . Finite ,
239- Schema . Finite ,
240- ] ) ,
241- ) ,
242- } ) ,
243- } ) ;
244-
245162const Ignored = Schema . Unknown ;
246163const Navigation = Schema . Struct ( { errorText : Schema . optional ( Schema . String ) } ) ;
247164const Measured = Schema . Struct ( { result : Schema . Struct ( { value : Schema . Finite } ) } ) ;
@@ -601,27 +518,7 @@ const launchBrowser = Effect.fnUntraced(function* (input: {
601518 Effect . ignore ,
602519 ) ;
603520
604- const outline = send (
605- "Runtime.evaluate" ,
606- { expression : OUTLINE_EXPRESSION , returnByValue : true } ,
607- Outlined ,
608- sessionId ,
609- ) . pipe (
610- Effect . map ( ( { result } ) =>
611- encodeHtmlRenderOutline (
612- result . value . map ( ( [ kind , x , y , boxWidth , height , lines ] ) => ( {
613- kind,
614- x,
615- y,
616- width : boxWidth ,
617- height,
618- lines,
619- } ) ) ,
620- ) ,
621- ) ,
622- ) ;
623-
624- return { load, outline, screenshot, consoleMessages, close } ;
521+ return { load, screenshot, consoleMessages, close } ;
625522 } ) ;
626523
627524 return { openPage } ;
@@ -692,27 +589,15 @@ export const measureHtmlHeights = Effect.fn("headlessChrome.measureHtmlHeights")
692589 profileDirectory : path . join ( directory , "profile" ) ,
693590 } ) ;
694591 const body = pageBody ( input . html ) ;
695- let outline : string | undefined ;
696- const heights = yield * Effect . forEach (
592+ return yield * Effect . forEach (
697593 input . widths ,
698594 ( width ) =>
699595 browser . openPage ( width ) . pipe (
700596 Effect . flatMap ( ( page ) =>
701- page . load ( body , input . urlFragment ) . pipe (
702- Effect . tap ( ( ) =>
703- width === HTML_RENDER_COLUMN_WIDTH
704- ? page . outline . pipe (
705- Effect . tap ( ( value ) => Effect . sync ( ( ) => ( outline = value ) ) ) ,
706- Effect . ignore ,
707- )
708- : Effect . void ,
709- ) ,
710- Effect . ensuring ( page . close ) ,
711- ) ,
597+ page . load ( body , input . urlFragment ) . pipe ( Effect . ensuring ( page . close ) ) ,
712598 ) ,
713599 Effect . map ( ( height ) => [ width , height ] as const ) ,
714600 ) ,
715601 { concurrency : MEASURE_CONCURRENCY } ,
716602 ) ;
717- return { heights, outline } ;
718603} , Effect . scoped ) ;
0 commit comments