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

fix(forums): normalize newlines from synthetic events #3083

Merged

Conversation

wescopeland
Copy link
Member

@wescopeland wescopeland commented Jan 19, 2025

Resolves #3074 (comment).

Also, adds margin bottom to code tags (we should probably also apply the same margin bottom to quote tags).

Root Cause
React uses its own synthetic event system to maximize browser compatibility. For example, when a component has onClick={(event) => {...}}, this is not a native onclick="..." event. It's running through React's synthetic event system that emulates native events.

I've worked with React for a number of years and haven't hit a bug caused by synthetic events until now.

In our case, newlines were being sent to the preview component as escaped sequences (↵\n) after making any edit (ie: when initiating a synthetic event), but are served as actual newlines (\n) on load. I was able to observe this after putting a ton of logging in all the BBCode processors.

This fix normalizes all line endings to actual newlines (\n) during the preprocessing stage, ensuring consistent handling of whitespace regardless of how the newlines are represented in the input.

@wescopeland wescopeland requested a review from Jamiras January 19, 2025 20:08
}

return <span className="codetags">{children}</span>;
return <span className="codetags mb-3 font-mono">{children}</span>;
Copy link
Member

@Jamiras Jamiras Jan 20, 2025

Choose a reason for hiding this comment

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

I've applied the mb-3 to the #3074 PR (for both this and ShortcodeQuotes). You should remove it from here to eliminate the merge conflict (I've touched this code for trimming the leading <br>).

@@ -30,7 +30,11 @@ const createPatterns = (type: string) => [
];

export function preProcessShortcodesInBody(body: string): string {
let result = body;
// First, normalize any escaped newlines back to actual newlines.
let result = body.replace(/↵\n/g, '\n');
Copy link
Member

Choose a reason for hiding this comment

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

A unicode character for escaping a newline? I've never seen that before.

Should you use an escape sequence for it (\u21B5) to prevent possible encoding issues?

Copy link
Member Author

Choose a reason for hiding this comment

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

No harm in adding an escape sequence. Updated in latest - I'll go ahead and merge so the changes in this branch can safely land in #3074.

@wescopeland wescopeland merged commit 28fc893 into RetroAchievements:master Jan 20, 2025
8 checks passed
@wescopeland wescopeland deleted the whitespace-normalization-fix branch January 20, 2025 15:10
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.

2 participants