Skip to content

Commit 00cc146

Browse files
authored
Merge pull request #16 from ndw/fix-trim
Make the text-trim feature consistent with the trim option on search=
2 parents 41027d8 + 50c5573 commit 00cc146

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
basename=sinclude
22
sincludeTitle=Saxon XInclude
3-
sincludeVersion=5.2.2
3+
sincludeVersion=5.2.3
44

55
saxonVersion=11.5

src/main/java/com/nwalsh/sinclude/XInclude.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -447,13 +447,26 @@ public XdmNode process(XdmNode node) throws XPathException {
447447
// spaces off each line. All trailing spaces are stripped, the number of leading
448448
// spaces is determined by the number of spaces on the first line.
449449
if (getTrimText() && parse == ParseType.TEXTPARSE) {
450-
String text = doc.getStringValue();
451-
String[] lines = text.split("\n", -1); // -1 == include empty trailing strings
450+
String[] lines = doc.getStringValue().split("\n", -1); // -1 == include empty trailing strings
451+
452452
if (lines.length > 0) {
453-
int trimleading = 0;
454-
while (trimleading < lines[0].length() && lines[0].charAt(trimleading) == ' ') {
455-
trimleading++;
453+
int trimleading = -1;
454+
for (String line : lines) {
455+
// (Effectively) blank lines don't count
456+
if (!"".equals(line.trim())) {
457+
int leading = 0;
458+
while (leading < line.length() && line.charAt(leading) == ' ') {
459+
leading++;
460+
}
461+
if (trimleading < 0 || leading < trimleading) {
462+
trimleading = leading;
463+
}
464+
if (trimleading == 0) {
465+
break;
466+
}
467+
}
456468
}
469+
457470
XdmDestination destination = ReceiverUtils.makeDestination(doc);
458471
Receiver receiver = ReceiverUtils.makeReceiver(doc, destination);
459472
for (int pos = 0; pos < lines.length; pos++) {

src/test/java/com/nwalsh/sinclude/FakeDocumentResolver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public class FakeDocumentResolver implements DocumentResolver {
182182
textMap.put("one.txt", "This is line one.\n");
183183
textMap.put("two.txt", "\n\n\n\n\n\n\n\n\nThis is line 10.\n\n\n\n\nThis is line 15.");
184184
textMap.put("three.xml", "<doc>Document three.</doc>");
185-
textMap.put("four.txt", " Four leading blanks\n {\n Three leading blanks\n }\n Four leading blanks\nNo leading blanks");
185+
textMap.put("four.txt", " Four leading blanks\n {\n Three leading blanks\n }\n Four leading blanks\n Two leading blanks");
186186
}
187187

188188
private static Map<String, String> expandedMap = null;
@@ -318,7 +318,7 @@ public class FakeDocumentResolver implements DocumentResolver {
318318
"</doc>\n");
319319
expandedMap.put("selfrefloop.xml", "<doc/>");
320320
expandedMap.put("trimtext.xml", "<doc xmlns:xi='http://www.w3.org/2001/XInclude'>\n" +
321-
"{\n Three leading blanks\n}\n Four leading blanks\nNo leading blanks\n\n" +
321+
" {\n Three leading blanks\n }\n Four leading blanks\nTwo leading blanks\n\n" +
322322
"</doc>\n");
323323

324324
}

0 commit comments

Comments
 (0)