Skip to content

fix: copy the whole code block from a folded commit - #1180

Open
Nejcc wants to merge 1 commit into
dev-null-sh:productionfrom
Nejcc:fix/copy-collapsed-code
Open

Nejcc wants to merge 1 commit into
dev-null-sh:productionfrom
Nejcc:fix/copy-collapsed-code

Conversation

@Nejcc

@Nejcc Nejcc commented Oct 26, 2026 •

Copy link
Copy Markdown

Summary

Closes #1161.

When a long commit starts with a code block, the log folds it and shows only the block's first lines. The copy button copied what was on screen, so you got those first lines and not the code. Now it copies the whole block, folded or not.

Changes

  • FoldedCommitBody gives the first code block of the preview the html of the block at the same place in the full body (wholeHtml).
    • The excerpt only ever cuts a code block that starts the body (CommitBodies::excerpt).
    • Empty text segments are never sent, so what comes before that block is the same in the preview and the full body.
    • No other block is touched, so a later block can't end up copying a different one.
  • CodeBlock copies wholeHtml when it has one. It reads the html as text through a <template>, whose content is inert: nothing in it loads or runs, and leading newlines survive. Without wholeHtml, it copies the shown block as before.
  • BodySegment's block variant gets an optional, client-only wholeHtml.

Validation

Local:

  • bun run test:lint: passed.
  • bun run test:unit: passed.
  • Chromium through Playwright, desktop, guest on /log/latest, with seeded commits, all folded:
    • 30-line block: copy gives 6 lines on production and all 30 on this branch.
    • Two short blocks (ls -la, then ls) before long prose: each copies its own code.
    • Block starting with a blank line: the folded copy is identical to the unfolded copy.
  • No PHP changes. The repo has no browser tests by design, so there's no automated test for this. It lives in the component, and the existing JS unit tests run in Node without a DOM.

Independent review

A separate reviewer did a read-only review of base d3fc1df6 against head 53887c55. The first version matched every preview block against the full blocks by prefix. It failed review: a short later block could copy an earlier one, and a leading blank line was lost. The rewrite above fixes both, and the re-review of 53887c55 is PASS. One non-blocking nit is left as is: the <template> is created on every copy, even without wholeHtml.

Limitations

  • Not tested on a mobile device. It's the same copy button and clipboard call; only the copied text differs.

Summary by CodeRabbit

  • New Features
    • Copying code from a folded commit body now includes the full code block, even when only part of the block is visible in the preview.
    • Code blocks without full-content information continue to copy the text shown on screen.

A folded commit shows only the first lines of a code block that starts it, and the copy button copied just those. The folded body now gives that block the whole block's html to copy.
@coderabbitai

coderabbitai Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b31a7ddf-d3f0-4595-abed-4526d360d143
Commits

Reviewing files that changed from the base of the PR and between d3fc1df and 53887c5.

Files selected for processing: 3
  • resources/js/components/code-block.tsx
  • resources/js/components/commit-body.tsx
  • resources/js/types/commit.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

Walkthrough

Code blocks now accept an optional full-HTML clipboard source. Folded commit bodies pass the corresponding full code block’s HTML to the preview’s first code block, so copying can use the full block instead of only the rendered preview.

Changes: Folded code copying

Layer / File(s) Summary
Full HTML clipboard source
resources/js/types/commit.ts, resources/js/components/code-block.tsx
BodySegment block entries support optional wholeHtml. When provided, CodeBlock extracts text from that HTML for copying; otherwise, it uses the rendered code element’s text.
Folded body source wiring
resources/js/components/commit-body.tsx
FoldedCommitBody sets wholeHtml on the preview’s first code block from the corresponding full-body block and renders the adjusted preview segments.

Priority: Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Low

Possibly related PRs

Suggested reviewers: xheaven, mnapoli

Merge Risk: Minimal · up to 53887

The change has no established actionable correctness or availability risk and is suitable to merge with normal checks.

Security architecture risk: Minimal · up to 53887

The full copy source comes from the same code block already supplied to the page. Copying remains an explicit user action that writes plain text, without introducing script execution or additional data access. No material security risk was identified in the changed flow.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — A commit author can influence the complete code text copied by a viewer, including lines hidden by folding. The changed outcome is confined to the viewer's clipboard after a copy action; the source content was already available for unfolding, and this flow does not execute the copied text.

Trust Boundaries and Controls

  • observed — The copy handler parses wholeHtml into a detached template without attaching its content to the active document. Only descendant text is passed to writeText, not HTML. The existing isCopyable control still determines whether the copy button is rendered.

Resilience and Maintainability Implications

  • observed — The clipboard operation still reports success only after writeText resolves. Unsupported or rejected writes retain the existing failure result and user feedback. The change introduces no persistent reservation, rollback obligation, or mutation of the source segments.
Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: copying the complete code block from a folded commit.
Linked Issues check Passed Issue #1161 requires the copy button to copy the complete code block when a post is collapsed. FoldedCommitBody supplies the full block HTML to the preview block, and CodeBlock extracts and copies…
Out of Scope Changes check Passed The changes stay within issue #1161. They update FoldedCommitBody, CodeBlock, and the BodySegment type to pass and copy the full HTML for a folded code block. No unrelated product behavior or fi…
Full details: Linked Issues check

Explanation

Issue #1161 requires the copy button to copy the complete code block when a post is collapsed. FoldedCommitBody supplies the full block HTML to the preview block, and CodeBlock extracts and copies that full block when wholeHtml is present. The existing copy path remains active when wholeHtml is absent.

Full details: Out of Scope Changes check

Explanation

The changes stay within issue #1161. They update FoldedCommitBody, CodeBlock, and the BodySegment type to pass and copy the full HTML for a folded code block. No unrelated product behavior or files are changed.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

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.

Copy Button Does Not Copy the Entire Code When a Post Is Collapsed

1 participant