Bug 158198 - My macro changes the bookmark text and the bookmark disappears
Summary: My macro changes the bookmark text and the bookmark disappears
Status: RESOLVED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Writer (show other bugs)
Version:
(earliest affected)
7.5.4.2 release
Hardware: x86-64 (AMD64) All
: medium normal
Assignee: Michael Stahl (allotropia)
URL:
Whiteboard: target:25.8.0 target:25.2.1 target:24...
Keywords: bibisectNotNeeded, regression
Depends on:
Blocks: Bookmarks
  Show dependency treegraph
 
Reported: 2023-11-13 15:25 UTC by JBrown
Modified: 2025-02-11 21:28 UTC (History)
5 users (show)

See Also:
Crash report or crash signature:


Attachments
Document showing the disappearance of a bookmark (12.92 KB, application/vnd.oasis.opendocument.text)
2023-11-13 15:29 UTC, JBrown
Details
add-text-before-bookmark (37.55 KB, image/png)
2023-11-16 18:06 UTC, ToanTran
Details
Reproducible-MacOS (12.39 MB, video/quicktime)
2023-11-16 18:07 UTC, ToanTran
Details

Note You need to log in before you can comment on or make changes to this bug.
Description JBrown 2023-11-13 15:25:06 UTC
Description:
I have a document with bookmarks. Each bookmark has text. There is a macro that makes the bookmark text empty. After running the macro, some bookmarks disappear. Those bookmarks that consist of one paragraph completely disappear.

Steps to Reproduce:
1.Open document 'whereIsMyBookmark'
2.Look at the bookmarks (Insert->Bookmark)
3.Run macro 'Main'
4.Look at the bookmarks again

Actual Results:
Not only the bookmark text disappears, but also the bookmark itself

Expected Results:
The bookmark should not disappear


Reproducible: Always


User Profile Reset: No

Additional Info:
1. A bookmark whose text consists of a paragraph disappears.
2. A bookmark whose text consists of a word does not disappear. But a bookmark created immediately without text and one created by a macro look different.
Comment 1 JBrown 2023-11-13 15:29:34 UTC
Created attachment 190812 [details]
Document showing the disappearance of a bookmark
Comment 2 JBrown 2023-11-13 15:34:03 UTC
In the previous version it works as it should:

Version: 7.4.2.3 / LibreOffice Community
Build ID: 40(Build:3)
CPU threads: 4; OS: FreeBSD 13.1; UI render: default; VCL: qt5 (cairo+xcb)
Locale: en-US (en_EN.UTF-8); UI: en-US
Calc: threaded
Comment 3 ToanTran 2023-11-16 18:06:00 UTC
I think I can reproduce it on my MacOS as well. 

However, if I add more text before the {PARAGRAPH} bookmark, all the bookmarks remain.

Can you confirm @JBrown?
Comment 4 ToanTran 2023-11-16 18:06:37 UTC
Created attachment 190879 [details]
add-text-before-bookmark
Comment 5 ToanTran 2023-11-16 18:07:22 UTC
Created attachment 190880 [details]
Reproducible-MacOS
Comment 6 Mike Kaganski 2023-11-19 17:34:49 UTC
(In reply to ToanTran from comment #3)

That is exactly what JBrown reported, isn't it? Are you asking JBrown to confirm comment 0?
Comment 7 Michael Stahl (allotropia) 2023-11-22 18:44:02 UTC
thinking the basic problem here is that the API only has this "setString" method, but no way to indicate whether the intent is to *delete* or to *replace* text.

these are different operations: deleting means that things like objects anchored in the deletion range and bookmarks should be deleted too, while replacing means that the current text should be deleted but the anchored objects should remain.

so the best idea i had is that setString("") would be interpreted as *delete* and any other setString call as *replace*.

the behavior when selecting an entire paragraph was changed in commit baf8d2c1c16cb3bdc4edad2560f95fea807a034f because in this situation it was actually quite difficult to get rid of the bookmark if the user does want to delete it, as no selection that is larger than the bookmark is possible inside the paragraph (and there may be no preceding or following paragraph).

so i'm going to call the new behavior a feature not a bug, but of course i'm biased...
Comment 8 raal 2023-11-26 08:54:36 UTC
(In reply to Michael Stahl (allotropia) from comment #7)
> 
> the behavior when selecting an entire paragraph was changed in commit
> baf8d2c1c16cb3bdc4edad2560f95fea807a034f 

=> bibisectNotNeeded
Comment 9 Commit Notification 2025-01-31 12:55:31 UTC
Michael Stahl committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/b0d4b4664a295631ce4d8dee2ceb5cd94ae12edb

tdf#158198 sw: prevent xBookmark.getAnchor().setString("") from deleting

It will be available in 25.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 10 Michael Stahl (allotropia) 2025-01-31 12:57:45 UTC
a new idea occurred to me, we can special case the anchor of a bookmark so that calling setString() on it will replace rather than delete the text, then the bookmark survives that.

of course this only works on the XTextRange that's returned from getAnchor()

fixed on master
Comment 11 Commit Notification 2025-02-01 15:42:51 UTC
Michael Stahl committed a patch related to this issue.
It has been pushed to "libreoffice-25-2":

https://git.libreoffice.org/core/commit/aca6f30c6769ad5573724ffa3a42a595cb20b7fb

tdf#158198 sw: prevent xBookmark.getAnchor().setString("") from deleting

It will be available in 25.2.1.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 12 Commit Notification 2025-02-01 15:43:54 UTC
Michael Stahl committed a patch related to this issue.
It has been pushed to "libreoffice-24-8":

https://git.libreoffice.org/core/commit/fe5635c78557ef9f51e3bac1d782cf1c8555655d

tdf#158198 sw: prevent xBookmark.getAnchor().setString("") from deleting

It will be available in 24.8.6.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 13 Commit Notification 2025-02-05 12:12:03 UTC
Xisco Fauli committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/6a680ef017e5a842113f4eb4b555acb51e921f20

tdf#158198: sw_uiwriter6: Add unittest

It will be available in 25.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 14 Commit Notification 2025-02-11 21:28:12 UTC
Michael Stahl committed a patch related to this issue.
It has been pushed to "libreoffice-24-8-5":

https://git.libreoffice.org/core/commit/2273885b0785b91546962925e5ec44326eb302de

tdf#158198 sw: prevent xBookmark.getAnchor().setString("") from deleting

It will be available in 24.8.5.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.