import { getTagName } from './utility.service.js' /** * This is a tiny purpose-built HTML parser/processor. This basically detects * any type of visual newline and converts entire HTML into a array structure. * * Text nodes are represented as object with single property - text - containing * the visual line. Intended usage is to process the array with .map() in which * map function returns a string and resulting array can be converted back to html * with a .join(''). * * Generally this isn't very useful except for when you really need to either * modify visual lines (greentext i.e. simple quoting) or do something with * first/last line. * * known issue: doesn't handle CDATA so nested CDATA might not work well * * @param {Object} input - input data * @return {(string|{ text: string })[]} processed html in form of a list. */ export const convertHtmlToLines = (html) => { // Elements that are implicitly self-closing // https://developer.mozilla.org/en-US/docs/Glossary/empty_element const emptyElements = new Set([ 'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr' ]) // Block-level element (they make a visual line) // https://developer.mozilla.org/en-US/docs/Web/HTML/Block-level_elements const blockElements = new Set([ 'address', 'article', 'aside', 'blockquote', 'details', 'dialog', 'dd', 'div', 'dl', 'dt', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'hgroup', 'hr', 'li', 'main', 'nav', 'ol', 'p', 'pre', 'section', 'table', 'ul' ]) // br is very weird in a way that it's technically not block-level, it's // essentially converted to a \n (or \r\n). There's also wbr but it doesn't // guarantee linebreak, only suggest it. const linebreakElements = new Set(['br']) const visualLineElements = new Set([ ...blockElements.values(), ...linebreakElements.values() ]) // All block-level elements that aren't empty elements, i.e. not