froala / wysiwyg-editor

The next generation Javascript WYSIWYG HTML Editor.
https://www.froala.com/wysiwyg-editor
Other
5.28k stars 673 forks source link

wordPaste plugin unconditionally removes empty table cells #4002

Closed earshinov closed 2 years ago

earshinov commented 4 years ago
Expected behavior.

I have the following HTML originally exported from Word:

image

Source (rename to .html): empty-cells-test.html.txt

When I copy the HTML from the browser and paste into Froala, I expect to see empty cells.

Actual behavior.

https://froala.com/wysiwyg-editor/examples/destroy-init/

image

Empty cells are gone.

In real-world cases it ruins table structure because tables normally contain >1 row.

Steps to reproduce the problem.
  1. Open the given empty-cell-test.html in a browser
  2. Ctrl-A, Ctrl-C
  3. Open any page with a Froala editor, for example https://froala.com/wysiwyg-editor/examples/destroy-init/
  4. Ctrl-V

This problem happens regardless of whether you choose Keep or Clean in the Word Paste Detected dialog.

Editor version.

3.1.1

OS.

Windows 10 x64, but it doesn't really matter.

Browser.

Google Chrome | 84.0.4147.105 (Official Build) (64-bit), but it doesn't really matter.

Suggested fix.

Here is the code that unconditionally removes empty cells (in js/plugins.pkgd.min.js, PLUGINS.wordPaste):

u(r, function (e) {
  if (e.nodeType === Node.ELEMENT_NODE) {
    var t = e.tagName;
    if (!e.innerHTML && -1 === ["BR", "IMG", "INPUT"].indexOf(t)) {
      for (var a = e.parentNode; a && (C(e), !(e = a).innerHTML); ) a = e.parentNode;   <<< While node has empty innerHTML, remove it and all its parents recursively
      return !1;
    }
    ...
  }
  return !0;
})

Call stack (abbreviated):

Error
    at i
    at t [as wordPaste.clean]

I guess you need to take htmlAllowedEmptyTags into account here.

earshinov commented 2 years ago

Not reproducible in 4.0.9