Skip to content

Commit 218e04f

Browse files
committed
Don't generate invalid anchor names.
HTML 4 anchor names have a number of restrictions. The analyzer sometimes generates anchor names that break those restrictions, so that the xref output doesn't fails the HTML validator. For now, skip generating anchors for symbols that cause broken HTML.
1 parent 0151d7e commit 218e04f

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/org/opensolaris/opengrok/analysis/JFlexXref.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -495,11 +495,19 @@ protected void writeSymbol(
495495
// do this when there's exactly one definition of the symbol in
496496
// this file? Otherwise, we may end up with multiple anchors with
497497
// the same name.)
498-
out.append("<a class=\"");
499-
out.append(style_class);
500-
out.append("\" name=\"");
501-
out.append(symbol);
502-
out.append("\"/>");
498+
//
499+
// Note: In HTML 4, the name must start with a letter, and can
500+
// only contain letters, digits, hyphens, underscores, colons,
501+
// and periods. https://www.w3.org/TR/html4/types.html#type-name
502+
// Skip the anchor if the symbol name is not a valid anchor
503+
// name. This restriction is lifted in HTML 5.
504+
if (symbol.matches("[a-zA-Z][a-zA-Z0-9_:.-]*")) {
505+
out.append("<a class=\"");
506+
out.append(style_class);
507+
out.append("\" name=\"");
508+
out.append(symbol);
509+
out.append("\"/>");
510+
}
503511

504512
// 2) Create a link that searches for all references to this symbol.
505513
out.append("<a href=\"");

test/org/opensolaris/opengrok/analysis/haskell/sampleXrefExpected.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
<a class="l" name="3" href="#3">3</a><a href="/source/s?defs=qsort" onmouseover="onMouseOverSymbol('qsort', 'undefined-in-file')">qsort</a> &#40;<a href="/source/s?defs=x" onmouseover="onMouseOverSymbol('x', 'undefined-in-file')">x</a>&#58;<a href="/source/s?defs=xs" onmouseover="onMouseOverSymbol('xs', 'undefined-in-file')">xs</a>&#41; &#61; <a href="/source/s?defs=qsort" onmouseover="onMouseOverSymbol('qsort', 'undefined-in-file')">qsort</a> &#91; <a href="/source/s?defs=x'" onmouseover="onMouseOverSymbol('x\'', 'undefined-in-file')">x'</a> &#124; <a href="/source/s?defs=x'" onmouseover="onMouseOverSymbol('x\'', 'undefined-in-file')">x'</a> &lt;&#45; <a href="/source/s?defs=xs" onmouseover="onMouseOverSymbol('xs', 'undefined-in-file')">xs</a>&#44; <a href="/source/s?defs=x'" onmouseover="onMouseOverSymbol('x\'', 'undefined-in-file')">x'</a> &lt; <a href="/source/s?defs=x" onmouseover="onMouseOverSymbol('x', 'undefined-in-file')">x</a> &#93; &#43;&#43; &#91;<a href="/source/s?defs=x" onmouseover="onMouseOverSymbol('x', 'undefined-in-file')">x</a>&#93; &#43;&#43; <a href="/source/s?defs=qsort" onmouseover="onMouseOverSymbol('qsort', 'undefined-in-file')">qsort</a> &#91; <a href="/source/s?defs=x'" onmouseover="onMouseOverSymbol('x\'', 'undefined-in-file')">x'</a> &#124; <a href="/source/s?defs=x'" onmouseover="onMouseOverSymbol('x\'', 'undefined-in-file')">x'</a> &lt;&#45; <a href="/source/s?defs=xs" onmouseover="onMouseOverSymbol('xs', 'undefined-in-file')">xs</a>&#44; <a href="/source/s?defs=x'" onmouseover="onMouseOverSymbol('x\'', 'undefined-in-file')">x'</a> &gt;&#61; <a href="/source/s?defs=x" onmouseover="onMouseOverSymbol('x', 'undefined-in-file')">x</a> &#93;
77
<a class="l" name="4" href="#4">4</a>
88
<a class="l" name="5" href="#5">5</a><span class="c">-- &#119;&#101;&#105;&#114;&#100; &#98;&#117;&#116; &#108;&#101;&#103;&#97;&#108; &#105;&#100;&#101;&#110;&#116;&#105;&#102;&#105;&#101;&#114;&#115;</span>
9-
<a class="l" name="6" href="#6">6</a><a class="xf" name="x'y'"/><a href="/source/s?refs=x'y'" class="xf" onmouseover="onMouseOverSymbol('x\'y\'', 'def')">x'y'</a> &#61; <b>let</b> <a href="/source/s?defs=f'" onmouseover="onMouseOverSymbol('f\'', 'undefined-in-file')">f'</a> &#61; <span class="n">1</span>&#59; <a href="/source/s?defs=g'h" onmouseover="onMouseOverSymbol('g\'h', 'undefined-in-file')">g'h</a> &#61; <span class="n">2</span> <b>in</b> <a href="/source/s?defs=f'" onmouseover="onMouseOverSymbol('f\'', 'undefined-in-file')">f'</a> &#43; <a href="/source/s?defs=g'h" onmouseover="onMouseOverSymbol('g\'h', 'undefined-in-file')">g'h</a>
9+
<a class="l" name="6" href="#6">6</a><a href="/source/s?refs=x'y'" class="xf" onmouseover="onMouseOverSymbol('x\'y\'', 'def')">x'y'</a> &#61; <b>let</b> <a href="/source/s?defs=f'" onmouseover="onMouseOverSymbol('f\'', 'undefined-in-file')">f'</a> &#61; <span class="n">1</span>&#59; <a href="/source/s?defs=g'h" onmouseover="onMouseOverSymbol('g\'h', 'undefined-in-file')">g'h</a> &#61; <span class="n">2</span> <b>in</b> <a href="/source/s?defs=f'" onmouseover="onMouseOverSymbol('f\'', 'undefined-in-file')">f'</a> &#43; <a href="/source/s?defs=g'h" onmouseover="onMouseOverSymbol('g\'h', 'undefined-in-file')">g'h</a>
1010
<a class="l" name="7" href="#7">7</a></pre></div></body></html>

0 commit comments

Comments
 (0)