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

[JENKINS-73156] handle svg cleanup of symbols via an xml document #9266

Merged
merged 1 commit into from
May 19, 2024
Merged
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
54 changes: 48 additions & 6 deletions core/src/main/java/org/jenkins/ui/symbol/Symbol.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,30 @@
import hudson.Util;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import jenkins.model.Jenkins;
import org.apache.commons.io.IOUtils;
import org.apache.tools.ant.filters.StringInputStream;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
* Helper class to load symbols from Jenkins core or plugins.
Expand Down Expand Up @@ -88,12 +105,37 @@
LOGGER.log(Level.FINE, "Failed to load symbol " + name, e);
}
}
return markup.replaceAll("(<title>).*?(</title>)", "$1$2")
.replaceAll("<svg", "<svg aria-hidden=\"true\"")
.replaceAll("(class=\").*?(\")", "")
.replaceAll("(tooltip=\").*?(\")", "")
.replaceAll("(data-html-tooltip=\").*?(\")", "")
.replace("stroke:#000", "stroke:currentColor");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new StringInputStream(markup));
Element root = doc.getDocumentElement();
NodeList titleList = root.getElementsByTagName("title");
for (int i = 0; i < titleList.getLength(); i++) {
Copy link
Contributor Author

@mawinter69 mawinter69 May 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As before just emptying the title elements. But why not just delete them completely?

Node title = titleList.item(i);
NodeList titleChildren = title.getChildNodes();
for (int j = 0; j < titleChildren.getLength(); j++) {
title.removeChild(titleChildren.item(j));
}
}
root.removeAttribute("class");
root.removeAttribute("id");
root.removeAttribute("tooltip");
root.removeAttribute("data-html-tooltip");
root.setAttribute("aria-hidden", "true");
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
markup = writer.getBuffer().toString().replaceAll("\n|\r", "");
} catch (ParserConfigurationException | SAXException | IOException | TransformerException e) {
LOGGER.log(Level.WARNING, e, () -> "The given src for the svg is not a valid xml document");
return PLACEHOLDER_SVG;

Check warning on line 135 in core/src/main/java/org/jenkins/ui/symbol/Symbol.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 133-135 are not covered by tests
}

return markup;
}

@CheckForNull
Expand Down
2 changes: 1 addition & 1 deletion war/src/main/resources/images/symbols/journal.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion war/src/main/resources/images/symbols/lock-closed.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion war/src/main/resources/images/symbols/play.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion war/src/main/resources/images/symbols/power.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion war/src/main/resources/images/symbols/trash-bin.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading