quill: [2.0.3] `getSemanticHTML` is broken

Steps for Reproduction

  1. Reproduction
  2. Check the console

Expected behavior: The text in the editor is a text with multiple spaces. getSemanticHTML returns <p>a&nbsp;text&nbsp;with&nbsp;multiple&nbsp;spaces</p>. Which is wrong - the semantics of the text has changed.

Actual behavior: See above.

Platforms: Any.

Version: 2.0.3

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 92
  • Comments: 38

Commits related to this issue

Most upvoted comments

IMHO this is critical bug

Introduced wtih Commit 07b68c9.

The end result of replacing all spaces with &nbsp; (Non Breaking Space) is that the words no-long wrap at word boundaries, and instead overflow.

Example

An alternative implementation of editor.js converHTML() that preserves whitespace might replace all but one consecutive space character with   be:

  if (blot instanceof TextBlot) {
    const escapedText = escapeText(blot.value().slice(index, index + length));
    return escapedText.replaceAll(/  +/g, (match) => "&nbsp;".repeat(match.length - 1) + " ")
  }

A work around (which replaces the last consecutive &nbsp; with a space) until a fix is implemented is:

quill.getSemanticHTML().replaceAll(/((?:&nbsp;)*)&nbsp;/g, '$1 ')

Completely destroys displaying edited text for me, since text now fails to wrap. Please revert.

We have the same issue please revert this change. Don’t want to save nbps to the database.

This is quite critical bug to our project. This changes how the values are saved to database. Luckily this didn’t hit production before noticing.

Jeez this thread is a joke. Just do this in your code: input.replace("&nbsp;", " ")

IMHO this is critical bug

Yes. Very much so. For example, ngx-quill (https://github.com/KillerCodeMonkey/ngx-quill) also suffers from this. If the editor is used as part of Angular reactive forms, the value of the form control has these added characters after user has finished typing something with whitespaces.

Spent the entire day only to discover this is a known and unaddressed bug.

The problem with all spaces becoming &nbsp; is that it completely breaks pervious word wrapping. It’s a non-breaking space, thus all instances that would otherwise line wrap suddenly won’t. For a WYSIWYG editor, what you see is currently not what you get.

I think this is bad. Sometimes I need to write &nbsp; literally in the editor.

Can we revert to the amazing getSemanticHTML() method of the 2.0.2 please?

We weren’t aware that a bug had been open here for several months. This has resulted in an unsightly number of non breakable characters in the database. It would be great if the bug could be fixed finally.

@frederikhors Well, “correct” is literally there in @KyeRussell 's question, I just reused that wording when answering it. And I beg to disagree, there’s an intended way to use Quill.

If that is the case, then why is getSemanticHTML() a supported function? Emphasis is mine:

Get the HTML representation of the editor contents. This method is useful for exporting the contents of the editor in a format that can be used in other applications.

An end user who presented with wrapping lines in an editor intends to have lines wrap. Swapping for &nbsp; destroys the intentions of the end user’s line wrapping. It is simply the incorrect interpretation of the end user’s intentions.

If the fix is using the Delta system, then getSemanticHTML() is an unsupported function as it doesn’t generate accurate HTML. This would also change the scope of Quill and explicitly only compatible with Delta.

If the fix is to revert 07b68c98b3bb61e6a6efb369a76f44a16b278517 and allow getSemanticHTML() to output the intended HTML, then we would have the expected results.

I created an alternative fix (PR #4598) that introduce options to getSemanticHTML, so it can be used as it is (with &nbsp;) for internal coping and pasting in quill. Setting preserveWhitespace option to true will preserve whitespace for all of us needing it.

Any update on this @luin?

Hello here,

We are also affected, and using ngx-quill (https://github.com/KillerCodeMonkey/ngx-quill) so cannot easily revert to 2.0.2 quill version.

Using temporary workaround while waiting for a fix :

On template

<quill-editor (onEditorCreated)=“onEditorCreate($event)”> On component

onEditorCreated(quill) { quill.legacyGetSemanticHTML = quill.getSemanticHTML; quill.getSemanticHTML = (a: number, b: number) => quill .legacyGetSemanticHTML(a, b) .replaceAll(/((?: )*) /g, '$1 '); } (Thanks @islandmyst for regex)

Not perfect, but no perfect but make the job.

Hey @dimitriaguera, according to this, you can use any version of Quill as long as it’s a v2 version. I’m using quill v2.0.2 with the latest ngx-quill (v27.0.2) without any issues.

this must be some psyop. trust no patch.

I opened a pull request with a fix. Hopefully it will get approved.

Is there something I’m missing?

Yes, it’s under the name of Slab now, which means soon or late it will become a commercial product (based on my experience throughout these years!).

Could this be a configuration option? So when someone needs it to be “normal” spaces it can?

We are using a very vanilla implementation and as such have reverted to 2.0.2 without incident.

Yikes… breaking backwards compatibility in a patch version… Guess the one take-away we got from this is to fix quill to whatever version works and leave it there until we hit critical vulnerabilities…

The fact that this hasn’t been addressed yet has me concerned that I’m not…using Quill correctly, or something. This respectfully seems like an incredibly critical bug. Is there something I’m missing?

Probably you are. Most comments complain that this bug has them save &nbsp;s to the DB. If that’s also your problem then yes, you’re using it wrong. You’re supposed to save the delta format, not something rendered. If you need to send html from the ui to your backend, Quill maybe isn’t the right editor for your requirements. If you need html for whatever reason, parse the delta. That’s the usp of Quill, a view agnostic storage format.

The fact that this hasn’t been addressed yet has me concerned that I’m not…using Quill correctly, or something. This respectfully seems like an incredibly critical bug. Is there something I’m missing?

commenting to follow, hoping to see an update here.

Since the issue is twofold (some need &nbsp; others regular spaces - as we do) maybe it’s worth adding optional parameter for getSemanticHTML function, that’s passed further to convertHTML?

E.g. as suggested here by @MaciejDabek

I created an alternative fix (PR #4598) that introduce options to getSemanticHTML, so it can be used as it is (with &nbsp;) for internal coping and pasting in quill. Setting preserveWhitespace option to true will preserve whitespace for all of us needing it.

Probably you are.

Or, @DirkLachowski, since there are so many of us here, our way of using Quill is not wrong either. 🥰

I think @luin can help us here

Thanks