Title: Bonsai: parse sheet CSS with a comment and string aware scanner by petruconduraru · Pull Request #8979 · IfcOpenShell/IfcOpenShell · GitHub
Open Graph Title: Bonsai: parse sheet CSS with a comment and string aware scanner by petruconduraru · Pull Request #8979 · IfcOpenShell/IfcOpenShell
X Title: Bonsai: parse sheet CSS with a comment and string aware scanner by petruconduraru · Pull Request #8979 · IfcOpenShell/IfcOpenShell
Description: Fixes #7312. @sl-x75 reported that generating a sheet with third party pattern CSS (his example: a hatch pattern library) produced broken output, and proposed a fix to ensure_drawing_unique_styles. @Moult's reply was the actual ask: We need something along these lines, but is there a safer way to do it than assumptions in parsing CSS this way? He is right to be suspicious. ensure_drawing_unique_styles scopes every CSS rule to its drawing by counting {/} characters one at a time over the raw CSS text. That breaks on: A brace inside a comment or a quoted string, e.g. content: "{". The counter desyncs and every rule after it silently loses its scoping prefix, so it leaks unscoped across the whole sheet. Concretely, before this PR: IN : '.foo::before { content: "{"; }\n.bar { fill: blue; }' OUT: '.foo::before.d42 { content: "{"; }\n.bar { fill: blue; }' Note .bar never gets .d42. An @media (...) prelude gets a bogus .prefix suffix appended to it: IN : '@media (min-width: 600px) { .foo { fill: red; } }' OUT: '@media (min-width: 600px).d42 { .foo { fill: red; } }' A comma inside a functional pseudo-class splits the selector in the wrong place: IN : ':is(.a, .b) { fill: red; }' OUT: ':is(.a.d42, .b).d42 { fill: red; }' This replaces the character-by-character brace counter with a small state machine (src/bonsai/bonsai/bim/module/drawing/css_scope.py) that treats /* ... */ comments and quoted strings as opaque so braces inside them do not desync anything, leaves at-rule preludes alone instead of corrupting them, and only splits selector lists on commas that are outside of ()/[]/quotes. After this change the three cases above become: '.foo::before.d42 { content: "{"; }.bar.d42 { fill: blue; }' '@media (min-width: 600px) { .foo { fill: red; } }' ':is(.a, .b).d42 { fill: red; }' (Nested rules inside an @media block are still not individually scoped, matching the previous behaviour; this is not a new regression.) For every currently working input I checked (multi-selector rules, multi-line rules, nth-of-type/attribute selectors, url() rewriting, and sl-x75's own sample CSS from the issue), the new output is byte for byte identical to the old output. On the dependency question: I looked at using tinycss2 (a real, small, pure-Python CSS parser, BSD-3-Clause, already a transitive dependency of cssselect2/CairoSVG elsewhere in the Python ecosystem but not currently something Bonsai bundles). It would fully close the remaining gap (scoping rules nested inside @media/@supports, full spec-correct tokenizing). I did not add it, since adding a dependency to Bonsai's bundled wheels (src/bonsai/Makefile) is a maintainer call. The comment/string/at-rule/paren-aware scanner above gets full correctness for the concrete cases reported and checked, without a new dependency, which seemed like the better default. @Moult, if you would rather depend on tinycss2 for full spec coverage (including the @media-nesting gap), say so and I will swap this out for it. The CSS-scoping logic is kept out of sheeter.py on purpose so it has no bpy/bonsai imports and can be exercised directly with plain python3 without a Blender environment, which is how I verified the before/after outputs above. Credit to @sl-x75 for identifying the real problem (styling from custom pattern SVGs colliding across a sheet) and proposing the first fix. Generated with the assistance of an AI coding tool. The whole diff (src/bonsai/bonsai/bim/module/drawing/css_scope.py and the corresponding change to sheeter.py) is AI-generated.
Open Graph Description: Fixes #7312. @sl-x75 reported that generating a sheet with third party pattern CSS (his example: a hatch pattern library) produced broken output, and proposed a fix to ensure_drawing_unique_styles....
X Description: Fixes #7312. @sl-x75 reported that generating a sheet with third party pattern CSS (his example: a hatch pattern library) produced broken output, and proposed a fix to ensure_drawing_unique_styles....
Opengraph URL: https://github.com/IfcOpenShell/IfcOpenShell/pull/8979
X: @github
Domain: github.com
IPv4 address: 140.82.113.3
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:031b19f6-868e-7998-75c6-98505a164eb3 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | EA50:C4ED3:26F95F:340BA2:6B04B635 |
| html-safe-nonce | 18b1bc14948e107d2b69ea93fb8cacecfd2bb78b7647aa3ab77b39611493edd9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQTUwOkM0RUQzOjI2Rjk1RjozNDBCQTI6NkIwNEI2MzUiLCJ2aXNpdG9yX2lkIjoiNzc1Njk4MzIwMDY0MzMzMTYzNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | fb2ba1d3d0f9be3f77efcb22ca2c726f8ca8f852bdbbaa9c329ec575e6d6330c |
| hovercard-subject-tag | pull_request:4132369384 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/IfcOpenShell/IfcOpenShell/pull/8979/files |
| twitter:image | https://avatars.githubusercontent.com/u/139251053?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/139251053?s=400&v=4 |
| og:image:alt | Fixes #7312. @sl-x75 reported that generating a sheet with third party pattern CSS (his example: a hatch pattern library) produced broken output, and proposed a fix to ensure_drawing_unique_styles.... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 378bd702ac792b3550e484ad39d9a898fcd25bb52ba6962b0c9cc457c1ea71c7 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/IfcOpenShell/IfcOpenShell git https://github.com/IfcOpenShell/IfcOpenShell.git |
| octolytics-dimension-user_id | 13692563 |
| octolytics-dimension-user_login | IfcOpenShell |
| octolytics-dimension-repository_id | 40473201 |
| octolytics-dimension-repository_nwo | IfcOpenShell/IfcOpenShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 40473201 |
| octolytics-dimension-repository_network_root_nwo | IfcOpenShell/IfcOpenShell |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | b032e77f1ea437ede6c115446a0a467709b468e7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | no-cache |
Viewport: width=device-width