@ckeditor/ckeditor5-clipboard 40.0.0 → 40.2.0

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.
@@ -1,37 +1,39 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
- */
5
- /**
6
- * @module clipboard/utils/plaintexttohtml
7
- */
8
- /**
9
- * Converts plain text to its HTML-ized version.
10
- *
11
- * @param text The plain text to convert.
12
- * @returns HTML generated from the plain text.
13
- */
14
- export default function plainTextToHtml(text) {
15
- text = text
16
- // Encode <>.
17
- .replace(/</g, '&lt;')
18
- .replace(/>/g, '&gt;')
19
- // Creates a paragraph for each double line break.
20
- .replace(/\r?\n\r?\n/g, '</p><p>')
21
- // Creates a line break for each single line break.
22
- .replace(/\r?\n/g, '<br>')
23
- // Replace tabs with four spaces.
24
- .replace(/\t/g, '&nbsp;&nbsp;&nbsp;&nbsp;')
25
- // Preserve trailing spaces (only the first and last one – the rest is handled below).
26
- .replace(/^\s/, '&nbsp;')
27
- .replace(/\s$/, '&nbsp;')
28
- // Preserve other subsequent spaces now.
29
- .replace(/\s\s/g, ' &nbsp;');
30
- if (text.includes('</p><p>') || text.includes('<br>')) {
31
- // If we created paragraphs above, add the trailing ones.
32
- text = `<p>${text}</p>`;
33
- }
34
- // TODO:
35
- // * What about '\nfoo' vs ' foo'?
36
- return text;
37
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @module clipboard/utils/plaintexttohtml
7
+ */
8
+ /**
9
+ * Converts plain text to its HTML-ized version.
10
+ *
11
+ * @param text The plain text to convert.
12
+ * @returns HTML generated from the plain text.
13
+ */
14
+ export default function plainTextToHtml(text) {
15
+ text = text
16
+ // Encode &.
17
+ .replace(/&/g, '&amp;')
18
+ // Encode <>.
19
+ .replace(/</g, '&lt;')
20
+ .replace(/>/g, '&gt;')
21
+ // Creates a paragraph for each double line break.
22
+ .replace(/\r?\n\r?\n/g, '</p><p>')
23
+ // Creates a line break for each single line break.
24
+ .replace(/\r?\n/g, '<br>')
25
+ // Replace tabs with four spaces.
26
+ .replace(/\t/g, '&nbsp;&nbsp;&nbsp;&nbsp;')
27
+ // Preserve trailing spaces (only the first and last one – the rest is handled below).
28
+ .replace(/^\s/, '&nbsp;')
29
+ .replace(/\s$/, '&nbsp;')
30
+ // Preserve other subsequent spaces now.
31
+ .replace(/\s\s/g, ' &nbsp;');
32
+ if (text.includes('</p><p>') || text.includes('<br>')) {
33
+ // If we created paragraphs above, add the trailing ones.
34
+ text = `<p>${text}</p>`;
35
+ }
36
+ // TODO:
37
+ // * What about '\nfoo' vs ' foo'?
38
+ return text;
39
+ }
@@ -1,15 +1,15 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
- */
5
- /**
6
- * @module clipboard/utils/viewtoplaintext
7
- */
8
- import type { ViewDocumentFragment, ViewItem } from '@ckeditor/ckeditor5-engine';
9
- /**
10
- * Converts {@link module:engine/view/item~Item view item} and all of its children to plain text.
11
- *
12
- * @param viewItem View item to convert.
13
- * @returns Plain text representation of `viewItem`.
14
- */
15
- export default function viewToPlainText(viewItem: ViewItem | ViewDocumentFragment): string;
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @module clipboard/utils/viewtoplaintext
7
+ */
8
+ import type { ViewDocumentFragment, ViewItem } from '@ckeditor/ckeditor5-engine';
9
+ /**
10
+ * Converts {@link module:engine/view/item~Item view item} and all of its children to plain text.
11
+ *
12
+ * @param viewItem View item to convert.
13
+ * @returns Plain text representation of `viewItem`.
14
+ */
15
+ export default function viewToPlainText(viewItem: ViewItem | ViewDocumentFragment): string;
@@ -1,50 +1,67 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
- */
5
- // Elements which should not have empty-line padding.
6
- // Most `view.ContainerElement` want to be separate by new-line, but some are creating one structure
7
- // together (like `<li>`) so it is better to separate them by only one "\n".
8
- const smallPaddingElements = ['figcaption', 'li'];
9
- /**
10
- * Converts {@link module:engine/view/item~Item view item} and all of its children to plain text.
11
- *
12
- * @param viewItem View item to convert.
13
- * @returns Plain text representation of `viewItem`.
14
- */
15
- export default function viewToPlainText(viewItem) {
16
- let text = '';
17
- if (viewItem.is('$text') || viewItem.is('$textProxy')) {
18
- // If item is `Text` or `TextProxy` simple take its text data.
19
- text = viewItem.data;
20
- }
21
- else if (viewItem.is('element', 'img') && viewItem.hasAttribute('alt')) {
22
- // Special case for images - use alt attribute if it is provided.
23
- text = viewItem.getAttribute('alt');
24
- }
25
- else if (viewItem.is('element', 'br')) {
26
- // A soft break should be converted into a single line break (#8045).
27
- text = '\n';
28
- }
29
- else {
30
- // Other elements are document fragments, attribute elements or container elements.
31
- // They don't have their own text value, so convert their children.
32
- let prev = null;
33
- for (const child of viewItem.getChildren()) {
34
- const childText = viewToPlainText(child);
35
- // Separate container element children with one or more new-line characters.
36
- if (prev && (prev.is('containerElement') || child.is('containerElement'))) {
37
- if (smallPaddingElements.includes(prev.name) ||
38
- smallPaddingElements.includes(child.name)) {
39
- text += '\n';
40
- }
41
- else {
42
- text += '\n\n';
43
- }
44
- }
45
- text += childText;
46
- prev = child;
47
- }
48
- }
49
- return text;
50
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ // Elements which should not have empty-line padding.
6
+ // Most `view.ContainerElement` want to be separate by new-line, but some are creating one structure
7
+ // together (like `<li>`) so it is better to separate them by only one "\n".
8
+ const smallPaddingElements = ['figcaption', 'li'];
9
+ const listElements = ['ol', 'ul'];
10
+ /**
11
+ * Converts {@link module:engine/view/item~Item view item} and all of its children to plain text.
12
+ *
13
+ * @param viewItem View item to convert.
14
+ * @returns Plain text representation of `viewItem`.
15
+ */
16
+ export default function viewToPlainText(viewItem) {
17
+ if (viewItem.is('$text') || viewItem.is('$textProxy')) {
18
+ return viewItem.data;
19
+ }
20
+ if (viewItem.is('element', 'img') && viewItem.hasAttribute('alt')) {
21
+ return viewItem.getAttribute('alt');
22
+ }
23
+ if (viewItem.is('element', 'br')) {
24
+ return '\n'; // Convert soft breaks to single line break (#8045).
25
+ }
26
+ /**
27
+ * Item is a document fragment, attribute element or container element. It doesn't
28
+ * have it's own text value, so we need to convert its children elements.
29
+ */
30
+ let text = '';
31
+ let prev = null;
32
+ for (const child of viewItem.getChildren()) {
33
+ text += newLinePadding(child, prev) + viewToPlainText(child);
34
+ prev = child;
35
+ }
36
+ return text;
37
+ }
38
+ /**
39
+ * Returns new line padding to prefix the given elements with.
40
+ */
41
+ function newLinePadding(element, previous) {
42
+ if (!previous) {
43
+ // Don't add padding to first elements in a level.
44
+ return '';
45
+ }
46
+ if (element.is('element', 'li') && !element.isEmpty && element.getChild(0).is('containerElement')) {
47
+ // Separate document list items with empty lines.
48
+ return '\n\n';
49
+ }
50
+ if (listElements.includes(element.name) && listElements.includes(previous.name)) {
51
+ /**
52
+ * Because `<ul>` and `<ol>` are AttributeElements, two consecutive lists will not have any padding between
53
+ * them (see the `if` statement below). To fix this, we need to make an exception for this case.
54
+ */
55
+ return '\n\n';
56
+ }
57
+ if (!element.is('containerElement') && !previous.is('containerElement')) {
58
+ // Don't add padding between non-container elements.
59
+ return '';
60
+ }
61
+ if (smallPaddingElements.includes(element.name) || smallPaddingElements.includes(previous.name)) {
62
+ // Add small padding between selected container elements.
63
+ return '\n';
64
+ }
65
+ // Add empty lines between container elements.
66
+ return '\n\n';
67
+ }