@ckeditor/ckeditor5-paste-from-office 45.1.0 → 45.2.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/paste-from-office.js +1 -1
- package/dist/index.js +43 -1
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
- package/src/filters/image.js +0 -1
- package/src/filters/parse.js +0 -1
- package/src/filters/table.d.ts +13 -0
- package/src/filters/table.js +44 -0
- package/src/filters/utils.js +1 -1
- package/src/normalizers/mswordnormalizer.js +2 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
3
|
* For licensing, see LICENSE.md.
|
|
4
|
-
*/(()=>{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,{MSWordNormalizer:()=>x,PasteFromOffice:()=>M,parseHtml:()=>P});var e=n(782),t=n(331);var s=n(783);function r(e){return void 0!==e&&e.endsWith("px")}function o(e){return e.toFixed(2).replace(/\.?0+$/,"")+"px"}function l(e,t,n){if(!e.childCount)return;const i=new s.UpcastWriter(e.document),l=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=g(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&&s.has(t)){const n=f(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(!l.length)return;const a={},u=[];for(const e of l)if(void 0!==e.indent){c(e)||(u.length=0);const s=`${e.id}:${e.indent}`,l=Math.min(e.indent-1,u.length);if(l<u.length&&u[l].id!==e.id&&(u.length=l),l<u.length-1)u.length=l+1;else{const c=m(e,t);if(l>u.length-1||u[l].listElement.name!=c.type){0==l&&"ol"==c.type&&void 0!==e.id&&a[s]&&(c.startIndex=a[s]);const t=d(c,i,n);if(r(e.marginLeft)&&(0==l||r(u[l-1].marginLeft))){let n=e.marginLeft;l>0&&(n=o(parseFloat(n)-parseFloat(u[l-1].marginLeft))),i.setStyle("padding-left",n,t)}if(0==u.length){const n=e.element.parent,s=n.getChildIndex(e.element)+1;i.insertChild(s,t,n)}else{const e=u[l-1].listItemElements;i.appendChild(t,e[e.length-1])}u[l]={...e,listElement:t,listItemElements:[]},0==l&&void 0!==e.id&&(a[s]=c.startIndex||1)}}const f="li"==e.element.name?e.element:i.createElement("li");i.appendChild(f,u[l].listElement),u[l].listItemElements.push(f),0==l&&void 0!==e.id&&a[s]++,e.element!=f&&i.appendChild(e.element,f),p(e.element,i),i.removeStyle("text-indent",e.element),i.removeStyle("margin-left",e.element)}else{const t=u.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 u.length=0}}function c(e){const t=e.element.previousSibling;return a(t||e.element.parent)}function a(e){return e.is("element","ol")||e.is("element","ul")}function m(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),c=o.exec(t),a=l&&!c,m=n.exec(t);let d="decimal",f="ol",p=null;if(m&&m[1]){const t=i.exec(m[1]);if(t&&t[1]&&(d=t[1].trim(),f="bullet"!==d&&"image"!==d?"ol":"ul"),"bullet"===d){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&&(d=t)}else{const e=s.exec(m[1]);e&&e[1]&&(p=parseInt(e[1]))}a&&(f="ol")}return{type:f,startIndex:p,style:u(d),isLegalStyleList:a}}function u(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 d(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 f(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 p(e,t){const n=new s.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 g(e){const t=e.getStyle("margin-left");return void 0===t||t.endsWith("px")?t:function(e){const t=parseFloat(e);return e.endsWith("pt")?o(96*t/72):e.endsWith("pc")?o(12*t*96/72):e.endsWith("in")?o(96*t):e.endsWith("cm")?o(96*t/2.54):e.endsWith("mm")?o(t/10*96/2.54):e}(t)}function h(e,t){if(!e.childCount)return;const n=new s.UpcastWriter(e.document),i=function(e,t){const n=t.createRangeIn(e),i=new s.Matcher({name:/v:(.+)/}),r=[];for(const e of n){if("elementStart"!=e.type)continue;const t=e.item,n=t.previousSibling,s=n&&n.is("element")?n.name:null,o=["Chart"],l=i.match(t),c=t.getAttribute("o:gfxdata"),a="v:shapetype"===s,m=c&&o.some((e=>t.getAttribute("id").includes(e)));l&&c&&!a&&!m&&r.push(e.item.getAttribute("id"))}return r}(e,n);!function(e,t,n){const i=n.createRangeIn(t),r=new s.Matcher({name:"img"}),o=[];for(const t of i)if(t.item.is("element")&&r.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 s.Matcher({name:/v:(.+)/}),r=[];for(const e of n)"elementStart"==e.type&&i.match(e.item)&&r.push(e.item);for(const e of r)t.remove(e)}(e,n);const r=function(e,t){const n=t.createRangeIn(e),i=new s.Matcher({name:"img"}),r=[];let o=0;for(const e of n)e.item.is("element")&&i.match(e.item)&&(e.item.getAttribute("src").startsWith("file://")&&r.push({element:e.item,imageIndex:o}),o++);return r}(e,n);r.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,${y(o.hex)}`;n.setAttribute("src",e,s)}}}(r,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 y(e){return btoa(e.match(/\w{2}/g).map((e=>String.fromCharCode(parseInt(e,16)))).join(""))}const b=/<meta\s*name="?generator"?\s*content="?microsoft\s*word\s*\d+"?\/?>/i,v=/xmlns:o="urn:schemas-microsoft-com/i;class x{document;hasMultiLevelListPlugin;constructor(e,t=!1){this.document=e,this.hasMultiLevelListPlugin=t}isActive(e){return b.test(e)||v.test(e)}execute(e){const t=new s.UpcastWriter(this.document),{body:n,stylesString:i}=e._parsedData;!function(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)}}(n,t),l(n,i,this.hasMultiLevelListPlugin),h(n,e.dataTransfer.getData("text/rtf")),function(e){const t=[],n=new s.UpcastWriter(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)}}(n),e.content=n}}function w(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 C(e,t){return!!e&&e.is("element")&&t.includes(e.name)}const S=/id=("|')docs-internal-guid-[-0-9a-f]+("|')/i;class A{document;constructor(e){this.document=e}isActive(e){return S.test(e)}execute(e){const t=new s.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 i=e.getChildIndex(n);t.remove(n),t.insertChild(i,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 s.ViewDocument(t.document.stylesProcessor),i=new s.DomConverter(n,{renderingMode:"data"}),r=i.blockElements,o=i.inlineObjectElements,l=[];for(const n of t.createRangeIn(e)){const e=n.item;if(e.is("element","br")){const n=w(e,"forward",t,{blockElements:r,inlineObjectElements:o}),i=w(e,"backward",t,{blockElements:r,inlineObjectElements:o}),s=C(n,r);(C(i,r)||s)&&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 I=/<google-sheets-html-origin/i;class E{document;constructor(e){this.document=e}isActive(e){return I.test(e)}execute(e){const t=new s.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 i=e.getChildIndex(n);t.remove(n),t.insertChild(i,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 L(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,i=function(e){return L(L(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>",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,""))),r=n.parseFromString(i,"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)}))}(r);const o=r.body.innerHTML,l=function(e,t){const n=new s.ViewDocument(t),i=new s.DomConverter(n,{renderingMode:"data"}),r=e.createDocumentFragment(),o=e.body.childNodes;for(;o.length>0;)r.appendChild(o[0]);return i.domToView(r,{skipComments:!0})}(r,t),c=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(" ")}}(r);return{body:l,bodyString:o,styles:c.styles,stylesString:c.stylesString}}class M 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,i=[],s=this.editor.plugins.has("MultiLevelList");i.push(new x(n,s)),i.push(new A(n)),i.push(new E(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=P(r,n.stylesProcessor)),o.execute(s),s._isTransformedWithPasteFromOffice=!0)}),{priority:"high"})}}})(),(window.CKEditor5=window.CKEditor5||{}).pasteFromOffice=i})();
|
|
4
|
+
*/(()=>{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})();
|
package/dist/index.js
CHANGED
|
@@ -69,7 +69,7 @@ import { UpcastWriter, Matcher, ViewDocument, DomConverter } from '@ckeditor/cke
|
|
|
69
69
|
*
|
|
70
70
|
* @internal
|
|
71
71
|
*/ function toPx(value) {
|
|
72
|
-
return
|
|
72
|
+
return Math.round(value) + 'px';
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
/**
|
|
@@ -747,6 +747,47 @@ function isList(element) {
|
|
|
747
747
|
}
|
|
748
748
|
}
|
|
749
749
|
|
|
750
|
+
/**
|
|
751
|
+
* Applies border none for table and cells without a border specified.
|
|
752
|
+
* Normalizes style length units to px.
|
|
753
|
+
*/ function transformTables(documentFragment, writer) {
|
|
754
|
+
for (const item of writer.createRangeIn(documentFragment).getItems()){
|
|
755
|
+
if (!item.is('element', 'table') && !item.is('element', 'td') && !item.is('element', 'th')) {
|
|
756
|
+
continue;
|
|
757
|
+
}
|
|
758
|
+
const sides = [
|
|
759
|
+
'left',
|
|
760
|
+
'top',
|
|
761
|
+
'right',
|
|
762
|
+
'bottom'
|
|
763
|
+
];
|
|
764
|
+
// As this is a pasted table, we do not want default table styles to apply here
|
|
765
|
+
// so we set border node for sides that does not have any border style.
|
|
766
|
+
// It is enough to verify border style as border color and border width properties have default values in DOM.
|
|
767
|
+
if (sides.every((side)=>!item.hasStyle(`border-${side}-style`))) {
|
|
768
|
+
writer.setStyle('border-style', 'none', item);
|
|
769
|
+
} else {
|
|
770
|
+
for (const side of sides){
|
|
771
|
+
if (!item.hasStyle(`border-${side}-style`)) {
|
|
772
|
+
writer.setStyle(`border-${side}-style`, 'none', item);
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
// Translate style length units to px.
|
|
777
|
+
const props = [
|
|
778
|
+
'width',
|
|
779
|
+
'height',
|
|
780
|
+
...sides.map((side)=>`border-${side}-width`),
|
|
781
|
+
...sides.map((side)=>`padding-${side}`)
|
|
782
|
+
];
|
|
783
|
+
for (const prop of props){
|
|
784
|
+
if (item.hasStyle(prop)) {
|
|
785
|
+
writer.setStyle(prop, convertCssLengthToPx(item.getStyle(prop)), item);
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
|
|
750
791
|
const msWordMatch1 = /<meta\s*name="?generator"?\s*content="?microsoft\s*word\s*\d+"?\/?>/i;
|
|
751
792
|
const msWordMatch2 = /xmlns:o="urn:schemas-microsoft-com/i;
|
|
752
793
|
/**
|
|
@@ -775,6 +816,7 @@ const msWordMatch2 = /xmlns:o="urn:schemas-microsoft-com/i;
|
|
|
775
816
|
transformBookmarks(documentFragment, writer);
|
|
776
817
|
transformListItemLikeElementsIntoLists(documentFragment, stylesString, this.hasMultiLevelListPlugin);
|
|
777
818
|
replaceImagesSourceWithBase64(documentFragment, data.dataTransfer.getData('text/rtf'));
|
|
819
|
+
transformTables(documentFragment, writer);
|
|
778
820
|
removeMSAttributes(documentFragment);
|
|
779
821
|
data.content = documentFragment;
|
|
780
822
|
}
|