Skip to content

Fix CSS style changes not applying in Preview pane#221

Merged
schuyler merged 2 commits intomainfrom
claude/resolve-issue-219-OYRPL
Dec 21, 2025
Merged

Fix CSS style changes not applying in Preview pane#221
schuyler merged 2 commits intomainfrom
claude/resolve-issue-219-OYRPL

Conversation

@schuyler
Copy link
Copy Markdown
Owner

Summary

Fixes the issue where CSS styles and syntax highlighting themes don't update in the Preview pane when changed in Preferences.

Root cause: The DOM replacement optimization (which preserves scroll position during text edits) only replaced <body> content, leaving the old CSS <link> tags in <head> unchanged.

Solution: Before using DOM replacement, check if CSS style or highlighting theme has changed. If so, force a full HTML reload to update the <head> with new CSS links. This preserves the fast DOM replacement path for normal text editing.

Changes

  • Add currentStyleName and currentHighlightingThemeName tracking properties
  • Check if styles changed before using DOM replacement optimization
  • Force full HTML reload when CSS style or highlighting theme changes
  • Add tests for style change detection logic

Related Issue

Related to #219

Manual Testing Plan

  1. Open a document in MacDown
  2. Go to Preferences → Rendering
  3. Change CSS style (e.g., GitHub to Clearness)
  4. Expected: Preview updates with new CSS styling immediately
  5. Change syntax highlighting theme
  6. Expected: Code blocks update with new highlighting colors
  7. Make text edits
  8. Expected: Scroll position preserved (DOM replacement still works)

When users change CSS style or syntax highlighting theme in Preferences,
the Preview pane now correctly updates to show the new styles.

The issue was that the DOM replacement optimization (which preserves scroll
position during edits) only replaced <body> content, leaving the old CSS
<link> tags in <head>. Now we detect style changes and force a full HTML
reload when needed, while preserving the fast DOM replacement path for
normal text editing.

Changes:
- Add currentStyleName and currentHighlightingThemeName tracking properties
- Check if styles changed before using DOM replacement optimization
- Force full HTML reload when CSS style or highlighting theme changes
- Add tests for style change detection logic

Related to #219
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Dec 21, 2025

Code Coverage Report

Current Coverage: 42.43%

Coverage Details (Summary)
Name                                                                                                                                   Coverage            
-------------------------------------------------------------------------------------------------------------------------------------- ------------------- 
MASPreferences.bundle                                                                                                                  0.00% (0/0)         
MacDown 3000.app                                                                                                                       54.39% (6765/12438) 
    /Users/runner/work/macdown3000/macdown3000/MacDown/Code/Document/MPAsset.m                                                         93.69% (104/111)    
        -[MPAsset typeName]                                                                                                            100.00% (3/3)       
        -[MPAsset defaultTypeName]                                                                                                     100.00% (3/3)       
        +[MPAsset assetWithURL:andType:]                                                                                               100.00% (3/3)       
        -[MPAsset initWithURL:andType:]                                                                                                87.50% (7/8)        
        -[MPAsset init]                                                                                                                100.00% (3/3)       
        -[MPAsset templateForOption:]                                                                                                  0.00% (0/6)         
        -[MPAsset htmlForOption:]                                                                                                      100.00% (28/28)     
        -[MPStyleSheet defaultTypeName]                                                                                                100.00% (3/3)       
        +[MPStyleSheet CSSWithURL:]                                                                                                    100.00% (3/3)       
        -[MPStyleSheet templateForOption:]                                                                                             100.00% (20/20)     
        -[MPScript defaultTypeName]                                                                                                    100.00% (3/3)       
        +[MPScript javaScriptWithURL:]                                                                                                 100.00% (3/3)       
        -[MPScript templateForOption:]                                                                                                 100.00% (20/20)     
        -[MPEmbeddedScript htmlForOption:]                                                                                             100.00% (5/5)       
    /Users/runner/work/macdown3000/macdown3000/MacDown/Code/Utility/MPMathJaxListener.m                                                0.00% (0/26)        
        -[MPMathJaxListener callbacks]                                                                                                 0.00% (0/5)         
        -[MPMathJaxListener addCallback:forKey:]                                                                                       0.00% (0/3)         
        -[MPMathJaxListener invokeCallbackForKey:]                                                                                     0.00% (0/8)         
        +[MPMathJaxListener isSelectorExcludedFromWebScript:]                                                                          0.00% (0/5)         
        +[MPMathJaxListener isKeyExcludedFromWebScript:]                                                                               0.00% (0/5)         
    /Users/runner/work/macdown3000/macdown3000/Dependency/peg-markdown-highlight/HGMarkdownHighlightingStyle.m                         87.69% (57/65)      
        +[HGMarkdownHighlightingStyle colorFromARGBColor:]                                                                             100.00% (6/6)       
        -[HGMarkdownHighlightingStyle initWithType:attributesToAdd:toRemove:fontTraitsToAdd:]                                          88.89% (8/9)        
        -[HGMarkdownHighlightingStyle initWithStyleAttributes:baseFont:]                                                               86.00% (43/50)      
    /Users/runner/work/macdown3000/macdown3000/MacDown/Code/Extension/hoedown_html_patch.c                                             73.64% (95/129)     
        hoedown_patch_render_blockcode                                                                                                 89.09% (49/55)      
        hoedown_patch_render_listitem                                                                                                  100.00% (46/46)     
        hoedown_patch_render_toc_header                                                                                                0.00% (0/28)        
    /Users/runner/work/macdown3000/macdown3000/MacDown/Code/Application/MPMainController.m                                             40.76% (97/238)     
        MPOpenBundledFile                                                                                                              95.24% (20/21)      
        __MPOpenBundledFile_block_invoke                                                                                               85.71% (6/7)        
        treat                                                                                                                          20.00% (6/30)       
        __treat_block_invoke                                                                                                           0.00% (0/3)         
        -[MPMainController applicationDidFinishLaunching:]                                                                             100.00% (6/6)       
        -[MPMainController openUrlSchemeAppleEvent:withReplyEvent:]                                                                    0.00% (0/45)        
        __59-[MPMainController openUrlSchemeAppleEvent:withReplyEvent:]_block_invoke                                                   0.00% (0/7)         
        -[MPMainController valueForKey:fromQueryItems:]                                                                                0.00% (0/5)         
        -[MPMainController preferences]                                                                                                100.00% (3/3)       
        -[MPMainController preferencesWindowController]                                                                                0.00% (0/18)        
        -[MPMainController showPreferencesWindow:]                                                                                     0.00% (0/2)         
        -[MPMainController showHelp:]                                                                                                  100.00% (3/3)       
        -[MPMainController showContributing:]                                                                                          100.00% (3/3)       
        -[MPMainController init]                                                                                                       90.91% (10/11)      
        -[MPMainController applicationShouldOpenUntitledFile:]                                                                         80.00% (4/5)        
        -[MPMainController applicationDidBecomeActive:]                                                                                100.00% (5/5)       

... (2114 more lines truncated)

📊 **Full coverage report available in workflow artifacts**

- Update issue_9_preview_flickering_investigation.md with Issue #219 context
- Add style change detection tests to test_coverage_improvement_plan.md

Related to #219
@schuyler schuyler merged commit 897fb36 into main Dec 21, 2025
5 checks passed
@schuyler
Copy link
Copy Markdown
Owner Author

Tested by hand

@schuyler schuyler deleted the claude/resolve-issue-219-OYRPL branch December 21, 2025 06:33
schuyler added a commit that referenced this pull request Feb 26, 2026
## Summary

- Add `invalidateStyleCaches` method to `MPDocument` that clears
WebView's URL cache (`[[NSURLCache sharedURLCache]
removeAllCachedResponses]`) and resets cached style/theme names to nil
- Call `invalidateStyleCaches` from both `reloadPreview:` (Preview
context menu) and `didRequestPreviewReload:` (Settings gear menu Reload
button) before triggering re-render
- This forces the full HTML reload path (bypassing DOM replacement
optimization) so edited CSS files are re-read from disk
- Add 9 unit tests verifying the cache invalidation logic and style
change detection
- Register `MPDocumentStyleUpdateTests.m` in the Xcode project (existed
on disk but was missing from project file)

## Related Issue

Related to #318

## Root Cause

The bug had two layers:
1. **DOM replacement optimization** in `renderer:didProduceHTMLOutput:`
only replaced `<body>` content when the style *name* hadn't changed —
but the user edited the file *content*, not the style selection. Setting
cached names to nil forces `stylesChanged = YES`, bypassing DOM
replacement.
2. **WebView URL cache** served stale CSS from `<link>` tags with the
same `file://` URL. Clearing `NSURLCache` forces WebView to re-read from
disk.

Normal editing is unaffected because `invalidateStyleCaches` is only
called from explicit reload handlers, never from the
`editorTextDidChange:` editing flow.

## Manual Testing Plan

1. Open a Markdown document, note the CSS style in Preview
2. Edit the active CSS file externally (e.g., add `body {
background-color: red !important; }`)
3. Right-click in Preview → "Reload" — **Expected:** Preview updates
with red background
4. Open Preferences → Rendering → click Reload button for CSS styles —
**Expected:** Same result
5. Same workflow for syntax highlighting themes (edit theme CSS, reload)
6. Verify normal typing still preserves scroll position (DOM replacement
regression check)
7. Verify switching styles via dropdown still works (issue #221
regression check)

---------

Co-authored-by: Claude <noreply@anthropic.com>
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