Skip to content

Commit

Permalink
Merge pull request #16007 from ckeditor/ck/11210
Browse files Browse the repository at this point in the history
Fix (paste-from-office): Paste from Office should not break the list on the following blocks in the same list item or deduce a start attribute for a split list. Closes #11210. Closes #15964.
  • Loading branch information
niegowski committed Mar 29, 2024
2 parents b4f159c + e19afc0 commit a25e3f2
Show file tree
Hide file tree
Showing 113 changed files with 4,361 additions and 707 deletions.
360 changes: 208 additions & 152 deletions packages/ckeditor5-paste-from-office/src/filters/list.ts

Large diffs are not rendered by default.

Expand Up @@ -7,15 +7,16 @@
* @module paste-from-office/filters/removemsattributes
*/

import { UpcastWriter, type ViewDocumentFragment } from 'ckeditor5/src/engine.js';
import { UpcastWriter, type ViewDocumentFragment, type ViewElement } from 'ckeditor5/src/engine.js';

/**
* Cleanup MS attributes like styles, attributes and elements.
*
* @param documentFragment element `data.content` obtained from clipboard.
*/
export default function removeMSAttributes( documentFragment: ViewDocumentFragment ): void {
const elementsToUnwrap = [];
const elementsToUnwrap: Array<ViewElement> = [];

const writer = new UpcastWriter( documentFragment.document );

for ( const { item } of writer.createRangeIn( documentFragment ) ) {
Expand All @@ -35,7 +36,11 @@ export default function removeMSAttributes( documentFragment: ViewDocumentFragme
}
}

if ( item.is( 'element', 'w:sdt' ) ) {
if (
item.is( 'element', 'w:sdt' ) ||
item.is( 'element', 'w:sdtpr' ) && item.isEmpty ||
item.is( 'element', 'o:p' ) && item.isEmpty
) {
elementsToUnwrap.push( item );
}
}
Expand Down
58 changes: 58 additions & 0 deletions packages/ckeditor5-paste-from-office/src/filters/utils.ts
@@ -0,0 +1,58 @@
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

/**
* @module paste-from-office/filters/utils
*/

/**
* Normalizes CSS length value to 'px'.
*
* @internal
*/
export function convertCssLengthToPx( value: string ): string {
const numericValue = parseFloat( value );

if ( value.endsWith( 'pt' ) ) {
// 1pt = 1in / 72
return toPx( numericValue * 96 / 72 );
}
else if ( value.endsWith( 'pc' ) ) {
// 1pc = 12pt = 1in / 6.
return toPx( numericValue * 12 * 96 / 72 );
}
else if ( value.endsWith( 'in' ) ) {
// 1in = 2.54cm = 96px
return toPx( numericValue * 96 );
}
else if ( value.endsWith( 'cm' ) ) {
// 1cm = 96px / 2.54
return toPx( numericValue * 96 / 2.54 );
}
else if ( value.endsWith( 'mm' ) ) {
// 1mm = 1cm / 10
return toPx( numericValue / 10 * 96 / 2.54 );
}

return value;
}

/**
* Returns true for value with 'px' unit.
*
* @internal
*/
export function isPx( value?: string ): value is string {
return value !== undefined && value.endsWith( 'px' );
}

/**
* Returns a rounded 'px' value.
*
* @internal
*/
export function toPx( value: number ): string {
return value.toFixed( 2 ).replace( /\.?0+$/, '' ) + 'px';
}
@@ -1 +1 @@
<p style="margin: 0cm 0cm 8pt; line-height: 15.693333625793457px; font-size: 11pt; font-family: Calibri, sans-serif; caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Some text <b>with bold</b>.<o:p></o:p></span></p>
<p style="margin: 0cm 0cm 8pt; line-height: 15.693333625793457px; font-size: 11pt; font-family: Calibri, sans-serif; caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Some text <b>with bold</b>.</span></p>
@@ -1,3 +1,3 @@
<p>
<span lang=PL>Some text <b>with bold</b>.<o:p></o:p></span>
<span lang=PL>Some text <b>with bold</b>.</span>
</p>
@@ -1 +1 @@
<p style="margin: 0cm 0cm 8pt; line-height: 15.693333625793457px; font-size: 11pt; font-family: Calibri, sans-serif; caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;"><i><span>Italic</span></i><span>text.<o:p></o:p></span></p>
<p style="margin: 0cm 0cm 8pt; line-height: 15.693333625793457px; font-size: 11pt; font-family: Calibri, sans-serif; caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;"><i><span>Italic</span></i><span>text.</span></p>
@@ -1,4 +1,4 @@
<p>
<i><span lang=PL>Italic</span></i>
<span lang=PL> text.<o:p></o:p></span>
<span lang=PL> text.</span>
</p>
@@ -1,3 +1,3 @@
<p style="margin: 0cm 0cm 8pt; line-height: 15.693333625793457px; font-size: 11pt; font-family: Calibri, sans-serif; caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Line <b>bold</b>and <i>italics</i><o:p></o:p></span></p>
<p style="margin: 0cm 0cm 8pt; line-height: 15.693333625793457px; font-size: 11pt; font-family: Calibri, sans-serif; caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Line <b><u>foo</u></b><u>bar</u><o:p></o:p></span></p>
<p style="margin: 0cm 0cm 8pt; line-height: 15.693333625793457px; font-size: 11pt; font-family: Calibri, sans-serif; caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;"><s><span lang="EN-US">Third</span></s><span lang="EN-US">line <b>styling, <i>space on e</i>nd&nbsp;</b><o:p></o:p></span></p>
<p style="margin: 0cm 0cm 8pt; line-height: 15.693333625793457px; font-size: 11pt; font-family: Calibri, sans-serif; caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Line <b>bold</b>and <i>italics</i></span></p>
<p style="margin: 0cm 0cm 8pt; line-height: 15.693333625793457px; font-size: 11pt; font-family: Calibri, sans-serif; caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Line <b><u>foo</u></b><u>bar</u></span></p>
<p style="margin: 0cm 0cm 8pt; line-height: 15.693333625793457px; font-size: 11pt; font-family: Calibri, sans-serif; caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;"><s><span lang="EN-US">Third</span></s><span lang="EN-US">line <b>styling, <i>space on e</i>nd&nbsp;</b></span></p>
@@ -1,9 +1,9 @@
<p>
<span lang=PL>Line <b>bold</b> and <i>italics</i><o:p></o:p></span>
<span lang=PL>Line <b>bold</b> and <i>italics</i></span>
</p>

<p>
<span lang=PL>Line <b><u>foo</u></b><u> bar</u><o:p></o:p></span>
<span lang=PL>Line <b><u>foo</u></b><u> bar</u></span>
</p>

<p><s>Third</s> line <b>styling, <i>space on e</i>nd&nbsp;</b><o:p></o:p></p>
<p><s>Third</s> line <b>styling, <i>space on e</i>nd&nbsp;</b></p>
@@ -1 +1 @@
<p style="margin: 0cm 0cm 8pt; line-height: 15.693333625793457px; font-size: 11pt; font-family: Calibri, sans-serif; caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Text <b><u>containi<s>ng</s></u></b><s><u>multi</u>ple </s><i>styling</i>.<o:p></o:p></span></p>
<p style="margin: 0cm 0cm 8pt; line-height: 15.693333625793457px; font-size: 11pt; font-family: Calibri, sans-serif; caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Text <b><u>containi<s>ng</s></u></b><s><u>multi</u>ple </s><i>styling</i>.</span></p>
@@ -1,3 +1,3 @@
<p>
<span lang=PL>Text <b><u>containi<s>ng</s></u></b><s><u> multi</u>ple </s><i>styling</i>.<o:p></o:p></span>
<span lang=PL>Text <b><u>containi<s>ng</s></u></b><s><u> multi</u>ple </s><i>styling</i>.</span>
</p>
@@ -1 +1 @@
<p style="margin: 0cm 0cm 8pt; line-height: 15.693333625793457px; font-size: 11pt; font-family: Calibri, sans-serif; caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Text <s>incorrect</s><o:p></o:p></span></p>
<p style="margin: 0cm 0cm 8pt; line-height: 15.693333625793457px; font-size: 11pt; font-family: Calibri, sans-serif; caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Text <s>incorrect</s></span></p>
@@ -1 +1 @@
<p><span lang=PL>Text <s>incorrect</s><o:p></o:p></span></p>
<p><span lang=PL>Text <s>incorrect</s></span></p>
@@ -1 +1 @@
<p style="margin: 0cm 0cm 8pt; line-height: 15.693333625793457px; font-size: 11pt; font-family: Calibri, sans-serif; caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;"><u><span>Whole text underlined<o:p></o:p></span></u></p>
<p style="margin: 0cm 0cm 8pt; line-height: 15.693333625793457px; font-size: 11pt; font-family: Calibri, sans-serif; caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;"><u><span>Whole text underlined</span></u></p>
@@ -1,3 +1,3 @@
<p>
<u><span lang=PL>Whole text underlined<o:p></o:p></span></u>
<u><span lang=PL>Whole text underlined</span></u>
</p>
Expand Up @@ -39,5 +39,5 @@
<span lang="EN-US">
<img width="153" height="153" src="blob:http://localhost:9876/4fe9e7ab-3fec-4cce-98b8-7773476dbbf0" v:shapes="Obraz_x0020_12">
</span>
<span lang="EN-US"><o:p></o:p></span>
<span lang="EN-US"></span>
</p>

0 comments on commit a25e3f2

Please sign in to comment.