Skip to content

Commit

Permalink
[JENKINS-71356] handle svg cleanup via a xml document
Browse files Browse the repository at this point in the history
some attributes and elements where removed via regular expressions.
Additionally `stroke:#000 was replaced with currentColor because there
are some icons that still use that.
This had sideeffects when symbols are used that are not just
black/white, e.g. using svg from jenkins artwork via customizable-header
plugin. There are construct like using class attributes and a style
definition referring to the classes. At other places there where color
definitions with `stroke:#000abc` which were incorrectly modified.

svg cleanup is now implemented with DOM manipulation which is more
robust.
5 icons that still used `stroke:#000` are converted to use
`stroke:currentColor`
  • Loading branch information
mawinter69 committed May 12, 2024
1 parent 0a5aa0e commit 289db0e
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 11 deletions.
55 changes: 49 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,31 @@
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 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;

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;

/**
* Helper class to load symbols from Jenkins core or plugins.
Expand Down Expand Up @@ -88,12 +106,37 @@ private static String loadSymbol(String namespace, String name) {
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++) {
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;
}

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.

0 comments on commit 289db0e

Please sign in to comment.