Skip to content

Commit 83bce94

Browse files
Masanori YanoJoeWang-Java
authored andcommitted
8268457: XML Transformer outputs Unicode supplementary character incorrectly to HTML
Reviewed-by: lancea, naoto, iris, joehw
1 parent 1810b1c commit 83bce94

7 files changed

Lines changed: 173 additions & 26 deletions

File tree

src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/ToHTMLStream.java

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
33
*/
44
/*
55
* Licensed to the Apache Software Foundation (ASF) under one or more
@@ -41,7 +41,7 @@
4141
* because it is used from another package.
4242
*
4343
* @xsl.usage internal
44-
* @LastModified: Aug 2019
44+
* @LastModified: June 2021
4545
*/
4646
public final class ToHTMLStream extends ToStream
4747
{
@@ -1441,32 +1441,23 @@ else if (
14411441
}
14421442
}
14431443
}
1444-
1445-
// The next is kind of a hack to keep from escaping in the case
1446-
// of Shift_JIS and the like.
1447-
1448-
/*
1449-
else if ((ch < m_maxCharacter) && (m_maxCharacter == 0xFFFF)
1450-
&& (ch != 160))
1451-
{
1452-
writer.write(ch); // no escaping in this case
1453-
}
1454-
else
1455-
*/
1456-
String outputStringForChar = m_charInfo.getOutputStringForChar(ch);
1457-
if (null != outputStringForChar)
1458-
{
1459-
writer.write(outputStringForChar);
1460-
}
1461-
else if (escapingNotNeeded(ch))
1462-
{
1463-
writer.write(ch); // no escaping in this case
1464-
}
14651444
else
14661445
{
1467-
writer.write("&#");
1468-
writer.write(Integer.toString(ch));
1469-
writer.write(';');
1446+
String outputStringForChar = m_charInfo.getOutputStringForChar(ch);
1447+
if (null != outputStringForChar)
1448+
{
1449+
writer.write(outputStringForChar);
1450+
}
1451+
else if (escapingNotNeeded(ch))
1452+
{
1453+
writer.write(ch); // no escaping in this case
1454+
}
1455+
else
1456+
{
1457+
writer.write("&#");
1458+
writer.write(Integer.toString(ch));
1459+
writer.write(';');
1460+
}
14701461
}
14711462
}
14721463
cleanStart = i + 1;
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
package transform;
25+
26+
import java.io.File;
27+
import java.io.FileInputStream;
28+
import java.io.FileOutputStream;
29+
import java.io.FileReader;
30+
import java.io.InputStream;
31+
32+
import javax.xml.parsers.SAXParser;
33+
import javax.xml.parsers.SAXParserFactory;
34+
import javax.xml.transform.Result;
35+
import javax.xml.transform.Source;
36+
import javax.xml.transform.Transformer;
37+
import javax.xml.transform.TransformerFactory;
38+
import javax.xml.transform.stream.StreamResult;
39+
import javax.xml.transform.stream.StreamSource;
40+
41+
import org.xml.sax.Attributes;
42+
import org.xml.sax.SAXException;
43+
import org.xml.sax.helpers.DefaultHandler;
44+
45+
import static jaxp.library.JAXPTestUtilities.compareWithGold;
46+
import static jaxp.library.JAXPTestUtilities.compareStringWithGold;
47+
import org.testng.Assert;
48+
import org.testng.annotations.Listeners;
49+
import org.testng.annotations.Test;
50+
51+
/*
52+
* @test
53+
* @bug 8268457
54+
* @library /javax/xml/jaxp/libs
55+
* @run testng transform.SurrogateTest
56+
* @summary XML Transformer outputs Unicode supplementary character incorrectly to HTML
57+
*/
58+
@Listeners({jaxp.library.FilePolicy.class})
59+
public class SurrogateTest {
60+
61+
final static String TEST_SRC = System.getProperty("test.src", ".");
62+
63+
@Test
64+
public void toHTMLTest() throws Exception {
65+
String out = "SurrogateTest1out.html";
66+
String expected = TEST_SRC + File.separator + "SurrogateTest1.html";
67+
String xsl = TEST_SRC + File.separator + "SurrogateTest1.xsl";
68+
69+
try (FileInputStream tFis = new FileInputStream(xsl);
70+
InputStream fis = this.getClass().getResourceAsStream("SurrogateTest1.xml");
71+
FileOutputStream fos = new FileOutputStream(out)) {
72+
73+
Source tSrc = new StreamSource(tFis);
74+
TransformerFactory tf = TransformerFactory.newInstance();
75+
Transformer t = tf.newTransformer(tSrc);
76+
t.setOutputProperty("method", "html");
77+
78+
Source src = new StreamSource(fis);
79+
Result res = new StreamResult(fos);
80+
t.transform(src, res);
81+
}
82+
compareWithGold(expected, out);
83+
}
84+
85+
@Test
86+
public void handlerTest() throws Exception {
87+
File xmlFile = new File(TEST_SRC, "SurrogateTest2.xml");
88+
SAXParserFactory spf = SAXParserFactory.newInstance();
89+
spf.setNamespaceAware(true);
90+
SAXParser sp = spf.newSAXParser();
91+
TestHandler th = new TestHandler();
92+
sp.parse(xmlFile, th);
93+
compareStringWithGold(TEST_SRC + File.separator + "SurrogateTest2.txt", th.sb.toString());
94+
}
95+
96+
private static class TestHandler extends DefaultHandler {
97+
private StringBuilder sb = new StringBuilder();
98+
99+
@Override
100+
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
101+
sb.append( localName + "@attr:" + attributes.getValue("attr") + '\n');
102+
}
103+
}
104+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2+
<html>
3+
<head>
4+
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
5+
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
6+
</head>
7+
<body>
8+
<form>
9+
<input id="tag1" value="𠮟">
10+
</form>
11+
</body>
12+
</html>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<root>
3+
<tag1>𠮟</tag1>
4+
</root>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
3+
<xsl:output doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"
4+
doctype-system="http://www.w3.org/TR/html4/loose.dtd"
5+
encoding="UTF-8" indent="yes" method="html" omit-xml-declaration="yes"/>
6+
<xsl:template match="/">
7+
<html>
8+
<head>
9+
<META http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
10+
</head>
11+
<body>
12+
<xsl:for-each select="root">
13+
<form>
14+
<xsl:for-each select="tag1">
15+
<input id="tag1">
16+
<xsl:attribute name="value">
17+
<xsl:value-of select="."/>
18+
</xsl:attribute>
19+
</input>
20+
</xsl:for-each>
21+
</form>
22+
</xsl:for-each>
23+
</body>
24+
</html>
25+
</xsl:template>
26+
</xsl:stylesheet>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
root@attr:null
2+
tag1@attr:𠮟
3+
tag2@attr:𠀋
4+
tag3@attr:𣱿
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<root>
3+
<tag1 attr="𠮟"/>
4+
<tag2 attr="𠀋"/>
5+
<tag3 attr="𣱿"/>
6+
</root>

0 commit comments

Comments
 (0)