/**
* Copyright 2026 aphrody-code
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @module bxc/internal/html-utils
*
* Tiny regex-based HTML helpers shared between transport-layer parsers
* (`StaticDomTransport`, `HttpProfileTransport`) that previously each owned a
* private copy of these routines.
*
* These are intentionally crude — they exist only to back the cdylib-less
* fallback path and to support the `
` / opening-tag attribute extraction
* that both transports need. For real HTML parsing/queries the native DOM
* is preferred (`src/ffi/native-dom.ts`).
*/
import {
extractTitle as nativeExtractTitle,
htmlToMarkdown as nativeHtmlToMarkdown,
stripTags as nativeStripTags,
} from "../rust/bridge.ts";
const ATTR_RE = /([a-zA-Z_:][^\s=]*)(?:=(?:"([^"]*)"|'([^']*)'|(\S+)))?/g;
/** Extracts `…` (trimmed) from an HTML document using the native Rust bridge. */
export function extractTitle(html: string): string {
try {
return nativeExtractTitle(html);
} catch {
// Fallback to basic regex if FFI fails for some reason
const TITLE_RE = /]*>([\s\S]*?)<\/title>/i;
const m = TITLE_RE.exec(html);
return m ? (m[1] ?? "").trim() : "";
}
}
/** Strips every tag from `html`, returning the raw text content using the native Rust bridge. */
export function stripTags(html: string): string {
try {
return nativeStripTags(html);
} catch {
// Fallback to basic regex if FFI fails
const STRIP_TAGS_RE = /<[^>]+>/g;
return html.replace(STRIP_TAGS_RE, "");
}
}
/**
* Converts `html` to GitHub-flavoured Markdown with the single canonical
* converter (`bxc-extract` in the Rust bridge). Scripts, styles, templates
* and other non-rendered subtrees are dropped by the converter; tables are
* emitted unpadded, so output stays linear in the input size.
*/
export function htmlToMarkdown(html: string, baseUrl?: string): string {
return nativeHtmlToMarkdown(html, baseUrl);
}
/**
* Parses the attribute list of an opening tag (or a leading attribute string)
* into a flat `name → value` map. Quoted, single-quoted, and bare attribute
* forms are all accepted.
*/
export function parseAttributes(openingTag: string): Record {
const out: Record = {};
ATTR_RE.lastIndex = 0;
let m: RegExpExecArray | null;
while ((m = ATTR_RE.exec(openingTag)) !== null) {
if (m[1] !== undefined) out[m[1]] = m[2] ?? m[3] ?? m[4] ?? "";
}
return out;
}
/**
* Returns the opening tag of `outerHTML` (everything up to and including the
* first `>`). Used to feed `parseAttributes` from a raw element representation.
*/
export function openingTagOf(outerHTML: string): string {
const close = outerHTML.indexOf(">");
return close === -1 ? outerHTML : outerHTML.slice(0, close + 1);
}