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

Change .append() to .appendChild() #953

Merged
merged 2 commits into from
Jun 27, 2022
Merged

Change .append() to .appendChild() #953

merged 2 commits into from
Jun 27, 2022

Conversation

jkenzer
Copy link
Contributor

@jkenzer jkenzer commented Jun 25, 2022

Reason for change
While using Plot with external frameworks that have their own Dom implementations, it's possible the framework's Dom implementation doesn't support the newer Dom.append() method. This pull requests addresses the instance where plot uses Dom.append() to add a legend or a caption by using the Dom.appendChild() method.

Testing with Salesforce's Lightning Web Component (LWC) framework
I found this issue while trying to use Plot inside of Salesforce.com to create customized visualizations that Salesforce isn't capable of out of the box. I was able to successfully use Plot version 0.5.0 with no issues other than when I tried to add either a legend or a caption.

To test this PR, I built a dist after running tests and uploaded dist/plot.umd.js to Salesforce and added both a legend and caption to my visualizations. Here is a sample screenshot

image

This contribution addresses the topic raised in this community post.

Copy link
Member

@mbostock mbostock left a comment

Choose a reason for hiding this comment

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

Thanks for this contribution! This is close but we’ll need some tweaks to maintain the functionality of append. I can help with this if you’d like—let me know.

@@ -222,11 +222,12 @@ export function plot(options = {}) {
if (caption != null || legends.length > 0) {
figure = document.createElement("figure");
figure.style.maxWidth = "initial";
figure.append(...legends, svg);
if (legends.length > 0) figure.appendChild(...legends);
Copy link
Member

Choose a reason for hiding this comment

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

appendChild unlike append only takes a single argument, so this will need to be a for loop now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it. Easy change. I think I was assuming that there would only ever be one legend in the array. I'll make this a for of loop.

if (caption != null) {
const figcaption = document.createElement("figcaption");
figcaption.append(caption);
figure.append(figcaption);
typeof caption === "object" ? figcaption.appendChild(caption) : (figcaption.innerHTML = caption);
Copy link
Member

Choose a reason for hiding this comment

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

We’ll need a more precise test for an element or text node here. I think we can crib from hypertext literal for the exact logic. And then in the non-element case, we’ll need to create a text node. (We shouldn’t set innerHTML since that would allow the caption to contain HTML and potentially cause encoding issues.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gotcha about the innerHTML. I'm looking at the code now from the hypertext literal. I might need assistance here but let me see what I come up with.

Thanks for the feedback.

@mbostock mbostock changed the base branch from main to mbostock/append June 27, 2022 17:34
@mbostock
Copy link
Member

Merging into a branch so I that I can finish the remaining steps.

@mbostock mbostock merged commit be7f0c2 into observablehq:mbostock/append Jun 27, 2022
mbostock added a commit that referenced this pull request Jun 27, 2022
* Change .append() to .appendChild() (#953)

* Update plot.js to use .appendChild() rather than .append()

* Refactor to use ternary and typeof check

* appendChild fixes

Co-authored-by: Josh Kenzer <[email protected]>
@jkenzer jkenzer deleted the change-append-to-appendChild branch June 27, 2022 21:27
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.

2 participants