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

Prevent newlines from being duplicated after copying an enhanced codeblock #248

Merged
merged 3 commits into from
Apr 8, 2023

Conversation

noah-weingarden
Copy link
Contributor

Closes #246. I wrote a test to expose this (and it's the first test for copying an enhanced codeblock in general). Unfortunately, jsdom doesn't implement innerText (see jsdom/jsdom#1245), so I had to modify the map function to use textContent instead. I think that should be ok in this use case since the element containing the text for a single line of code is unlikely to ever have child nodes. If not though, I probably wrote that test for nothing.

@github-actions github-actions bot changed the base branch from main to develop March 17, 2023 05:24
@github-actions
Copy link
Contributor

Thanks for opening this pull request! 🙏

I've changed this PR's base branch to develop. A maintainer will review your PR soon!

@github-actions
Copy link
Contributor

github-actions bot commented Mar 17, 2023

The spec from this PR is available at https://preview.sesh.rs/previews/eecs485staff/primer-spec/248/.

(Available until Mon May 08 2023.)

if (!node) {
throw new Error('node cannot be null');
}
const mouseEvent = new CustomEvent(eventType);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this to use CustomEvent instead of document.createEvent because the latter relies on Event.initEvent, which is deprecated.

@seshrs
Copy link
Member

seshrs commented Mar 17, 2023

Thanks for putting this up @noah-weingarden! I’ll try to get to this over the weekend.
Seems like we should just release a patch update asap to fix the bug.

I had to modify the map function to use textContent instead. I think that should be ok in this use case since the element containing the text for a single line of code is unlikely to ever have child nodes.

Actually, it’s likely for a single line of code to have child nodes since syntax highlighting is applied as span elements over specific tokens.

However, I agree with you that textContent is likely more appropriate for this use case. According to the MDN docs, textContent returns text from descendant nodes as well.

Could you update the unit test with more nested syntax highlighting nodes from an actual rendered code block? You should be able to get this by viewing the HTML source of a Primer Spec page.

@noah-weingarden
Copy link
Contributor Author

noah-weingarden commented Mar 17, 2023

Actually, it’s likely for a single line of code to have child nodes since syntax highlighting is applied as span elements over specific tokens. However, I agree with you that textContent is likely more appropriate for this use case. According to the MDN docs 1, textContent returns text from descendant nodes as well.

Haha, then textContent is appropriate for the exact opposite of why I thought it is. XD

Could you update the unit test with more nested syntax highlighting nodes from an actual rendered code block? You should be able to get this by viewing the HTML source of a Primer Spec page.

I don't think that would work: the event handler for clicking the button won't be attached if I just use the raw HTML for the rendered code block. We'd have to convert the HTML code to DOM nodes and then manually call copyLines instead of clicking the button. A couple downsides of that would be that 1) we'd need to export copyLines even though it makes more sense to be private to its module like it currently is and 2) the test would be less realistic at simulating a real user's interaction with the page. I guess another option would be to dynamically attach an event handler within the test, but that would still require extracting the event handler to be an exportable function, which feels a little arbitrary if it only needs to be exported to a test. Also, that would mean the test would be duplicating work that Primer Spec already does.

@seshrs seshrs added the semver/patch Pull Request proposes "patch" change label Mar 20, 2023
@seshrs
Copy link
Member

seshrs commented Mar 30, 2023

I'm sorry I still haven't got to this. It's on my todo list for this weekend though!

- Test copy button with syntax highlighting
- Update comment to indicate that textContent is the right choice for reasons other than jsdom
Copy link
Member

@seshrs seshrs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM after pushing the nits that I referenced in my previous comment. Thanks for putting this up @noah-weingarden!

@seshrs seshrs merged commit f8c11d9 into develop Apr 8, 2023
@seshrs seshrs deleted the enhanced-codeblocks-newlines-duplicated branch April 8, 2023 21:08
@noah-weingarden
Copy link
Contributor Author

Oh, I see what you meant now! And no problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver/patch Pull Request proposes "patch" change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Copying an enhanced codeblock duplicates newlines
2 participants