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

fix!: ensure HTML ID attributes are unique #607

Merged
merged 5 commits into from
Feb 21, 2025

Conversation

BenGale93
Copy link
Contributor

Summary

Previously, if you had multiple GT HTML tables inside a single document, that had column names in common, you would have repeated HTML ID attributes. This PR makes these ID attributes unique by prepending the table ID, if it exists.

Related GitHub Issues and PRs

Checklist

Copy link

codecov bot commented Feb 13, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.85%. Comparing base (2256e36) to head (09a6fb7).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #607      +/-   ##
==========================================
+ Coverage   90.84%   90.85%   +0.01%     
==========================================
  Files          46       46              
  Lines        5417     5423       +6     
==========================================
+ Hits         4921     4927       +6     
  Misses        496      496              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@rich-iannone
Copy link
Member

This looks great, thanks! However, it looks like the formatting used for the new ID values shouldn't have a space. According to a W3Schools page:

The id name must contain at least one character, cannot start with a number, and must not contain whitespaces (spaces, tabs, etc.).

Could you revise to use a hyphen instead?

@BenGale93
Copy link
Contributor Author

Just updated it. I also applied the same rule to the column label and updated the test to have a column with a space in the name to demonstrate that.

@@ -225,7 +236,7 @@ def create_columns_component_h(data: GTData) -> str:
colspan=len(stub_layout),
style=_flatten_styles(styles_stubhead),
scope="colgroup" if len(stub_layout) > 1 else "col",
id=_process_text_id(stub_label),
id=_create_element_id(table_id, stub_label),
Copy link
Member

Choose a reason for hiding this comment

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

Just noting for the future (nothing needs to be done here) but the stub doesn't really have an ID value (I suppose the column that created the stub labels could be it?) and it often won't have a label unless tab_stubhead() was called.

The issue with all this is that for the stubhead <th> element we get id="", which is invalid HTML according to https://validator.w3.org/nu/.

Copy link
Member

@rich-iannone rich-iannone left a comment

Choose a reason for hiding this comment

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

This is so close, but I have a few suggested changes.

@rich-iannone
Copy link
Member

@BenGale93 Sorry to reverse course here a bit, but I did notice that if labels are used to compose element IDs, there's a good chance one might get duplicate ID values (since labels can be user-modified but column names are immutable and usually always unique). Could you address these changes? I tried out all of the suggested changes locally and tests pass and the element IDs look reasonable to me.

@BenGale93
Copy link
Contributor Author

@rich-iannone makes total sense. Will work on this in a few hours time. Thanks for the review!

@rich-iannone rich-iannone changed the title fix: ensure HTML ID attributes are unique fix!: ensure HTML ID attributes are unique Feb 20, 2025
Copy link
Member

@rich-iannone rich-iannone left a comment

Choose a reason for hiding this comment

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

LGTM!

@machow machow merged commit dc82197 into posit-dev:main Feb 21, 2025
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.

3 participants