Skip to content

Commit

Permalink
[JENKINS-73243] quote replacement string in symbol tooltips (jenkinsc…
Browse files Browse the repository at this point in the history
…i#9347)

(cherry picked from commit 5978ebe)
  • Loading branch information
mawinter69 authored and krisstern committed Jun 24, 2024
1 parent 62ba922 commit 6a8594d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
20 changes: 17 additions & 3 deletions core/src/main/java/org/jenkins/ui/symbol/Symbol.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
<<<<<<< HEAD
=======
import java.util.regex.Matcher;
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;
>>>>>>> 5978ebe594 ([JENKINS-73243] quote replacement string in symbol tooltips (#9347))
import jenkins.model.Jenkins;
import org.apache.commons.io.IOUtils;

Expand Down Expand Up @@ -55,13 +69,13 @@ public static String get(@NonNull SymbolRequest request) {
.computeIfAbsent(identifier, key -> new ConcurrentHashMap<>())
.computeIfAbsent(name, key -> loadSymbol(identifier, key));
if ((tooltip != null && !tooltip.isBlank()) && (htmlTooltip == null || htmlTooltip.isBlank())) {
symbol = symbol.replaceAll("<svg", "<svg tooltip=\"" + Functions.htmlAttributeEscape(tooltip) + "\"");
symbol = symbol.replaceAll("<svg", Matcher.quoteReplacement("<svg tooltip=\"" + Functions.htmlAttributeEscape(tooltip) + "\""));
}
if (htmlTooltip != null && !htmlTooltip.isBlank()) {
symbol = symbol.replaceAll("<svg", "<svg data-html-tooltip=\"" + Functions.htmlAttributeEscape(htmlTooltip) + "\"");
symbol = symbol.replaceAll("<svg", Matcher.quoteReplacement("<svg data-html-tooltip=\"" + Functions.htmlAttributeEscape(htmlTooltip) + "\""));
}
if (id != null && !id.isBlank()) {
symbol = symbol.replaceAll("<svg", "<svg id=\"" + Functions.htmlAttributeEscape(id) + "\"");
symbol = symbol.replaceAll("<svg", Matcher.quoteReplacement("<svg id=\"" + Functions.htmlAttributeEscape(id) + "\""));
}
if (classes != null && !classes.isBlank()) {
symbol = symbol.replaceAll("<svg", "<svg class=\"" + Functions.htmlAttributeEscape(classes) + "\"");
Expand Down
17 changes: 17 additions & 0 deletions test/src/test/java/org/jenkins/ui/symbol/SymbolJenkinsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.DisplayName;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.RealJenkinsRule;

Expand All @@ -15,6 +16,22 @@ public class SymbolJenkinsTest {
public RealJenkinsRule rjr = new RealJenkinsRule()
.addPlugins("plugins/design-library.jpi", "plugins/prism-api.jpi", "plugins/bootstrap5-api.jpi");

@Test
@Issue("JENKINS-73243")
@DisplayName("When resolving a symbol where the tooltip contains '$' no error is thrown")
public void dollarInToolTipSucceeds() throws Throwable {
rjr.then(SymbolJenkinsTest::_dollarInTooltipSucceeds);
}

private static void _dollarInTooltipSucceeds(JenkinsRule j) {
String symbol = Symbol.get(new SymbolRequest.Builder()
.withName("add")
.withTooltip("$test")
.build()
);
assertThat(symbol, containsString("tooltip=\"$test\""));
}

@Test
@DisplayName("When resolving a symbol from a missing plugin, the placeholder is generated instead")
public void missingSymbolFromPluginDefaultsToPlaceholder() throws Throwable {
Expand Down

0 comments on commit 6a8594d

Please sign in to comment.