fix(rich-text-widget): match most specific style when tags overlap#5507
fix(rich-text-widget): match most specific style when tags overlap#5507mmustafasenoglu wants to merge 1 commit into
Conversation
|
I don't think I'm able to reproduce the issue with the current version of Apostrophe. I think it was fixed in 4.20.0, but the related issue wasn't closed. I might be testing incorrectly, so if you can provide a test scenario where the bug replicates, I'd be happy to review your PR. |
|
Hi @BoDonkey, thanks for looking into this. The bug only manifests when you have multiple styles sharing the same HTML tag with different classes. Here is a minimal reproduction: 1. Configure the rich text widget with overlapping styles: // In your module that registers the rich-text-widget options:
styles: [
{ tag: 'p', label: 'Paragraph (P)', def: true },
{ tag: 'p', label: 'Small (P)', class: 'small' },
{ tag: 'h2', label: 'Heading 2 (H2)' },
]2. Steps to reproduce:
3. Why it happens: The fix (already in this PR) sorts styles by specificity (number of classes) so the most specific style matches first, then maps back to the original dropdown index. This follows boutell's recommended approach in #3622. To test in the Apostrophe admin UI:
Let me know if you need any additional information! |
What does this PR do?
Fixes #3622 — the rich text style picker marks the wrong style as active when multiple styles share the same tag (e.g. a plain
<p>and a<p class="small">).Why
The
activecomputed inAposTiptapStyles.vueusednodes.findIndex(...), which returns the first match in array order. So a<p class="small">matched the generic<p>style first, leaving "Paragraph" highlighted instead of "Small" — making the style impossible to undo without first selecting a different tag.Following maintainer guidance in the issue, styles are now matched in descending order of specificity (number of classes). The most specific style wins the first match, then it is resolved back to its original index so the dropdown (which keeps the original order) highlights the correct option.
How to test
Configure a rich text widget with overlapping tags, e.g.:
Apply "Small (P)" — the picker now correctly shows "Small (P)" as the active style, and selecting "Paragraph (P)" reverts it as expected.
Checklist
CONTRIBUTINGdocument..changeset/rich-text-style-selection-bug.md).