Bug 196127 - Reproducible case of backwards nextParagraph returning a position ahead of the input position
Summary: Reproducible case of backwards nextParagraph returning a position ahead of th...
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: HTML Editing (show other bugs)
Version: Other
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Daniel Bates
URL:
Keywords: InRadar
Depends on: 205142
Blocks: 205284
  Show dependency treegraph
 
Reported: 2019-03-21 19:07 PDT by Tim Horton
Modified: 2019-12-16 14:32 PST (History)
6 users (show)

See Also:


Attachments
Bad repro (515 bytes, text/html)
2019-03-21 19:19 PDT, Ryosuke Niwa
no flags Details
Fixes the bug (18.69 KB, patch)
2019-03-27 20:48 PDT, Ryosuke Niwa
no flags Details | Formatted Diff | Diff
Patch and unit test (7.49 KB, patch)
2019-12-13 14:57 PST, Daniel Bates
no flags Details | Formatted Diff | Diff
Patch and unit test (7.38 KB, patch)
2019-12-16 10:20 PST, Daniel Bates
no flags Details | Formatted Diff | Diff
To land (7.22 KB, patch)
2019-12-16 14:29 PST, Daniel Bates
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Tim Horton 2019-03-21 19:07:48 PDT
Don't know how to write a standalone test for this yet, but:

<style>body { margin: 0; } iframe { border: none; }</style><meta name='viewport' content='initial-scale=1'><span id='the'>The</span> quick brown fox <span id='jumps'>jumps</span> over the lazy <span id='dog'>dog.</span>

And then select jumps:

getSelection().setBaseAndExtent(jumps, 0, jumps, 1)

If you take the selection start and then call positionOfNextBoundaryOfGranularity() with granularity=paragraph and direction=backwards, *twice*, you end up at the *end* of the document.
Comment 1 Ryosuke Niwa 2019-03-21 19:19:36 PDT
Created attachment 365668 [details]
Bad repro

I can't reproduce this... Maybe nextParagraphBoundaryInDirection behaves differently from modify.
Comment 2 Radar WebKit Bug Importer 2019-03-21 19:24:42 PDT
<rdar://problem/49135890>
Comment 3 Ryosuke Niwa 2019-03-21 19:24:45 PDT
We should look into this. A bug like this can result in an infinite loop elsewhere.
Comment 4 Tim Horton 2019-03-21 19:24:53 PDT
You should be able to reproduce with the API test in https://bugs.webkit.org/show_bug.cgi?id=196040 if you remove the silly check in moveByGranularity...
Comment 5 Ryosuke Niwa 2019-03-27 20:48:55 PDT
Created attachment 366146 [details]
Fixes the bug
Comment 6 Ryosuke Niwa 2019-03-27 20:50:53 PDT
Comment on attachment 366146 [details]
Fixes the bug

Wrong bug :(
Comment 7 Tim Horton 2019-03-27 22:27:30 PDT
Aww, you had me excited
Comment 8 Daniel Bates 2019-12-13 14:48:48 PST
Hit this bug... I'm going to take a stab.
Comment 9 Daniel Bates 2019-12-13 14:51:55 PST
The issue is that WebCore::nextParagraphBoundaryInDirection() does actually handle the case where the specified position is not within a paragraph. It handle that case as if the position was on a paragraph boundary. The latter needs to be handled as well.
Comment 10 Daniel Bates 2019-12-13 14:57:41 PST
Created attachment 385645 [details]
Patch and unit test
Comment 11 Daniel Bates 2019-12-13 14:58:36 PST
Kinda wavering on the changes here...trying to figure out what's the most readable code. Left some duplication on the table to try to get something more readable. Maybe solution should be DRY-er.
Comment 12 Daniel Bates 2019-12-13 14:59:03 PST
Patch will not apply until bug #205142 lands.
Comment 13 Daniel Bates 2019-12-14 08:09:16 PST
Comment on attachment 385645 [details]
Patch and unit test

View in context: https://bugs.webkit.org/attachment.cgi?id=385645&action=review

> Tools/TestWebKitAPI/Tests/WebKitCocoa/DocumentEditingContext.mm:631
> +    [webView synchronouslyLoadHTMLString:applyAhemStyle(@"<style>body { margin: 0; } iframe { border: none; }</style><meta name='viewport' content='initial-scale=1'><span id='the'>The</span> quick brown fox <span id='jumps'>jumps</span> over the lazy <span id='dog'>dog.</span>")];

Remove the <style> as  applyAhemStyle() adds it for us.
Comment 14 Daniel Bates 2019-12-16 10:20:41 PST
Created attachment 385779 [details]
Patch and unit test
Comment 15 Wenson Hsieh 2019-12-16 14:05:22 PST
Comment on attachment 385779 [details]
Patch and unit test

View in context: https://bugs.webkit.org/attachment.cgi?id=385779&action=review

> Source/WebCore/ChangeLog:16
> +                        or backwards, respectively.

I think it would help to clarify what causes the last (third) scenario here — i.e. the last of the 3 return statements in nextParagraphBoundaryInDirection.
Comment 16 Daniel Bates 2019-12-16 14:10:30 PST
Comment on attachment 385779 [details]
Patch and unit test

View in context: https://bugs.webkit.org/attachment.cgi?id=385779&action=review

> Tools/TestWebKitAPI/Tests/WebKitCocoa/DocumentEditingContext.mm:628
> +TEST(WebKit, DocumentEditingContextRequestLastTwoParagraphsWithCaretNotInParagraph)

No caret in this test. Will rename to DocumentEditingContextRequestLastTwoParagraphsWithSelectiontNotInAParagraph
Comment 17 Daniel Bates 2019-12-16 14:28:08 PST
(In reply to Wenson Hsieh from comment #15)
> Comment on attachment 385779 [details]
> Patch and unit test
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=385779&action=review
> 
> > Source/WebCore/ChangeLog:16
> > +                        or backwards, respectively.
> 
> I think it would help to clarify what causes the last (third) scenario here
> — i.e. the last of the 3 return statements in
> nextParagraphBoundaryInDirection.

Nothing! There are only two cases: at boundary of paragraph and in paragraph. Somehow I wound up with a third case.
Comment 18 Wenson Hsieh 2019-12-16 14:29:25 PST
Comment on attachment 385779 [details]
Patch and unit test

View in context: https://bugs.webkit.org/attachment.cgi?id=385779&action=review

>>> Source/WebCore/ChangeLog:16
>>> +                        or backwards, respectively.
>> 
>> I think it would help to clarify what causes the last (third) scenario here — i.e. the last of the 3 return statements in nextParagraphBoundaryInDirection.
> 
> Nothing! There are only two cases: at boundary of paragraph and in paragraph. Somehow I wound up with a third case.

Sounds like we can simplify the logic a bit, then!
Comment 19 Daniel Bates 2019-12-16 14:29:42 PST
Created attachment 385811 [details]
To land
Comment 20 Daniel Bates 2019-12-16 14:32:15 PST
Comment on attachment 385811 [details]
To land

Clearing flags on attachment: 385811

Committed r253578: <https://trac.webkit.org/changeset/253578>
Comment 21 Daniel Bates 2019-12-16 14:32:17 PST
All reviewed patches have been landed.  Closing bug.