Intentional is converted to a regular space on upcast when adjacent to a space, at the end of a text node, or after <br>
📝 Provide detailed reproduction steps (if any)
- Create an editor with the default Paragraph and Essentials plugins.
- Run:
editor.setData( '<p>a b</p>' );
console.log( JSON.stringify( getModelData( editor.model, { withoutSelection: true } ) ) );
editor.setData( '<p>Lorem <br> <br>ipsum</p>' );
console.log( JSON.stringify( getModelData( editor.model, { withoutSelection: true } ) ) );
- Inspect the model text, for example with
[ ...text ].map( c => c.charCodeAt( 0 ).toString( 16 ) ).
Other inputs that are affected:
┌───────────────────────────┬──────────────────────────────┐
│ Input HTML │ Text in model │
├───────────────────────────┼──────────────────────────────┤
│ <p>1 2</p> │ 1 + two \u0020 + 2 │
├───────────────────────────┼──────────────────────────────┤
│ <p>Lorem </p> │ Lorem + \u0020 │
├───────────────────────────┼──────────────────────────────┤
│ <p>x<br> y</p> │ \u0020\u0020y after the <br> │
└───────────────────────────┴──────────────────────────────┘
✔️ Expected result
An in the source HTML that the author wrote deliberately ends up as \u00A0 in tsee it and treat it differently from a regular space.
❌ Actual result
The model contains no \u00A0 for these inputs. DomConverter#_processDomInlineNodes (ckmconverter.ts) assumes that any in these positions was produced by _processDataFromViewText for rendering and converts it back:
- a \u00A0 pair (space + nbsp) becomes two spaces,
- a trailing \u00A0 becomes a space when it ends the text node, follows a nbsp or spacline element or a text node starting with a space,
- a leading \u00A0 becomes a space when it follows
or an inline element, or starts the block.
The same happens for editor.data.processor.toView() and for pasted HTML. On the way out the space is rendered as nbsp again, but the number and position of nbsp characters can change. When source editing is toggled repeatedly, nbsp characters can be added or removed (see also #14680).
❓ Impact
A plugin that visualizes non-breaking spaces (TYPO3's Whitespace plugin marks every \upan.ck-nbsp) cannot highlight these characters. They never reach the model, so editors see only some of the nbsp in their content. Source: TYPO3 #101726 (https://forge.typo3.org/issues/101726).
❓ Possible solution
Either of these would help:
- A DomConverter / HtmlDataProcessor option, or an event, to keep \u00A0 as-is on upcast (for example
preserveNbsp: true, or a hook to override the normalization). - Documenting the normalization rules and how integrators can opt out.
Related issues: #10745, #11042 (nbsp replaced by a regular space, closed as not planneund trip multiplying nbsp), #16098 (whitespace not preserved in toView).
📃 Other details
- Browser: any (the behaviour is in the engine's DOM→view conversion)
- CKEditor version: 48.5.2
- Installed plugins: Essentials, Paragraph
Hi @bmack, thanks for the detailed report!
In the editor data, is used to preserve runs of consecutive spaces (and spaces at boundaries that would otherwise collapse), so on upcast in those positions is turned back into a regular space. The editor doesn't officially support user-inserted non-breaking spaces. It only partly works, in positions not used for space runs. Proper support would need a dedicated feature that marks such spaces in the data (e.g. <span class="ck-nbsp"> </span>) and handles them with its own upcast and downcast converters. I'll relabel this as a feature request.
The editor doesn't officially support user-inserted non-breaking spaces
Not correct. If you insert the space intentionally between two words (with alt+space) it is correctly handled:
I created the content by typing "foo" + alt+space + "bar". The is returned and can also survive the roundtrip via setData -> getData.
What might get re-shuffled is a combination of subsequent spaces and non-breaking spaces. Our data pipeline will return the minimal number of nbsps in the output that still has the same meaning. In the extreme scenario of 10 subsequent nbsps inserted into the editing view by the user, the editor will return exactly tjis:
One important remark: getModelData() is a test tool which has some limitations. If I remember correctly, one of them being processing non-breaking spaces. IIRC, it's not reliable in such tests.
@niegowski, am I right?
One important remark:
getModelData()is a test tool which has some limitations. If I remember correctly, one of them being processing non-breaking spaces. IIRC, it's not reliable in such tests.@niegowski, am I right?
AFAIK it does not do any processing on text nodes. Only setModelData() does some whitespace processing as it uses XmlDataProcessor.
We got a bit off-topic but I wanted to first clarify that CKEditor does correctly support non-breaking spaces intentionally inserted by the user.
@bmack, as for the actual use case:
A plugin that visualizes non-breaking spaces (TYPO3's Whitespace plugin marks every \upan.ck-nbsp) cannot highlight these characters. They never reach the model, so editors see only some of the nbsp in their content. Source: TYPO3 #101726 (https://forge.typo3.org/issues/101726).
And the markup that George posted there:
<h3>
Visible nbsp
</h3>
<p>
Lorem i<strong> p </strong> sum
</p>
<p>
indentation
</p>
<h3>
Invisible nbsp
</h3>
<p>
</p>
<p>
<br>
<br>
Lorem impsum
<br>
</p>
<p>
indentation
</p>
<p>
dolor sit amet
</p>
If you pass it via the CKEditor's setData() you'll get this:
This experiment shows that the editor correctly preserves all non-breaking characters. All the white spaces were preserved, with just one difference (we render 3 spaces before the "Lorem mipsum" line, while native DOM renders 2). Note: The content on the right looks a bit different visually because of different typography (width of a space character).
What might be confusing is why there are fewer non-breaking spaces in the CKEditor model than in the data. Let's look at this example:
<paragrap>foo bar</paragraph>
This CKEditor model contains a text node with 3 subsequent spaces. The user could have inserted them e.g. via pressing the space button 3 times. These are 3 normal spaces. But that's how the output HTML needs to look to correctly represent that model:
<p>foo bar</p>
So, in general, HTML will contain more non-breaking spaces than the model. This is unfortunate, but it's how HTML works.
Earlier, I said that CKEditor correctly handles user-originated non-breaking spaces. This is true for a vast majority of real-life scenarios. However, it slightly breaks in some odd ones. For example, what if the user typed this?
- "foo"
- space
- alt+space
- space
- "bar"
This will be the model:
<paragrap>foo \u00a0 bar</paragraph>
But the data will be identical to the one from the previous example:
<p>foo bar</p>
Meaning that if we load this content back to CKEditor the intentionally typed non-breaking space will be lost:
<paragrap>foo bar</paragraph>
To prevent that, we'd indeed need to mark it with some wrapping span to distinguish this scenario. But that'd significantly raise the complexity of text node processing and slow down the data pipeline.
The other option is to not encode subsequent spaces at all and rely on globally applied white-space: break-spaces. But it's a relatively fresh addition to the standard (supported since 2020) and might not be interoperable because the HTML won't be understood by tooling that does not understand this style (or the style cannot be provided to it). Though, to be fair, more of CKEditor features rely on particular stylesheets, so perhaps this is an option for the future to not-encode subsquent spaces by default. However, upon loading data to the editor or pasting content into it, we might still need to normalize those white-spaces (treat them as if they were encoded) because that's e.g. what browsers will do for copied content. Otherwise (and this happened in the past), pasted content would have too many nbsps preserved, negatively impacting the UX.