From cb8df6752734dbd61b2b3c2528044fba68dd977c Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Tue, 10 Aug 2021 17:03:50 +0200 Subject: [PATCH] Improve Parser docs --- src/parser/config/config.js | 14 ++++++++++---- src/parser/model/ParserHtml.js | 6 ++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/parser/config/config.js b/src/parser/config/config.js index d25e7c25e..aa11e24fa 100644 --- a/src/parser/config/config.js +++ b/src/parser/config/config.js @@ -5,11 +5,17 @@ export default { // @see https://grapesjs.com/docs/guides/Custom-CSS-parser.html parserCss: null, + // Custom HTML parser + // At the moment, the custom HTML parser should rely on DOM Node instance as the result + // @example + // The return should be an instance of an Node as the root to traverse + // https://developer.mozilla.org/en-US/docs/Web/API/Node + // parserHtml: (input, opts = {}) => (new DOMParser()).parseFromString(input, 'text/xml') + // Here the result will be XMLDocument, which extends Node + parserHtml: null, + // DOMParser mime type (default 'text/html') // @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString // If you use the `text/html` parser, it will fix the invalid syntax automatically - htmlType: null, - - // TODO: Custom HTML parser - parserHtml: null + htmlType: null }; diff --git a/src/parser/model/ParserHtml.js b/src/parser/model/ParserHtml.js index 7aa4adea7..dee26c945 100644 --- a/src/parser/model/ParserHtml.js +++ b/src/parser/model/ParserHtml.js @@ -1,4 +1,4 @@ -import { each, isString } from 'underscore'; +import { each, isString, isFunction } from 'underscore'; import BrowserParserHtml from './BrowserParserHtml'; export default config => { @@ -287,7 +287,9 @@ export default config => { const conf = (em && em.get('Config')) || {}; const res = { html: null, css: null }; const cf = { ...config, ...opts }; - const el = BrowserParserHtml(str, cf); + const el = isFunction(cf.parserHtml) + ? cf.parserHtml(str, cf) + : BrowserParserHtml(str, cf); const scripts = el.querySelectorAll('script'); let i = scripts.length;