Browse Source

Update browser html parser

pull/3691/head
Artur Arseniev 5 years ago
parent
commit
8287e8a443
  1. 10
      src/parser/config/config.js
  2. 19
      src/parser/model/BrowserParserHtml.js
  3. 4
      src/utils/index.js

10
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
};

19
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 : `<div>${str}</div>`;
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;
};
/**

4
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 }
};
};

Loading…
Cancel
Save