Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TINY-10854: Now insert format also manage nbsps correctly #9636

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

lorenzo-pomili
Copy link
Contributor

@lorenzo-pomili lorenzo-pomili commented May 9, 2024

Related Ticket: TINY-10854

Description of Changes:
Since to normalize the nbsps we use normalizeNbsps but this mechanism is based on the text elements and to fix our case we would need to find the parent and parse all the children element which could require way more time.

Since when we add a format we apply a CaretFormat, so to fix this case my idea is to apply an nbsp normalization with on input (like for normalizeNbspsInEditor in InputKeys), but only for the next insertion (so using editor.once) since this is needed only after the first insertion.

Pre-checks:

  • Changelog entry added
  • Tests have been added (if applicable)
  • Branch prefixed with feature/, hotfix/ or spike/

Review:

  • Milestone set
  • Docs ticket created (if applicable)

GitHub issues (if applicable):

@lorenzo-pomili lorenzo-pomili added this to the 7.2.0 milestone May 10, 2024
@lorenzo-pomili lorenzo-pomili marked this pull request as ready for review May 10, 2024 10:34
@lorenzo-pomili lorenzo-pomili requested a review from a team as a code owner May 10, 2024 10:34
@lorenzo-pomili lorenzo-pomili requested review from spocke, TheSpyder, vpyshnenko, a team, lostkeys, hamza0867 and HAFRMO and removed request for a team May 10, 2024 10:34
Copy link
Member

@spocke spocke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Fix typos in PR title npsp -> nbsp and correclty -> correctly

modules/tinymce/src/core/main/ts/fmt/CaretFormat.ts Outdated Show resolved Hide resolved
modules/tinymce/src/core/main/ts/fmt/CaretFormat.ts Outdated Show resolved Hide resolved
modules/tinymce/src/core/main/ts/fmt/CaretFormat.ts Outdated Show resolved Hide resolved
.changes/unreleased/tinymce-TINY-10854-2024-05-10.yaml Outdated Show resolved Hide resolved
@lorenzo-pomili lorenzo-pomili changed the title TINY-10854: Now insert format also manage npsps correclty TINY-10854: Now insert format also manage nbsps correctly May 10, 2024
@@ -0,0 +1,6 @@
project: tinymce
kind: Fixed
body: 'Extra unnecessary nbsp entities was inserted before and after inline elements created by input of formatted text.'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
body: 'Extra unnecessary nbsp entities was inserted before and after inline elements created by input of formatted text.'
body: Unnecessary nbsp entities were inserted when typing at the edges of inline elements.


const normalizeNbspsBetween = (editor: Editor, caretContainer: Node | null) => {
const handler = () => {
if (caretContainer && !editor.dom.isEmpty(caretContainer)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised that removing every nbsp passes the tests. But our tests for NBSP normalisation probably don't cover this new caretContainer condition well enough. Speaking of which...

Suggested change
if (caretContainer && !editor.dom.isEmpty(caretContainer)) {
if (caretContainer !== null && !editor.dom.isEmpty(caretContainer)) {

Try something like <p>text <strong>&nbsp;more text</strong></p>. Your code will probably remove the nbsp, which is wrong.

  • do some wider checks of this logic

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I'm writing the logic for it, I'd like to make an abstraction starting from Nbsps.ts to use it in both cases, but I'm not sure how possible is since it's strong relation with the elements of the DOM.

Since the logic in Nbsps.ts isn't working for this cases, what logic do we want to implement?
by now we have these tests: NbspsTest.ts, should I make these cases work considering that instead of a simple text that could be an inline element?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would break out the everything inside the isEmpty check. Then make a separate function that takes a target node that checks the sibling before target, first child of target, last child of target, sibling after target. Then checks if any of those nodes has a non whitespace character followed by a nbsp since then you can replace that nbsp with a space.
Maybe feed those siblings into an array if that makes things easier. We might need to expand this in the future for other cases like <strong>foo&nbsp;<span>bar</span> etc.
Maybe it makes sense to stick that new function into the Nbsps module?


const normalizeNbspsBetween = (editor: Editor, caretContainer: Node | null) => {
const handler = () => {
if (caretContainer && !editor.dom.isEmpty(caretContainer)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would break out the everything inside the isEmpty check. Then make a separate function that takes a target node that checks the sibling before target, first child of target, last child of target, sibling after target. Then checks if any of those nodes has a non whitespace character followed by a nbsp since then you can replace that nbsp with a space.
Maybe feed those siblings into an array if that makes things easier. We might need to expand this in the future for other cases like <strong>foo&nbsp;<span>bar</span> etc.
Maybe it makes sense to stick that new function into the Nbsps module?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants