Skip to content

Commit f55cd2e

Browse files
committed
add HTML encoding of links generated by history search
fixes #1735 fixes #476
1 parent d071f6f commit f55cd2e

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

src/org/opensolaris/opengrok/search/context/HistoryContext.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323

2424
package org.opensolaris.opengrok.search.context;
@@ -43,10 +43,11 @@
4343
import org.opensolaris.opengrok.search.Hit;
4444
import org.opensolaris.opengrok.search.QueryBuilder;
4545
import org.opensolaris.opengrok.web.Prefix;
46+
import org.opensolaris.opengrok.web.Util;
4647

4748
/**
4849
* it is supposed to get the matching lines from history log files.
49-
* since lucene does not easily give the match context.
50+
* since Lucene does not easily give the match context.
5051
*/
5152
public class HistoryContext {
5253

@@ -199,21 +200,28 @@ private boolean getHistoryContext(
199200
* @param end position of the first char after the match
200201
* @param flatten should multi-line log entries be flattened to a single
201202
* @param path path to the file
202-
* @param wcontext web context (begin of url)
203+
* @param wcontext web context (begin of URL)
203204
* @param nrev old revision
204205
* @param rev current revision
205206
* line? If {@code true}, replace newline with space.
206207
*/
207-
private void writeMatch(Appendable out, String line,
208-
int start, int end, boolean flatten, String path, String wcontext, String nrev, String rev)
208+
protected static void writeMatch(Appendable out, String line,
209+
int start, int end, boolean flatten, String path,
210+
String wcontext, String nrev, String rev)
209211
throws IOException {
212+
210213
String prefix = line.substring(0, start);
211214
String match = line.substring(start, end);
212215
String suffix = line.substring(end);
213216

214-
if (wcontext!=null && nrev!=null && !wcontext.isEmpty() ) {
215-
//does below need to be encoded? see bug 16985
216-
out.append("<a href="+wcontext+Prefix.DIFF_P+path+"?r2="+path+"@"+rev+"&r1="+path+"@"+nrev+" title=\"diff to previous version\">diff</a> ");
217+
if (wcontext != null && nrev != null && !wcontext.isEmpty()) {
218+
out.append("<a href=\"");
219+
printHTML(out, wcontext + Prefix.DIFF_P +
220+
Util.URIEncodePath(path) +
221+
"?r2=" + Util.URIEncodePath(path) + "@" + rev +
222+
"&r1=" + Util.URIEncodePath(path) + "@" + nrev +
223+
"\" title=\"diff to previous version\"", flatten);
224+
out.append(">diff</a> ");
217225
}
218226

219227
printHTML(out, prefix, flatten);
@@ -231,7 +239,7 @@ private void writeMatch(Appendable out, String line,
231239
* @param flatten should multi-line strings be flattened to a single
232240
* line? If {@code true}, replace newline with space.
233241
*/
234-
private void printHTML(Appendable out, String str, boolean flatten)
242+
private static void printHTML(Appendable out, String str, boolean flatten)
235243
throws IOException {
236244
for (int i = 0; i < str.length(); i++) {
237245
char ch = str.charAt(i);

src/org/opensolaris/opengrok/web/PageConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public String getHeaderData() {
185185
}
186186

187187
/**
188-
* Get all data required to create a diff view wrt. to this request in one
188+
* Get all data required to create a diff view w.r.t. to this request in one
189189
* go.
190190
*
191191
* @return an instance with just enough information to render a sufficient

test/org/opensolaris/opengrok/search/context/HistoryContextTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package org.opensolaris.opengrok.search.context;
2525

2626
import java.io.File;
27+
import java.io.IOException;
2728
import java.io.StringWriter;
2829
import java.util.ArrayList;
2930
import org.apache.lucene.index.Term;
@@ -32,6 +33,7 @@
3233
import org.apache.lucene.search.PhraseQuery;
3334
import org.apache.lucene.search.TermQuery;
3435
import org.junit.AfterClass;
36+
import org.junit.Assert;
3537
import org.junit.BeforeClass;
3638
import org.junit.Test;
3739
import org.opensolaris.opengrok.history.HistoryGuru;
@@ -167,4 +169,17 @@ public void testGetContext_4args() throws Exception {
167169
"Created a <b>small</b> dummy program"));
168170
}
169171

172+
/**
173+
* Test URI and HTML encoding of {@code writeMatch()}.
174+
* @throws IOException
175+
*/
176+
@Test
177+
public void testWriteMatch() throws IOException {
178+
StringBuilder sb = new StringBuilder();
179+
HistoryContext.writeMatch(sb, "foo", 0, 3, true, "/foo bar/haf+haf",
180+
"ctx", "1", "2");
181+
Assert.assertEquals("<a href=\"ctx/diff/foo%20bar/haf%2Bhaf?r2=/foo%20bar/haf%2Bhaf@2&amp;" +
182+
"r1=/foo%20bar/haf%2Bhaf@1\" title=\"diff to previous version\">diff</a> <b>foo</b>",
183+
sb.toString());
184+
}
170185
}

0 commit comments

Comments
 (0)