Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 #16007

Merged
merged 19 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
360 changes: 208 additions & 152 deletions packages/ckeditor5-paste-from-office/src/filters/list.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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';
}
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
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>
Loading