From 8287e8a4434c12ce9a301b66437fd1939dfb7c06 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Mon, 9 Aug 2021 15:36:18 +0200 Subject: [PATCH] Update browser html parser --- src/parser/config/config.js | 10 ---------- src/parser/model/BrowserParserHtml.js | 19 ++++++++++++++++--- src/utils/index.js | 4 +++- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/parser/config/config.js b/src/parser/config/config.js index 7c6401e07..9964c81e5 100644 --- a/src/parser/config/config.js +++ b/src/parser/config/config.js @@ -8,16 +8,6 @@ export default { // @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString htmlType: null, - // Custom function which tells which root Element to extract - // from the DOMParser.parseFromString result - // @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString - // @example - // // with htmlType as 'text/html' the function will be (default): - // htmlResult: (doc) => doc.body, - // // in case of `application/xml` - // htmlResult: (doc) => doc, - htmlResult: null, - // TODO: Custom HTML parser parserHtml: null }; diff --git a/src/parser/model/BrowserParserHtml.js b/src/parser/model/BrowserParserHtml.js index 38a98216c..b64b2e9ff 100644 --- a/src/parser/model/BrowserParserHtml.js +++ b/src/parser/model/BrowserParserHtml.js @@ -1,9 +1,22 @@ +const htmlType = 'text/html'; +// const defaultType = 'application/xml'; +const defaultType = htmlType; + export default (str, config = {}) => { - const { htmlResult } = config; const parser = new DOMParser(); - const doc = parser.parseFromString(str, config.htmlType || 'text/html'); + const mimeType = config.htmlType || defaultType; + const toHTML = mimeType === htmlType; + const strF = toHTML ? str : `
${str}
`; + const doc = parser.parseFromString(strF, mimeType); + let res; + + if (toHTML) { + res = doc.body; + } else { + res = doc.firstChild; + } - return htmlResult ? htmlResult(doc, str) : doc.body; + return res; }; /** diff --git a/src/utils/index.js b/src/utils/index.js index 14fc93262..c1931d98c 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -1,6 +1,7 @@ import Dragger from './Dragger'; import Sorter from './Sorter'; import Resizer from './Resizer'; +import * as mixins from './mixins'; export default () => { return { @@ -22,6 +23,7 @@ export default () => { Sorter, Resizer, - Dragger + Dragger, + helpers: { ...mixins } }; };