Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improve diffing related RTE links with custom form of represent… #29

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 35 additions & 11 deletions src/main/java/org/outerj/daisy/diff/html/dom/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
*/
package org.outerj.daisy.diff.html.dom;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import org.outerj.daisy.diff.html.dom.helper.LastCommonParentResult;
import org.xml.sax.Attributes;
Expand All @@ -28,6 +27,9 @@
*/
public abstract class Node {

private static final String SPAN_TAG = "span";
private static final String ANCHOR_TAG = "a";

protected TagNode parent;
private TagNode root;

Expand Down Expand Up @@ -224,7 +226,7 @@ protected TagNode getEnclosingPolarionRteLink(TagNode node) {
return node;
} else {
TagNode parent = node.getParent();
if (parent != null && ("span".equals(parent.getQName()) || "a".equals(parent.getQName()))) {
if (parent != null && (SPAN_TAG.equals(parent.getQName()) || ANCHOR_TAG.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
Expand All @@ -233,15 +235,37 @@ protected TagNode getEnclosingPolarionRteLink(TagNode node) {
}

protected boolean isPolarionRteLink(TagNode node) {
return "span".equals(node.getQName()) && "polarion-rte-link".equals(node.getAttributes().getValue("class"));
return SPAN_TAG.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()))
);
String optionId = getOptionId(linkA.getAttributes());
if (Objects.equals(optionId, getOptionId(linkB.getAttributes()))
&& (Objects.equals(getItemId(linkA.getAttributes()), getPairedItemId(linkB.getAttributes()))
|| Objects.equals(getItemId(linkB.getAttributes()), getPairedItemId(linkA.getAttributes())))) {
if ("custom".equals(optionId)) {
Node childNodeA = linkA.getNbChildren() == 1 ? linkA.getChild(0) : null;
Node childNodeB = linkB.getNbChildren() == 1 ? linkB.getChild(0) : null;
if (childNodeA instanceof TagNode && childNodeB instanceof TagNode) {
TagNode childTagNodeA = (TagNode) childNodeA;
TagNode childTagNodeB = (TagNode) childNodeB;
return getInnerText(childTagNodeA).equals(getInnerText(childTagNodeB));
} else {
return false;
}
} else {
return true;
}
} else {
return false;
}
}

private String getInnerText(TagNode node) {
return StreamSupport.stream(node.spliterator(), false)
.filter(n -> n instanceof TextNode)
.map(n -> ((TextNode) n).getText())
.collect(Collectors.joining(" "));
}

protected String getItemId(Attributes attributes) {
Expand Down
Loading