Spaces:
Running
Running
| export interface ParsedDocument { | |
| world: string; | |
| scripts: string[]; | |
| } | |
| /** | |
| * Simple HTML Document Parser for JSFiddle-style content processing | |
| * Parses complete HTML documents and extracts world/script content | |
| */ | |
| export class HTMLDocumentParser { | |
| static parseDocument(html: string): ParsedDocument { | |
| try { | |
| const parser = new DOMParser(); | |
| const doc = parser.parseFromString(html, "text/html"); | |
| const world = this.extractWorld(doc); | |
| const scripts = this.extractScripts(doc); | |
| return { | |
| world: world || '<world canvas="#game-canvas"></world>', | |
| scripts, | |
| }; | |
| } catch { | |
| return { | |
| world: '<world canvas="#game-canvas"></world>', | |
| scripts: [], | |
| }; | |
| } | |
| } | |
| private static extractWorld(doc: Document): string { | |
| const worldElements = doc.getElementsByTagName("world"); | |
| if (worldElements.length === 0) { | |
| return ""; | |
| } | |
| return worldElements[0].outerHTML; | |
| } | |
| private static extractScripts(doc: Document): string[] { | |
| const scripts: string[] = []; | |
| const scriptElements = doc.getElementsByTagName("script"); | |
| for (let i = 0; i < scriptElements.length; i++) { | |
| const script = scriptElements[i]; | |
| const content = script.textContent || script.innerHTML; | |
| if (content && content.trim()) { | |
| scripts.push(content.trim()); | |
| } | |
| } | |
| return scripts; | |
| } | |
| } | |