Skip to content

Commit

Permalink
feat: take related polarion-rte-links into account when diffing nodes (
Browse files Browse the repository at this point in the history
…#15)

* feat: take related polarion-rte-links into account when diffing nodes

* Signing commit
  • Loading branch information
yurtsevich-sbb authored Mar 14, 2024
1 parent 65e3160 commit 023ce55
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 10 deletions.
8 changes: 7 additions & 1 deletion src/main/java/org/outerj/daisy/diff/html/dom/ImageNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ public boolean isSameText(Object other) {
} catch (ClassCastException e) {
return false;
}
return getText().equalsIgnoreCase(otherImageNode.getText());
TagNode polarionRteLink = getEnclosingPolarionRteLink(this.getParent());
TagNode anotherPolarionRteLink = otherImageNode.getParent();
if (polarionRteLink != null && anotherPolarionRteLink != null && pairedLinks(polarionRteLink, anotherPolarionRteLink)) {
return true;
} else {
return getText().equalsIgnoreCase(otherImageNode.getText());
}
}

public AttributesImpl getAttributes() {
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/org/outerj/daisy/diff/html/dom/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import org.outerj.daisy.diff.html.dom.helper.LastCommonParentResult;
import org.xml.sax.Attributes;

/**
* Represents any element in the DOM tree of a HTML file.
Expand Down Expand Up @@ -215,4 +217,43 @@ public void setWhiteAfter(boolean whiteAfter) {

public abstract Node getRightMostChild();

protected TagNode getEnclosingPolarionRteLink(TagNode node) {
if (node == null) {
return null;
} else if (isPolarionRteLink(node)) {
return node;
} else {
TagNode parent = node.getParent();
if (parent != null && ("span".equals(parent.getQName()) || "a".equals(parent.getQName()))) {
return getEnclosingPolarionRteLink(parent);
} else {
return null; // If parent node is not span or anchor - stop navigating, because it's not a Polarion RTE link subtree
}
}
}

protected boolean isPolarionRteLink(TagNode node) {
return "span".equals(node.getQName()) && "polarion-rte-link".equals(node.getAttributes().getValue("class"));
}

protected boolean pairedLinks(TagNode linkA, TagNode linkB) {
return Objects.equals(getOptionId(linkA.getAttributes()), getOptionId(linkB.getAttributes()))
&& (
Objects.equals(getItemId(linkA.getAttributes()), getPairedItemId(linkB.getAttributes()))
|| Objects.equals(getItemId(linkB.getAttributes()), getPairedItemId(linkA.getAttributes()))
);
}

protected String getItemId(Attributes attributes) {
return attributes.getValue("data-item-id");
}

protected String getPairedItemId(Attributes attributes) {
return attributes.getValue("data-paired-item-id");
}

protected String getOptionId(Attributes attributes) {
return attributes.getValue("data-option-id");
}

}
14 changes: 8 additions & 6 deletions src/main/java/org/outerj/daisy/diff/html/dom/SeparatingNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ public SeparatingNode(TagNode parent) {

@Override
public boolean equals(Object other) {
// No other separator is equal to this one. This has the effect
// that text nodes separated by such a separator can never be
// treated as a text sequence by the RangeDifferencer/TextNodeComparator.

if (other == this) {
TagNode polarionRteLink = getEnclosingPolarionRteLink(this.getParent());
TagNode anotherPolarionRteLink = (other instanceof TextNode) ? getEnclosingPolarionRteLink(((TextNode) other).getParent()) : null;
if (polarionRteLink != null && anotherPolarionRteLink != null && pairedLinks(polarionRteLink, anotherPolarionRteLink)) {
return true;
} else {
// No other separator is equal to this one. This has the effect
// that text nodes separated by such a separator can never be
// treated as a text sequence by the RangeDifferencer/TextNodeComparator.
return other == this;
}
return false;
}

}
6 changes: 5 additions & 1 deletion src/main/java/org/outerj/daisy/diff/html/dom/TagNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,11 @@ protected boolean isSimilarTag(Node another) {
boolean result = false;
if (another instanceof TagNode) {
TagNode otherNode = (TagNode) another;
if (this.getQName().equalsIgnoreCase(otherNode.getQName())) {
TagNode polarionRteLink = getEnclosingPolarionRteLink(this);
TagNode anotherPolarionRteLink = getEnclosingPolarionRteLink(otherNode);
if (polarionRteLink != null && anotherPolarionRteLink != null && pairedLinks(polarionRteLink, anotherPolarionRteLink)) {
return true;
} else if (this.getQName().equalsIgnoreCase(otherNode.getQName())) {
result = hasSameAttributes(otherNode.getAttributes());
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/org/outerj/daisy/diff/html/dom/TextNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,15 @@ public boolean isSameText(Object other) {
} catch (ClassCastException e) {
return false;
}
return getText().replace('\n', ' ').equals(
otherTextNode.getText().replace('\n', ' '));

TagNode polarionRteLink = getEnclosingPolarionRteLink(this.getParent());
TagNode anotherPolarionRteLink = getEnclosingPolarionRteLink(otherTextNode.getParent());
if (polarionRteLink != null && anotherPolarionRteLink != null && pairedLinks(polarionRteLink, anotherPolarionRteLink)) {
return true;
} else {
return getText().replace('\n', ' ').equals(
otherTextNode.getText().replace('\n', ' '));
}
}

public void setModification(Modification m) {
Expand Down

0 comments on commit 023ce55

Please sign in to comment.