1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html.impl;
16
17 import org.htmlunit.html.DomNode;
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public class SelectableTextSelectionDelegate implements SelectionDelegate {
32
33
34 private final SelectableTextInput element_;
35
36
37 private final SimpleRange selection_;
38
39
40
41
42
43 public SelectableTextSelectionDelegate(final SelectableTextInput element) {
44 element_ = element;
45 selection_ = new SimpleRange((DomNode) element, 0);
46 }
47
48
49
50
51 public void select() {
52 element_.focus();
53 setSelectionStart(0);
54 setSelectionEnd(element_.getText().length());
55 }
56
57
58
59
60
61 public String getSelectedText() {
62 return selection_.toString();
63 }
64
65
66
67
68 @Override
69 public int getSelectionStart() {
70 return selection_.getStartOffset();
71 }
72
73
74
75
76 @Override
77 public void setSelectionStart(int selectionStart) {
78 if (selectionStart < 0) {
79 return;
80 }
81
82 final int length = element_.getText().length();
83 selectionStart = Math.max(0, Math.min(selectionStart, length));
84 selection_.setStart((DomNode) element_, selectionStart);
85 if (selection_.getEndOffset() < selectionStart) {
86 selection_.setEnd((DomNode) element_, selectionStart);
87 }
88 }
89
90
91
92
93 @Override
94 public int getSelectionEnd() {
95 return selection_.getEndOffset();
96 }
97
98
99
100
101 @Override
102 public void setSelectionEnd(int selectionEnd) {
103 final int length = element_.getText().length();
104 selectionEnd = Math.min(length, Math.max(selectionEnd, 0));
105 selection_.setEnd((DomNode) element_, selectionEnd);
106 if (selection_.getStartOffset() > selectionEnd) {
107 selection_.setStart((DomNode) element_, selectionEnd);
108 }
109 }
110 }