fix: prevent querySelector crash on empty heading slug#4135
Open
Bowl42 wants to merge 2 commits intomarktext:developfrom
Open
fix: prevent querySelector crash on empty heading slug#4135Bowl42 wants to merge 2 commits intomarktext:developfrom
Bowl42 wants to merge 2 commits intomarktext:developfrom
Conversation
When a heading has no text content or contains only special characters,
the slugger produces an empty slug. This causes querySelector('#') to
throw "Failed to execute 'querySelector': '#' is not a valid selector",
crashing the renderer process.
- Slugger now returns fallback 'heading' slug for empty content
- scrollToHeader guards against empty slug
- scrollToElement wraps querySelector in try/catch for invalid selectors
- tableDragBarCtrl guards against empty/missing table IDs
- Add comprehensive unit tests for Slugger including edge cases
Fixes marktext#4087
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add reverse-collision test: empty heading before real "heading" - Add chained collision test: multiple empty + explicit "heading-1" - Add Unicode tests: CJK characters, emoji-only, mixed ASCII+Unicode Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #4087
Also fixes #4092 (duplicate)
When a heading has no text content or contains only special characters, the slugger produces an empty slug. This causes
querySelector('#')to throw"Failed to execute 'querySelector': '#' is not a valid selector", crashing the renderer process.Changes:
slugger.js: Return fallback slug'heading'when content produces an empty slug (with deduplication support for multiple empty headings)editor.vue: Add empty-slug guard inscrollToHeaderand wrapquerySelectorin try/catch inscrollToElementtableDragBarCtrl.js: Add null guards incalculateAspects,getAllTableCells, andgetDragCellsto prevent the same crash when table IDs are missingTest plan
Sluggercovering normal slugs, empty strings, whitespace-only input, special-character-only input, fallback deduplication, and collision with real headings (test/unit/specs/slugger.spec.js)# !@#$%), verify no crash#with no text), click it in the TOC sidebar, verify no crash🤖 Generated with Claude Code