feat(editor): try to have ckeditor not crash when handling stranger tags #6867
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
this definitely isn't being merged, but keeping it open for Elian. Here's what the annoying AI said the fix was for below lol:
CKEditor Generic Type Syntax Fix
The Problem
We discovered a critical bug where CKEditor would crash when switching from read-only to edit mode on notes containing programming language generic type syntax. Here's what was happening:
The Crash
When a note contained code like this:
Or Rust code like:
CKEditor would crash with this error:
Why It Crashed
CKEditor was trying to parse the generic type syntax as HTML tags! When it saw
<String, PromptTemplate>
, it thought:String,
"String,
isn't a valid HTML tag name (it has a comma!)The same happened with:
Box<dyn
→ tried to create abox<dyn
element<OpenAIClient>
→ tried to create anopenaiclient
elementWhy Existing Solutions Didn't Work
Server-side HTML Sanitizer
Trilium already has an HTML sanitizer that uses an
allowedHtmlTags
setting. You might think, "Why not just use that?" Here's why that wouldn't work:The sanitizer removes non-allowed tags entirely:
We don't want to delete the code - we want to preserve it as text!
The Key Insight
We need escaping, not sanitizing!
Our Solution
We implemented a smart escaping mechanism that:
allowedHtmlTags
settingHere's how it works:
Example Transformation
Input:
After Step 1 (Protect HTML):
After Step 2 (Escape remaining brackets):
Final Output:
CKEditor receives this and:
<p>
,<pre>
,<div>
as HTML tags<String, PromptTemplate>
as visible text:<String, PromptTemplate>
Why Our Fix is Correct
allowedHtmlTags
option so users can customize what's treated as HTMLConfiguration
Users can customize which tags are treated as HTML through:
Settings → Other → Allowed HTML Tags
The default list includes common HTML tags (div, span, p, etc.) and Trilium-specific elements (includenote).
Testing
The fix handles all these patterns correctly:
HashMap<String, List<Item>>
Record<string, unknown>
Box<dyn Error + Send>
std::vector<int>
<String,
(missing closing bracket)Summary
The bug occurred because CKEditor tried to parse programming language generic syntax as HTML tags. Our fix intelligently escapes non-HTML angle brackets while preserving real HTML tags, preventing crashes while maintaining all content. The solution respects user preferences through the configurable
allowedHtmlTags
setting and ensures no data loss occurs.