Browse Source

Move BrowserParserHtml to TS

pull/4746/head
Artur Arseniev 4 years ago
parent
commit
67900189ba
  1. 4
      src/parser/config/config.ts
  2. 9
      src/parser/model/BrowserParserHtml.ts

4
src/parser/config/config.ts

@ -1,11 +1,11 @@
interface HTMLParserOptions {
export interface HTMLParserOptions {
/**
* DOMParser mime type.
* If you use the `text/html` parser, it will fix the invalid syntax automatically.
* @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString
* @default 'text/html'
*/
htmlType?: string;
htmlType?: DOMParserSupportedType;
/**
* Allow <script> tags.

9
src/parser/model/BrowserParserHtml.js → src/parser/model/BrowserParserHtml.ts

@ -1,15 +1,16 @@
import { each } from 'underscore';
import { HTMLParserOptions } from '../config/config';
const htmlType = 'text/html';
const defaultType = htmlType; // 'application/xml';
export default (str, config = {}) => {
export default (str: string, config: HTMLParserOptions = {}) => {
const parser = new DOMParser();
const mimeType = config.htmlType || defaultType;
const toHTML = mimeType === htmlType;
const strF = toHTML ? str : `<div>${str}</div>`;
const doc = parser.parseFromString(strF, mimeType);
let res;
let res: HTMLElement;
if (toHTML) {
// Replicate the old parser in order to avoid breaking changes
@ -18,12 +19,12 @@ export default (str, config = {}) => {
const scripts = head.querySelectorAll('script');
each(scripts, node => body.appendChild(node));
// Move inside body all head children
const hEls = [];
const hEls: Element[] = [];
each(head.children, n => hEls.push(n));
each(hEls, (node, i) => body.insertBefore(node, body.children[i]));
res = body;
} else {
res = doc.firstChild;
res = doc.firstChild as HTMLElement;
}
return res;
Loading…
Cancel
Save