Skip to content

Commit e92cbd7

Browse files
committed
cleanup and small optimization
1 parent b8fd157 commit e92cbd7

4 files changed

Lines changed: 76 additions & 80 deletions

File tree

src/main/java/org/htmlunit/html/DomAttr.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ public class DomAttr extends DomNamespaceNode implements Attr {
4545
public DomAttr(final SgmlPage page, final String namespaceURI, final String qualifiedName, final String value,
4646
final boolean specified) {
4747
super(namespaceURI, qualifiedName, page);
48+
value_ = normalizeValue(value);
49+
specified_ = specified;
50+
}
4851

52+
private static String normalizeValue(final String value) {
4953
if (value != null && value.isEmpty()) {
50-
value_ = DomElement.ATTRIBUTE_VALUE_EMPTY;
51-
}
52-
else {
53-
value_ = value;
54+
return DomElement.ATTRIBUTE_VALUE_EMPTY;
5455
}
55-
56-
specified_ = specified;
56+
return value;
5757
}
5858

5959
/**
@@ -109,13 +109,7 @@ public void setNodeValue(final String value) {
109109
*/
110110
@Override
111111
public void setValue(final String value) {
112-
if (value != null
113-
&& value.isEmpty()) {
114-
value_ = DomElement.ATTRIBUTE_VALUE_EMPTY;
115-
}
116-
else {
117-
value_ = value;
118-
}
112+
value_ = normalizeValue(value);
119113
specified_ = true;
120114
}
121115

@@ -165,7 +159,11 @@ public String toString() {
165159
*/
166160
@Override
167161
public String getCanonicalXPath() {
168-
return getParentNode().getCanonicalXPath() + "/@" + getName();
162+
final DomNode parent = getParentNode();
163+
if (parent == null) {
164+
return "/@" + getName();
165+
}
166+
return parent.getCanonicalXPath() + "/@" + getName();
169167
}
170168

171169
/**
@@ -181,15 +179,19 @@ public String getTextContent() {
181179
*/
182180
@Override
183181
public void setTextContent(final String textContent) {
184-
final boolean mappedElement =
185-
getOwnerDocument() instanceof HtmlPage
182+
final DomElement owner = getOwnerElement();
183+
final boolean mappedElement = owner != null
184+
&& getOwnerDocument() instanceof HtmlPage
186185
&& (DomElement.NAME_ATTRIBUTE.equals(getName()) || DomElement.ID_ATTRIBUTE.equals(getName()));
186+
187+
final HtmlPage htmlPage = mappedElement ? (HtmlPage) getPage() : null;
188+
187189
if (mappedElement) {
188-
((HtmlPage) getPage()).removeMappedElement(getOwnerElement(), false, false);
190+
htmlPage.removeMappedElement(owner, false, false);
189191
}
190192
setValue(textContent);
191193
if (mappedElement) {
192-
((HtmlPage) getPage()).addMappedElement(getOwnerElement(), false);
194+
htmlPage.addMappedElement(owner, false);
193195
}
194196
}
195197
}

src/main/java/org/htmlunit/html/DomCharacterData.java

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void setTextContent(final String textContent) {
9090
*/
9191
@Override
9292
public int getLength() {
93-
return data_.length();
93+
return data_ == null ? 0 : data_.length();
9494
}
9595

9696
/**
@@ -99,7 +99,9 @@ public int getLength() {
9999
*/
100100
@Override
101101
public void appendData(final String newData) {
102-
data_ += newData;
102+
if (newData != null) {
103+
setData(data_ == null ? newData : data_ + newData);
104+
}
103105
}
104106

105107
/**
@@ -114,8 +116,11 @@ public void deleteData(final int offset, final int count) {
114116
if (offset < 0) {
115117
throw new IllegalArgumentException("Provided offset: " + offset + " is less than zero.");
116118
}
119+
if (data_ == null) {
120+
return;
121+
}
117122

118-
final String data = data_.substring(0, offset);
123+
final String data = data_.substring(0, Math.min(offset, data_.length()));
119124
if (count >= 0) {
120125
final int fromLeft = offset + count;
121126
if (fromLeft < data_.length()) {
@@ -133,6 +138,11 @@ public void deleteData(final int offset, final int count) {
133138
*/
134139
@Override
135140
public void insertData(final int offset, final String arg) {
141+
if (data_ == null) {
142+
setData(arg);
143+
return;
144+
}
145+
136146
setData(new StringBuilder(data_).insert(offset, arg).toString());
137147
}
138148

@@ -144,8 +154,18 @@ public void insertData(final int offset, final String arg) {
144154
*/
145155
@Override
146156
public void replaceData(final int offset, final int count, final String arg) {
147-
deleteData(offset, count);
148-
insertData(offset, arg);
157+
if (offset < 0 || count < 0) {
158+
throw new IllegalArgumentException("offset: " + offset + " count: " + count);
159+
}
160+
if (data_ == null) {
161+
setData(arg);
162+
return;
163+
}
164+
165+
final int end = Math.min(offset + count, data_.length());
166+
final StringBuilder sb = new StringBuilder(data_);
167+
sb.replace(offset, end, arg != null ? arg : "");
168+
setData(sb.toString());
149169
}
150170

151171
/**
@@ -157,6 +177,9 @@ public void replaceData(final int offset, final int count, final String arg) {
157177
*/
158178
@Override
159179
public String substringData(final int offset, final int count) {
180+
if (data_ == null) {
181+
throw new IllegalArgumentException("offset: " + offset + " count: " + count);
182+
}
160183
final int length = data_.length();
161184
if (count < 0 || offset < 0 || offset > length - 1) {
162185
throw new IllegalArgumentException("offset: " + offset + " count: " + count);
@@ -180,14 +203,21 @@ public String getNodeValue() {
180203
*/
181204
@Override
182205
public String getCanonicalXPath() {
183-
return getParentNode().getCanonicalXPath() + '/' + getXPathToken();
206+
final DomNode parent = getParentNode();
207+
if (parent == null) {
208+
return "/" + getXPathToken();
209+
}
210+
return parent.getCanonicalXPath() + '/' + getXPathToken();
184211
}
185212

186213
/**
187214
* Returns the XPath token for this node only.
188215
*/
189216
private String getXPathToken() {
190217
final DomNode parent = getParentNode();
218+
if (parent == null) {
219+
return getNodeName().substring(1) + "()";
220+
}
191221

192222
// If there are other siblings of the same node type, we have to provide
193223
// the node's index.

src/main/java/org/htmlunit/html/DomText.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -198,18 +198,6 @@ protected boolean acceptChar(final char c) {
198198
return (c < '\uE000' || c > '\uF8FF') && (c == ' ' || !Character.isWhitespace(c));
199199
}
200200

201-
/**
202-
* {@inheritDoc}
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-
213201
/**
214202
* Moves the selection to the end.
215203
*/

src/main/java/org/htmlunit/html/HtmlElement.java

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ protected static void notifyAttributeChangeListeners(final HtmlAttributeChangeEv
261261
}
262262
}
263263
}
264+
264265
final DomNode parentNode = element.getParentNode();
265266
if (parentNode instanceof HtmlElement htmlElement) {
266267
notifyAttributeChangeListeners(event, htmlElement, oldAttributeValue, notifyMutationObservers);
@@ -696,34 +697,21 @@ public Page type(final Keyboard keyboard) throws IOException {
696697
final int key = (int) entry[0];
697698
final boolean pressed = (boolean) entry[1];
698699
switch (key) {
699-
case KeyboardEvent.DOM_VK_SHIFT:
700-
shiftPressed_ = pressed;
701-
break;
702-
703-
case KeyboardEvent.DOM_VK_CONTROL:
704-
ctrlPressed_ = pressed;
705-
break;
706-
707-
case KeyboardEvent.DOM_VK_ALT:
708-
altPressed_ = pressed;
709-
break;
710-
711-
default:
700+
case KeyboardEvent.DOM_VK_SHIFT -> shiftPressed_ = pressed;
701+
case KeyboardEvent.DOM_VK_CONTROL -> ctrlPressed_ = pressed;
702+
case KeyboardEvent.DOM_VK_ALT -> altPressed_ = pressed;
703+
default -> { }
712704
}
705+
713706
if (pressed) {
714-
boolean keyPress = true;
715-
boolean keyUp = true;
716-
switch (key) {
717-
case KeyboardEvent.DOM_VK_SHIFT:
718-
case KeyboardEvent.DOM_VK_CONTROL:
719-
case KeyboardEvent.DOM_VK_ALT:
720-
keyPress = false;
721-
keyUp = false;
722-
break;
723-
724-
default:
707+
if (key == KeyboardEvent.DOM_VK_SHIFT
708+
|| key == KeyboardEvent.DOM_VK_CONTROL
709+
|| key == KeyboardEvent.DOM_VK_ALT) {
710+
page = type(key, true, false, false, i == keys.size() - 1);
711+
}
712+
else {
713+
page = type(key, true, true, true, i == keys.size() - 1);
725714
}
726-
page = type(key, true, keyPress, keyUp, i == keys.size() - 1);
727715
}
728716
else {
729717
page = type(key, false, false, true, i == keys.size() - 1);
@@ -794,18 +782,6 @@ private Page type(final int keyCode,
794782
fireEvent(keyUp);
795783
}
796784

797-
// final HtmlForm form = getEnclosingForm();
798-
// if (form != null && keyCode == '\n' && isSubmittableByEnter()) {
799-
// if (!getPage().getWebClient().getBrowserVersion()
800-
// .hasFeature(BUTTON_EMPTY_TYPE_BUTTON)) {
801-
// final HtmlSubmitInput submit = form.getFirstByXPath(".//input[@type='submit']");
802-
// if (submit != null) {
803-
// return submit.click();
804-
// }
805-
// }
806-
// form.submit((SubmittableElement) this);
807-
// page.getWebClient().getJavaScriptEngine().processPostponedActions();
808-
// }
809785
return page.getWebClient().getCurrentWindow().getEnclosedPage();
810786
}
811787

@@ -846,13 +822,13 @@ private DomText getDoTypeNode() {
846822
if (scriptElement.isIsContentEditable()
847823
|| "on".equals(((Document) scriptElement.getOwnerDocument()).getDesignMode())) {
848824

849-
DomNodeList<DomNode> children = getChildNodes();
850-
while (!children.isEmpty()) {
851-
final DomNode lastChild = children.get(children.size() - 1);
852-
if (lastChild instanceof DomText text) {
853-
return text;
854-
}
855-
children = lastChild.getChildNodes();
825+
DomNode node = this;
826+
while (node.getLastChild() != null) {
827+
node = node.getLastChild();
828+
}
829+
830+
if (node instanceof DomText text) {
831+
return text;
856832
}
857833

858834
final DomText domText = new DomText(getPage(), "");

0 commit comments

Comments
 (0)