Skip to content

Commit

Permalink
fix and simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Nov 22, 2024
1 parent 508bde2 commit e44f628
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
7 changes: 0 additions & 7 deletions src/main/java/org/htmlunit/html/DomAttr.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,6 @@ public String getTextContent() {
*/
@Override
public void setTextContent(final String textContent) {
final boolean mappedElement = HtmlPage.isMappedElement(getOwnerDocument(), getName());
if (mappedElement) {
((HtmlPage) getPage()).removeMappedElement(getOwnerElement(), false, false);
}
setValue(textContent);
if (mappedElement) {
((HtmlPage) getPage()).addMappedElement(getOwnerElement(), false);
}
}
}
14 changes: 10 additions & 4 deletions src/main/java/org/htmlunit/html/HtmlElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ protected void setAttributeNS(final String namespaceURI, final String qualifiedN
final String oldAttributeValue = getAttribute(qualifiedName);
final HtmlPage htmlPage = (HtmlPage) getPage();
final boolean mappedElement = isAttachedToPage()
&& HtmlPage.isMappedElement(htmlPage, qualifiedName);
&& htmlPage != null
&& (DomElement.NAME_ATTRIBUTE.equals(qualifiedName) || DomElement.ID_ATTRIBUTE.equals(qualifiedName));
if (mappedElement) {
// cast is save here because isMappedElement checks for HtmlPage
htmlPage.removeMappedElement(this, false, false);
Expand Down Expand Up @@ -290,7 +291,8 @@ public Attr setAttributeNode(final Attr attribute) {
final String oldAttributeValue = getAttribute(qualifiedName);
final HtmlPage htmlPage = (HtmlPage) getPage();
final boolean mappedElement = isAttachedToPage()
&& HtmlPage.isMappedElement(htmlPage, qualifiedName);
&& htmlPage != null
&& (DomElement.NAME_ATTRIBUTE.equals(qualifiedName) || DomElement.ID_ATTRIBUTE.equals(qualifiedName));
if (mappedElement) {
htmlPage.removeMappedElement(this, false, false);
}
Expand Down Expand Up @@ -323,14 +325,18 @@ public void removeAttribute(final String attributeName) {
}

final HtmlPage htmlPage = getHtmlPageOrNull();
if (htmlPage != null) {
final boolean mapped = htmlPage != null
&& (DomElement.NAME_ATTRIBUTE.equals(attributeName) || DomElement.ID_ATTRIBUTE.equals(attributeName));
if (mapped) {
htmlPage.removeMappedElement(this, false, false);
}

super.removeAttribute(attributeName);

if (htmlPage != null) {
htmlPage.addMappedElement(this, false);
if (mapped) {
htmlPage.addMappedElement(this, false);
}

final HtmlAttributeChangeEvent event = new HtmlAttributeChangeEvent(this, attributeName, value);
fireHtmlAttributeRemoved(event);
Expand Down
25 changes: 11 additions & 14 deletions src/main/java/org/htmlunit/html/HtmlPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -1743,7 +1743,12 @@ public List<DomElement> getElementsByIdAndOrName(final String idAndOrName) {
*/
void notifyNodeAdded(final DomNode node) {
if (node instanceof DomElement) {
addMappedElement((DomElement) node, true);
final DomElement element = (DomElement) node;

if (ATTRIBUTE_NOT_DEFINED != element.getAttribute(DomElement.NAME_ATTRIBUTE)
|| ATTRIBUTE_NOT_DEFINED != element.getAttribute(DomElement.ID_ATTRIBUTE)) {
addMappedElement((DomElement) node, true);
}

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

if (ATTRIBUTE_NOT_DEFINED != value) {
final MappedElementIndexEntry elements = map.remove(value);
if (elements != null && elements.remove(element)) {
map.put(value, elements);
if (elements != null) {
elements.remove(element);
if (!elements.elements_.isEmpty()) {
map.put(value, elements);
}
}
}
if (recurse) {
Expand All @@ -1860,17 +1868,6 @@ private void removeElement(final Map<String, MappedElementIndexEntry> map, final
}
}

/**
* Indicates if the attribute name indicates that the owning element is mapped.
* @param document the owning document
* @param attributeName the name of the attribute to consider
* @return {@code true} if the owning element should be mapped in its owning page
*/
static boolean isMappedElement(final Document document, final String attributeName) {
return document instanceof HtmlPage
&& (DomElement.NAME_ATTRIBUTE.equals(attributeName) || DomElement.ID_ATTRIBUTE.equals(attributeName));
}

private void calculateBase() {
final List<HtmlElement> baseElements = getDocumentElement().getStaticElementsByTagName("base");

Expand Down

0 comments on commit e44f628

Please sign in to comment.