-
Notifications
You must be signed in to change notification settings - Fork 840
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
[EuiCodeBlock] Fix line numbers appearing in copied content #6824
Conversation
Line numbers must be rendered via CSS pseudo element and not text nodes in order for it not to be copied
- requires giving `EuiCodeBlockAnnotation` a bit more prop flexibility
describe('EuiCodeBlock copy UX', () => { | ||
beforeEach(() => { | ||
// Clipboard permissions are required for copy behavior to work in non-Electron browsers | ||
// @see https://github.com/cypress-io/cypress-example-recipes/blob/182400395817a14f0c7f18889b2fcc6b4d189434/examples/testing-dom__clipboard/cypress/e2e/permissions-spec.cy.js#L37 | ||
cy.wrap( | ||
Cypress.automation('remote:debugger:protocol', { | ||
command: 'Browser.grantPermissions', | ||
params: { | ||
permissions: ['clipboardReadWrite', 'clipboardSanitizedWrite'], | ||
origin: window.location.origin, | ||
}, | ||
}) | ||
); | ||
}); | ||
|
||
const assertClipboardContentEquals = (expectedText: string) => { | ||
cy.window() | ||
.its('navigator.clipboard') | ||
.invoke('readText') | ||
.then((clipboard) => { | ||
expect(clipboard).equals(expectedText); | ||
}); | ||
}; |
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.
This was a fun rabbit hole to go down - turns out it's much more annoying to assert on copy/clipboard content than I thought.
I went down a separate rabbit hole for RTL to try and get a paste test with that working, but it turns out we're missing several requirements on that front:
@testing-library/user-events
needs to be at 14+ (for.setup()
API)jest
needs to be at 26+ in order for JSDOM to be at 16+ - otherwise the.getSelection
API errors
Since upgrading Jest is a whole nother fun bucket of shenanigans (#6813) I opted for Cypress with these tests for now, but may spike out converting these tests down the road when we upgrade Jest.
Preview documentation changes for this PR: https://eui.elastic.co/pr_6824/ |
Preview documentation changes for this PR: https://eui.elastic.co/pr_6824/ |
// Note: This must use a CSS pseudo element to print the line number | ||
// to prevent line numbers from being copied as text | ||
&::before { | ||
content: attr(data-line-number); |
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.
I love this solution for its simplicity!
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.
Code looks good and the changes are working as expected. Great job on fixing the bug so cleanly!
This reverts commit fac3385.
Preview documentation changes for this PR: https://eui.elastic.co/pr_6824/ |
This is a backport EUI upgrade to Kibana v8.8.1 containing an `EuiCodeBlock` bugfix requested by the Observability team: #158644 (comment) ## [`77.1.5`](https://github.com/elastic/eui/tree/v77.1.5) **Bug fixes** - Fixed `EuiCodeBlock` potentially incorrectly ignoring lines ending with a question mark when using the Copy button. ([#6794](elastic/eui#6794)) - Fixed `EuiCodeBlock` to not include line numbers when copying content ([#6824](elastic/eui#6824)) - Fixed the expanded row animation on `EuiBasicTable` causing cross-browser Safari issues ([#6826](elastic/eui#6826)) Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
## Summary `eui@81.2.0` ⏩ `eui@81.3.0` ✨ Highlights: - fixes #158644 - Adds a new Timeline icon for use within `EuiMarkdownEditor` (cc @kqualters-elastic) - Expandable rows used within `EuiBasicTable` and `EuiInMemoryTable` now have a faster and snappier expand animation ___ ## [`81.3.0`](https://github.com/elastic/eui/tree/v81.3.0) - Added `timelineWithArrow` glyph to `EuiIcon` ([#6822](elastic/eui#6822)) **Bug fixes** - Fixed `EuiCodeBlock` potentially incorrectly ignoring lines ending with a question mark when using the Copy button. ([#6794](elastic/eui#6794)) - Fixed `EuiCodeBlock` to not include line numbers when copying content ([#6824](elastic/eui#6824)) - Fixed the expanded row animation on `EuiBasicTable` causing cross-browser Safari issues ([#6826](elastic/eui#6826))
Summary
closes #6818
I broke this behavior in #6580 (annotation feature) by switching from rendering the line numbers as
::before
pseudo elements to actual text node. I've reverted the rendering to use pseudo elements, and cleaned up line number classNames slightly to better reflect the intended DOM structure (see individual commits).I've additionally added new Cypress tests to ensure copy functionality doesn't regress in the future.
QA
General checklist
jest andcypress tests