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
6 changes: 6 additions & 0 deletions pom-central.xml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@
<version>5.14.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.14.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
Expand Down
6 changes: 6 additions & 0 deletions pom-pack.xml
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@
<version>5.14.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.14.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@
<version>5.14.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.14.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/com/github/underscore/Xml.java
Original file line number Diff line number Diff line change
Expand Up @@ -1521,15 +1521,22 @@ static Map<String, String> parseAttributes(final String source) {
StringBuilder key = new StringBuilder();
StringBuilder value = new StringBuilder();
boolean inQuotes = false;
char quoteChar = 0;
boolean expectingValue = false;
for (char c : source.toCharArray()) {
if (c == '"') {
inQuotes = !inQuotes;
if (!inQuotes && expectingValue) {
result.put(key.toString(), value.toString());
key.setLength(0);
value.setLength(0);
expectingValue = false;
if ((c == '"' || c == '\'') && (!inQuotes || c == quoteChar)) {
if (!inQuotes) {
inQuotes = true;
quoteChar = c;
} else {
inQuotes = false;
quoteChar = 0;
if (expectingValue) {
result.put(key.toString(), value.toString());
key.setLength(0);
value.setLength(0);
expectingValue = false;
}
}
} else if (c == '=' && !inQuotes) {
expectingValue = true;
Expand Down
83 changes: 83 additions & 0 deletions src/test/java/com/github/underscore/LodashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import static java.util.Collections.singletonList;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import java.io.IOException;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -1152,6 +1154,87 @@ void xmpToJson6() {
+ "</root>"));
}

@Test
void xmpToJson7() {
assertEquals(
"{\n"
+ " \"Comment\": {\n"
+ " \"-stringValue\": \"a\",\n"
+ " \"-self-closing\": \"true\"\n"
+ " },\n"
+ " \"#omit-xml-declaration\": \"yes\"\n"
+ "}",
U.xmlToJson("<Comment stringValue='a'/>"));
assertEquals(
"{\n"
+ " \"Comment\": {\n"
+ " },\n"
+ " \"#omit-xml-declaration\": \"yes\"\n"
+ "}",
U.xmlToJson("<Comment stringValue='a\"'/>"));
assertEquals(
"{\n"
+ " \"Comment\": {\n"
+ " \"-stringValue\": \"a'\",\n"
+ " \"-self-closing\": \"true\"\n"
+ " },\n"
+ " \"#omit-xml-declaration\": \"yes\"\n"
+ "}",
U.xmlToJson("<Comment stringValue=\"a'\"/>"));
assertThrows(
IllegalArgumentException.class, () -> U.xmlToJson("<Comment stringValue=\"a'/>"));
assertThrows(
IllegalArgumentException.class, () -> U.xmlToJson("<Comment stringValue='a\"/>"));
}

@ParameterizedTest(name = "{0}")
@CsvSource(delimiter = '|', value = {
// input | expected (k=v,k=v)
"key=\"value\" | key=value",
"key='value' | key=value",
"a=\"1\" b='2' | a=1,b=2",
"key=\"it's a value\" | key=it's a value",
"key='say \"hi\"' | key=say \"hi\"",
"key=\"a=b=c\" | key=a=b=c",
"key=\"\" | key=",
" key =\"value\" | key=value",
"data-id=\"5\" | data-id=5",
"x==\"y\" | x=y",
"k=\"first\" k=\"second\" | k=second",
})
void parses(String input, String expected) {
assertEquals(parse(expected), Xml.parseAttributes(input));
}

@ParameterizedTest(name = "empty: \"{0}\"")
@CsvSource({
"''",
"\"orphan\"",
"lonekey",
"key=\"value",
})
void producesNothing(String input) {
assertTrue(Xml.parseAttributes(input).isEmpty());
}

@Test
void preservesInsertionOrder() {
assertEquals("[z, a, m]",
Xml.parseAttributes("z=\"1\" a=\"2\" m=\"3\"").keySet().toString());
}

// builds expected map from "k=v,k=v"
private static Map<String, String> parse(String s) {
Map<String, String> m = new LinkedHashMap<>();
if (s != null && !s.isEmpty()) {
for (String pair : s.split(",", -1)) {
int i = pair.indexOf('=');
m.put(pair.substring(0, i), pair.substring(i + 1));
}
}
return m;
}

@Test
void xmlToJsonMinimum() {
assertEquals(
Expand Down
Loading