Skip to content

Commit 96440da

Browse files
authored
Merge pull request #808 from grzesiek2010/parseAttributes
Improved parsing attributes in tests
2 parents 3d3efac + d2dd309 commit 96440da

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

src/main/java/org/javarosa/test/XFormsElement.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.List;
2323
import java.util.Map;
2424
import java.util.stream.Collectors;
25+
import java.util.regex.Pattern;
2526

2627
import static java.util.Arrays.asList;
2728
import static java.util.Collections.emptyMap;
@@ -42,7 +43,12 @@ static Map<String, String> parseAttributes(String name) {
4243
if (!name.contains(" "))
4344
return emptyMap();
4445
Map<String, String> attributes = new HashMap<>();
45-
String[] words = name.split(" ");
46+
47+
// Regex to split on spaces, ignoring spaces inside quoted text
48+
final String SPACE_OUTSIDE_QUOTES_REGEX = " (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)";
49+
Pattern spaceOutsideQuotesPattern = Pattern.compile(SPACE_OUTSIDE_QUOTES_REGEX);
50+
String[] words = spaceOutsideQuotesPattern.split(name);
51+
4652
for (String word : asList(words).subList(1, words.length)) {
4753
String[] parts = word.split("(?<!\\))=(\"|')", 2);
4854
attributes.put(parts[0], parts[1].substring(0, parts[1].length() - 1));
@@ -135,16 +141,7 @@ static XFormsElement select1(String ref, XFormsElement... children) {
135141
}
136142

137143
static XFormsElement select1Dynamic(String ref, String nodesetRef) {
138-
XFormsElement value = t("value ref=\"value\"");
139-
XFormsElement label = t("label ref=\"label\"");
140-
141-
HashMap<String, String> itemsetAttributes = new HashMap<>();
142-
itemsetAttributes.put("nodeset", nodesetRef);
143-
TagXFormsElement itemset = new TagXFormsElement("itemset", itemsetAttributes, asList(value, label));
144-
145-
HashMap<String, String> select1Attributes = new HashMap<>();
146-
select1Attributes.put("ref", ref);
147-
return new TagXFormsElement("select1", select1Attributes, asList(itemset));
144+
return select1Dynamic(ref, nodesetRef, "value", "label");
148145
}
149146

150147
static XFormsElement select1Dynamic(String ref, String nodesetRef, String valueRef, String labelRef) {

src/test/java/org/javarosa/core/model/test/FormDefTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ public void fillTemplateString_resolvesRelativeReferences_inItext() throws IOExc
381381
title("output with relative ref in translation"),
382382
model(
383383
t("itext", t("translation lang=\"Français\"",
384-
t("text id=\"/data/repeat/position_in_label:label",
384+
t("text id=\"/data/repeat/position_in_label:label\"",
385385
t("value", "Position: <output value=\"../position\"/>"))
386386
)),
387387
mainInstance(t("data id=\"relative-output\"",

0 commit comments

Comments
 (0)