Skip to content

Commit 0fec19a

Browse files
committed
Improved read XML attributes
1 parent de9d010 commit 0fec19a

2 files changed

Lines changed: 29 additions & 7 deletions

File tree

src/main/java/com/github/underscore/Xml.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,15 +1521,22 @@ static Map<String, String> parseAttributes(final String source) {
15211521
StringBuilder key = new StringBuilder();
15221522
StringBuilder value = new StringBuilder();
15231523
boolean inQuotes = false;
1524+
char quoteChar = 0;
15241525
boolean expectingValue = false;
15251526
for (char c : source.toCharArray()) {
1526-
if (c == '"') {
1527-
inQuotes = !inQuotes;
1528-
if (!inQuotes && expectingValue) {
1529-
result.put(key.toString(), value.toString());
1530-
key.setLength(0);
1531-
value.setLength(0);
1532-
expectingValue = false;
1527+
if ((c == '"' || c == '\'') && (!inQuotes || c == quoteChar)) {
1528+
if (!inQuotes) {
1529+
inQuotes = true;
1530+
quoteChar = c;
1531+
} else {
1532+
inQuotes = false;
1533+
quoteChar = 0;
1534+
if (expectingValue) {
1535+
result.put(key.toString(), value.toString());
1536+
key.setLength(0);
1537+
value.setLength(0);
1538+
expectingValue = false;
1539+
}
15331540
}
15341541
} else if (c == '=' && !inQuotes) {
15351542
expectingValue = true;

src/test/java/com/github/underscore/LodashTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,21 @@ void xmpToJson6() {
11521152
+ "</root>"));
11531153
}
11541154

1155+
@Test
1156+
void xmpToJson7() {
1157+
assertEquals(
1158+
"{\n"
1159+
+ " \"Comment\": {\n"
1160+
+ " \"-stringValue\": \"a\",\n"
1161+
+ " \"-self-closing\": \"true\"\n"
1162+
+ " },\n"
1163+
+ " \"#omit-xml-declaration\": \"yes\"\n"
1164+
+ "}",
1165+
U.xmlToJson("<Comment stringValue='a'/>"));
1166+
assertThrows(IllegalArgumentException.class, () -> U.xmlToJson("<Comment stringValue=\"a'/>"));
1167+
assertThrows(IllegalArgumentException.class, () -> U.xmlToJson("<Comment stringValue='a\"/>"));
1168+
}
1169+
11551170
@Test
11561171
void xmlToJsonMinimum() {
11571172
assertEquals(

0 commit comments

Comments
 (0)