Repository navigation
fix(docs): copy plain text from the read-only docs preview #9948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MaxonT
wants to merge
3
commits into
usebruno:main
Choose a base branch
from
MaxonT:contrib/9947-auto
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+170
−0
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
packages/bruno-app/src/ui/RichTextEditor/extensions/EditorPlainTextCopy/index.js
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { Extension } from '@tiptap/core'; | ||
| import { Plugin, PluginKey } from '@tiptap/pm/state'; | ||
|
|
||
| // `Markdown` is configured with `transformCopiedText` so that copying out of the editable editor | ||
| // yields Markdown. In the read-only docs preview the user sees rendered text, so copying should give | ||
| // back the selected text without list markers, heading hashes, emphasis delimiters or code fences. | ||
| // ProseMirror asks plugins for a clipboard text serializer in order and uses the first answer, so this | ||
| // extension runs before the Markdown one and answers only while the editor is not editable. | ||
| const EditorPlainTextCopy = Extension.create({ | ||
| name: 'plainTextCopy', | ||
| priority: 1000, | ||
|
|
||
| addProseMirrorPlugins() { | ||
| return [ | ||
| new Plugin({ | ||
| key: new PluginKey('plainTextCopy'), | ||
| props: { | ||
| clipboardTextSerializer: (slice) => { | ||
| if (this.editor.isEditable) { | ||
| return null; | ||
| } | ||
| return slice.content.textBetween(0, slice.content.size, '\n\n'); | ||
| } | ||
| } | ||
| }) | ||
| ]; | ||
| } | ||
| }); | ||
|
|
||
| export default EditorPlainTextCopy; | ||
84 changes: 84 additions & 0 deletions
84
packages/bruno-app/src/ui/RichTextEditor/extensions/EditorPlainTextCopy/index.spec.js
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import { Editor } from '@tiptap/core'; | ||
| import { TextSelection } from '@tiptap/pm/state'; | ||
| import createExtensions from '../index'; | ||
|
|
||
| const DOCS = [ | ||
| '### Heading with a green apple inside', | ||
| 'Plain paragraph with a green apple inside.', | ||
| '- A bullet item with a green apple inside.\n- Another bullet item with a blue notebook inside.', | ||
| '1. A numbered item with a blue notebook inside.\n2. A numbered item with a green apple inside.', | ||
| '**Bold text with a green apple inside.**', | ||
| '*Italic text with a green apple inside.*', | ||
| 'Inline code with `a green apple inside`.', | ||
| '```sql\nSELECT \'a green apple inside\' AS sample;\n```' | ||
| ].join('\n\n'); | ||
|
|
||
| const createEditor = (editable) => | ||
| new Editor({ | ||
| extensions: createExtensions().filter((ext) => !['rawHtmlBlock', 'rawHtmlTextBlock'].includes(ext.name)), | ||
| content: DOCS, | ||
| editable | ||
| }); | ||
|
|
||
| // Selects the n-th occurrence of `needle` in the document and returns what ProseMirror | ||
| // would put on the clipboard as text/plain for that selection. | ||
| const copyText = (editor, needle, occurrence = 0) => { | ||
| const { doc } = editor.state; | ||
| const found = []; | ||
| doc.descendants((node, pos) => { | ||
| if (!node.isText) return; | ||
| let idx = node.text.indexOf(needle); | ||
| while (idx !== -1) { | ||
| found.push(pos + idx); | ||
| idx = node.text.indexOf(needle, idx + 1); | ||
| } | ||
| }); | ||
| const from = found[occurrence]; | ||
| const selection = TextSelection.create(doc, from, from + needle.length); | ||
| const { text } = editor.view.serializeForClipboard(selection.content()); | ||
| return text; | ||
| }; | ||
|
|
||
| describe('EditorPlainTextCopy', () => { | ||
| describe('read-only (docs preview)', () => { | ||
| let editor; | ||
| beforeEach(() => { | ||
| editor = createEditor(false); | ||
| }); | ||
| afterEach(() => editor.destroy()); | ||
|
|
||
| // Every "green apple" in DOCS, in document order; the fenced block is the last one. | ||
| it.each([ | ||
| ['heading', 0], | ||
| ['paragraph', 1], | ||
| ['bullet item', 2], | ||
| ['numbered item', 3], | ||
| ['bold', 4], | ||
| ['italic', 5], | ||
| ['inline code', 6], | ||
| ['code block', 7] | ||
| ])('copies only the selected words from a %s', (_label, occurrence) => { | ||
| expect(copyText(editor, 'green apple', occurrence)).toBe('green apple'); | ||
| }); | ||
|
|
||
| it('copies a selection across blocks without Markdown syntax', () => { | ||
| const { doc } = editor.state; | ||
| const selection = TextSelection.create(doc, 1, doc.content.size - 1); | ||
| const { text } = editor.view.serializeForClipboard(selection.content()); | ||
| expect(text).not.toMatch(/^#|\*\*|^- |^1\. |```/m); | ||
| expect(text).toContain('Plain paragraph with a green apple inside.'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('editable', () => { | ||
| it('keeps copying as Markdown', () => { | ||
| const editor = createEditor(true); | ||
| const { doc } = editor.state; | ||
| const slice = TextSelection.create(doc, 1, doc.content.size - 1).content(); | ||
| const { text } = editor.view.serializeForClipboard(slice); | ||
| editor.destroy(); | ||
| expect(text).toMatch(/^### Heading/m); | ||
| expect(text).toContain('- A bullet item'); | ||
| }); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Include raw HTML block text in read-only plain-text copies.
The serializer at line 22 uses
textBetween. For a selection that spans a paragraph, a raw HTML block, and another paragraph, the output omits the raw HTML block's text. For example,<div><strong>green apple</strong></div>between two paragraphs is stored as an opaquerawHtmlBlock. That node has no text children and noleafText, so it contributes nothing. The copy keeps the surrounding paragraphs and drops the visible text without an error.Make the
rawHtmlBlocknode supply its visible text totextBetween. Derive that text from the same DOMPurify-sanitized markup that the node view renders. The separator behavior oftextBetweenthen keeps block boundaries in multi-block selections.ast-grep (0.51.3)
[warning] 22-22: The application creates a database connection with an empty password. This can lead to unauthorized access by either an internal or external malicious actor. To prevent this vulnerability, enforce authentication when connecting to a database by using environment variables to securely provide credentials or retrieving them from a secure vault or HSM (Hardware Security Module).
Context: '\n\n'
Note: [CWE-287] Improper Authentication.
(node-sequelize-empty-password-argument-javascript)
Map hard breaks to a newline when copying read-only text.
In the read-only preview,
textBetweengives no text for ahardBreakleaf. The repository'sEditorHardBreakdefines noleafText. A selection that spans a Shift+Enter break therefore copies the words joined, for examplegreenapple. Return'\n'forhardBreaknodes. Keep the existingleafTextfallback for other leaves.Suggested fix
Committable suggestion
ast-grep (0.51.3)
[warning] 22-22: The application creates a database connection with an empty password. This can lead to unauthorized access by either an internal or external malicious actor. To prevent this vulnerability, enforce authentication when connecting to a database by using environment variables to securely provide credentials or retrieving them from a secure vault or HSM (Hardware Security Module).
Context: '\n\n'
Note: [CWE-287] Improper Authentication.
(node-sequelize-empty-password-argument-javascript)