From 12b85f06c364f7b71b736ff2cce8d6f3d4d01e17 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Sat, 27 Apr 2024 15:14:30 +0400 Subject: [PATCH] Add asDocument to HTML parser --- src/dom_components/model/ComponentWrapper.ts | 1 - src/parser/config/config.ts | 20 ++++ src/parser/model/BrowserParserHtml.ts | 2 + src/parser/model/ParserHtml.ts | 107 ++++++++++++++----- src/utils/dom.ts | 9 ++ test/specs/parser/model/ParserHtml.ts | 64 +++++++++++ 6 files changed, 177 insertions(+), 26 deletions(-) diff --git a/src/dom_components/model/ComponentWrapper.ts b/src/dom_components/model/ComponentWrapper.ts index 71a0552a1..ba9f66f4f 100644 --- a/src/dom_components/model/ComponentWrapper.ts +++ b/src/dom_components/model/ComponentWrapper.ts @@ -13,7 +13,6 @@ export default class ComponentWrapper extends Component { draggable: false, components: [], traits: [], - // In case we might need the doctype as component https://stackoverflow.com/a/10162353 doctype: '', head: null, stylable: [ diff --git a/src/parser/config/config.ts b/src/parser/config/config.ts index ceaa7b437..06ea3b856 100644 --- a/src/parser/config/config.ts +++ b/src/parser/config/config.ts @@ -1,3 +1,5 @@ +import { CssRuleJSON } from '../../css_composer/model/CssRule'; +import { ComponentDefinitionDefined } from '../../dom_components/model/types'; import Editor from '../../editor'; export interface ParsedCssRule { @@ -11,6 +13,18 @@ export type CustomParserCss = (input: string, editor: Editor) => ParsedCssRule[] export type CustomParserHtml = (input: string, options: HTMLParserOptions) => HTMLElement; +export interface HTMLParseResult { + html: ComponentDefinitionDefined | ComponentDefinitionDefined[]; + css?: CssRuleJSON[]; + doctype?: string; + root?: ComponentDefinitionDefined; + head?: ComponentDefinitionDefined; +} + +export interface ParseNodeOptions extends HTMLParserOptions { + inSvg?: boolean; +} + export interface HTMLParserOptions { /** * DOMParser mime type. @@ -20,6 +34,12 @@ export interface HTMLParserOptions { */ htmlType?: DOMParserSupportedType; + /** + * Parse the string as HTML document. The result will include additional data (eg. doctype, head, etc.) + * @default false + */ + asDocument?: boolean; + /** * Allow + + + +

H1

+ + + `; + // asDocument: true + console.log(obj.parse(str, null, { asDocument: true })); + expect(obj.parse(str, null, { asDocument: true })).toEqual({ + doctype: '', + root: { classes: ['cls-html'], attributes: { lang: 'en' }, htmlp: true }, + head: { + type: 'head', + headp: true, + classes: ['cls-head'], + components: [ + { tagName: 'meta', attributes: { charset: 'utf-8' } }, + { + tagName: 'title', + type: 'text', + components: { type: 'textnode', content: 'Test' }, + }, + { + tagName: 'link', + attributes: { rel: 'stylesheet', href: '/noop.css' }, + }, + { + type: 'comment', + tagName: '', + content: ' comment ', + }, + { + tagName: 'style', + type: 'text', + components: { type: 'textnode', content: '.test { color: red }' }, + }, + ], + }, + html: { + tagName: 'body', + bodyp: true, + classes: ['cls-body'], + components: [ + { + tagName: 'h1', + type: 'text', + components: { type: 'textnode', content: 'H1' }, + }, + ], + }, + }); + }); }); });