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
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion great_tables/_scss.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!

else:
compiled_css = re.sub(r"^p \{", f"#{id} p " + "{", compiled_css, 0, re.MULTILINE)

finalized_css = f"{gt_table_class_str}\n\n{compiled_css}"

Expand Down
Loading