mirror of
https://github.com/hapifhir/org.hl7.fhir.core.git
synced 2025-02-07 21:38:15 +00:00
fix bug parsing <script> in xhtml - handling < characters
This commit is contained in:
parent
ac4f050bac
commit
7aae2cb525
@ -290,9 +290,12 @@ public class XhtmlComposer {
|
|||||||
if (node.getName() == "head" && node.getElement("meta") == null)
|
if (node.getName() == "head" && node.getElement("meta") == null)
|
||||||
dst.append(indent + " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>" + (pretty && !noPrettyOverride ? "\r\n" : ""));
|
dst.append(indent + " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>" + (pretty && !noPrettyOverride ? "\r\n" : ""));
|
||||||
|
|
||||||
|
if (act && "script".equals(node.getName())) {
|
||||||
for (XhtmlNode c : node.getChildNodes())
|
dst.append(node.allText());
|
||||||
writeNode(indent + " ", c, noPrettyOverride || node.isNoPretty());
|
} else {
|
||||||
|
for (XhtmlNode c : node.getChildNodes())
|
||||||
|
writeNode(indent + " ", c, noPrettyOverride || node.isNoPretty());
|
||||||
|
}
|
||||||
if (act)
|
if (act)
|
||||||
dst.append("</" + node.getName() + ">" + (pretty && !noPrettyOverride ? "\r\n" : ""));
|
dst.append("</" + node.getName() + ">" + (pretty && !noPrettyOverride ? "\r\n" : ""));
|
||||||
else if (node.getChildNodes().get(node.getChildNodes().size() - 1).getNodeType() == NodeType.Text)
|
else if (node.getChildNodes().get(node.getChildNodes().size() - 1).getNodeType() == NodeType.Text)
|
||||||
|
@ -513,7 +513,7 @@ public class XhtmlParser {
|
|||||||
} else {
|
} else {
|
||||||
unwindPoint = null;
|
unwindPoint = null;
|
||||||
List<XhtmlNode> p = new ArrayList<>();
|
List<XhtmlNode> p = new ArrayList<>();
|
||||||
parseElementInner(root, p, nsm, true);
|
parseElementInner(root, p, nsm);
|
||||||
root.setEmptyExpanded(true);
|
root.setEmptyExpanded(true);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
@ -580,17 +580,16 @@ public class XhtmlParser {
|
|||||||
return nodeNamespaceMap.hasDefaultNamespace() && (parentNamespaceMap == null || !nodeNamespaceMap.getDefaultNamespace().equals(parentNamespaceMap.getDefaultNamespace()));
|
return nodeNamespaceMap.hasDefaultNamespace() && (parentNamespaceMap == null || !nodeNamespaceMap.getDefaultNamespace().equals(parentNamespaceMap.getDefaultNamespace()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addTextNode(XhtmlNode node, StringBuilder s)
|
private void addTextNode(XhtmlNode node, StringBuilder s) {
|
||||||
{
|
|
||||||
String t = isTrimWhitespace() ? s.toString().trim() : s.toString();
|
String t = isTrimWhitespace() ? s.toString().trim() : s.toString();
|
||||||
if (t.length() > 0)
|
if (t.length() > 0) {
|
||||||
{
|
|
||||||
lastText = t;
|
lastText = t;
|
||||||
node.addText(t).setLocation(markLocation());
|
node.addText(t).setLocation(markLocation());
|
||||||
s.setLength(0);
|
s.setLength(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void parseElementInner(XhtmlNode node, List<XhtmlNode> parents, NamespaceNormalizationMap nsm, boolean escaping) throws FHIRFormatError, IOException
|
|
||||||
|
private void parseElementInner(XhtmlNode node, List<XhtmlNode> parents, NamespaceNormalizationMap nsm) throws FHIRFormatError, IOException
|
||||||
{
|
{
|
||||||
StringBuilder s = new StringBuilder();
|
StringBuilder s = new StringBuilder();
|
||||||
while (peekChar() != END_OF_CHARS && !parents.contains(unwindPoint) && !(node == unwindPoint))
|
while (peekChar() != END_OF_CHARS && !parents.contains(unwindPoint) && !(node == unwindPoint))
|
||||||
@ -659,6 +658,23 @@ public class XhtmlParser {
|
|||||||
addTextNode(node, s);
|
addTextNode(node, s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void parseScriptInner(XhtmlNode node) throws FHIRFormatError, IOException {
|
||||||
|
StringBuilder s = new StringBuilder();
|
||||||
|
while (peekChar() != END_OF_CHARS && !s.toString().endsWith("</script>")) {
|
||||||
|
s.append(readChar());
|
||||||
|
}
|
||||||
|
String ss = s.toString();
|
||||||
|
if (ss.length() >= 9) {
|
||||||
|
ss = ss.substring(0, ss.length()-9);
|
||||||
|
}
|
||||||
|
String t = isTrimWhitespace() ? ss.trim() : ss;
|
||||||
|
if (t.length() > 0) {
|
||||||
|
lastText = t;
|
||||||
|
node.addText(t).setLocation(markLocation());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void parseElement(XhtmlNode parent, List<XhtmlNode> parents, NamespaceNormalizationMap namespaceMap) throws IOException, FHIRFormatError
|
private void parseElement(XhtmlNode parent, List<XhtmlNode> parents, NamespaceNormalizationMap namespaceMap) throws IOException, FHIRFormatError
|
||||||
{
|
{
|
||||||
markLocation();
|
markLocation();
|
||||||
@ -676,9 +692,11 @@ public class XhtmlParser {
|
|||||||
throw new FHIRFormatError("unexpected non-end of element "+name+" "+descLoc());
|
throw new FHIRFormatError("unexpected non-end of element "+name+" "+descLoc());
|
||||||
readChar();
|
readChar();
|
||||||
node.setEmptyExpanded(false);
|
node.setEmptyExpanded(false);
|
||||||
|
} else if ("script".equals(name.getName())) {
|
||||||
|
parseScriptInner(node);
|
||||||
} else {
|
} else {
|
||||||
node.setEmptyExpanded(true);
|
node.setEmptyExpanded(true);
|
||||||
parseElementInner(node, newParents, namespaceMap, "script".equals(name.getName()));
|
parseElementInner(node, newParents, namespaceMap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1341,7 +1359,7 @@ public class XhtmlParser {
|
|||||||
result.setName(n);
|
result.setName(n);
|
||||||
unwindPoint = null;
|
unwindPoint = null;
|
||||||
List<XhtmlNode> p = new ArrayList<>();
|
List<XhtmlNode> p = new ArrayList<>();
|
||||||
parseElementInner(result, p, null, true);
|
parseElementInner(result, p, null);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,38 @@
|
|||||||
package org.hl7.fhir.utilities.xhtml;
|
package org.hl7.fhir.utilities.xhtml;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.hl7.fhir.exceptions.FHIRFormatError;
|
||||||
|
|
||||||
public class XhtmlTests {
|
public class XhtmlTests {
|
||||||
|
|
||||||
|
private static final String SOURCE_SCRIPT =
|
||||||
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"+
|
||||||
|
"<!DOCTYPE HTML>\r\n"+
|
||||||
|
"<html xml:lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\">\r\n"+
|
||||||
|
" <head>\r\n"+
|
||||||
|
" <title>This</title>\r\n"+
|
||||||
|
" <script type=\"text/javascript\" src=\"fhir-table-scripts.js\"> </script>\r\n"+
|
||||||
|
" </head>\r\n"+
|
||||||
|
" <body onload=\"document.body.style.opacity='1'\">\r\n"+
|
||||||
|
" <script src=\"assets/js/prism.js\"></script>\r\n"+
|
||||||
|
"<script>\r\n"+
|
||||||
|
" var statements = document.getElementById(\"statements\");\r\n"+
|
||||||
|
" var requirements = statements.getElementsByClassName(\"requirement\");\r\n"+
|
||||||
|
" for(var req of requirements) {\r\n"+
|
||||||
|
" req.innerHTML = req.innerHTML.replace(/\\[\\[([^\\]]+)\\]\\]/g, '<a href=\"Requirements-EHRSFMR2.1-\\$1.html\">\\$1</a>')\r\n"+
|
||||||
|
" }\r\n"+
|
||||||
|
" var description = document.getElementById(\"description\");\r\n"+
|
||||||
|
" description.innerHTML = description.innerHTML.replace(/</g,'<').replace(/>/g,'>');\r\n"+
|
||||||
|
"</script>\r\n"+
|
||||||
|
" </body>\r\n"+
|
||||||
|
"</html>\r\n";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testToStringOnNullType()
|
public void testToStringOnNullType()
|
||||||
{
|
{
|
||||||
@ -13,4 +40,10 @@ public class XhtmlTests {
|
|||||||
String actual = node.toString();
|
String actual = node.toString();
|
||||||
assertTrue(actual.startsWith("org.hl7.fhir.utilities.xhtml.XhtmlNode@"), "toString() should return java the toString default method for objects, which starts with the full class name");
|
assertTrue(actual.startsWith("org.hl7.fhir.utilities.xhtml.XhtmlNode@"), "toString() should return java the toString default method for objects, which starts with the full class name");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParseScript() throws FHIRFormatError, IOException {
|
||||||
|
XhtmlNode x = new XhtmlParser().setMustBeWellFormed(true).parse(SOURCE_SCRIPT, "html");
|
||||||
|
Assertions.assertTrue(x != null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user