Skip to content

Commit 1ce5246

Browse files
github-actions[bot]KB BotJekata
authored
Added new kb article editor-enter-new-line-gets-deleted-list-issue (#528)
* Added new kb article editor-enter-new-line-gets-deleted-list-issue * kb(editor): improve the example part * kb(editor): fix formatting --------- Co-authored-by: KB Bot <[email protected]> Co-authored-by: Rumen Zhekov <[email protected]>
1 parent 9d61e95 commit 1ce5246

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
title: The line below is deleted when clicking enter in a list pasted from Word
3+
description: Learn how to resolve the issue where ordered and unordered lists copied from Word are not properly displayed when pasted into RadEditor for ASP.NET AJAX.
4+
type: how-to
5+
page_title: The new line gets deleted when clicking enter in a list pasted from Word
6+
slug: editor-enter-new-line-gets-deleted-list-issue
7+
tags: radeditor, asp.net ajax, paste from Word, unordered list, issue
8+
res_type: kb
9+
---
10+
11+
## Environment
12+
13+
| Product | RadEditor for ASP.NET AJAX |
14+
| ------- | ------------------------- |
15+
| Version | all |
16+
17+
## Description
18+
19+
When pasting content from Microsoft Word into RadEditor, you may encounter an issue where lists (ol and ul) are not properly displayed. Specifically, when you click enter after pasting the list, a line is deleted. This issue occurs because the pasted content sometimes has HTML structure anomalies, particularly with the list elements.
20+
21+
For example, if you paste content into a [standard editable div/iframe element](https://dojo.telerik.com/UlaCozIk) (like the content area of RadEditor) and inspect it, you might notice an issue where a nested `<ul>` tag is not properly enclosed within `<li>` tags. According to XHTML and HTML5 standards, a `<ul>` must be nested inside a `<li>` when it is a child of another `<ul>`. However, the pasted content does not always adhere to this requirement, which leads to the malformed structure shown below:
22+
23+
````HTML
24+
<ul>
25+
<li>Parent List Item</li>
26+
<ul> <!-- This <ul> should be wrapped within <li> tags -->
27+
<li>Newsted List Item</li>
28+
</ul>
29+
</ul>
30+
````
31+
32+
This incorrect nesting can disrupt the content validation functionality of RadEditor, potentially leading to issues like the deletion of lines or improper list formatting.
33+
34+
## Solution
35+
36+
To resolve this issue, you can modify the OnClientPasteHtml event in the RadEditor configuration to automatically correct the structure of the pasted HTML. Follow the steps below:
37+
38+
1. Add the following JavaScript code to your RadEditor configuration:
39+
40+
```javascript
41+
<script>
42+
function onClientPasteHtml(sender, args) {
43+
var pastedHtml = args.get_value();
44+
var wrapper = document.createElement('div');
45+
wrapper.innerHTML = pastedHtml;
46+
47+
// Query all <ul> elements
48+
var allULs = wrapper.getElementsByTagName('ul');
49+
Array.from(allULs).forEach(function (ul) {
50+
// Check if the <ul> is a direct sibling of an <li>
51+
var previousSibling = ul.previousElementSibling;
52+
if (previousSibling && previousSibling.tagName.toLowerCase() === 'li') {
53+
// Move the <ul> to be a child of the previous <li>
54+
previousSibling.appendChild(ul);
55+
}
56+
});
57+
58+
// Update the HTML to be pasted
59+
args.set_value(wrapper.innerHTML);
60+
}
61+
</script>
62+
```
63+
64+
2. Use the `OnClientPasteHtml` event in the RadEditor control to call the `onClientPasteHtml` function:
65+
66+
```html
67+
<telerik:RadEditor runat="server" ID="RadEditor1" Height="600px" OnClientPasteHtml="onClientPasteHtml">
68+
</telerik:RadEditor>
69+
```
70+
71+
By implementing this code, the function will check for any `<ul>` elements that are direct siblings of `<li>` elements and adjust the DOM to wrap these `<ul>` elements within the preceding `<li>`. This ensures that the pasted HTML has the correct structure and resolves the issue with unordered lists pasted from Word.
72+
73+

0 commit comments

Comments
 (0)