Skip to content
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

Fix getStyleRangeAtPosition() bug #716

Merged
merged 2 commits into from
Mar 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ public S getStyleAtPosition(int position) {
* then the range preceding {@code position} is returned.
*/
public IndexRange getStyleRangeAtPosition(int position) {
Position pos = navigator.offsetToPosition(position, Backward);
Position pos = styles.offsetToPosition(position, Backward);
int start = position - pos.getMinor();
int end = start + styles.getStyleSpan(pos.getMajor()).getLength();
return new IndexRange(start, end);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.fxmisc.richtext.model;

import static org.junit.Assert.*;

import javafx.scene.control.IndexRange;
import org.junit.Test;

public class SimpleEditableStyledDocumentTest {
Expand Down Expand Up @@ -111,4 +113,24 @@ public void testSetNonEmptyParagraphStyle() {
document.setParagraphStyle(0, newParStyle);
assertEquals(newParStyle, document.getParagraphStyle(0));
}

@Test
public void testGetStyleRangeAtPosition() {
SimpleEditableStyledDocument<String, String> document = new SimpleEditableStyledDocument<>("", "");
String first = "some";
String second = " text";
replaceText(document, 0, 0, first + second);
document.setStyle(0, first.length(), "abc");

IndexRange range = document.getStyleRangeAtPosition(0);
IndexRange expected = new IndexRange(0, first.length());
assertEquals(expected, range);

range = document.getStyleRangeAtPosition(first.length());
assertEquals(expected, range);

range = document.getStyleRangeAtPosition(first.length() + 1);
expected = new IndexRange(first.length(), (first + second).length());
assertEquals(expected, range);
}
}