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

Fix HTML names truncation. #399

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gsrs.module.substance.repository;

import gsrs.module.substance.utils.HtmlUtil;
import gsrs.repository.GsrsVersionedRepository;
import gsrs.springUtils.StaticContextAccessor;
import ix.core.models.Keyword;
Expand Down Expand Up @@ -152,8 +153,8 @@ default SubstanceReference toSubstanceReference(){
//How best to do this?
//For now use explicit query
NameRepository nr= StaticContextAccessor.getBean(NameRepository.class);
ref.refPname = nr.findDisplayNameByOwnerID(getUuid()).map(nn->nn.getName())
.orElse("NO NAME");
ref.refPname = HtmlUtil.truncate(nr.findDisplayNameByOwnerID(getUuid()).map(nn->nn.getName())
.orElse("NO NAME"));

//This is reasonable to have done, but it's not the way references work
//now. The substance class is set to be a reference if it's a reference.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gsrs.module.substance.utils;

import gsrs.module.substance.SubstanceDataConfiguration;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashSet;
Expand Down Expand Up @@ -54,20 +55,21 @@ public void head(Node node, int depth) {
String curText = ((TextNode) node).getWholeText();
if (resHtmlLen + nodeHtmlLen > maxLen) {
StringBuilder sb = new StringBuilder(curText);
int curHtmlLen = maxNodeLen;
sb.setLength(curHtmlLen);
sb.setLength(maxNodeLen);
int curHtmlLen = sb.toString().getBytes(StandardCharsets.UTF_8).length;
curHtmlLen += Long.valueOf(sb.chars().filter(c -> c == '&').count()).intValue() * 4;
curHtmlLen += Long.valueOf(sb.chars().filter(c -> (c == '<' || c == '>')).count()).intValue() * 3;
while (curHtmlLen > maxNodeLen) {
char lastChar = sb.charAt(sb.length() - 1);
int lastCharPos = sb.length() - 1;
char lastChar = sb.charAt(lastCharPos);
if (lastChar == '&') {
curHtmlLen = curHtmlLen - 5;
} else if (lastChar == '<' || lastChar == '>') {
curHtmlLen = curHtmlLen - 4;
} else {
curHtmlLen = curHtmlLen - 1;
curHtmlLen = curHtmlLen - String.valueOf(lastChar).getBytes(StandardCharsets.UTF_8).length;
}
sb.setLength(sb.length() - 1);
sb.setLength(lastCharPos);
}
cur.appendText(sb.toString());
throw new IllegalStateException();
Expand All @@ -86,10 +88,14 @@ public void tail(Node node, int depth) {
}
}

public static String truncate(String s){
return truncate(s, SubstanceDataConfiguration.INSTANCE().getNameColumnLength());
}

public static String truncate(String s, int len){
Document srcDoc = Parser.parseBodyFragment(s, "");
srcDoc.outputSettings().prettyPrint(false);
if (srcDoc.body().html().length() <= len) {
if (srcDoc.body().html().getBytes(StandardCharsets.UTF_8).length <= len) {
return srcDoc.body().html();
}

Expand Down Expand Up @@ -132,4 +138,14 @@ public static boolean isValid(String content) {
Safelist sl = Safelist.none().addTags(safetags.toArray(new String[safetags.size()]));
return Jsoup.isValid(content, sl);
}

public static boolean isTruncatable(String s) {
return isTruncatable(s, SubstanceDataConfiguration.INSTANCE().getNameColumnLength());
}

public static boolean isTruncatable(String s, int len) {
Document srcDoc = Parser.parseBodyFragment(s, "");
srcDoc.outputSettings().prettyPrint(false);
return srcDoc.body().html().getBytes(StandardCharsets.UTF_8).length > len;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import gsrs.module.substance.SubstanceDataConfiguration;
import gsrs.module.substance.utils.HtmlUtil;
import gsrs.springUtils.StaticContextAccessor;
import ix.core.SingleParent;
Expand Down Expand Up @@ -198,12 +197,10 @@ private void prePersist(){
}

private void tidyName () {
int clen= SubstanceDataConfiguration.INSTANCE().getNameColumnLength();

if (name.length() > clen) {
fullName = name;
name = HtmlUtil.truncate(name, clen);
}
if (HtmlUtil.isTruncatable(name)) {
fullName = name;
name = HtmlUtil.truncate(name);
}
}

public void addLocator(Substance sub, String loc){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.fasterxml.jackson.annotation.JsonIgnore;

import gsrs.module.substance.SubstanceDataConfiguration;
import gsrs.module.substance.utils.HtmlUtil;
import ix.core.models.Indexable;
import ix.core.util.EntityUtils;
Expand All @@ -24,7 +23,7 @@ public static SubstanceReference newReferenceFor(Substance s){
SubstanceReference ref = new SubstanceReference();

ref.refuuid = s.getOrGenerateUUID().toString();
ref.refPname = HtmlUtil.truncate(s.getName(), SubstanceDataConfiguration.INSTANCE().getNameColumnLength());
ref.refPname = HtmlUtil.truncate(s.getName());
ref.approvalID = s.approvalID;
ref.substanceClass = Substance.SubstanceClass.reference.toString();
ref.wrappedSubstance = s;
Expand Down Expand Up @@ -156,7 +155,12 @@ public void postLoad(){


}


@PrePersist
private void prePersist(){
this.refPname = HtmlUtil.truncate(this.refPname);
}

/**
* Tests if the referenced record is the same for the given {@link SubstanceReference}
* as for this one. This just compares the refuuid field.
Expand Down