Skip to content

element/text: index link regions into the displayed text, not the markup - #158

Open
UncleJ4ck wants to merge 2 commits into
hyprwm:mainfrom
UncleJ4ck:fix-link-region-offsets
Open

UncleJ4ck wants to merge 2 commits into
hyprwm:mainfrom
UncleJ4ck:fix-link-region-offsets

Conversation

@UncleJ4ck

Copy link
Copy Markdown
Contributor

parseText records each link's begin/end as byte offsets into the raw marked-up string it assembles, but recheckTextBoxes later feeds those offsets to pango (pango_layout_index_to_pos), whose layout text is the markup-stripped output of pango_parse_markup. So any markup sitting before a link, including the <u><span foreground=...> wrapper the parser adds to every link, shifted the region off the actual glyphs. With more than one link the second one drifts the most.

Count displayed bytes (tags skipped, entities resolved) when recording begin/end so they index into the same text pango lays out. The region loop is now half-open since end became an exclusive offset.

Added two regression tests that strip parsedText with pango and assert the link's [begin, end) slice equals its displayed text. Before the change they resolve to link! Hi link2! and w; after, to link/link2/LINK. Full suite 92/92.

Fixes #127.

parseText recorded each link's begin/end as offsets into the raw marked-up
string, but recheckTextBoxes feeds those to pango, whose layout text is the
markup-stripped output. So any styling markup before a link, including the
link's own <u><span> wrapper, shifted its clickable region off the glyphs.
Count displayed bytes when recording begin/end, and make the region loop
half-open now that end is an exclusive offset.
Comment thread src/element/text/Text.cpp Outdated
// markup assembled here. counting displayed bytes keeps the region on the link's glyphs instead of
// drifting by the length of the styling tags in front of it. valid markup only: a broken fragment
// would already have failed the whole-string parse in setPangoText.
static size_t displayedLength(std::string_view s) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this is a hellish function that needs tests

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

added a test for it in 5d6db54. it checks every case against pango's own stripped text, and that caught a real bug: pango takes a raw > inside a quoted attribute value, and this ended the tag there. fixed that too.

pango takes a raw '>' inside a quoted attribute value, so ending the tag at the
first one miscounted everything after such a span. the new test checks each case
against pango's own stripped text, plus the unbalanced fragments parseText feeds it.
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.

Link parsing is based on raw text, not on the displayed text without markup

2 participants