Ckeditor5: Error thrown when trying to paste entire content copied from specific Word document

Created on 25 Jan 2019  路  7Comments  路  Source: ckeditor/ckeditor5

Extracted from https://github.com/ckeditor/ckeditor5/issues/1474 (the sample file was posted in the mentioned issue).

  1. Go to any CKEditor sample with PFO plugin enabled (e.g. https://ckeditor5.github.io/).
  2. Copy entire content of the SUITE MEMORIES.docx file.
  3. Paste into editor.

The Uncaught TypeError: Cannot read property 'data' of undefined error is thrown. Looks like an issue with normalizeSpacerunSpans() function.
From what I have checked it seems the problematic content is on page 2 in the document, rest is pasted without any errors.

paste-from-office bug

Most helpful comment

I have the same issue as well. I reproduced by (using macOS 10.13.6 using Firefox Developer edition 66.0b2):

  1. Downloading the referenced word document, SUITE MEMORIES.docx.
  2. Going to the ckeditor classic demo page on the ckeditor site.
  3. Deleting all of the content in the editor.
  4. Selecting all content in the SUITE.MEMORIES.docx document and copying it.
  5. Pasting into the now-empty editor.

Expected: The contents of the word document (even just the text without formatting) is displayed.
Actual: Nothing is pasted.

In the firefox developer console, there's an error:

TypeError: t.childNodes[0] is undefined [Learn More] space.js:37

When expanded this becomes:

TypeError: t.childNodes[0] is undefined [Learn More] space.js:37

    ap space.js:37 
    forEach (index):262 
    ap space.js:34 
    ap space.js:33 
    _normalizeWordInput pastefromoffice.js:64 
    init pastefromoffice.js:45 
    fire emittermixin.js:196 
    init clipboard.js:78 
    fire emittermixin.js:196 
    n clipboardobserver.js:48 
    fire emittermixin.js:196 
    fire domeventobserver.js:96 
    onDomEvent clipboardobserver.js:64 
    observe domeventobserver.js:79 
    fire emittermixin.js:196 
    n emittermixin.js:235 

On Chrome Version 71.0.3578.98 (Official Build) (64-bit) the error is the same:

space.js:37 Uncaught TypeError: Cannot read property 'data' of undefined
    at t.querySelectorAll.forEach.t (space.js:37)
    at NodeList.forEach (<anonymous>)
    at space.js:34
    at ap (parse.js:40)
    at hp._normalizeWordInput (pastefromoffice.js:64)
    at md.listenTo (pastefromoffice.js:45)
    at md.fire (emittermixin.js:196)
    at to.listenTo (clipboard.js:78)
    at to.fire (emittermixin.js:196)
    at to.n (clipboardobserver.js:48)

All 7 comments

It seems the issue is caused by the double space on the second page:

image

Strangely, if I remove it and retype, it starts to work 馃

I have the same issue as well. I reproduced by (using macOS 10.13.6 using Firefox Developer edition 66.0b2):

  1. Downloading the referenced word document, SUITE MEMORIES.docx.
  2. Going to the ckeditor classic demo page on the ckeditor site.
  3. Deleting all of the content in the editor.
  4. Selecting all content in the SUITE.MEMORIES.docx document and copying it.
  5. Pasting into the now-empty editor.

Expected: The contents of the word document (even just the text without formatting) is displayed.
Actual: Nothing is pasted.

In the firefox developer console, there's an error:

TypeError: t.childNodes[0] is undefined [Learn More] space.js:37

When expanded this becomes:

TypeError: t.childNodes[0] is undefined [Learn More] space.js:37

    ap space.js:37 
    forEach (index):262 
    ap space.js:34 
    ap space.js:33 
    _normalizeWordInput pastefromoffice.js:64 
    init pastefromoffice.js:45 
    fire emittermixin.js:196 
    init clipboard.js:78 
    fire emittermixin.js:196 
    n clipboardobserver.js:48 
    fire emittermixin.js:196 
    fire domeventobserver.js:96 
    onDomEvent clipboardobserver.js:64 
    observe domeventobserver.js:79 
    fire emittermixin.js:196 
    n emittermixin.js:235 

On Chrome Version 71.0.3578.98 (Official Build) (64-bit) the error is the same:

space.js:37 Uncaught TypeError: Cannot read property 'data' of undefined
    at t.querySelectorAll.forEach.t (space.js:37)
    at NodeList.forEach (<anonymous>)
    at space.js:34
    at ap (parse.js:40)
    at hp._normalizeWordInput (pastefromoffice.js:64)
    at md.listenTo (pastefromoffice.js:45)
    at md.fire (emittermixin.js:196)
    at to.listenTo (clipboard.js:78)
    at to.fire (emittermixin.js:196)
    at to.n (clipboardobserver.js:48)

@f1ames - I struggled for a long time to find why some word documents could be pasted while others couldn't. Great catch!

It seems that the bug is somewhere here: https://github.com/ckeditor/ckeditor5-paste-from-office/blob/c8e0a0d9349c21984e92fb27f7c0cee7ff01df44/src/filters/space.js#L34-L40

Perhaps el.childNodes is empty in this case. Would you like to try to make a PR?

I think the bug might actually be occurring here:
https://github.com/ckeditor/ckeditor5-paste-from-office/blob/c8e0a0d9349c21984e92fb27f7c0cee7ff01df44/src/filters/space.js#L23

Taking a look at the html copied to the clipboard from Word for Mac Version 16.21 (190115), part of the offending output looks like:

<p class=MsoNormal style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'><span style='font-size:10.0pt;font-family:"Arial",sans-serif;
mso-fareast-font-family:"Times New Roman"'><span style='mso-spacerun:yes'>聽聽
</span>This is the actual text content<o:p></o:p></span></p>

鈽濓笍 Note the extra spaces after <span style='mso-spacerun:yes'> but with a newline immediately before the closing </span> tag. As a result, the spaces in the spacerun get stripped, leaving el.childNodes empty by the time it gets to normalizeSpacing() in the above comment. Would there be an elegant way of modifying this regex to make an exception for spaces found inside of a spacerun span? 馃

Good catch @kobsy! That might be the issue indeed. The assumption was the <span style='mso-spacerun:yes'> contains only spaces, but it seems it can also contain line breaks (is there a line break in Word document on that position? 馃).

Adjusting the regex to omit "spacerun spans" could be a proper solution. @kobsy if you feel like contributing feel free to add a PR (see our contributing guide).

I'm having the same issue. It could have something to do with linebreaks indeed.
I debugged 2 scenarios.

Breaks on:

<span style='mso-spacerun:yes'>脗聽
</span>

Works on:

<span
style='mso-spacerun:yes'>脗聽 </span>
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Reinmar picture Reinmar  路  3Comments

jodator picture jodator  路  3Comments

pjasiun picture pjasiun  路  3Comments

oleq picture oleq  路  3Comments

hamenon picture hamenon  路  3Comments