Skip to content

Cut inefficient CssClassSet out of content parsing #505

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

Merged
merged 8 commits into from
Feb 6, 2024

Conversation

gnprice
Copy link
Member

@gnprice gnprice commented Feb 5, 2024

Fixes: #497

The first two commits here are by @chrisbobbe, taken from #503. The later commits go in the same direction for checking a class list is empty, and then handle the remaining couple of places that are a bit trickier, applying regexps to className to cover the different possibilities.

chrisbobbe and others added 8 commits February 5, 2024 15:25
It turns out that `classes.contains` instantiates a Set every time
it's called. That's unfortunate.

So, remove some of this work by just checking the class string
(`className`) directly, in the case where we expect just one class.
When we expect two or more classes, we'll want a check that doesn't
enforce a particular order, so perhaps we'll use a regular
expression for those, or something, in later work. See zulip#497.

Fixes-partly: zulip#497
This className string can never be empty, because it's an item from
a CssClassSet, and those consist of nonempty strings.  (If for
example the element's "class" attribute is absent, or empty, or
pure whitespace, then the resulting class set still has no empty
strings; it'll just be an empty set.)
This is NFC except in the case where className looks like "  foo  ",
with leading and/or trailing whitespace.  In that case, the previous
code would accept it (if the class name in the middle was one we
recognized), and this code will reject as unimplemented.

For cases where the previous code would reject because `classes.length`
was not 1, this code will reject because className won't be one that
codeBlockSpanTypeFromClassName recognizes.
This is more efficient but should otherwise behave exactly the same,
except if the className has extraneous whitespace: the previous code
would accept that, and this code will reject as unimplemented.

To help verify that we've gotten this change right, add some more
tests, so that we cover all the different alternatives in this regexp.
We never look at the captured group, so it's more efficient to
not ask the regexp engine to capture it in the first place.
And add another test for more complete coverage.

This removes our last use of the `classes` class set,
so it completes zulip#497.

Fixes: zulip#497
@chrisbobbe
Copy link
Collaborator

chrisbobbe commented Feb 6, 2024

Thanks, LGTM! Merging.

@chrisbobbe chrisbobbe merged commit 6956d5d into zulip:main Feb 6, 2024
@gnprice gnprice deleted the pr-classname branch February 6, 2024 03:24
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.

content model: Avoid classes.contains, which does extra work like creating a Set
2 participants