1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import java.io.PrintWriter;
18
19 import org.htmlunit.SgmlPage;
20 import org.htmlunit.html.impl.SelectionDelegate;
21 import org.htmlunit.html.impl.SimpleSelectionDelegate;
22 import org.htmlunit.util.StringUtils;
23 import org.w3c.dom.DOMException;
24 import org.w3c.dom.Text;
25
26
27
28
29
30
31
32
33
34
35
36
37 public class DomText extends DomCharacterData implements Text {
38
39 private SelectionDelegate selectionDelegate_;
40 private DoTypeProcessor doTypeProcessor_;
41
42
43 public static final String NODE_NAME = "#text";
44
45
46
47
48
49
50
51 public DomText(final SgmlPage page, final String data) {
52 super(page, data);
53 }
54
55
56
57
58 @Override
59 public DomText splitText(final int offset) {
60 if (offset < 0 || offset > getLength()) {
61 throw new IllegalArgumentException("offset: " + offset + " data.length: " + getLength());
62 }
63
64
65 final DomText newText = createSplitTextNode(offset);
66 setData(getData().substring(0, offset));
67
68
69 if (getParentNode() != null) {
70 getParentNode().insertBefore(newText, getNextSibling());
71 }
72 return newText;
73 }
74
75
76
77
78
79
80
81
82 protected DomText createSplitTextNode(final int offset) {
83 return new DomText(getPage(), getData().substring(offset));
84 }
85
86
87
88
89
90 @Override
91 public boolean isElementContentWhitespace() {
92 throw new UnsupportedOperationException("DomText.isElementContentWhitespace is not yet implemented.");
93 }
94
95
96
97
98 @Override
99 public String getWholeText() {
100
101
102 return getNodeValue();
103 }
104
105
106
107
108
109 @Override
110 public Text replaceWholeText(final String content) throws DOMException {
111 throw new UnsupportedOperationException("DomText.replaceWholeText is not yet implemented.");
112 }
113
114
115
116
117 @Override
118 public short getNodeType() {
119 return org.w3c.dom.Node.TEXT_NODE;
120 }
121
122
123
124
125 @Override
126 public String getNodeName() {
127 return NODE_NAME;
128 }
129
130
131
132
133
134
135
136 @Override
137 protected void printXml(final String indent, final PrintWriter printWriter) {
138 String data = getData();
139 if (org.apache.commons.lang3.StringUtils.isNotBlank(data)) {
140 printWriter.print(indent);
141 if (!(getParentNode() instanceof HtmlStyle) || !data.startsWith("<!--") || !data.endsWith("-->")) {
142 data = StringUtils.escapeXmlChars(data);
143 }
144 printWriter.print(data);
145 printWriter.print("\r\n");
146 }
147 printChildrenAsXml(indent, printWriter);
148 }
149
150
151
152
153
154 @Override
155 public String toString() {
156 return asNormalizedText();
157 }
158
159
160
161
162
163
164
165 protected void doType(final char c, final HtmlElement htmlElement, final boolean lastType) {
166 initDoTypeProcessor();
167 doTypeProcessor_.doType(getData(), selectionDelegate_, c, htmlElement, lastType);
168 }
169
170
171
172
173
174
175
176
177 protected void doType(final int keyCode, final HtmlElement htmlElement, final boolean lastType) {
178 initDoTypeProcessor();
179 doTypeProcessor_.doType(getData(), selectionDelegate_, keyCode, htmlElement, lastType);
180 }
181
182 private void initDoTypeProcessor() {
183 if (selectionDelegate_ == null) {
184 selectionDelegate_ = new SimpleSelectionDelegate();
185 doTypeProcessor_ = new DoTypeProcessor(this);
186 }
187 }
188
189
190
191
192
193
194 protected boolean acceptChar(final char c) {
195
196
197 return (c < '\uE000' || c > '\uF8FF') && (c == ' ' || !Character.isWhitespace(c));
198 }
199
200
201
202
203 @Override
204 public DomNode cloneNode(final boolean deep) {
205 final DomText newnode = (DomText) super.cloneNode(deep);
206 selectionDelegate_ = new SimpleSelectionDelegate();
207 doTypeProcessor_ = new DoTypeProcessor(this);
208
209 return newnode;
210 }
211
212
213
214
215 public void moveSelectionToEnd() {
216 initDoTypeProcessor();
217 selectionDelegate_.setSelectionStart(getData().length());
218 }
219
220
221
222
223 @Override
224 public void setPrefix(final String prefix) {
225
226 }
227 }