UNPKG

@slightning/anything-to-string

Version:
55 lines (54 loc) 1.51 kB
import { MaximumObjectRule } from "./object"; function isHTMLElement(data) { return typeof HTMLElement != "undefined" && data instanceof HTMLElement; } export class MinimumHTMLElementRule { constructor() { this.test = isHTMLElement; } toString(data, __config, __context) { let result = data.tagName; if (data.id != "") { result += `#${data.id}`; } for (const name of Array.from(data.classList)) { result += `.${name}`; } return `[${Object.getPrototypeOf(data).constructor.name}: ${result}]`; } } export class LesserHTMLElementRule { constructor() { this.test = isHTMLElement; } toString(data, __config, __context) { if (data.innerHTML == "") { return data.outerHTML; } else { const newData = data.cloneNode(false); newData.innerText = "..."; return data.outerHTML; } } } export class MajorHTMLElementRule { constructor() { this.test = isHTMLElement; } toString(data, __config, __context) { return data.outerHTML; } } export class MaximumHTMLElementRule { constructor() { this.test = isHTMLElement; } prepare(data, config, context) { new MaximumObjectRule().prepare(data, config, context); } toString(data, config, context) { return data.outerHTML + " " + new MaximumObjectRule().toString(data, config, context); } }