Browse Source

Add DOMParser for HTML

pull/3691/head
Artur Arseniev 5 years ago
parent
commit
dacfc42c7d
  1. 16
      src/parser/config/config.js
  2. 57
      src/parser/model/BrowserParserHtml.js
  3. 8
      src/parser/model/ParserHtml.js

16
src/parser/config/config.js

@ -4,6 +4,20 @@ export default {
// Custom CSS parser // Custom CSS parser
parserCss: null, parserCss: null,
// Custom HTML parser // Default DOMParser mime type
// @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString
htmlType: 'text/html',
// 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 parserHtml: null
}; };

57
src/parser/model/BrowserParserHtml.js

@ -1,4 +1,34 @@
// PoC export default (str, config = {}) => {
const { htmlResult } = config;
const parser = new DOMParser();
const doc = parser.parseFromString(str, config.htmlType);
return htmlResult ? htmlResult(doc, str) : doc.body;
};
/**
* POC, custom html parser specs
* Parse an HTML string to an array of nodes
* example
* parse(`<div class="mycls" data-test>Hello</div><span>World <b>example</b></span>`)
* // result
* [
* {
* tagName: 'div',
* attributes: { class: 'mycls', 'data-test': '' },
* childNodes: ['Hello'],
* },{
* tagName: 'span',
* childNodes: [
* 'World ',
* {
* tagName: 'b',
* childNodes: ['example'],
* }
* ],
* }
* ]
*
export const parseNodes = nodes => { export const parseNodes = nodes => {
const result = []; const result = [];
@ -44,29 +74,7 @@ export const parseNode = el => {
}; };
}; };
/** export default (str, config = {}) => {
* Parse an HTML string to an array of nodes
* @example
* parse(`<div class="mycls" data-test>Hello</div><span>World <b>example</b></span>`)
* // result
* [
* {
* tagName: 'div',
* attributes: { class: 'mycls', 'data-test': '' },
* childNodes: ['Hello'],
* },{
* tagName: 'span',
* childNodes: [
* 'World ',
* {
* tagName: 'b',
* childNodes: ['example'],
* }
* ],
* }
* ]
*/
export default (str, config) => {
const result = []; const result = [];
const el = document.createElement('div'); const el = document.createElement('div');
el.innerHTML = str; el.innerHTML = str;
@ -79,3 +87,4 @@ export default (str, config) => {
return result; return result;
}; };
*/

8
src/parser/model/ParserHtml.js

@ -1,4 +1,5 @@
import { each, isString } from 'underscore'; import { each, isString } from 'underscore';
import BrowserParserHtml from './BrowserParserHtml';
export default config => { export default config => {
var TEXT_NODE = 'span'; var TEXT_NODE = 'span';
@ -275,15 +276,14 @@ export default config => {
*/ */
parse(str, parserCss) { parse(str, parserCss) {
const { em } = c; const { em } = c;
const config = (em && em.get('Config')) || {}; const conf = (em && em.get('Config')) || {};
const res = { html: '', css: '' }; const res = { html: '', css: '' };
const el = document.createElement('div'); const el = BrowserParserHtml(str, config);
el.innerHTML = str;
const scripts = el.querySelectorAll('script'); const scripts = el.querySelectorAll('script');
let i = scripts.length; let i = scripts.length;
// Remove all scripts // Remove all scripts
if (!config.allowScripts) { if (!conf.allowScripts) {
while (i--) scripts[i].parentNode.removeChild(scripts[i]); while (i--) scripts[i].parentNode.removeChild(scripts[i]);
} }

Loading…
Cancel
Save