@ckeditor/ckeditor5-paste-from-office
Version:
Paste from Office feature for CKEditor 5.
4 lines • 18.3 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(i){var s=t[i];if(void 0!==s)return s.exports;var r=t[i]={exports:{}};return e[i](r,r.exports,n),r.exports}n.d=(e,t)=>{for(var i in t)n.o(t,i)&&!n.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:t[i]})},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 i={};(()=>{"use strict";n.r(i),n.d(i,{PasteFromOffice:()=>G,PasteFromOfficeGoogleDocsNormalizer:()=>k,PasteFromOfficeGoogleSheetsNormalizer:()=>N,PasteFromOfficeMSWordNormalizer:()=>T,_convertHexToBase64:()=>x,_convertPasteOfficeCssLengthToPx:()=>o,_isPasteOfficePxValue:()=>l,_normalizePasteOfficeSpaceRunSpans:()=>U,_normalizePasteOfficeSpacing:()=>z,_removePasteGoogleOfficeSheetsTag:()=>B,_removePasteMSOfficeAttributes:()=>S,_removePasteOfficeBoldWrapper:()=>L,_removePasteOfficeInvalidTableWidths:()=>C,_removePasteOfficeStyleBlock:()=>D,_removePasteOfficeXmlnsAttributes:()=>j,_replacePasteOfficeImagesSourceWithBase64:()=>v,_toPasteOfficePxValue:()=>a,_transformPasteOfficeBlockBrsToParagraphs:()=>_,_transformPasteOfficeBookmarks:()=>s,_transformPasteOfficeListItemLikeElementsIntoLists:()=>c,_transformPasteOfficeTables:()=>w,_unwrapPasteOfficeParagraphInListItem:()=>f,parsePasteOfficeHtml:()=>H});var e=n(782),t=n(331);function s(e,t){const n=[];for(const i of t.createRangeIn(e)){const e=i.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,i=e.getChildren();t.insertChild(n,i,e.parent)}}var r=n(783);function o(e){const t=parseFloat(e);return e.endsWith("pt")?a(96*t/72):e.endsWith("pc")?a(12*t*96/72):e.endsWith("in")?a(96*t):e.endsWith("cm")?a(96*t/2.54):e.endsWith("mm")?a(t/10*96/2.54):e}function l(e){return void 0!==e&&e.endsWith("px")}function a(e){return Math.round(e)+"px"}function c(e,t,n){if(!e.childCount)return;const i=new r.ViewUpcastWriter(e.document),s=function(e,t){const n=t.createRangeIn(e),i=[],s=new Set;for(const e of n.getItems()){if(!e.is("element")||!e.name.match(/^(p|h\d+|li|div)$/))continue;let t=y(e);if(void 0===t||0!=parseFloat(t)||Array.from(e.getClassNames()).find(e=>e.startsWith("MsoList"))||(t=void 0),e.hasStyle("mso-list")&&"none"!==e.getStyle("mso-list")||void 0!==t&&s.has(t)){const n=h(e);i.push({element:e,id:n.id,order:n.order,indent:n.indent,marginLeft:t}),void 0!==t&&s.add(t)}else s.clear()}return i}(e,i);if(!s.length)return;const o={},c=[];for(const e of s)if(void 0!==e.indent){m(e)||(c.length=0);const s=`${e.id}:${e.indent}`,r=Math.min(e.indent-1,c.length);if(r<c.length&&c[r].id!==e.id&&(c.length=r),r<c.length-1)c.length=r+1;else{const f=d(e,t);if(r>c.length-1||c[r].listElement.name!=f.type){0==r&&"ol"==f.type&&void 0!==e.id&&o[s]&&(f.startIndex=o[s]);const t=p(f,i,n);if(l(e.marginLeft)&&(0==r||l(c[r-1].marginLeft))){let n=e.marginLeft;r>0&&(n=a(parseFloat(n)-parseFloat(c[r-1].marginLeft))),i.setStyle("padding-left",n,t)}if(0==c.length){const n=e.element.parent,s=n.getChildIndex(e.element)+1;i.insertChild(s,t,n)}else{const e=c[r-1].listItemElements;i.appendChild(t,e[e.length-1])}c[r]={...e,listElement:t,listItemElements:[]},0==r&&void 0!==e.id&&(o[s]=f.startIndex||1)}}const f="li"==e.element.name?e.element:i.createElement("li");i.appendChild(f,c[r].listElement),c[r].listItemElements.push(f),0==r&&void 0!==e.id&&o[s]++,e.element!=f&&i.appendChild(e.element,f),b(e.element,i),i.removeStyle("text-indent",e.element),i.removeStyle("margin-left",e.element)}else{const t=c.find(t=>t.marginLeft==e.marginLeft);if(t){const n=t.listItemElements;i.appendChild(e.element,n[n.length-1]),i.removeStyle("margin-left",e.element)}else c.length=0}}function f(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)}}}function m(e){const t=e.element.previousSibling;if(!t){const t=e.element.parent;return u(t)&&(!t.previousSibling||u(t.previousSibling))}return u(t)}function u(e){return e.is("element","ol")||e.is("element","ul")}function d(e,t){const n=new RegExp(`@list l${e.id}:level${e.indent}\\s*({[^}]*)`,"gi"),i=/mso-level-number-format:([^;]{0,100});/gi,s=/mso-level-start-at:\s{0,100}([0-9]{0,10})\s{0,100};/gi,r=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=r.exec(t),a=o.exec(t),c=l&&!a,f=n.exec(t);let m="decimal",u="ol",d=null;if(f&&f[1]){const t=i.exec(f[1]);if(t&&t[1]&&(m=t[1].trim(),u="bullet"!==m&&"image"!==m?"ol":"ul"),"bullet"===m){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&&(m=t)}else{const e=s.exec(f[1]);e&&e[1]&&(d=parseInt(e[1]))}c&&(u="ol")}return{type:u,startIndex:d,style:g(m),isLegalStyleList:c}}function g(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 p(e,t,n){const i=t.createElement(e.type);return e.style&&t.setStyle("list-style-type",e.style,i),e.startIndex&&e.startIndex>1&&t.setAttribute("start",e.startIndex,i),e.isLegalStyleList&&n&&t.addClass("legal-list",i),i}function h(e){const t=e.getStyle("mso-list");if(void 0===t)return{};const n=t.match(/(^|\s{1,100})l(\d+)/i),i=t.match(/\s{0,100}lfo(\d+)/i),s=t.match(/\s{0,100}level(\d+)/i);return n&&i&&s?{id:n[2],order:i[1],indent:parseInt(s[1])}:{indent:1}}function b(e,t){const n=new r.Matcher({name:"span",styles:{"mso-list":"Ignore"}}),i=t.createRangeIn(e);for(const e of i)"elementStart"===e.type&&n.match(e.item)&&t.remove(e.item)}function y(e){const t=e.getStyle("margin-left");return void 0===t||t.endsWith("px")?t:o(t)}function v(e,t){if(!e.childCount)return;const n=new r.ViewUpcastWriter(e.document),i=function(e,t){const n=t.createRangeIn(e),i=new r.Matcher({name:/v:(.+)/}),s=[];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=i.match(t),a=t.getAttribute("o:gfxdata"),c="v:shapetype"===r,f=a&&o.some(e=>t.getAttribute("id").includes(e));l&&a&&!c&&!f&&s.push(e.item.getAttribute("id"))}return s}(e,n);!function(e,t,n){const i=n.createRangeIn(t),s=new r.Matcher({name:"img"}),o=[];for(const t of i)if(t.item.is("element")&&s.match(t.item)){const n=t.item,i=n.getAttribute("v:shapes")?n.getAttribute("v:shapes").split(" "):[];i.length&&i.every(t=>e.indexOf(t)>-1)?o.push(n):n.getAttribute("src")||o.push(n)}for(const e of o)n.remove(e)}(i,e,n),function(e,t,n){const i=n.createRangeIn(t),s=[];for(const t of i)if("elementStart"==t.type&&t.item.is("element","v:shape")){const n=t.item.getAttribute("id");if(e.includes(n))continue;r(t.item.parent.getChildren(),n)||s.push(t.item)}for(const e of s){const t={src:o(e)};e.hasAttribute("alt")&&(t.alt=e.getAttribute("alt"));const i=n.createElement("img",t);n.insertChild(e.index+1,i,e.parent)}function r(e,t){for(const n of e)if(n.is("element")){if("img"==n.name&&n.getAttribute("v:shapes")==t)return!0;if(r(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")}}(i,e,n),function(e,t){const n=t.createRangeIn(e),i=new r.Matcher({name:/v:(.+)/}),s=[];for(const e of n)"elementStart"==e.type&&i.match(e.item)&&s.push(e.item);for(const e of s)t.remove(e)}(e,n);const s=function(e,t){const n=t.createRangeIn(e),i=new r.Matcher({name:"img"}),s=[];let o=0;for(const e of n)e.item.is("element")&&i.match(e.item)&&(e.item.getAttribute("src").startsWith("file://")&&s.push({element:e.item,imageIndex:o}),o++);return s}(e,n);s.length&&function(e,t,n){for(let i=0;i<e.length;i++){const{element:s,imageIndex:r}=e[i],o=t[r];if(o){const e=`data:${o.type};base64,${x(o.hex)}`;n.setAttribute("src",e,s)}}}(s,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"),i=e.match(n),s=[];if(i)for(const e of i){let n=!1;e.includes("\\pngblip")?n="image/png":e.includes("\\jpegblip")&&(n="image/jpeg"),n&&s.push({hex:e.replace(t,"").replace(/[^\da-fA-F]/g,""),type:n})}return s}(t),n)}function x(e){return btoa(e.match(/\w{2}/g).map(e=>String.fromCharCode(parseInt(e,16))).join(""))}function S(e){const t=[],n=new r.ViewUpcastWriter(e.document);for(const{item:i}of n.createRangeIn(e))if(i.is("element")){for(const e of i.getClassNames())/\bmso/gi.exec(e)&&n.removeClass(e,i);for(const e of i.getStyleNames())/\bmso/gi.exec(e)&&n.removeStyle(e,i);(i.is("element","w:sdt")||i.is("element","w:sdtpr")&&i.isEmpty||i.is("element","o:p")&&i.isEmpty)&&t.push(i)}for(const e of t){const t=e.parent,i=t.getChildIndex(e);n.insertChild(i,e.getChildren(),t),n.remove(e)}}function w(e,t,n=!1,i=!1){for(const s of t.createRangeIn(e).getItems()){if(!s.is("element","table")&&!s.is("element","td")&&!s.is("element","th"))continue;if(n&&i&&s.is("element","table")){const e=s.parent?.is("element","div")?s.parent:null,n=s.parent?.parent?.is("element","div")?s.parent.parent:null,i=e??n;i&&"center"===i.getAttribute("align")&&!s.getAttribute("align")?(t.setStyle("margin-left","auto",s),t.setStyle("margin-right","auto",s)):i&&"right"===i.getAttribute("align")&&!s.getAttribute("align")?(t.setStyle("margin-left","auto",s),t.setStyle("margin-right","0",s)):i||s.getAttribute("align")||(t.setStyle("margin-left","0",s),t.setStyle("margin-right","auto",s))}const e=["left","top","right","bottom"];if(e.every(e=>!s.hasStyle(`border-${e}-style`)))t.setStyle("border-style","none",s);else for(const n of e)s.hasStyle(`border-${n}-style`)||t.setStyle(`border-${n}-style`,"none",s);const r=["width","height",...e.map(e=>`border-${e}-width`),...e.map(e=>`padding-${e}`)];for(const e of r)s.hasStyle(e)&&t.setStyle(e,o(s.getStyle(e)),s)}}function C(e,t){for(const n of t.createRangeIn(e).getItems())n.is("element","table")&&("0px"===n.getStyle("width")&&t.removeStyle("width",n),"0"===n.getAttribute("width")&&t.removeAttribute("width",n))}function P(e,t){const n=[],i=[];for(const{item:s}of e.createRangeIn(t))if(s.is("element")&&s.getStyle("mso-footnote-id")){n.unshift(s);const{nextSibling:e}=s;e?.is("$text")&&e.data.startsWith(" ")&&i.unshift(e)}for(const t of n)e.remove(t);for(const t of i){const n=t.data.substring(1);if(n.length>0){const i=t.parent,s=i.getChildIndex(t),r=e.createText(n);e.remove(t),e.insertChild(s,r,i)}else e.remove(t)}return t}function A(e,t){const n=e.createElement("sup",{class:"footnote"}),i=e.createElement("a",{id:`ref-${t}`,href:`#${t}`});return e.appendChild(i,n),n}function I(e,t){const n=e.createElement("li",{id:t,class:"footnote-definition"}),i=e.createElement("a",{href:`#ref-${t}`,class:"footnote-backlink"}),s=e.createElement("div",{class:"footnote-content"});return e.appendChild(e.createText("^"),i),e.appendChild(i,n),e.appendChild(s,n),{listItem:n,content:s}}const E=/<meta\s*name="?generator"?\s*content="?microsoft\s*word\s*\d+"?\/?>/i,O=/xmlns:o="urn:schemas-microsoft-com/i;class T{document;hasMultiLevelListPlugin;hasTablePropertiesPlugin;hasExtendedTableBlockAlignment;constructor(e,t=!1,n=!1,i=!1){this.document=e,this.hasMultiLevelListPlugin=t,this.hasTablePropertiesPlugin=n,this.hasExtendedTableBlockAlignment=i}isActive(e){return E.test(e)||O.test(e)}execute(e){const t=new r.ViewUpcastWriter(this.document),{body:n,stylesString:i}=e._parsedData;s(n,t),c(n,i,this.hasMultiLevelListPlugin),v(n,e.dataTransfer.getData("text/rtf")),w(n,t,this.hasTablePropertiesPlugin,this.hasExtendedTableBlockAlignment),C(n,t),function(e,t){const n=new Map,i=new Map;let s=null;for(const{item:r}of t.createRangeIn(e))if(r.is("element"))if("footnote-list"!==r.getStyle("mso-element")){if(r.hasStyle("mso-footnote-id")){const e=r.findAncestor("element",e=>"footnote"===e.getStyle("mso-element"));if(e){const t=e.getAttribute("id");i.set(t,e)}else{const e=r.getStyle("mso-footnote-id");n.set(e,r)}continue}}else s=r;if(!n.size||!s)return;const r=function(e){return e.createElement("ol",{class:"footnotes"})}(t);t.replace(s,r);for(const[e,s]of n){const n=i.get(e);if(!n)continue;t.replace(s,A(t,e));const o=I(t,e);P(t,n);for(const e of n.getChildren()){let n=e;e.is("element")&&(n=t.clone(e,!0)),t.appendChild(n,o.content)}t.appendChild(o.listItem,r)}}(n,t),S(n),e.content=n}}function L(e,t){for(const n of e.getChildren())if(n.is("element","b")&&"normal"===n.getStyle("font-weight")){const i=e.getChildIndex(n);t.remove(n),t.insertChild(i,n.getChildren(),e)}}function _(e,t){const n=new r.ViewDocument(t.document.stylesProcessor),i=new r.ViewDomConverter(n,{renderingMode:"data"}),s=i.blockElements,o=i.inlineObjectElements,l=[];for(const n of t.createRangeIn(e)){const e=n.item;if(e.is("element","br")){const n=$(e,"forward",t,{blockElements:s,inlineObjectElements:o}),i=$(e,"backward",t,{blockElements:s,inlineObjectElements:o}),r=M(n,s);(M(i,s)||r)&&l.push(e)}}for(const e of l)e.hasClass("Apple-interchange-newline")?t.remove(e):t.replace(e,t.createElement("p"))}function $(e,t,n,{blockElements:i,inlineObjectElements:s}){let r=n.createPositionAt(e,"forward"==t?"after":"before");return r=r.getLastMatchingPosition(({item:e})=>e.is("element")&&!i.includes(e.name)&&!s.includes(e.name),{direction:t}),"forward"==t?r.nodeAfter:r.nodeBefore}function M(e,t){return!!e&&e.is("element")&&t.includes(e.name)}function R(e){let t=e;for(;t;){if(t.is("element")){const e=t.getStyle?.("white-space");if("pre-wrap"===e)return!0}t=t.parent}return!1}function W(e,t,n){const{parent:i,data:s}=e,r=s.replaceAll("\t"," ".repeat(n)),o=i.getChildIndex(e);t.remove(e),t.insertChild(o,t.createText(r),i)}const F=/id=("|')docs-internal-guid-[-0-9a-f]+("|')/i;class k{document;constructor(e){this.document=e}isActive(e){return F.test(e)}execute(e){const t=new r.ViewUpcastWriter(this.document),{body:n}=e._parsedData;L(n,t),f(n,t),_(n,t),function(e,t,n){const i=new Set;for(const n of t.createRangeIn(e).getItems())n.is("view:$textProxy")&&n.data.includes("\t")&&R(n.parent)&&i.add(n.textNode);for(const e of i)W(e,t,n)}(n,t,8),e.content=n}}function j(e,t){for(const n of e.getChildren())n.is("element","table")&&n.hasAttribute("xmlns")&&t.removeAttribute("xmlns",n)}function B(e,t){for(const n of e.getChildren())if(n.is("element","google-sheets-html-origin")){const i=e.getChildIndex(n);t.remove(n),t.insertChild(i,n.getChildren(),e)}}function D(e,t){for(const n of Array.from(e.getChildren()))n.is("element","style")&&t.remove(n)}const V=/<google-sheets-html-origin/i;class N{document;constructor(e){this.document=e}isActive(e){return V.test(e)}execute(e){const t=new r.ViewUpcastWriter(this.document),{body:n}=e._parsedData;B(n,t),j(n,t),C(n,t),D(n,t),e.content=n}}function z(e){return q(q(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 U(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)})}function q(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 H(e,t){const n=new DOMParser,i=z(function(e){const t="</body>",n="</html>",i=e.indexOf(t);if(i<0)return e;const s=e.indexOf(n,i+t.length);return e.substring(0,i+t.length)+(s>=0?e.substring(s):"")}(e=(e=e.replace(/<!--\[if gte vml 1]>/g,"")).replace(/<o:SmartTagType(?:\s+[^\s>=]+(?:="[^"]*")?)*\s*\/?>/gi,""))),s=n.parseFromString(i,"text/html");U(s);const o=s.body.innerHTML,l=function(e,t){const n=new r.ViewDocument(t),i=new r.ViewDomConverter(n,{renderingMode:"data"}),s=e.createDocumentFragment(),o=e.body.childNodes;for(;o.length>0;)s.appendChild(o[0]);return i.domToView(s,{skipComments:!0})}(s,t),a=function(e){const t=[],n=[],i=Array.from(e.getElementsByTagName("style"));for(const e of i)e.sheet&&e.sheet.cssRules&&e.sheet.cssRules.length&&(t.push(e.sheet),n.push(e.innerHTML));return{styles:t,stylesString:n.join(" ")}}(s);return{body:l,bodyString:o,styles:a.styles,stylesString:a.stylesString}}class G extends e.Plugin{static get pluginName(){return"PasteFromOffice"}static get licenseFeatureCode(){return"PFO"}static get isOfficialPlugin(){return!0}static get isPremiumPlugin(){return!0}static get requires(){return[t.ClipboardPipeline]}init(){const e=this.editor,t=e.plugins.get("ClipboardPipeline"),n=e.editing.view.document,i=[],s=this.editor.plugins.has("MultiLevelList"),r=this.editor.plugins.has("TableProperties"),o=!!this.editor.config.get("experimentalFlags.useExtendedTableBlockAlignment");i.push(new T(n,s,r,o)),i.push(new k(n)),i.push(new N(n)),t.on("inputTransformation",(t,s)=>{if(s._isTransformedWithPasteFromOffice)return;if(e.model.document.selection.getFirstPosition().parent.is("element","codeBlock"))return;const r=s.dataTransfer.getData("text/html"),o=i.find(e=>e.isActive(r));o&&(s._parsedData||(s._parsedData=H(r,n.stylesProcessor)),o.execute(s),s._isTransformedWithPasteFromOffice=!0)},{priority:"high"})}}})(),(window.CKEditor5=window.CKEditor5||{}).pasteFromOffice=i})();