@ckeditor/ckeditor5-paste-from-office
Version:
Paste from Office feature for CKEditor 5.
4 lines • 14.2 kB
JavaScript
/*!
* @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md.
*/(()=>{var e={237:e=>{"use strict";e.exports=CKEditor5.dll},331:(e,t,n)=>{e.exports=n(237)("./src/clipboard.js")},782:(e,t,n)=>{e.exports=n(237)("./src/core.js")},783:(e,t,n)=>{e.exports=n(237)("./src/engine.js")}},t={};function n(s){var r=t[s];if(void 0!==r)return r.exports;var i=t[s]={exports:{}};return e[s](i,i.exports,n),i.exports}n.d=(e,t)=>{for(var s in t)n.o(t,s)&&!n.o(e,s)&&Object.defineProperty(e,s,{enumerable:!0,get:t[s]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var s={};(()=>{"use strict";n.r(s),n.d(s,{MSWordNormalizer:()=>w,PasteFromOffice:()=>$,parseHtml:()=>P});var e=n(782),t=n(331);var r=n(783);function i(e){const t=parseFloat(e);return e.endsWith("pt")?l(96*t/72):e.endsWith("pc")?l(12*t*96/72):e.endsWith("in")?l(96*t):e.endsWith("cm")?l(96*t/2.54):e.endsWith("mm")?l(t/10*96/2.54):e}function o(e){return void 0!==e&&e.endsWith("px")}function l(e){return Math.round(e)+"px"}function c(e,t,n){if(!e.childCount)return;const s=new r.UpcastWriter(e.document),i=function(e,t){const n=t.createRangeIn(e),s=[],r=new Set;for(const e of n.getItems()){if(!e.is("element")||!e.name.match(/^(p|h\d+|li|div)$/))continue;let t=h(e);if(void 0===t||0!=parseFloat(t)||Array.from(e.getClassNames()).find(e=>e.startsWith("MsoList"))||(t=void 0),e.hasStyle("mso-list")||void 0!==t&&r.has(t)){const n=p(e);s.push({element:e,id:n.id,order:n.order,indent:n.indent,marginLeft:t}),void 0!==t&&r.add(t)}else r.clear()}return s}(e,s);if(!i.length)return;const c={},m=[];for(const e of i)if(void 0!==e.indent){a(e)||(m.length=0);const r=`${e.id}:${e.indent}`,i=Math.min(e.indent-1,m.length);if(i<m.length&&m[i].id!==e.id&&(m.length=i),i<m.length-1)m.length=i+1;else{const a=u(e,t);if(i>m.length-1||m[i].listElement.name!=a.type){0==i&&"ol"==a.type&&void 0!==e.id&&c[r]&&(a.startIndex=c[r]);const t=f(a,s,n);if(o(e.marginLeft)&&(0==i||o(m[i-1].marginLeft))){let n=e.marginLeft;i>0&&(n=l(parseFloat(n)-parseFloat(m[i-1].marginLeft))),s.setStyle("padding-left",n,t)}if(0==m.length){const n=e.element.parent,r=n.getChildIndex(e.element)+1;s.insertChild(r,t,n)}else{const e=m[i-1].listItemElements;s.appendChild(t,e[e.length-1])}m[i]={...e,listElement:t,listItemElements:[]},0==i&&void 0!==e.id&&(c[r]=a.startIndex||1)}}const d="li"==e.element.name?e.element:s.createElement("li");s.appendChild(d,m[i].listElement),m[i].listItemElements.push(d),0==i&&void 0!==e.id&&c[r]++,e.element!=d&&s.appendChild(e.element,d),g(e.element,s),s.removeStyle("text-indent",e.element),s.removeStyle("margin-left",e.element)}else{const t=m.find(t=>t.marginLeft==e.marginLeft);if(t){const n=t.listItemElements;s.appendChild(e.element,n[n.length-1]),s.removeStyle("margin-left",e.element)}else m.length=0}}function a(e){const t=e.element.previousSibling;return m(t||e.element.parent)}function m(e){return e.is("element","ol")||e.is("element","ul")}function u(e,t){const n=new RegExp(`@list l${e.id}:level${e.indent}\\s*({[^}]*)`,"gi"),s=/mso-level-number-format:([^;]{0,100});/gi,r=/mso-level-start-at:\s{0,100}([0-9]{0,10})\s{0,100};/gi,i=new RegExp(`@list\\s+l${e.id}:level\\d\\s*{[^{]*mso-level-text:"%\\d\\\\.`,"gi"),o=new RegExp(`@list l${e.id}:level\\d\\s*{[^{]*mso-level-number-format:`,"gi"),l=i.exec(t),c=o.exec(t),a=l&&!c,m=n.exec(t);let u="decimal",f="ol",p=null;if(m&&m[1]){const t=s.exec(m[1]);if(t&&t[1]&&(u=t[1].trim(),f="bullet"!==u&&"image"!==u?"ol":"ul"),"bullet"===u){const t=function(e){if("li"==e.name&&"ul"==e.parent.name&&e.parent.hasAttribute("type"))return e.parent.getAttribute("type");const t=function(e){if(e.getChild(0).is("$text"))return null;for(const t of e.getChildren()){if(!t.is("element","span"))continue;const e=t.getChild(0);if(e)return e.is("$text")?e:e.getChild(0)}return null}(e);if(!t)return null;const n=t._data;if("o"===n)return"circle";if("·"===n)return"disc";if("§"===n)return"square";return null}(e.element);t&&(u=t)}else{const e=r.exec(m[1]);e&&e[1]&&(p=parseInt(e[1]))}a&&(f="ol")}return{type:f,startIndex:p,style:d(u),isLegalStyleList:a}}function d(e){if(e.startsWith("arabic-leading-zero"))return"decimal-leading-zero";switch(e){case"alpha-upper":return"upper-alpha";case"alpha-lower":return"lower-alpha";case"roman-upper":return"upper-roman";case"roman-lower":return"lower-roman";case"circle":case"disc":case"square":return e;default:return null}}function f(e,t,n){const s=t.createElement(e.type);return e.style&&t.setStyle("list-style-type",e.style,s),e.startIndex&&e.startIndex>1&&t.setAttribute("start",e.startIndex,s),e.isLegalStyleList&&n&&t.addClass("legal-list",s),s}function p(e){const t=e.getStyle("mso-list");if(void 0===t)return{};const n=t.match(/(^|\s{1,100})l(\d+)/i),s=t.match(/\s{0,100}lfo(\d+)/i),r=t.match(/\s{0,100}level(\d+)/i);return n&&s&&r?{id:n[2],order:s[1],indent:parseInt(r[1])}:{indent:1}}function g(e,t){const n=new r.Matcher({name:"span",styles:{"mso-list":"Ignore"}}),s=t.createRangeIn(e);for(const e of s)"elementStart"===e.type&&n.match(e.item)&&t.remove(e.item)}function h(e){const t=e.getStyle("margin-left");return void 0===t||t.endsWith("px")?t:i(t)}function y(e,t){if(!e.childCount)return;const n=new r.UpcastWriter(e.document),s=function(e,t){const n=t.createRangeIn(e),s=new r.Matcher({name:/v:(.+)/}),i=[];for(const e of n){if("elementStart"!=e.type)continue;const t=e.item,n=t.previousSibling,r=n&&n.is("element")?n.name:null,o=["Chart"],l=s.match(t),c=t.getAttribute("o:gfxdata"),a="v:shapetype"===r,m=c&&o.some(e=>t.getAttribute("id").includes(e));l&&c&&!a&&!m&&i.push(e.item.getAttribute("id"))}return i}(e,n);!function(e,t,n){const s=n.createRangeIn(t),i=new r.Matcher({name:"img"}),o=[];for(const t of s)if(t.item.is("element")&&i.match(t.item)){const n=t.item,s=n.getAttribute("v:shapes")?n.getAttribute("v:shapes").split(" "):[];s.length&&s.every(t=>e.indexOf(t)>-1)?o.push(n):n.getAttribute("src")||o.push(n)}for(const e of o)n.remove(e)}(s,e,n),function(e,t,n){const s=n.createRangeIn(t),r=[];for(const t of s)if("elementStart"==t.type&&t.item.is("element","v:shape")){const n=t.item.getAttribute("id");if(e.includes(n))continue;i(t.item.parent.getChildren(),n)||r.push(t.item)}for(const e of r){const t={src:o(e)};e.hasAttribute("alt")&&(t.alt=e.getAttribute("alt"));const s=n.createElement("img",t);n.insertChild(e.index+1,s,e.parent)}function i(e,t){for(const n of e)if(n.is("element")){if("img"==n.name&&n.getAttribute("v:shapes")==t)return!0;if(i(n.getChildren(),t))return!0}return!1}function o(e){for(const t of e.getChildren())if(t.is("element")&&t.getAttribute("src"))return t.getAttribute("src")}}(s,e,n),function(e,t){const n=t.createRangeIn(e),s=new r.Matcher({name:/v:(.+)/}),i=[];for(const e of n)"elementStart"==e.type&&s.match(e.item)&&i.push(e.item);for(const e of i)t.remove(e)}(e,n);const i=function(e,t){const n=t.createRangeIn(e),s=new r.Matcher({name:"img"}),i=[];let o=0;for(const e of n)e.item.is("element")&&s.match(e.item)&&(e.item.getAttribute("src").startsWith("file://")&&i.push({element:e.item,imageIndex:o}),o++);return i}(e,n);i.length&&function(e,t,n){for(let s=0;s<e.length;s++){const{element:r,imageIndex:i}=e[s],o=t[i];if(o){const e=`data:${o.type};base64,${b(o.hex)}`;n.setAttribute("src",e,r)}}}(i,function(e){if(!e)return[];const t=/{\\pict[\s\S]+?\\bliptag-?\d+(\\blipupi-?\d+)?({\\\*\\blipuid\s?[\da-fA-F]+)?[\s}]*?/,n=new RegExp("(?:("+t.source+"))([\\da-fA-F\\s]+)\\}","g"),s=e.match(n),r=[];if(s)for(const e of s){let n=!1;e.includes("\\pngblip")?n="image/png":e.includes("\\jpegblip")&&(n="image/jpeg"),n&&r.push({hex:e.replace(t,"").replace(/[^\da-fA-F]/g,""),type:n})}return r}(t),n)}function b(e){return btoa(e.match(/\w{2}/g).map(e=>String.fromCharCode(parseInt(e,16))).join(""))}const v=/<meta\s*name="?generator"?\s*content="?microsoft\s*word\s*\d+"?\/?>/i,x=/xmlns:o="urn:schemas-microsoft-com/i;class w{document;hasMultiLevelListPlugin;constructor(e,t=!1){this.document=e,this.hasMultiLevelListPlugin=t}isActive(e){return v.test(e)||x.test(e)}execute(e){const t=new r.UpcastWriter(this.document),{body:n,stylesString:s}=e._parsedData;!function(e,t){const n=[];for(const s of t.createRangeIn(e)){const e=s.item;e.is("element","a")&&!e.hasAttribute("href")&&(e.hasAttribute("id")||e.hasAttribute("name"))&&n.push(e)}for(const e of n){const n=e.parent.getChildIndex(e)+1,s=e.getChildren();t.insertChild(n,s,e.parent)}}(n,t),c(n,s,this.hasMultiLevelListPlugin),y(n,e.dataTransfer.getData("text/rtf")),function(e,t){for(const n of t.createRangeIn(e).getItems()){if(!n.is("element","table")&&!n.is("element","td")&&!n.is("element","th"))continue;const e=["left","top","right","bottom"];if(e.every(e=>!n.hasStyle(`border-${e}-style`)))t.setStyle("border-style","none",n);else for(const s of e)n.hasStyle(`border-${s}-style`)||t.setStyle(`border-${s}-style`,"none",n);const s=["width","height",...e.map(e=>`border-${e}-width`),...e.map(e=>`padding-${e}`)];for(const e of s)n.hasStyle(e)&&t.setStyle(e,i(n.getStyle(e)),n)}}(n,t),function(e){const t=[],n=new r.UpcastWriter(e.document);for(const{item:s}of n.createRangeIn(e))if(s.is("element")){for(const e of s.getClassNames())/\bmso/gi.exec(e)&&n.removeClass(e,s);for(const e of s.getStyleNames())/\bmso/gi.exec(e)&&n.removeStyle(e,s);(s.is("element","w:sdt")||s.is("element","w:sdtpr")&&s.isEmpty||s.is("element","o:p")&&s.isEmpty)&&t.push(s)}for(const e of t){const t=e.parent,s=t.getChildIndex(e);n.insertChild(s,e.getChildren(),t),n.remove(e)}}(n),e.content=n}}function C(e,t,n,{blockElements:s,inlineObjectElements:r}){let i=n.createPositionAt(e,"forward"==t?"after":"before");return i=i.getLastMatchingPosition(({item:e})=>e.is("element")&&!s.includes(e.name)&&!r.includes(e.name),{direction:t}),"forward"==t?i.nodeAfter:i.nodeBefore}function S(e,t){return!!e&&e.is("element")&&t.includes(e.name)}const A=/id=("|')docs-internal-guid-[-0-9a-f]+("|')/i;class I{document;constructor(e){this.document=e}isActive(e){return A.test(e)}execute(e){const t=new r.UpcastWriter(this.document),{body:n}=e._parsedData;!function(e,t){for(const n of e.getChildren())if(n.is("element","b")&&"normal"===n.getStyle("font-weight")){const s=e.getChildIndex(n);t.remove(n),t.insertChild(s,n.getChildren(),e)}}(n,t),function(e,t){for(const n of t.createRangeIn(e)){const e=n.item;if(e.is("element","li")){const n=e.getChild(0);n&&n.is("element","p")&&t.unwrapElement(n)}}}(n,t),function(e,t){const n=new r.ViewDocument(t.document.stylesProcessor),s=new r.DomConverter(n,{renderingMode:"data"}),i=s.blockElements,o=s.inlineObjectElements,l=[];for(const n of t.createRangeIn(e)){const e=n.item;if(e.is("element","br")){const n=C(e,"forward",t,{blockElements:i,inlineObjectElements:o}),s=C(e,"backward",t,{blockElements:i,inlineObjectElements:o}),r=S(n,i);(S(s,i)||r)&&l.push(e)}}for(const e of l)e.hasClass("Apple-interchange-newline")?t.remove(e):t.replace(e,t.createElement("p"))}(n,t),e.content=n}}const E=/<google-sheets-html-origin/i;class L{document;constructor(e){this.document=e}isActive(e){return E.test(e)}execute(e){const t=new r.UpcastWriter(this.document),{body:n}=e._parsedData;!function(e,t){for(const n of e.getChildren())if(n.is("element","google-sheets-html-origin")){const s=e.getChildIndex(n);t.remove(n),t.insertChild(s,n.getChildren(),e)}}(n,t),function(e,t){for(const n of e.getChildren())n.is("element","table")&&n.hasAttribute("xmlns")&&t.removeAttribute("xmlns",n)}(n,t),function(e,t){for(const n of e.getChildren())n.is("element","table")&&"0px"===n.getStyle("width")&&t.removeStyle("width",n)}(n,t),function(e,t){for(const n of Array.from(e.getChildren()))n.is("element","style")&&t.remove(n)}(n,t),e.content=n}}function M(e){return e.replace(/<span(?: class="Apple-converted-space"|)>(\s+)<\/span>/g,(e,t)=>1===t.length?" ":Array(t.length+1).join(" ").substr(0,t.length))}function P(e,t){const n=new DOMParser,s=function(e){return M(M(e)).replace(/(<span\s+style=['"]mso-spacerun:yes['"]>[^\S\r\n]*?)[\r\n]+([^\S\r\n]*<\/span>)/g,"$1$2").replace(/<span\s+style=['"]mso-spacerun:yes['"]><\/span>/g,"").replace(/(<span\s+style=['"]letter-spacing:[^'"]+?['"]>)[\r\n]+(<\/span>)/g,"$1 $2").replace(/ <\//g," </").replace(/ <o:p><\/o:p>/g," <o:p></o:p>").replace(/<o:p>( |\u00A0)<\/o:p>/g,"").replace(/>([^\S\r\n]*[\r\n]\s*)</g,"><")}(function(e){const t="</body>",n="</html>",s=e.indexOf(t);if(s<0)return e;const r=e.indexOf(n,s+t.length);return e.substring(0,s+t.length)+(r>=0?e.substring(r):"")}(e=(e=e.replace(/<!--\[if gte vml 1]>/g,"")).replace(/<o:SmartTagType(?:\s+[^\s>=]+(?:="[^"]*")?)*\s*\/?>/gi,""))),i=n.parseFromString(s,"text/html");!function(e){e.querySelectorAll("span[style*=spacerun]").forEach(e=>{const t=e,n=t.innerText.length||0;t.innerText=Array(n+1).join(" ").substr(0,n)})}(i);const o=i.body.innerHTML,l=function(e,t){const n=new r.ViewDocument(t),s=new r.DomConverter(n,{renderingMode:"data"}),i=e.createDocumentFragment(),o=e.body.childNodes;for(;o.length>0;)i.appendChild(o[0]);return s.domToView(i,{skipComments:!0})}(i,t),c=function(e){const t=[],n=[],s=Array.from(e.getElementsByTagName("style"));for(const e of s)e.sheet&&e.sheet.cssRules&&e.sheet.cssRules.length&&(t.push(e.sheet),n.push(e.innerHTML));return{styles:t,stylesString:n.join(" ")}}(i);return{body:l,bodyString:o,styles:c.styles,stylesString:c.stylesString}}class $ extends e.Plugin{static get pluginName(){return"PasteFromOffice"}static get isOfficialPlugin(){return!0}static get requires(){return[t.ClipboardPipeline]}init(){const e=this.editor,t=e.plugins.get("ClipboardPipeline"),n=e.editing.view.document,s=[],r=this.editor.plugins.has("MultiLevelList");s.push(new w(n,r)),s.push(new I(n)),s.push(new L(n)),t.on("inputTransformation",(t,r)=>{if(r._isTransformedWithPasteFromOffice)return;if(e.model.document.selection.getFirstPosition().parent.is("element","codeBlock"))return;const i=r.dataTransfer.getData("text/html"),o=s.find(e=>e.isActive(i));o&&(r._parsedData||(r._parsedData=P(i,n.stylesProcessor)),o.execute(r),r._isTransformedWithPasteFromOffice=!0)},{priority:"high"})}}})(),(window.CKEditor5=window.CKEditor5||{}).pasteFromOffice=s})();