Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,8 @@ private Content generateContent(Element holder, DocTree tag)
}

if (inlineSnippet != null && externalSnippet != null) {
String inlineStr = inlineSnippet.asCharSequence().toString();
String externalStr = externalSnippet.asCharSequence().toString();
String inlineStr = stripLineBreaks(inlineSnippet.asCharSequence().toString());
String externalStr = stripLineBreaks(externalSnippet.asCharSequence().toString());
if (!Objects.equals(inlineStr, externalStr)) {
throw new BadSnippetException(tag, "doclet.snippet.contents.mismatch", diff(inlineStr, externalStr));
}
Expand All @@ -445,6 +445,23 @@ private Content generateContent(Element holder, DocTree tag)
return snippetTagOutput(holder, snippetTag, text, id, lang);
}

// Strips leading and trailing newline characters from a string.
private static String stripLineBreaks(String str) {
int left = 0;
int right = str.length();
// Line breaks are always encoded as \n
while (left < right && str.charAt(left) == '\n') {
left++;
}
if (left == right) {
return "";
}
while (right > left && str.charAt(right - 1) == '\n') {
right--;
}
return str.substring(left, right);
}

/*
* Maybe there's a case for implementing a proper (or at least more helpful)
* diff view, but for now simply outputting both sides of a hybrid snippet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/*
* @test
* @bug 8266666 8275788 8276964 8299080 8276966
* @bug 8266666 8275788 8276964 8299080 8276966 8304408
* @summary Implementation for snippets
* @library /tools/lib ../../lib
* @modules jdk.compiler/com.sun.tools.javac.api
Expand Down Expand Up @@ -2213,21 +2213,18 @@ public void testNegativeHybridTagMarkup_RegionRegionMismatch(Path base) throws E
"""
----------------- inline -------------------
Hello, Snippet! ...more
\s\s
----------------- external -----------------
Hello, Snippet!
\s\s
--------------------------------------------""");
--------------------------------------------
""");
checkOutput("pkg/A.html", true, """
<details class="invalid-tag">
<summary>invalid @snippet</summary>
<pre>contents mismatch:
----------------- inline -------------------
Hello, Snippet! ...more

----------------- external -----------------
Hello, Snippet!

--------------------------------------------
</pre>
</details>
Expand Down Expand Up @@ -2405,6 +2402,68 @@ record TestCase(Snippet snippet, String expectedOutput) { }
Snippet!
"""
)
,
new TestCase(newSnippetBuilder()
.body("""
Hello
,
Snippet!""")
.region("here")
.fileContent(
"""
Above the region.
// @start region=here
Hello
,
Snippet!
// @end
Below the region.
""")
.build(),
"""
Hello
,
Snippet!"""
)
,
new TestCase(newSnippetBuilder()
.body("""
Hello
,
Snippet!""")
.region("here")
.fileContent(
"""
Above the region.
// @start region=here
// @replace region substring=Goodbye replacement=Hello
Goodbye
,
Snippet!
// @end
// @end
Below the region.
""")
.build(),
"""
Hello
,
Snippet!"""
)
,
new TestCase(newSnippetBuilder()
.body("\n\n\n")
.region("here")
.fileContent(
"""
Above the region.
// @start region=here:
// @end
Below the region.
""")
.build(),
"\n\n\n"
)
);
Path srcDir = base.resolve("src");
Path outDir = base.resolve("out");
Expand Down