Skip to content

Address issue #318: Style reload doesn't update Preview pane#328

Merged
schuyler merged 2 commits intomainfrom
claude/resolve-issue-318-sT5Px
Feb 26, 2026
Merged

Address issue #318: Style reload doesn't update Preview pane#328
schuyler merged 2 commits intomainfrom
claude/resolve-issue-318-sT5Px

Conversation

@schuyler
Copy link
Copy Markdown
Owner

@schuyler schuyler commented Feb 24, 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 Fix CSS style changes not applying in Preview pane #221 regression check)

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Feb 24, 2026

Code Coverage Report

Current Coverage: 56.67%

Coverage Details (Summary)
Name                                                                                                                                   Coverage            
-------------------------------------------------------------------------------------------------------------------------------------- ------------------- 
MASPreferences.bundle                                                                                                                  0.00% (0/0)         
MacDown 3000.app                                                                                                                       59.58% (8155/13688) 
    /Users/runner/work/macdown3000/macdown3000/MacDown/Code/Extension/NSColor+HTML.m                                                   94.05% (332/353)    
        +[NSColor(HTML) colorWithHexString:]                                                                                           94.12% (16/17)      
        +[NSColor(HTML) colorWithHTMLName:]                                                                                            89.13% (164/184)    
        __35+[NSColor(HTML) colorWithHTMLName:]_block_invoke                                                                           100.00% (152/152)   
    /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/FileURLInlining.m                                                  26.60% (25/94)      
        +[FileURLInlining inlineFromIterable:]                                                                                         0.00% (0/11)        
        -[FileURLInlining initWithURL:]                                                                                                0.00% (0/6)         
        -[FileURLInlining init]                                                                                                        100.00% (3/3)       
        -[FileURLInlining inlineContent]                                                                                               0.00% (0/9)         
        -[FileURLInlining imageContent]                                                                                                0.00% (0/12)        
        +[FileURLInlining mimeTypeForFilePath:]                                                                                        100.00% (22/22)     
        -[FileURLInlining clippingContent]                                                                                             0.00% (0/4)         
        +[FileURLInlining isTextClippingAtPath:]                                                                                       0.00% (0/12)        
        +[FileURLInlining textContentFromClipping:]                                                                                    0.00% (0/15)        
    /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/Utility/MPResourceWatcherSet.m                                             87.10% (54/62)      
        -[MPResourceWatcherSet init]                                                                                                   100.00% (6/6)       
        -[MPResourceWatcherSet dealloc]                                                                                                0.00% (0/3)         
        -[MPResourceWatcherSet watchedPaths]                                                                                           100.00% (3/3)       
        -[MPResourceWatcherSet updateWatchedPaths:]                                                                                    100.00% (16/16)     
        -[MPResourceWatcherSet addWatcherForPath:]                                                                                     100.00% (18/18)     
        __42-[MPResourceWatcherSet addWatcherForPath:]_block_invoke                                                                    100.00% (6/6)       
        __42-[MPResourceWatcherSet addWatcherForPath:]_block_invoke.5                                                                  0.00% (0/5)         
        -[MPResourceWatcherSet stopAll]                                                                                                100.00% (5/5)       
    /Users/runner/work/macdown3000/macdown3000/MacDown/Code/Extension/hoedown_html_patch.c                                             86.47% (115/133)    
        hoedown_patch_reset_checkbox_index                                                                                             100.00% (3/3)       
        hoedown_patch_get_checkbox_index                                                                                               0.00% (0/3)         
        hoedown_patch_render_blockcode                                                                                                 89.09% (49/55)      

... (2817 more lines truncated)

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

Add invalidateStyleCaches method that clears WebView's URL cache and
resets cached style/theme names before explicit reload. This forces
the full HTML reload path (bypassing DOM replacement) so edited CSS
files are re-read from disk.

The bug had two layers: (1) DOM replacement optimization only updated
<body>, leaving <head> CSS <link> tags untouched because the style
*name* hadn't changed, and (2) WebView cached CSS by URL, serving
stale content even on full reload.

Both reloadPreview: (context menu) and didRequestPreviewReload:
(Settings gear menu) now call invalidateStyleCaches before rendering.
Normal editing is unaffected since the method is only called on
explicit user-initiated reload actions.

Related to #318
@schuyler schuyler force-pushed the claude/resolve-issue-318-sT5Px branch from 62c0438 to 24d170d Compare February 26, 2026 20:03
@schuyler schuyler merged commit 6443c19 into main Feb 26, 2026
10 checks passed
@schuyler schuyler deleted the claude/resolve-issue-318-sT5Px branch February 26, 2026 20:06
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