From f46e44bbafb6de5c1757f63b8e3447a4743686ec Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Fri, 1 Apr 2011 22:48:58 -0700 Subject: [PATCH] [FIXED JENKINS-9009] Fixed a bug in handling ' and " in matrix build label axis --- changelog.html | 3 +++ core/src/main/java/hudson/Functions.java | 24 +++++++++++++++++++ .../main/java/hudson/matrix/LabelAxis.java | 22 +++++++++++++++++ .../hudson/matrix/LabelAxis/config.jelly | 4 ++-- .../hudson/matrix/MatrixProjectTest.groovy | 11 +++++++++ 5 files changed, 62 insertions(+), 2 deletions(-) diff --git a/changelog.html b/changelog.html index 1f0bf6a8e2..5faf281a55 100644 --- a/changelog.html +++ b/changelog.html @@ -67,6 +67,9 @@ Upcoming changes In matrix security, newly added rows weren't removable
  • Improve the stability of the test harness +
  • + Fixed a bug in handling ' and " in matrix build label axis + (issue 9009)
  • Performance: Specify image sizes for faster page loading (issue 9182) diff --git a/core/src/main/java/hudson/Functions.java b/core/src/main/java/hudson/Functions.java index 6d1a529ca1..35b8bde113 100644 --- a/core/src/main/java/hudson/Functions.java +++ b/core/src/main/java/hudson/Functions.java @@ -538,6 +538,30 @@ public class Functions { return Util.xmlEscape(s); } + public static String htmlAttributeEscape(String text) { + StringBuilder buf = new StringBuilder(text.length()+64); + for( int i=0; i') + buf.append(">"); + else + if(ch=='&') + buf.append("&"); + else + if(ch=='"') + buf.append("""); + else + if(ch=='\'') + buf.append("'"); + else + buf.append(ch); + } + return buf.toString(); + } + public static void checkPermission(Permission permission) throws IOException, ServletException { checkPermission(Hudson.getInstance(),permission); } diff --git a/core/src/main/java/hudson/matrix/LabelAxis.java b/core/src/main/java/hudson/matrix/LabelAxis.java index d71e8e37e2..ef6c3df9f7 100644 --- a/core/src/main/java/hudson/matrix/LabelAxis.java +++ b/core/src/main/java/hudson/matrix/LabelAxis.java @@ -24,9 +24,13 @@ package hudson.matrix; import hudson.Extension; +import hudson.Functions; +import hudson.Util; import hudson.model.Hudson; +import hudson.model.labels.LabelAtom; import org.kohsuke.stapler.DataBoundConstructor; +import java.text.MessageFormat; import java.util.List; /** @@ -45,6 +49,11 @@ public class LabelAxis extends Axis { return true; } + @Override + public String getValueString() { + return Util.join(getValues(),"/"); + } + @Extension public static class DescriptorImpl extends AxisDescriptor { @Override @@ -60,5 +69,18 @@ public class LabelAxis extends Axis { Hudson h = Hudson.getInstance(); return !h.getNodes().isEmpty() || !h.clouds.isEmpty(); } + + private String jsstr(String body, Object... args) { + return '\"'+Functions.jsStringEscape(String.format(body,args))+'\"'; + } + + public String buildLabelCheckBox(LabelAtom la, LabelAxis instance) { + return jsstr("", + la.getName(),la.getDescription()); + // '${h.jsStringEscape('')}' + } } } diff --git a/core/src/main/resources/hudson/matrix/LabelAxis/config.jelly b/core/src/main/resources/hudson/matrix/LabelAxis/config.jelly index 0ba23b81ae..572bc437c9 100644 --- a/core/src/main/resources/hudson/matrix/LabelAxis/config.jelly +++ b/core/src/main/resources/hudson/matrix/LabelAxis/config.jelly @@ -35,12 +35,12 @@ THE SOFTWARE. var labels = new YAHOO.widget.TextNode("${%Labels}", tree.getRoot(), false); var machines = new YAHOO.widget.TextNode("${%Individual nodes}", tree.getRoot(), false); - var values = (e.getAttribute("values") || "").split(/[ \n]/); + var values = (e.getAttribute("values") || "").split("/"); function has(v) { return values.include(v) ? 'checked="checked" ' : ""; } - new YAHOO.widget.TextNode('<input type="checkbox" name="values" json="${l.name}" '+has("${l.name}")+'/><label class="attach-previous">${h.jsStringEscape(l.name)} (${h.jsStringEscape(l.description)})</label>', ${l.isSelfLabel()?'machines':'labels'}, false); + new YAHOO.widget.TextNode(, ${l.isSelfLabel()?'machines':'labels'}, false); tree.draw(); diff --git a/test/src/test/groovy/hudson/matrix/MatrixProjectTest.groovy b/test/src/test/groovy/hudson/matrix/MatrixProjectTest.groovy index 82f093e6d2..046999cdae 100644 --- a/test/src/test/groovy/hudson/matrix/MatrixProjectTest.groovy +++ b/test/src/test/groovy/hudson/matrix/MatrixProjectTest.groovy @@ -279,4 +279,15 @@ public class MatrixProjectTest extends HudsonTestCase { // expected } } + + @Bug(9009) + void testTrickyNodeName() { + def names = [ createSlave("Sean's Workstation",null,null), createSlave("John\"s Workstation",null,null) ]*.nodeName; + def p = createMatrixProject(); + p.setAxes(new AxisList([new LabelAxis("label",names)])); + configRoundtrip(p); + + LabelAxis a = p.axes.find("label"); + assertEquals(a.values as Set,names as Set); + } }