Skip to content

Commit e44f628

Browse files
committed
fix and simplify
1 parent 508bde2 commit e44f628

File tree

3 files changed

+21
-25
lines changed

3 files changed

+21
-25
lines changed

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,6 @@ public String getTextContent() {
174174
*/
175175
@Override
176176
public void setTextContent(final String textContent) {
177-
final boolean mappedElement = HtmlPage.isMappedElement(getOwnerDocument(), getName());
178-
if (mappedElement) {
179-
((HtmlPage) getPage()).removeMappedElement(getOwnerElement(), false, false);
180-
}
181177
setValue(textContent);
182-
if (mappedElement) {
183-
((HtmlPage) getPage()).addMappedElement(getOwnerElement(), false);
184-
}
185178
}
186179
}

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ protected void setAttributeNS(final String namespaceURI, final String qualifiedN
201201
final String oldAttributeValue = getAttribute(qualifiedName);
202202
final HtmlPage htmlPage = (HtmlPage) getPage();
203203
final boolean mappedElement = isAttachedToPage()
204-
&& HtmlPage.isMappedElement(htmlPage, qualifiedName);
204+
&& htmlPage != null
205+
&& (DomElement.NAME_ATTRIBUTE.equals(qualifiedName) || DomElement.ID_ATTRIBUTE.equals(qualifiedName));
205206
if (mappedElement) {
206207
// cast is save here because isMappedElement checks for HtmlPage
207208
htmlPage.removeMappedElement(this, false, false);
@@ -290,7 +291,8 @@ public Attr setAttributeNode(final Attr attribute) {
290291
final String oldAttributeValue = getAttribute(qualifiedName);
291292
final HtmlPage htmlPage = (HtmlPage) getPage();
292293
final boolean mappedElement = isAttachedToPage()
293-
&& HtmlPage.isMappedElement(htmlPage, qualifiedName);
294+
&& htmlPage != null
295+
&& (DomElement.NAME_ATTRIBUTE.equals(qualifiedName) || DomElement.ID_ATTRIBUTE.equals(qualifiedName));
294296
if (mappedElement) {
295297
htmlPage.removeMappedElement(this, false, false);
296298
}
@@ -323,14 +325,18 @@ public void removeAttribute(final String attributeName) {
323325
}
324326

325327
final HtmlPage htmlPage = getHtmlPageOrNull();
326-
if (htmlPage != null) {
328+
final boolean mapped = htmlPage != null
329+
&& (DomElement.NAME_ATTRIBUTE.equals(attributeName) || DomElement.ID_ATTRIBUTE.equals(attributeName));
330+
if (mapped) {
327331
htmlPage.removeMappedElement(this, false, false);
328332
}
329333

330334
super.removeAttribute(attributeName);
331335

332336
if (htmlPage != null) {
333-
htmlPage.addMappedElement(this, false);
337+
if (mapped) {
338+
htmlPage.addMappedElement(this, false);
339+
}
334340

335341
final HtmlAttributeChangeEvent event = new HtmlAttributeChangeEvent(this, attributeName, value);
336342
fireHtmlAttributeRemoved(event);

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,7 +1743,12 @@ public List<DomElement> getElementsByIdAndOrName(final String idAndOrName) {
17431743
*/
17441744
void notifyNodeAdded(final DomNode node) {
17451745
if (node instanceof DomElement) {
1746-
addMappedElement((DomElement) node, true);
1746+
final DomElement element = (DomElement) node;
1747+
1748+
if (ATTRIBUTE_NOT_DEFINED != element.getAttribute(DomElement.NAME_ATTRIBUTE)
1749+
|| ATTRIBUTE_NOT_DEFINED != element.getAttribute(DomElement.ID_ATTRIBUTE)) {
1750+
addMappedElement((DomElement) node, true);
1751+
}
17471752

17481753
if (node instanceof BaseFrameElement) {
17491754
frameElements_.add((BaseFrameElement) node);
@@ -1849,8 +1854,11 @@ private void removeElement(final Map<String, MappedElementIndexEntry> map, final
18491854

18501855
if (ATTRIBUTE_NOT_DEFINED != value) {
18511856
final MappedElementIndexEntry elements = map.remove(value);
1852-
if (elements != null && elements.remove(element)) {
1853-
map.put(value, elements);
1857+
if (elements != null) {
1858+
elements.remove(element);
1859+
if (!elements.elements_.isEmpty()) {
1860+
map.put(value, elements);
1861+
}
18541862
}
18551863
}
18561864
if (recurse) {
@@ -1860,17 +1868,6 @@ private void removeElement(final Map<String, MappedElementIndexEntry> map, final
18601868
}
18611869
}
18621870

1863-
/**
1864-
* Indicates if the attribute name indicates that the owning element is mapped.
1865-
* @param document the owning document
1866-
* @param attributeName the name of the attribute to consider
1867-
* @return {@code true} if the owning element should be mapped in its owning page
1868-
*/
1869-
static boolean isMappedElement(final Document document, final String attributeName) {
1870-
return document instanceof HtmlPage
1871-
&& (DomElement.NAME_ATTRIBUTE.equals(attributeName) || DomElement.ID_ATTRIBUTE.equals(attributeName));
1872-
}
1873-
18741871
private void calculateBase() {
18751872
final List<HtmlElement> baseElements = getDocumentElement().getStaticElementsByTagName("base");
18761873

0 commit comments

Comments
 (0)