Skip to content
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

Merged
merged 7 commits into from
Jun 2, 2023

Conversation

cee-chen
Copy link
Contributor

@cee-chen cee-chen commented Jun 1, 2023

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

  • Revert [REVERT ME] commit
  • Checked in Chrome, Safari, Edge, and Firefox
  • Checked for accessibility including keyboard-only and screenreader modes
    • Line numbers continue to not be navigable/read out by screen readers (although they possibly should be). Annotations remain tabbable and announced by SRs
  • Added or updated jest and cypress tests
  • A changelog entry exists and is marked appropriately

cee-chen added 4 commits June 1, 2023 10:19
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
@cee-chen cee-chen requested a review from tkajtoch June 1, 2023 18:27
Comment on lines 34 to 56
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);
});
};
Copy link
Contributor Author

@cee-chen cee-chen Jun 1, 2023

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.

@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_6824/

@kibanamachine
Copy link

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);
Copy link
Member

@tkajtoch tkajtoch Jun 2, 2023

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!

Copy link
Member

@tkajtoch tkajtoch left a 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!

@cee-chen cee-chen enabled auto-merge (squash) June 2, 2023 16:25
@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_6824/

@cee-chen cee-chen merged commit 55f56c6 into elastic:main Jun 2, 2023
@cee-chen cee-chen deleted the codeblock/copy branch June 2, 2023 17:04
cee-chen added a commit to elastic/kibana that referenced this pull request Jun 3, 2023
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>
cee-chen added a commit to elastic/kibana that referenced this pull request Jun 6, 2023
## 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))
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.

[EuiCodeBlock] enabling lineNumbers causes copied text to contain actual line numbers
3 participants