UNPKG

jodit

Version:

Jodit is an awesome and useful wysiwyg editor with filebrowser

77 lines (76 loc) 2.87 kB
/*! * Jodit Editor (https://xdsoft.net/jodit/) * Released under MIT see LICENSE.txt in the project root for license information. * Copyright (c) 2013-2026 Valerii Chupurnov. All rights reserved. https://xdsoft.net */ /** * @module helpers/html */ import { globalDocument } from "../../constants.js"; import { Dom } from "../../dom/dom.js"; import { toArray } from "../array/to-array.js"; import { trim } from "../string/trim.js"; /** * The method automatically cleans up content from Microsoft Word and other HTML sources to ensure clean, compliant * content that matches the look and feel of the site. */ export function cleanFromWord(html) { if (html.indexOf('<html ') !== -1) { html = html.substring(html.indexOf('<html '), html.length); html = html.substring(0, html.lastIndexOf('</html>') + '</html>'.length); } let convertedString = ''; try { const div = globalDocument.createElement('div'); div.innerHTML = html; const marks = []; if (div.firstChild) { Dom.each(div, node => { if (!node) { return; } switch (node.nodeType) { case Node.ELEMENT_NODE: switch (node.nodeName) { case 'STYLE': case 'LINK': case 'META': marks.push(node); break; case 'W:SDT': case 'W:SDTPR': case 'FONT': Dom.unwrap(node); break; default: toArray(node.attributes).forEach((attr) => { if ([ 'src', 'href', 'rel', 'content' ].indexOf(attr.name.toLowerCase()) === -1) { node.removeAttribute(attr.name); } }); } break; case Node.TEXT_NODE: break; default: marks.push(node); } }); } Dom.safeRemove.apply(null, marks); convertedString = div.innerHTML; } catch (e) { } if (convertedString) { html = convertedString; } html = html.split(/(\n)/).filter(trim).join('\n'); return html .replace(/<(\/)?(html|colgroup|col|o:p)[^>]*>/g, '') .replace(/<!--[^>]*>/g, ''); }