Skip to content

Commit cd34046

Browse files
authored
Fix #290 by reverting #95 (and PR #270) (#296)
1 parent 67fd520 commit cd34046

4 files changed

Lines changed: 25 additions & 41 deletions

File tree

release-notes/CREDITS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ Konrad Windszus (kwin@github)
4747
(6.1.0)
4848
* Reported #95: `BaseStreamWriter.writeRaw()` should not close open element
4949
(7.2.0)
50+
* Reported #290: Regression in 7.2.0: `writeRaw()` no longer closing an open start
51+
element (from #95) breaks emitting whitespace after start/end tags;
52+
reverted #95 so `writeRaw()` again closes an open start element
53+
(7.2.1)
5054

5155
Daniel Kulp (dkulp@github)
5256

release-notes/VERSION

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Project: woodstox
1414
(reported by @Skrethel)
1515
#293: Reject out-of-range code points in `UTF32Reader`
1616
(contributed by @aizu-m)
17+
#290: Regression in 7.2.0: `writeRaw()` no longer closing an open start
18+
element (from #95) breaks emitting whitespace after start/end tags;
19+
reverted #95 so `writeRaw()` again closes an open start element
20+
(reported by Konrad W)
1721

1822
7.2.0 (19-May-2026)
1923

src/main/java/com/ctc/wstx/sw/BaseStreamWriter.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,14 +1161,14 @@ public void writeStartDocument(String version, String encoding,
11611161
doWriteStartDocument(version, encoding, standAlone ? "yes" : "no");
11621162
}
11631163

1164-
// 13-May-2026, tatu: [woodstox-core#95] writeRaw() must NOT close an
1165-
// open start element, so that callers can use it to emit content
1166-
// (typically whitespace) between attributes within a start tag.
11671164
@Override
11681165
public void writeRaw(String text)
11691166
throws XMLStreamException
11701167
{
11711168
mAnyOutput = true;
1169+
if (mStartElementOpen) {
1170+
closeStartElement(mEmptyElement);
1171+
}
11721172
try {
11731173
mWriter.writeRaw(text, 0, text.length());
11741174
} catch (IOException ioe) {
@@ -1181,6 +1181,9 @@ public void writeRaw(String text, int start, int offset)
11811181
throws XMLStreamException
11821182
{
11831183
mAnyOutput = true;
1184+
if (mStartElementOpen) {
1185+
closeStartElement(mEmptyElement);
1186+
}
11841187
try {
11851188
mWriter.writeRaw(text, start, offset);
11861189
} catch (IOException ioe) {
@@ -1193,6 +1196,9 @@ public void writeRaw(char[] text, int start, int offset)
11931196
throws XMLStreamException
11941197
{
11951198
mAnyOutput = true;
1199+
if (mStartElementOpen) {
1200+
closeStartElement(mEmptyElement);
1201+
}
11961202
try {
11971203
mWriter.writeRaw(text, start, offset);
11981204
} catch (IOException ioe) {

src/test/java/stax2/wstream/TestStreamWriter.java

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,6 @@ public void testRaw()
258258
w.writeStartDocument();
259259
w.writeStartElement("test");
260260
w.writeAttribute("attr", "value");
261-
// 13-May-2026, tatu: As of [woodstox-core#95], writeRaw() no longer
262-
// closes an open start element; an empty writeCharacters() is the
263-
// idiomatic way to commit the start tag before raw content.
264-
w.writeCharacters("");
265261
w.writeRaw("this or 'that'");
266262
char[] cbuf = new char[RAW2.length() + 10];
267263
RAW2.getChars(0, RAW2.length(), cbuf, 3);
@@ -293,55 +289,29 @@ public void testRaw()
293289
}
294290

295291
/**
296-
* Unit test for [woodstox-core#95]: writeRaw() must not close an open
297-
* start element, so it can be used to emit content (typically whitespace)
298-
* between attributes within the start tag, e.g. for indenting attributes.
292+
* Unit test for [woodstox-core#290]: emitting whitespace (via
293+
* {@code writeSpace()}, which delegates to {@code writeRaw()}) right
294+
* after a start element must close the open start tag first, so that the
295+
* whitespace ends up as element content rather than inside the start tag.
299296
*/
300297
@Test
301-
public void testRawBetweenAttributes()
298+
public void testSpaceAfterStartElement()
302299
throws XMLStreamException
303300
{
304301
for (int i = 0; i < 3; ++i) {
305302
boolean ns = (i > 0);
306303
StringWriter strw = new StringWriter();
307304
XMLStreamWriter2 w = (i == 2) ? getRepairingWriter(strw)
308305
: getNonRepairingWriter(strw, ns);
309-
w.writeStartDocument();
310306
w.writeStartElement("test");
311-
w.writeAttribute("a", "1");
312-
// Raw whitespace between attributes must be allowed and must
313-
// not close the start tag.
314-
w.writeRaw("\n ");
315-
w.writeAttribute("b", "2");
316-
w.writeRaw("\n ");
317-
w.writeAttribute("c", "3");
307+
w.writeSpace("\n ");
308+
w.writeComment("comment");
318309
w.writeEndElement();
319310
w.writeEndDocument();
320311
w.close();
321312

322313
String xml = strw.toString();
323-
// Verify the literal raw whitespace made it into the start tag,
324-
// between the attributes. (The writer also prepends its own ' '
325-
// before each attribute name, so the exact number of spaces after
326-
// the newline is implementation detail; we just check the newline
327-
// and indentation we asked for is present.)
328-
assertTrue("Expected raw newline+indent between attributes, got: " + xml,
329-
xml.contains("a=\"1\"\n ")
330-
&& xml.contains("b=\"2\"\n "));
331-
332-
// And the result must still parse as well-formed XML with all
333-
// three attributes present.
334-
XMLStreamReader sr = constructNsStreamReader(xml, true);
335-
assertTokenType(START_DOCUMENT, sr.getEventType());
336-
assertTokenType(START_ELEMENT, sr.next());
337-
assertEquals("test", sr.getLocalName());
338-
assertEquals(3, sr.getAttributeCount());
339-
assertEquals("1", sr.getAttributeValue(null, "a"));
340-
assertEquals("2", sr.getAttributeValue(null, "b"));
341-
assertEquals("3", sr.getAttributeValue(null, "c"));
342-
assertTokenType(END_ELEMENT, sr.next());
343-
assertTokenType(END_DOCUMENT, sr.next());
344-
sr.close();
314+
assertEquals("<test>\n <!--comment--></test>", xml);
345315
}
346316
}
347317

0 commit comments

Comments
 (0)