Skip to content

Conversation

@petschki
Copy link
Member

fixes #4224

@mister-roboto
Copy link

@petschki thanks for creating this Pull Request and helping to improve Plone!

TL;DR: Finish pushing changes, pass all other checks, then paste a comment:

@jenkins-plone-org please run jobs

To ensure that these changes do not break other parts of Plone, the Plone test suite matrix needs to pass, but it takes 30-60 min. Other CI checks are usually much faster and the Plone Jenkins resources are limited, so when done pushing changes and all other checks pass either start all Jenkins PR jobs yourself, or simply add the comment above in this PR to start all the jobs automatically.

Happy hacking!

@petschki petschki requested a review from lyralemos September 24, 2025 10:37
@petschki
Copy link
Member Author

@jenkins-plone-org please run jobs

@petschki petschki requested a review from jensens September 24, 2025 11:26
@jensens
Copy link
Member

jensens commented Sep 26, 2025

IIRC correctly, IDs were stripped to avoid duplicated IDs in DOM, which lead to failures in W3C site validation.

@yurj
Copy link
Contributor

yurj commented Sep 26, 2025

Can't you just can replace/prepend them with a random string?

@petschki
Copy link
Member Author

Yeah that also came into my mind. Let me try something this weekend.

@petschki petschki changed the title Remove svg modifier "strip_id" [6.1] Make referencing IDs in inline rendered SVGs unique Oct 9, 2025
@petschki
Copy link
Member Author

petschki commented Oct 9, 2025

I've refactored this to make the referencing IDs unique. I use a hash of time.time() so that when the same SVG is placed multiple times on a page every instance has its unique ID ... I've tested this with the countryflags, because they have many references inside and are placed inline in the languageselector.

I've also fixed the aria-labelledby attribute, when you provide a tag_alt parameter. This needs to be an id of the generated <title> tag. This came up in an accessibility check ...

let me know what you think.

@petschki
Copy link
Member Author

petschki commented Oct 9, 2025

@jenkins-plone-org please run jobs

@petschki
Copy link
Member Author

petschki commented Oct 9, 2025

Also worth noting: the @@iconresolver.tag factory could need some caching, because this is called every time when rendering a page ... I've locally tried to decorate it with @view.memoize_contextless but that didn't work ...

del el.attrib["id"]
def _unique_id(svgtree, cfg):
# for <use (xlink:)href="#someid" /> references to work
# the ids in the svg must be unique in the document
Copy link
Member

Choose a reason for hiding this comment

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

How do you actually use this svg in a <use /> reference if its id includes an unpredictable hash?

Copy link
Member Author

Choose a reason for hiding this comment

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

well, the idea was to:

  1. create a unique hash on each tag() call
  2. replace all id="xyz" with id="xyz-<hash>" inside the SVG
  3. replace all referencing #xyz occurrences with #xyz-<hash> inside the SVG node attributes

while this worked in my local tests it seems, that there are breaking CI tests, because calling the same template with a delay isn't equal anymore (see https://github.com/plone/Products.CMFPlone/blob/6.1.x/Products/CMFPlone/tests/testBrowserDefault.py#L67)

so, to make this hash more predictable, we could create a hash from the svg filename including a counter of the occurrence on the page.

modifier_cfg = {
"cssclass": tag_class,
"title": tag_alt,
"hash": hashlib.md5(str(time.time()).encode()).hexdigest(),
Copy link
Member

Choose a reason for hiding this comment

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

time.time() is not guaranteed to have resolution higher than 1 second on all platforms (https://docs.python.org/3/library/time.html#time.time)

I recommend using time.perf_counter() instead -- but see my other comment

Copy link
Member

Choose a reason for hiding this comment

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

why not an UUID4?

@petschki petschki force-pushed the svg-remove-stripid-svgmodifier-6.1.x branch from 713afa3 to 33e9d3a Compare October 9, 2025 21:15
@petschki
Copy link
Member Author

petschki commented Oct 9, 2025

@jenkins-plone-org please run jobs

@petschki petschki marked this pull request as draft October 9, 2025 22:36
@petschki
Copy link
Member Author

petschki commented Oct 9, 2025

@jenkins-plone-org please run jobs

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.

6 participants