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