Behaviour SwRS-MDOKFSTUDIO-CODE-019
The copy button copies the raw code and confirms it
When the reader clicks the copy button, the extension SHALL put the block's code only on the clipboard, without file name or line numbers, lines joined with newlines and a trailing newline, label the button Copied with a check mark for 1.5 s only after the host confirms the copy, show the host's reason instead when it refuses, and keep the focus and the editors untouched; the host SHALL refuse a copy request whose text is not a string, is longer than 1 000 000 characters or does not follow a click of the user, without writing the clipboard, and answer every copy request it handles.
Step 2 of path 1 in StRS-MDOKFSTUDIO-CODE-004: from “The Copy code button is shown” to “The code is on the clipboard and the button reads Copied”.
Scenarios 13
The situations the tests check; open one to read it.
nominalThe clipboard holds the code lines only
Givena numbered, emphasized block with a file name
Whenthe reader clicks Copy code
Thenthe clipboard holds the code lines only, joined by newlines with a trailing newline (the page sends the text in a `previewCopyText` request, which the host writes to the clipboard)
boundaryA diff block keeps its markers
Givena diff block
Whenthe reader clicks Copy code
Thenthe clipboard holds the lines with their `+` and `-` markers and nothing else
nominalThe button confirms with Copied for 1.5 seconds
Giventhe reader has clicked Copy code
When1.5 s pass
Thenthe button was labelled Copied with a check mark from the host's confirmation and returns to Copy code without the check mark
refusalThe button never claims a copy that did not happen
Giventhe host answers a copy request with a refusal and a reason (for example "Too large to copy.")
Whenthe answer arrives
Thenthe button shows a cross and the reason as its label and title for 3 s instead of Copied, then returns to Copy code; before any answer, and for an answer to an older click, it keeps the label Copy code
boundaryA second click restarts the confirmation
Giventhe reader clicks the button twice within 1.5 s
Whenthe host confirms the second click
Thenthe Copied state lasts 1.5 s from that confirmation
nominalAn open block editor keeps the focus
Givena block editor is open elsewhere
Whenthe reader presses the copy button
Thenthe editor keeps the focus
refusalEdit mode opens and commits no editor
Givenedit mode is on
Whenthe reader clicks the copy button of a block
Thenthe code is copied and no editor opens or commits, and the click does not reach the block's own click handling
boundaryA copy request of 1 000 000 characters is written
Givena `previewCopyText` request with 1 000 000 characters
Whenthe host receives it
Thenthe text is written to the clipboard
refusalAn oversized or non-string request is refused
Givena `previewCopyText` request with 1 000 001 characters or a numeric text
Whenthe host receives it
Thenthe clipboard is unchanged; a text of 1 000 001 to 20 000 000 characters is answered `ok: false` with "Too large to copy."; anything else that fails the shape check is dropped without an answer, and the page shows its "did not answer" refusal after 6 s
refusalAccepted shape of the copy request
A `previewCopyText` message has the identity fields of every page message (see SwRS-MDOKFSTUDIO-TRUST-052) an `editId` (non-empty) a boolean `userGesture` and one field `text`:
| `text` | Outcome |
|---|
| `"code"` | accepted |
| `""` (empty string) | accepted |
| a string of 1 000 000 characters | accepted |
| a string of 1 000 001 to 20 000 000 characters | accepted, then answered ok false "Too large to copy." |
| a string of 20 000 001 characters | rejected |
| `5` (a number) | rejected |
| `undefined` (absent) | rejected |
A request is answered by `editId` (see SwRS-MDOKFSTUDIO-TRUST-090). The shape check accepts a string of up to 20 000 000 characters, so that the host can answer the size limit; a non-string is dropped: nothing is written to the clipboard.
refusalA copy request without a click of the user is refused
Givena well-formed `previewCopyText` message whose `userGesture` is false (the page sets it true only when it sends the request from a click event the user made, `isTrusted`)
Whenthe host receives it
Thenit is answered `ok: false`, "Copy needs a click on the copy button.", and the clipboard is unchanged; a message without a boolean `userGesture` is rejected by the shape check
nominalEvery handled request is answered
Givena copy request the host handles
Whenthe clipboard write succeeds or fails
Thenthe host sends `previewCopyTextResult` with the request's `editId` and `ok: true`, or `ok: false` with "Could not copy."
boundaryA stale page or a preview with restricted reads
Givena `previewCopyText` message that passes the shape check
Whenits document version or render request is stale, or the preview has restricted reads
Thena stale message is still copied and answered, because the copy is gated by the document only (see SwRS-MDOKFSTUDIO-TRUST-065), and a preview with restricted reads still writes the text and edits nothing (see SwRS-MDOKFSTUDIO-TRUST-005)