Skip to content

Commit

Permalink
Fix embedded hCard counting
Browse files Browse the repository at this point in the history
  • Loading branch information
mangstadt committed Jan 13, 2024
1 parent b24edf3 commit 2278ac4
Showing 1 changed file with 45 additions and 35 deletions.
80 changes: 45 additions & 35 deletions src/main/java/ezvcard/io/html/HCardParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,28 +283,21 @@ private void parseVCardElement(Element vcardElement) {
}

//visit all descendant nodes, depth-first
for (Element child : vcardElement.children()) {
visit(child);
}
vcardElement.children().forEach(this::visit);

//assign labels to their addresses
assignLabels(vcard, labels);
}

private void visit(Element element) {
int embeddedVCardCount = embeddedVCards.size();

Set<String> classNames = adjustClassNames(element);

for (String className : classNames) {
if (urlPropertyName.equals(className)) {
if (tryToParseAsImpp(element)) {
continue;
}
}
classNames.forEach(className -> parseProperty(element, className));

parseProperty(element, className);
}

if (embeddedVCards.isEmpty()) {
boolean noEmbeddedVCardsWereAdded = (embeddedVCardCount == embeddedVCards.size());
if (noEmbeddedVCardsWereAdded) {
//do not visit children if there are any embedded vCards
element.children().forEach(this::visit);
}
Expand Down Expand Up @@ -346,10 +339,10 @@ private String adjustClassName(String className, Element element, Set<String> or
return className;
}

private boolean tryToParseAsImpp(Element element) {
private VCardProperty tryToParseAsImpp(Element element) {
String href = element.attr("href");
if (href.isEmpty()) {
return false;
return null;
}

VCardPropertyScribe<? extends VCardProperty> scribe = index.getPropertyScribe(Impp.class);
Expand All @@ -358,12 +351,11 @@ private boolean tryToParseAsImpp(Element element) {
context.setPropertyName(scribe.getPropertyName());
try {
VCardProperty property = scribe.parseHtml(new HCardElement(element), context);
vcard.addProperty(property);
warnings.addAll(context.getWarnings());
return true;
return property;
} catch (SkipMeException | CannotParseException e) {
//URL is not an instant messenger URL
return false;
return null;
}
}

Expand Down Expand Up @@ -398,12 +390,20 @@ private VCard parseEmbeddedVCard(Element element) {
}

private void parseProperty(Element element, String className) {
if (urlPropertyName.equals(className)) {
VCardProperty impp = tryToParseAsImpp(element);
if (impp != null) {
vcard.addProperty(impp);
return;
}
}

VCardPropertyScribe<? extends VCardProperty> scribe = getPropertyScribe(className);
if (scribe == null) {
//it's a CSS class that's unrelated to hCard
return;
}

context.getWarnings().clear();
context.setPropertyName(scribe.getPropertyName());

Expand Down Expand Up @@ -443,49 +443,59 @@ private void parseProperty(Element element, String className) {
vcard.addProperty(property);
return;
}

warnings.addAll(context.getWarnings());

/*
* LABELs must be treated specially so they can be matched up with their
* ADRs.
*/
if (property instanceof Label) {
labels.add((Label) property);
handleLabel((Label) property);
return;
}

/*
* Add all NICKNAMEs to the same type object.
*/
if (property instanceof Nickname) {
Nickname nn = (Nickname) property;
if (nickname == null) {
nickname = nn;
vcard.addProperty(nickname);
} else {
nickname.getValues().addAll(nn.getValues());
}
handleNickname((Nickname) property);
return;
}

/*
* Add all CATEGORIES to the same type object.
*/
if (property instanceof Categories) {
Categories c = (Categories) property;
if (categories == null) {
categories = c;
vcard.addProperty(categories);
} else {
categories.getValues().addAll(c.getValues());
}
handleCategories((Categories) property);
return;
}

vcard.addProperty(property);
}

private void handleLabel(Label property) {
labels.add(property);
}

private void handleNickname(Nickname property) {
if (nickname == null) {
nickname = property;
vcard.addProperty(nickname);
} else {
nickname.getValues().addAll(property.getValues());
}
}

private void handleCategories(Categories property) {
if (categories == null) {
categories = property;
vcard.addProperty(categories);
} else {
categories.getValues().addAll(property.getValues());
}
}

public void close() {
//empty
}
Expand Down

0 comments on commit 2278ac4

Please sign in to comment.