-
-
Notifications
You must be signed in to change notification settings - Fork 4
Escape json when writing in html #312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+506
−265
Merged
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
java/src/main/java/io/cucumber/htmlformatter/JsonInHtmlWriter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package io.cucumber.htmlformatter; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.Writer; | ||
|
|
||
| /** | ||
| * Writes json with the forward slash ({@code /}) escaped. | ||
| */ | ||
| class JsonInHtmlWriter extends Writer { | ||
| private static final int WRITE_BUFFER_SIZE = 1024; | ||
| private final Writer delegate; | ||
| private char[] writeBuffer; | ||
|
|
||
| JsonInHtmlWriter(Writer delegate) { | ||
| this.delegate = delegate; | ||
| } | ||
|
|
||
| @Override | ||
| public void write(char[] buffer, int offset, int length) throws IOException { | ||
| int escapes = countEscapes(buffer, offset, length); | ||
| if (escapes == 0) { | ||
| delegate.write(buffer, offset, length); | ||
| return; | ||
| } | ||
| int escapedLength = length + escapes; | ||
| char[] escapedBuffer = prepareWriteBuffer(escapedLength); | ||
| writeEscapeTo(buffer, offset, length, escapedBuffer); | ||
| delegate.write(escapedBuffer, 0, escapedLength); | ||
| } | ||
|
|
||
| private static int countEscapes(char[] source, int startAt, int length) { | ||
| int count = 0; | ||
| for (int i = startAt; i < startAt + length; i++) { | ||
| if (source[i] == '/') { | ||
| count++; | ||
| } | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| private char[] prepareWriteBuffer(int length) { | ||
| if (length > WRITE_BUFFER_SIZE) { | ||
| return new char[length]; | ||
| } | ||
| // Reuse the same write buffer, avoid array allocations | ||
| if (writeBuffer == null) { | ||
| writeBuffer = new char[WRITE_BUFFER_SIZE]; | ||
| } | ||
| return writeBuffer; | ||
| } | ||
|
|
||
| private static void writeEscapeTo(char[] source, int startAt, int length, char[] destination) { | ||
| for (int i = startAt, j = 0; i < startAt + length; i++) { | ||
| char c = source[i]; | ||
| if (c == '/') { | ||
| destination[j++] = '\\'; | ||
| } | ||
| destination[j++] = c; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void flush() throws IOException { | ||
| delegate.flush(); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| delegate.close(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
java/src/test/java/io/cucumber/htmlformatter/JsonInHtmlWriterTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package io.cucumber.htmlformatter; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.OutputStreamWriter; | ||
| import java.util.Arrays; | ||
|
|
||
| import static java.nio.charset.StandardCharsets.UTF_8; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| class JsonInHtmlWriterTest { | ||
|
|
||
| private final ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
| private final OutputStreamWriter outputStreamWriter = new OutputStreamWriter(out, UTF_8); | ||
| private final JsonInHtmlWriter writer = new JsonInHtmlWriter(outputStreamWriter); | ||
|
|
||
| @Test | ||
| void writes() throws IOException { | ||
| writer.write("<script>"); | ||
| assertEquals("<script>", output()); | ||
| } | ||
|
|
||
| @Test | ||
| void escapes_single() throws IOException { | ||
| writer.write("/"); | ||
| assertEquals("\\/", output()); | ||
| } | ||
|
|
||
| @Test | ||
| void escapes_multiple() throws IOException { | ||
| writer.write("</script><script></script>"); | ||
| assertEquals("<\\/script><script><\\/script>", output()); | ||
| } | ||
|
|
||
| @Test | ||
| void partial_writes() throws IOException { | ||
| char[] buffer = new char[100]; | ||
| String text = "</script><script></script>"; | ||
|
|
||
| text.getChars(0, 9, buffer, 0); | ||
| writer.write(buffer, 0, 9); | ||
|
|
||
| text.getChars(9, 17, buffer, 2); | ||
| writer.write(buffer, 2, 8); | ||
|
|
||
| text.getChars(17, 26, buffer, 4); | ||
| writer.write(buffer, 4, 9); | ||
|
|
||
| assertEquals("<\\/script><script><\\/script>", output()); | ||
| } | ||
|
|
||
| @Test | ||
| void large_writes() throws IOException { | ||
| char[] buffer = new char[1024]; | ||
| Arrays.fill(buffer, '/'); | ||
| writer.write(buffer); | ||
|
|
||
| StringBuilder expected = new StringBuilder(); | ||
| for (int i = 0; i < 1024; i++) { | ||
| expected.append("\\/"); | ||
| } | ||
| assertEquals(expected.toString(), output()); | ||
| } | ||
|
|
||
| private String output() throws IOException { | ||
| writer.flush(); | ||
| return new String(out.toByteArray(), UTF_8); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.