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
Change Offset_to_text_point function and add unit tests #19946
Conversation
Thanks for the pull request, and welcome! The Servo team is excited to review your changes, and you should hear from @emilio (or someone else) soon. |
Heads up! This PR modifies the following files:
|
If you change the testcase in #18613 to use any other value than 1, does the assertion still fail? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can try to take a closer look, but also maybe @mbrubeck and @jonleighton can take a look.
components/script/textinput.rs
Outdated
@@ -276,7 +276,7 @@ impl<T: ClipboardProvider> TextInput<T> { | |||
} | |||
|
|||
debug_assert!(self.edit_point.line < self.lines.len()); | |||
debug_assert!(self.edit_point.index <= self.lines[self.edit_point.line].len()); | |||
debug_assert!(self.edit_point.index <= max(1, self.lines[self.edit_point.line].len())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to take a look at what's going on on the test-case exactly, but I suspect this is wrong.
In particular, this should be self.lines[self.edit_point.line].len() + 1
instead, but in any case, I still suspect it's the wrong solution, and the edit_point
should end up being 1, 0
instead of 0, 1
in this case, since we have two lines in the test-case.
components/script/textinput.rs
Outdated
@@ -276,7 +276,7 @@ impl<T: ClipboardProvider> TextInput<T> { | |||
} | |||
|
|||
debug_assert!(self.edit_point.line < self.lines.len()); | |||
debug_assert!(self.edit_point.index <= self.lines[self.edit_point.line].len()); | |||
debug_assert!(self.edit_point.index <= max(1, self.lines[self.edit_point.line].len())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to take a look at what's going on on the test-case exactly, but I suspect this is wrong.
In particular, this should be self.lines[self.edit_point.line].len() + 1
instead, but in any case, I still suspect it's the wrong solution, and the edit_point
should end up being 1, 0
instead of 0, 1
in this case, since we have two lines in the test-case.
So the issue is that with two empty lines ( |
@jdm Yes it fails then also. |
@emilio So should we alter the offset_to_text_point function instead? |
Yes, I think so... |
In particular I think that what offset_to_text_point wants to do is something like this: for line_content in self.lines.iter() {
// If the index fits on the line, we're done.
if index <= line_content.len() {
break;
}
// else advance a line.
index -= line_content.len();
line += 1;
if index > 0 {
index -= 1; // Account for the \n
}
} But I'm not sure that's 100% right, it may need to go through tests and all that :) |
It would be great to add additional unit test(s) to cover this case I think. (The relevant file is @emilio: I'm happy to try to review this if you like. |
Note: unit tests can be run with |
@bors-servo try |
Change Offset_to_text_point function and add unit tests <!-- Please describe your changes on the following line: --> --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [x] These changes fix #(#18613). <!-- Either: --> - [x] These changes do not require tests because they solve an issue related to a already existing code <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/19946) <!-- Reviewable:end -->
☀️ Test successful - android, arm32, arm64, linux-dev, linux-rel-css, linux-rel-wpt, mac-dev-unit, mac-rel-css1, mac-rel-css2, mac-rel-wpt1, mac-rel-wpt2, mac-rel-wpt3, mac-rel-wpt4, windows-msvc-dev |
r? @emilio |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thanks!
if abs_point > new_acc && index > line_end { | ||
let line_end = val.len(); | ||
let new_acc = acc + line_end + 1; | ||
if abs_point >= new_acc && index > line_end { | ||
index -= line_end + 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think this function is way harder to understand than what it should, but I'll file a followup after this lands to do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah! Actually changing the function altogether was creating some new issues. So i preferred changing it this way only.
@@ -600,6 +600,13 @@ fn test_textinput_set_selection_with_direction() { | |||
assert!(textinput.selection_origin.is_some()); | |||
assert_eq!(textinput.selection_origin.unwrap().line, 0); | |||
assert_eq!(textinput.selection_origin.unwrap().index, 6); | |||
|
|||
textinput = text_input(Lines::Multiple, "\n\n"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's test also with only \n
, which is the case we're concerned about. Also, let's add a crashtest for this based on @mateon1's test-case, into tests/wpt/web-platform-tests/selection/
. See https://github.com/servo/servo/blob/master/tests/wpt/README.md#writing-new-tests.
The test should call setSelectionRange, but also that the selectionStart
and selectionEnd
values agree with other browsers, that is, something like:
<!doctype html>
<textarea>
</textarea>
<script>
test(function() {
let textarea = document.querySelector('textarea');
assert_equals(textarea.selectionStart, 0);
assert_equals(textarea.selectionEnd, 0);
textarea.setSelectionRange(0, 1);
assert_equals(textarea.selectionStart, 0);
assert_equals(textarea.selectionEnd, 1);
}, "setSelectionRange on line boundaries");
</script>
Opened new PR for upstreamable changes. Completed upstream sync of web-platform-test changes at jdm/web-platform-tests#23. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the unit test you wrote seems to be failing on travis:
thread 'textinput::test_textinput_set_selection_with_direction' panicked at 'assertion failed: `(left == right)`
left: `1`,
right: `0`', tests/unit/script/textinput.rs:616:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
stack backtrace:
0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
at libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
1: std::sys_common::backtrace::print
at libstd/sys_common/backtrace.rs:68
at libstd/sys_common/backtrace.rs:57
2: std::panicking::default_hook::{{closure}}
at libstd/panicking.rs:380
3: std::panicking::default_hook
at libstd/panicking.rs:390
4: std::panicking::rust_panic_with_hook
at libstd/panicking.rs:576
5: std::panicking::begin_panic
at libstd/panicking.rs:537
6: std::panicking::begin_panic_fmt
at libstd/panicking.rs:521
7: script_tests::textinput::test_textinput_set_selection_with_direction
at tests/unit/script/textinput.rs:616
8: <F as alloc::boxed::FnBox<A>>::call_box
at libtest/lib.rs:1441
at /checkout/src/libcore/ops/function.rs:223
at /checkout/src/liballoc/boxed.rs:788
9: __rust_maybe_catch_panic
at libpanic_unwind/lib.rs:102
Additionally, you need to make sure the manifest is up-to-date running ./mach update-manifest
.
tests/unit/script/textinput.rs
Outdated
|
||
textinput = text_input(Lines::Multiple, "\n"); | ||
textinput.set_selection_range(0, 1, SelectionDirection::Forward); | ||
assert_eq!(textinput.edit_point.line, 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I suspect this should be 1...
Transplanted upstreamable changes to existing PR. Completed upstream sync of web-platform-test changes at jdm/web-platform-tests#23. |
1 similar comment
Transplanted upstreamable changes to existing PR. Completed upstream sync of web-platform-test changes at jdm/web-platform-tests#23. |
0c2b774
to
7b58fb5
Compare
Transplanted upstreamable changes to existing PR. Completed upstream sync of web-platform-test changes at jdm/web-platform-tests#23. |
@bors-servo r+ |
📌 Commit 7b58fb5 has been approved by |
Change Offset_to_text_point function and add unit tests <!-- Please describe your changes on the following line: --> --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [x] These changes fix #18613. <!-- Either: --> - [x] Added unit tests and web platform tests <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/19946) <!-- Reviewable:end -->
☀️ Test successful - android, arm32, arm64, linux-dev, linux-rel-css, linux-rel-wpt, mac-dev-unit, mac-rel-css1, mac-rel-css2, mac-rel-wpt1, mac-rel-wpt2, mac-rel-wpt3, mac-rel-wpt4, windows-msvc-dev |
Upstreamed from servo/servo#19946 [ci skip]
./mach build -d
does not report any errors./mach test-tidy
does not report any errorsThis change is