|
|
|
@ -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; |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
|