From de9c563abd455259b22c4fe2734a60c29f47d788 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Thu, 26 Dec 2024 14:21:02 +0400 Subject: [PATCH] Move parser events --- packages/core/src/parser/index.ts | 9 +++++++ packages/core/src/parser/model/ParserCss.ts | 16 ++++++++++--- packages/core/src/parser/model/ParserHtml.ts | 6 ++--- packages/core/src/parser/types.ts | 25 ++++++++++++++++++++ 4 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 packages/core/src/parser/types.ts diff --git a/packages/core/src/parser/index.ts b/packages/core/src/parser/index.ts index e4ec87f41..a5769125b 100644 --- a/packages/core/src/parser/index.ts +++ b/packages/core/src/parser/index.ts @@ -25,14 +25,17 @@ * @module Parser */ import { Module } from '../abstract'; +import { ObjectAny } from '../common'; import EditorModel from '../editor/model/Editor'; import defConfig, { HTMLParserOptions, ParserConfig } from './config/config'; import ParserCss from './model/ParserCss'; import ParserHtml from './model/ParserHtml'; +import { ParserEvents } from './types'; export default class ParserModule extends Module { parserHtml: ReturnType; parserCss: ReturnType; + events = ParserEvents; constructor(em: EditorModel) { super(em, 'Parser', defConfig()); @@ -85,5 +88,11 @@ export default class ParserModule extends Module ({ /** @@ -10,13 +11,22 @@ const ParserCss = (em?: EditorModel, config: ParserConfig = {}) => ({ * @param {String} input CSS string * @return {Array} */ - parse(input: string) { + parse(input: string, opts: { throwOnError?: boolean } = {}) { let output: CssRuleJSON[] = []; const { parserCss } = config; const editor = em?.Editor; - const nodes = parserCss ? parserCss(input, editor!) : BrowserCssParser(input); + let nodes: CssRuleJSON[] | ParsedCssRule[] = []; + let error: unknown; + + try { + nodes = parserCss ? parserCss(input, editor!) : BrowserCssParser(input); + } catch (err) { + error = err; + if (opts.throwOnError) throw err; + } + nodes.forEach((node) => (output = output.concat(this.checkNode(node)))); - em?.trigger('parse:css', { input, output, nodes }); + em?.Parser.__emitEvent(ParserEvents.css, { input, output, nodes, error }); return output; }, diff --git a/packages/core/src/parser/model/ParserHtml.ts b/packages/core/src/parser/model/ParserHtml.ts index e05c5bd1f..54f58c504 100644 --- a/packages/core/src/parser/model/ParserHtml.ts +++ b/packages/core/src/parser/model/ParserHtml.ts @@ -6,9 +6,9 @@ import { HTMLParseResult, HTMLParserOptions, ParseNodeOptions, ParserConfig } fr import BrowserParserHtml from './BrowserParserHtml'; import { doctypeToString } from '../../utils/dom'; import { isDef } from '../../utils/mixins'; +import { ParserEvents } from '../types'; const modelAttrStart = 'data-gjs-'; -const event = 'parse:html'; const ParserHtml = (em?: EditorModel, config: ParserConfig & { returnArray?: boolean } = {}) => { return { @@ -365,7 +365,7 @@ const ParserHtml = (em?: EditorModel, config: ParserConfig & { returnArray?: boo if (styleStr) res.css = parserCss.parse(styleStr); } - em?.trigger(`${event}:root`, { input, root: root }); + em?.Parser.__emitEvent(ParserEvents.htmlRoot, { input, root }); let resHtml: HTMLParseResult['html'] = []; if (asDocument) { @@ -379,7 +379,7 @@ const ParserHtml = (em?: EditorModel, config: ParserConfig & { returnArray?: boo } res.html = resHtml; - em?.trigger(event, { input, output: res, options }); + em?.Parser.__emitEvent(ParserEvents.html, { input, output: res, options }); return res; }, diff --git a/packages/core/src/parser/types.ts b/packages/core/src/parser/types.ts new file mode 100644 index 000000000..bf1e2bcf3 --- /dev/null +++ b/packages/core/src/parser/types.ts @@ -0,0 +1,25 @@ +/**{START_EVENTS}*/ +export enum ParserEvents { + /** + * @event `parse:html` On HTML parse, an object containing the input and the output of the parser is passed as an argument. + * @example + * editor.on('parse:html', ({ input, output }) => { ... }); + */ + html = 'parse:html', + htmlRoot = 'parse:html:root', + + /** + * @event `parse:css` On CSS parse, an object containing the input and the output of the parser is passed as an argument. + * @example + * editor.on('parse:css', ({ input, output }) => { ... }); + */ + css = 'parse:css', + + /** + * @event `parse` Catch-all event for all the events mentioned above. An object containing all the available data about the triggered event is passed as an argument to the callback. + * @example + * editor.on('parse', ({ event, ... }) => { ... }); + */ + all = 'parse', +} +/**{END_EVENTS}*/