Skip to content

Commit 542db9a

Browse files
committed
fix: del-param impl in url functions
1 parent ad2987d commit 542db9a

File tree

2 files changed

+32
-38
lines changed

2 files changed

+32
-38
lines changed

mycore-base/src/main/resources/xslt/functions/url.xsl

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -133,37 +133,34 @@
133133
</xsl:choose>
134134
</xsl:function>
135135

136-
<xsl:function name="mcrurl:del-param" as="xs:string">
137-
<xsl:param name="url" as="xs:string?"/>
138-
<xsl:param name="par" as="xs:string"/>
136+
<xsl:function name="mcrurl:del-param" as="xs:string">
137+
<xsl:param name="url" as="xs:string?" />
138+
<xsl:param name="par" as="xs:string" />
139139

140-
<!-- Normalize: XPath has empty-sequence; treat it like empty string. -->
141-
<xsl:variable name="u" as="xs:string" select="string($url)"/>
140+
<xsl:variable name="u" as="xs:string" select="string($url)" />
142141

143-
<!-- Escape the parameter name so it is safe to embed into a regex. -->
144-
<xsl:variable name="par-esc" as="xs:string"
145-
select="replace($par, '([\\.^$|?*+(){}\[\]-])', '\\$1')"/>
142+
<!-- escape param name -->
143+
<xsl:variable name="par-esc" as="xs:string" select="replace($par, '([\\.^$|?*+(){}\[\]-])', '\\$1')" />
146144

147-
<!--
148-
Case A: The parameter is NOT the last one (…par=value&…).
149-
- If it starts with '?', '?par=value&' collapses to '?'.
150-
- If it starts with '&', '&par=value&' collapses to '&'.
151-
-->
152-
<xsl:variable name="case-a" as="xs:string"
153-
select="replace($u, concat('([?&amp;])', $par-esc, '=[^&amp;]*&amp;'), '$1', 'q')"/>
145+
<!-- remove parameter -->
146+
<xsl:variable name="step1" as="xs:string" select="
147+
replace(
148+
$u,
149+
concat('([?&amp;])', $par-esc, '=[^&amp;]*(&amp;)?'),
150+
'$1'
151+
)
152+
" />
154153

155-
<!--
156-
Case B: The parameter IS the last one (…?par=value or …&par=value at end).
157-
Remove it (including the leading separator) to the end of the string.
158-
-->
159-
<xsl:variable name="case-b" as="xs:string"
160-
select="replace($u, concat('([?&amp;])', $par-esc, '=[^&amp;]*$'), '', 'q')"/>
154+
<!-- cleanup dangling separators -->
155+
<xsl:variable name="step2" as="xs:string" select="replace($step1, '\?&amp;?', '?')" />
161156

162-
<!-- Choose which transformation actually applied; otherwise return the original URL. -->
163-
<xsl:sequence select="
164-
if ($case-a ne $u) then $case-a
165-
else if ($case-b ne $u) then $case-b
166-
else $u
167-
"/>
168-
</xsl:function>
157+
<xsl:variable name="step3" as="xs:string" select="replace($step2, '[?&amp;]+$', '')" />
158+
159+
<!-- final safety: remove trailing '?' -->
160+
<xsl:sequence select="
161+
if (ends-with($step3, '?'))
162+
then substring($step3, 1, string-length($step3) - 1)
163+
else $step3
164+
" />
165+
</xsl:function>
169166
</xsl:stylesheet>

mycore-base/src/test/java/org/mycore/common/xsl/MCRURLFunctionsTests.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,11 @@
3333
@MyCoReTest
3434
public class MCRURLFunctionsTests {
3535

36-
private static final String XSL = "/xslt/functions/url-test.xsl";
37-
38-
private static final String FUNCTION_PARAM_NAME = "fn-name";
3936
private static final String URL_PARAM_NAME = "url";
4037
private static final String PAR_PARAM_NAME = "par";
4138
private static final String VALUE_PARAM_NAME = "value";
4239

43-
private static final String URI_PREFIX = "http://mycore.org";
40+
private static final String URL_PREFIX = "http://mycore.org";
4441

4542
@ParameterizedTest
4643
@CsvSource({
@@ -61,7 +58,7 @@ public class MCRURLFunctionsTests {
6158
"'?foo', foo, ''"
6259
})
6360
public void testGetParam(String queryUrl, String param, String expected) throws Exception {
64-
assertEquals(expected, getParam(URI_PREFIX + queryUrl, param));
61+
assertEquals(expected, getParam(URL_PREFIX + queryUrl, param));
6562
}
6663

6764
@ParameterizedTest
@@ -84,7 +81,7 @@ public void testGetParam(String queryUrl, String param, String expected) throws
8481
"'?my-param=bar', my-param, os, '?my-param=os'"
8582
})
8683
public void testSetParam(String queryUrl, String param, String value, String expected) throws Exception {
87-
assertEquals(URI_PREFIX + expected, setParam(URI_PREFIX + queryUrl, param, value));
84+
assertEquals(URL_PREFIX + expected, setParam(URL_PREFIX + queryUrl, param, value));
8885
}
8986

9087
@ParameterizedTest
@@ -107,7 +104,7 @@ public void testSetParam(String queryUrl, String param, String value, String exp
107104
"'?foo=bar&foo=os', foo, '?foo=os'"
108105
})
109106
public void testDelParam(String queryUrl, String param, String expected) throws Exception {
110-
assertEquals(URI_PREFIX + expected, delParam(URI_PREFIX + queryUrl, param));
107+
assertEquals(URL_PREFIX + expected, delParam(URL_PREFIX + queryUrl, param));
111108
}
112109

113110
private String getParam(String url, String param) throws TransformerException {
@@ -125,7 +122,7 @@ private String setParam(String url, String param, String value) throws Transform
125122
return callFunction("set-param", parameters);
126123
}
127124

128-
public String delParam(String url, String param) throws TransformerException {
125+
private String delParam(String url, String param) throws TransformerException {
129126
Map<String, String> parameters = new HashMap<>();
130127
parameters.put(URL_PARAM_NAME, url);
131128
parameters.put(PAR_PARAM_NAME, param);
@@ -134,7 +131,7 @@ public String delParam(String url, String param) throws TransformerException {
134131

135132
private String callFunction(String name, Map<String, String> params) throws TransformerException {
136133
Map<String, Object> parameters = new HashMap<>(params);
137-
parameters.put(FUNCTION_PARAM_NAME, name);
138-
return MCRTestCaseXSLTUtil.transform(XSL, parameters).getRootElement().getText();
134+
parameters.put("fn-name", name);
135+
return MCRTestCaseXSLTUtil.transform("/xslt/functions/url-test.xsl", parameters).getRootElement().getText();
139136
}
140137
}

0 commit comments

Comments
 (0)