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

Ensure that ID value in compiled CSS is applied to all rules #92

Merged
merged 3 commits into from
Dec 15, 2023

Conversation

rich-iannone
Copy link
Member

This fixes a re.sub() statement in the compile_scss() function so that the table ID is inserted before the rule for the p element. This corrects a regression where the compiled CSS no longer worked with one of the re.sub() statements. With this fix, the rule for the table's <p> element is specific to the table (and doesn't affect the page itself).

Copy link
Collaborator

@machow machow left a comment

Choose a reason for hiding this comment

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

I left a suggestion for condensing into a single regex, but feel free to merge if you'd rather keep as is!

@@ -138,7 +138,10 @@ def compile_scss(data: GTData, id: Optional[str], compress: bool = True) -> str:
if has_id:
compiled_css = re.sub(r"\.gt_", f"#{id} .gt_", compiled_css, 0, re.MULTILINE)
compiled_css = re.sub(r"thead", f"#{id} thead", compiled_css, 0, re.MULTILINE)
compiled_css = re.sub(r"^p \{", f"#{id} p " + "{", compiled_css, 0, re.MULTILINE)
if compress:
compiled_css = re.sub(r"^ p \{", f" #{id} p " + "{", compiled_css, 0, re.MULTILINE)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if this could be condensed into a single regex?

 re.sub(r"^( p|p) \{", f" #{id} p {{", compiled_css, 0, re.MULTILINE)

The main difference is using ^( p|p) to capture that the difference is whether people has a space or not.

Here's how I tested:

f = lambda x, id="ABC":  re.sub(r"^( p|p) \{", f" #{id} p {{", x, 0, re.MULTILINE)

# substitutes
f(" p {abc")
f("p {abc")

# no substitute (good!)
f("group {abc")

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, that is totally good. I'll make the change!

@machow
Copy link
Collaborator

machow commented Dec 15, 2023

I'm surprised this didn't fail / require regenerating any of the snapshot tests. (I guess maybe it's because the css one used condensed = False?).

@rich-iannone
Copy link
Member Author

^^^ that's right. The snapshot test is only for that case.

@rich-iannone rich-iannone requested a review from machow December 15, 2023 21:02
@rich-iannone rich-iannone merged commit 4817e12 into main Dec 15, 2023
6 checks passed
@rich-iannone rich-iannone deleted the css-fix-p-element branch December 15, 2023 22:19
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