UNPKG

scriptable-testlab

Version:

A lightweight, efficient tool designed to manage and update scripts for Scriptable.

151 lines 3.66 kB
import { AbsXMLParser } from "scriptable-abstract"; class MockXMLParser extends AbsXMLParser { constructor(string) { super({ string, didStartDocument: () => { }, didEndDocument: () => { }, didStartElement: () => { }, didEndElement: () => { }, foundCharacters: () => { }, parseErrorOccurred: () => { } }); } /** * Text to parse */ get string() { return this.state.string; } /** * Sets the text to parse */ set string(value) { this.setState({ string: value }); } /** * Function called when parsing starts */ get didStartDocument() { return this.state.didStartDocument; } /** * Sets the function called when parsing starts */ set didStartDocument(value) { this.setState({ didStartDocument: value }); } /** * Function called when parsing ends */ get didEndDocument() { return this.state.didEndDocument; } /** * Sets the function called when parsing ends */ set didEndDocument(value) { this.setState({ didEndDocument: value }); } /** * Function called when an element starts */ get didStartElement() { return this.state.didStartElement; } /** * Sets the function called when an element starts */ set didStartElement(value) { this.setState({ didStartElement: value }); } /** * Function called when an element ends */ get didEndElement() { return this.state.didEndElement; } /** * Sets the function called when an element ends */ set didEndElement(value) { this.setState({ didEndElement: value }); } /** * Function called when characters are found */ get foundCharacters() { return this.state.foundCharacters; } /** * Sets the function called when characters are found */ set foundCharacters(value) { this.setState({ foundCharacters: value }); } /** * Function called when a parse error occurs */ get parseErrorOccurred() { return this.state.parseErrorOccurred; } /** * Sets the function called when a parse error occurs */ set parseErrorOccurred(value) { this.setState({ parseErrorOccurred: value }); } /** * Parses the XML document * @returns true if parsing was successful, false otherwise */ parse() { try { this.didStartDocument(); const xmlString = this.string; if (!xmlString.match(/^<[^>]+>.*<\/[^>]+>$/s)) { this.parseErrorOccurred("Invalid XML structure"); return false; } const tagRegex = /<(\/?)([\w-]+)((?:\s+[\w-]+="[^"]*")*)\s*\/?>/g; const textRegex = />([^<]+)</g; let match; while ((match = tagRegex.exec(xmlString)) !== null) { const [, isClosing, tagName, attributesStr] = match; if (!isClosing) { const attributes = {}; const attrRegex = /(\w+)="([^"]*)"/g; let attrMatch; while ((attrMatch = attrRegex.exec(attributesStr)) !== null) { const [, name, value] = attrMatch; attributes[name] = value; } this.didStartElement(tagName, attributes); } else { this.didEndElement(tagName); } } while ((match = textRegex.exec(xmlString)) !== null) { const [, text] = match; if (text.trim()) { this.foundCharacters(text.trim()); } } this.didEndDocument(); return true; } catch (error) { this.parseErrorOccurred(error instanceof Error ? error.message : String(error)); return false; } } } export { MockXMLParser }; //# sourceMappingURL=xml-parser.js.map