-
-
Notifications
You must be signed in to change notification settings - Fork 14
feat: support CSS3 break-after:page #61
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
base: release/1.1.4
Are you sure you want to change the base?
feat: support CSS3 break-after:page #61
Conversation
Co-authored-by: chrisonntag <5236298+chrisonntag@users.noreply.github.com>
Co-authored-by: chrisonntag <5236298+chrisonntag@users.noreply.github.com>
…itions Support CSS3 break-after:page alongside deprecated CSS2 page-break-after:always
| if 'style' in current_attrs: | ||
| style = current_attrs['style'] | ||
| # Match CSS2 page-break-after: always or CSS3 break-after: page | ||
| # Using regex to ensure we match the exact property-value pairs | ||
| # Also handles optional !important flag | ||
| if re.search(r'page-break-after\s*:\s*always\s*(!important)?\s*(;|$)', style) or \ | ||
| re.search(r'break-after\s*:\s*page\s*(!important)?\s*(;|$)', style): | ||
| self.doc.add_page_break() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First of all, thanks for you support and contribution!
I think it's a good idea move the regex pattern to the constants.py, compile it once and just call it on the h4d.py. This way, we avoid recompiling the patterns on every execution, and the logic becomes cleaner and easier to read. What do you think, do you agree?
constants.py
PAGE_BREAK_REGEXES = (
re.compile(r'page-break-after\s*:\s*always\s*(?:!important)?\s*(?:;|$)'),
re.compile(r'break-after\s*:\s*page\s*(?:!important)?\s*(?:;|$)'),
)
h4d.py
if 'style' in current_attrs:
style = current_attrs['style']
# Match CSS2 page-break-after: always or CSS3 break-after: page
# Using regex to ensure we match the exact property-value pairs
# Also handles optional !important flag
if style and any(regex.search(style) for regex in constants.PAGE_BREAK_REGEXES):
self.doc.add_page_break()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do, makes sense!
Description
The current page break handling on divs only recognized the deprecated
page-break-after: alwaysproperty. However, modern browsers and CSS generators usebreak-after: page.This extends the current logic by a more detailed regex to catch the deprecated
page-break-after, as well asbreak-afterand handle whitespace variations and!importantflags.Checklist Before Requesting a Review
all existingand new tests pass.Test issues
I've run all existing and new tests, but both on this branch and the main branch the following test failes: