- md5
- e4ec08390f448a317d3ca3f3e414db04
- sha1
- 908cfbf112141a2866b7fbc5456c3e0f77be9638
- sha256
- ba00f045dff899b4c574f9b594997681ad1fccc90ea64318c0fdf966a13584cd
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
/**
* Copyright (c) Tiny Technologies, Inc. All rights reserved.
* Licensed under the LGPL or a commercial license.
* For LGPL see License.txt in the project root for license information.
* For commercial licenses see https://www.tiny.cloud/
* */
/**
* Based on TinyMCE 5.10.7 paste plugin
* @see https://github.com/tinymce/tinymce/blob/5.10.7/modules/tinymce/src/plugins/paste/main/ts/api/Settings.ts
*/
/**
* Left only Word paste options. Other options already a part of TinyMCE 6.
* @see https://www.tiny.cloud/docs/plugins/opensource/paste/
* @see https://www.tiny.cloud/docs/tinymce/6/copy-and-paste/
*/
const getRetainStyleProps = editor => editor.getParam('paste_retain_style_properties');
const getWordValidElements = editor => {
const defaultValidElements = '-strong/b,-em/i,-u,-span,-p,-ol,-ul,-li,-h1,-h2,-h3,-h4,-h5,-h6,' + '-p/div,-a[href|name],sub,sup,strike,br,del,table[width],tr,' + 'td[colspan|rowspan|width],th[colspan|rowspan|width],thead,tfoot,tbody';
return editor.getParam('paste_word_valid_elements', defaultValidElements);
};
const shouldConvertWordFakeLists = editor => editor.getParam('paste_convert_word_fake_lists', true);
const shouldUseDefaultFilters = editor => editor.getParam('paste_enable_default_filters', true);
const getValidate = editor => {
var _editor$getParam;
return (_editor$getParam = editor.getParam('validate')) != null ? _editor$getParam : true;
};
/**
* Copyright (c) Tiny Technologies, Inc. All rights reserved.
* Licensed under the LGPL or a commercial license.
* For LGPL see License.txt in the project root for license information.
* For commercial licenses see https://www.tiny.cloud/
*/
/**
* Based on TinyMCE 5.10.7 paste plugin
* @see https://github.com/tinymce/tinymce/blob/5.10.7/modules/tinymce/src/plugins/paste/main/ts/core/Utils.ts
*/
const {
Tools: Tools$1
} = tinymce.util;
const isRegExp = val => val.constructor === RegExp;
/**
* This class contails various utility functions for the paste plugin.
*
* @class tinymce.pasteplugin.Utils
*/
const filter = (content, items) => {
Tools$1.each(items, v => {
if (isRegExp(v)) {
content = content.replace(v, '');
} else {
content = content.replace(v[0], v[1]);
}
});
return content;
};
/**
* Copyright (c) Tiny Technologies, Inc. All rights reserved.
* Licensed under the LGPL or a commercial license.
* For LGPL see License.txt in the project root for license information.
* For commercial licenses see https://www.tiny.cloud/
*/
const nbsp = '\u00A0';
const AstNode = tinymce.html.Node;
const {
Tools
} = tinymce.util;
const {
DomParser
} = tinymce.html;
const {
Schema
} = tinymce.html;
const HtmlSerializer = tinymce.html.Serializer;
/**
* This class parses word HTML into proper TinyMCE markup.
*
* @class tinymce.pasteplugin.WordFilter
* @private
*/
/**
* Checks if the specified content is from any of the following sources: MS Word/Office 365/Google docs.
*/
const isWordContent = content => /<font face="Times New Roman"|class="?Mso|style="[^"]*\bmso-|style='[^']*\bmso-|w:WordDocument/i.test(content) || /class="OutlineElement/.test(content) || /id="?docs-internal-guid-/.test(content);
/**
* Checks if the specified text starts with "1. " or "a. " etc.
*/
const isNumericList = text => {
let found = false;
const patterns = [/^[IVXLMCD]+\.[ \u00a0]/,
// Roman upper case
/^[ivxlmcd]+\.[ \u00a0]/,
// Roman lower case
/^[a-z]{1,2}[.)][ \u00a0]/,
// Alphabetical a-z
/^[A-Z]{1,2}[.)][ \u00a0]/,
// Alphabetical A-Z
/^[0-9]+\.[ \u00a0]/,
// Numeric lists
/^[\u3007\u4e00\u4e8c\u4e09\u56db\u4e94\u516d\u4e03\u516b\u4e5d]+\.[ \u00a0]/,
// Japanese
/^[\u58f1\u5f10\u53c2\u56db\u4f0d\u516d\u4e03\u516b\u4e5d\u62fe]+\.[ \u00a0]/ // Chinese
];
text = text.replace(/^[\u00a0 ]+/, '');
Tools.each(patterns, pattern => {
if (pattern.test(text)) {
found = true;
return false;
}
return true;
});
return found;
};
const isBulletList = text => /^[\s\u00a0]*[\u2022\u00b7\u00a7\u25CF]\s*/.test(text);
/**
* Converts fake bullet and numbered lists to real semantic OL/UL.
*
* @param {tinymce.html.Node} node Root node to convert children of.
*/
const convertFakeListsToProperLists = node => {
let currentListNode = null;
let prevListNode;
let lastLevel = 1;
const getText = node => {
let txt = '';
if (node.type === 3) {
return node.value || '';
}
if (node = node.firstChild) {
do {
txt += getText(node);
} while (node = node.next);
}
return txt;
};
const trimListStart = (node, regExp) => {
if (node.type === 3) {
if (regExp.test(node.value || '')) {
node.value = (node.value || '').replace(regExp, '');
return false;
}
}
if (node = node.firstChild) {
do {
if (!trimListStart(node, regExp)) {
return false;
}
} while (node = node.next);
}
return true;
};
const removeIgnoredNodes = node => {
if (node._listIgnore) {
node.remove();
return;
}
if (node = node.firstChild) {
do {
removeIgnoredNodes(node);
} while (node = node.next);
}
};
const convertParagraphToLi = (paragraphNode, listName, start) => {
const level = paragraphNode._listLevel || lastLevel;
// Handle list nesting
if (level !== lastLevel) {
if (level < lastLevel) {
// Move to parent list
if (currentListNode && currentListNode.parent && currentListNode.parent.parent) {
currentListNode = currentListNode.parent.parent;
}
} else {
// Create new list
prevListNode = currentListNode;
currentListNode = null;
}
}
if (!currentListNode || currentListNode.name !== listName) {
prevListNode = prevListNode || currentListNode;
currentListNode = new AstNode(listName, 1);
if (start && start > 1) {
currentListNode.attr('start', `${start}`);
}
paragraphNode.wrap(currentListNode);
} else {
currentListNode.append(paragraphNode);
}
paragraphNode.name = 'li';
// Append list to previous list if it exists
if (level > lastLevel && prevListNode) {
var _prevListNode$lastChi;
(_prevListNode$lastChi = prevListNode.lastChild) == null || _prevListNode$lastChi.append(currentListNode);
}
lastLevel = level;
// Remove start of list item "1. " or "· " etc
removeIgnoredNodes(paragraphNode);
trimListStart(paragraphNode, /^\u00a0+/);
trimListStart(paragraphNode, /^\s*([\u2022\u00b7\u00a7\u25CF]|\w+\.)/);
trimListStart(paragraphNode, /^\u00a0+/);
};
// Build a list of all root level elements before we start
// altering them in the loop below.
const elements = [];
let child = node.firstChild;
while (typeof child !== 'undefined' && child !== null) {
elements.push(child);
child = child.walk();
if (child !== null) {
while (typeof child !== 'undefined' && ((_child = child) == null ? void 0 : _child.parent) !== node) {
var _child, _child2;
child = (_child2 = child) == null ? void 0 : _child2.walk();
}
}
}
for (let i = 0; i < elements.length; i++) {
node = elements[i];
if (node.name === 'p' && node.firstChild) {
// Find first text node in paragraph
const nodeText = getText(node);
// Detect unordered lists look for bullets
if (isBulletList(nodeText)) {
convertParagraphToLi(node, 'ul');
continue;
}
// Detect ordered lists 1., a. or ixv.
if (isNumericList(nodeText)) {
// Parse OL start number
const matches = /([0-9]+)\./.exec(nodeText);
let start = 1;
if (matches) {
start = parseInt(matches[1], 10);
}
convertParagraphToLi(node, 'ol', start);
continue;
}
// Convert paragraphs marked as lists but doesn't look like anything
if (node._listLevel) {
convertParagraphToLi(node, 'ul', 1);
continue;
}
currentListNode = null;
} else {
// If the root level element isn't a p tag which can be
// processed by convertParagraphToLi, it interrupts the
// lists, causing a new list to start instead of having
// elements from the next list inserted above this tag.
prevListNode = currentListNode;
currentListNode = null;
}
}
};
const filterStyles = (editor, validStyles, node, styleValue) => {
const outputStyles = {};
const styles = editor.dom.parseStyle(styleValue);
Tools.each(styles, (value, name) => {
// Convert various MS styles to W3C styles
switch (name) {
case 'mso-list':
// Parse out list indent level for lists
const matches = /\w+ \w+([0-9]+)/i.exec(styleValue);
if (matches) {
node._listLevel = parseInt(matches[1], 10);
}
// Remove these nodes <span style="mso-list:Ignore">o</span>
// Since the span gets removed we mark the text node and the span
if (/Ignore/i.test(value) && node.firstChild) {
node._listIgnore = true;
node.firstChild._listIgnore = true;
}
break;
case 'horiz-align':
name = 'text-align';
break;
case 'vert-align':
name = 'vertical-align';
break;
case 'font-color':
case 'mso-foreground':
name = 'color';
break;
case 'mso-background':
case 'mso-highlight':
name = 'background';
break;
case 'font-weight':
case 'font-style':
if (value !== 'normal') {
outputStyles[name] = value;
}
return;
case 'mso-element':
// Remove track changes code
if (/^(comment|comment-list)$/i.test(value)) {
node.remove();
return;
}
break;
}
if (name.indexOf('mso-comment') === 0) {
node.remove();
return;
}
// Never allow mso- prefixed names
if (name.indexOf('mso-') === 0) {
return;
}
// Output only valid styles
if (getRetainStyleProps(editor) === 'all' || validStyles && validStyles[name]) {
outputStyles[name] = value;
}
});
// Convert bold style to "b" element
if (/(bold)/i.test(outputStyles['font-weight'])) {
delete outputStyles['font-weight'];
node.wrap(new AstNode('b', 1));
}
// Convert italic style to "i" element
if (/(italic)/i.test(outputStyles['font-style'])) {
delete outputStyles['font-style'];
node.wrap(new AstNode('i', 1));
}
// Serialize the styles and see if there is something left to keep
const outputStyle = editor.dom.serializeStyle(outputStyles, node.name);
if (outputStyle) {
return outputStyle;
}
return null;
};
const filterWordContent = (editor, content) => {
let validStyles;
const retainStyleProperties = getRetainStyleProps(editor);
if (retainStyleProperties) {
validStyles = Tools.makeMap(retainStyleProperties.split(/[, ]/));
}
// Remove basic Word junk
content = filter(content, [
// Remove apple new line markers
/<br class="?Apple-interchange-newline"?>/gi,
// Remove google docs internal guid markers
/<b[^>]+id="?docs-internal-[^>]*>/gi,
// Word comments like conditional comments etc
/<!--[\s\S]+?-->/gi,
// Remove comments, scripts (e.g., msoShowComment), XML tag, VML content,
// MS Office namespaced tags, and a few other tags
/<(!|script[^>]*>.*?<\/script(?=[>\s])|\/?(\?xml(:\w+)?|img|meta|link|style|\w:\w+)(?=[\s/>]))[^>]*>/gi,
// Convert <s> into <strike> for line-though
[/<(\/?)s>/gi, '<$1strike>'],
// Replace nsbp entities to char since it's easier to handle
[/ /gi, nbsp],
// Convert <span style="mso-spacerun:yes">___</span> to string of alternating
// breaking/non-breaking spaces of same length
[/<span\s+style\s*=\s*"\s*mso-spacerun\s*:\s*yes\s*;?\s*"\s*>([\s\u00a0]*)<\/span>/gi, (_str, spaces) => spaces.length > 0 ? spaces.replace(/./, ' ').slice(Math.floor(spaces.length / 2)).split('').join(nbsp) : '']]);
const validElements = getWordValidElements(editor);
// Setup strict schema
const schema = Schema({
valid_elements: validElements,
valid_children: '-li[p]'
});
// Add style/class attribute to all element rules since the user might have removed them from
// paste_word_valid_elements config option and we need to check them for properties
Tools.each(schema.elements, rule => {
if (!rule.attributes.class) {
rule.attributes.class = {};
rule.attributesOrder.push('class');
}
if (!rule.attributes.style) {
rule.attributes.style = {};
rule.attributesOrder.push('style');
}
});
// Parse HTML into DOM structure
const domParser = DomParser({}, schema);
// Filter styles to remove "mso" specific styles and convert some of them
domParser.addAttributeFilter('style', nodes => {
let i = nodes.length;
let node;
while (i--) {
var _node$attributes;
node = nodes[i];
node.attr('style', filterStyles(editor, validStyles, node, node.attr('style') || ''));
// Remove pointless spans
if (node.name === 'span' && node.parent && !((_node$attributes = node.attributes) != null && _node$attributes.length)) {
node.unwrap();
}
}
});
// Check the class attribute for comments or del items and remove those
domParser.addAttributeFilter('class', nodes => {
let i = nodes.length;
let node;
let className;
while (i--) {
node = nodes[i];
className = node.attr('class');
if (/^(MsoCommentReference|MsoCommentText|msoDel)$/i.test(className || '')) {
node.remove();
}
node.attr('class', null);
}
});
// Remove all del elements since we don't want the track changes code in the editor
domParser.addNodeFilter('del', nodes => {
let i = nodes.length;
while (i--) {
nodes[i].remove();
}
});
// Keep some of the links and anchors
domParser.addNodeFilter('a', nodes => {
let i = nodes.length;
let node;
let href;
let name;
while (i--) {
node = nodes[i];
href = node.attr('href');
name = node.attr('name');
if (href && href.indexOf('#_msocom_') !== -1) {
node.remove();
continue;
}
if (href && href.indexOf('file://') === 0) {
href = href.split('#')[1];
if (href) {
href = `#${href}`;
}
}
if (!href && !name) {
node.unwrap();
} else {
// Remove all named anchors that aren't specific to TOC, Footnotes or Endnotes
if (name && !/^_?(?:toc|edn|ftn)/i.test(name)) {
node.unwrap();
continue;
}
node.attr({
href,
name
});
}
}
});
// Parse into DOM structure
const rootNode = domParser.parse(content);
// Process DOM
if (shouldConvertWordFakeLists(editor)) {
convertFakeListsToProperLists(rootNode);
}
// Serialize DOM back to HTML
content = HtmlSerializer({
validate: getValidate(editor)
}, schema).serialize(rootNode);
return content;
};
const preProcess = (editor, content) => shouldUseDefaultFilters(editor) ? filterWordContent(editor, content) : content;
var Plugin = () => {
tinymce.PluginManager.add('pasteword', editor => {
let isRemoveStylesIfWebkit;
editor.on('PastePreProcess', event => {
var _options$get;
const {
content
} = event;
const {
options
} = editor;
isRemoveStylesIfWebkit = (_options$get = options.get('paste_remove_styles_if_webkit')) != null ? _options$get : true;
if (isWordContent(content)) {
event.content = preProcess(editor, content);
if (isRemoveStylesIfWebkit) {
/**
* Don't know why, but with it enabled styles from Word are sanitized
* @see https://github.com/tinymce/tinymce/blob/6.6.2/modules/tinymce/src/core/main/ts/paste/Quirks.ts#L40C59-L40C59
*/
options.set('paste_remove_styles_if_webkit', false);
}
}
});
editor.on('PastePostProcess', () => {
const {
options
} = editor;
if (options.get('paste_remove_styles_if_webkit') !== isRemoveStylesIfWebkit) {
options.set('paste_remove_styles_if_webkit', isRemoveStylesIfWebkit);
}
});
/* Return the metadata for the help plugin */
return {
getMetadata: () => ({
name: 'Paste from Word',
url: 'https://www.openregion.info/'
})
};
});
};
Plugin();
No other release has this exact content. See every version of this file.