Bug 541937 - XMLSerializer should serialize content of link tags. r=Olli.Pettay sr=bzbarsky

This commit is contained in:
Laurent Jouanneau 2010-03-23 21:55:39 +01:00
parent be4c9ad69d
commit ebf772c85a
6 changed files with 188 additions and 20 deletions

View File

@ -461,18 +461,26 @@ nsXHTMLContentSerializer::AppendEndOfElementStart(nsIDOMElement *aOriginalElemen
return;
}
nsIParserService* parserService = nsContentUtils::GetParserService();
nsCOMPtr<nsIContent> content = do_QueryInterface(aOriginalElement);
if (parserService) {
PRBool isContainer;
parserService->IsContainer(parserService->HTMLAtomTagToId(aName),
isContainer);
if (!isContainer) {
// for backward compatibility with HTML 4 user agents
// only non-container HTML elements can be closed immediatly,
// and a space is added before />
AppendToString(NS_LITERAL_STRING(" />"), aStr);
return;
// for non empty elements, even if they are not a container, we always
// serialize their content, because the XHTML element could contain non XHTML
// nodes useful in some context, like in an XSLT stylesheet
if (HasNoChildren(content)) {
nsIParserService* parserService = nsContentUtils::GetParserService();
if (parserService) {
PRBool isContainer;
parserService->IsContainer(parserService->HTMLAtomTagToId(aName),
isContainer);
if (!isContainer) {
// for backward compatibility with HTML 4 user agents
// only non-container HTML elements can be closed immediatly,
// and a space is added before />
AppendToString(NS_LITERAL_STRING(" />"), aStr);
return;
}
}
}
AppendToString(kGreaterThan, aStr);
@ -605,17 +613,19 @@ nsXHTMLContentSerializer::CheckElementEnd(nsIContent * aContent,
}
}
nsIParserService* parserService = nsContentUtils::GetParserService();
if (HasNoChildren(aContent)) {
nsIParserService* parserService = nsContentUtils::GetParserService();
if (parserService) {
PRBool isContainer;
if (parserService) {
PRBool isContainer;
parserService->IsContainer(parserService->HTMLAtomTagToId(name),
isContainer);
if (!isContainer) {
// non-container HTML elements are already closed,
// see AppendEndOfElementStart
return PR_FALSE;
parserService->IsContainer(parserService->HTMLAtomTagToId(name),
isContainer);
if (!isContainer) {
// non-container HTML elements are already closed,
// see AppendEndOfElementStart
return PR_FALSE;
}
}
}
// for backward compatibility with old HTML user agents,
@ -1066,3 +1076,22 @@ nsXHTMLContentSerializer::IsFirstChildOfOL(nsIDOMElement* aElement)
else
return PR_FALSE;
}
PRBool
nsXHTMLContentSerializer::HasNoChildren(nsIContent * aContent) {
PRUint32 i, childCount = aContent->GetChildCount();
for (i = 0; i < childCount; ++i) {
nsIContent* child = aContent->GetChildAt(i);
if (!child->IsNodeOfType(nsINode::eTEXT))
return PR_FALSE;
if (child->TextLength())
return PR_FALSE;
}
return PR_TRUE;
}

View File

@ -200,6 +200,7 @@ class nsXHTMLContentSerializer : public nsXMLContentSerializer {
// Stack to store one olState struct per <OL>.
nsAutoTArray<olState, 8> mOLStateStack;
PRBool HasNoChildren(nsIContent* aContent);
};
nsresult

View File

@ -362,6 +362,9 @@ _TEST_FILES = test_bug5141.html \
test_bug545644.html \
test_bug545644.xhtml \
test_bug553896.xhtml \
test_bug541937.html \
file_bug541937.html \
file_bug541937.xhtml \
$(NULL)
# This test fails on the Mac for some reason

View File

@ -0,0 +1,8 @@
<!DOCTYPE html><html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Test</title>
<link rel="Top" href=""> foo </link>
</head><body>
<p>Hello world</p>
</body>
</html>

View File

@ -0,0 +1,12 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Test</title>
<link rel="Top" href=""> foo </link>
</head>
<body>
<p>Hello world</p>
</body>
</html>

View File

@ -0,0 +1,115 @@
<!DOCTYPE HTML>
<html>
<!--
-->
<head>
<title>Test for XHTML serializer, bug 541937</title>
<script type="text/javascript" src="/MochiKit/packed.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=541937">Mozilla Bug </a>
<p id="display"></p>
<div id="content" style="display: none">
<iframe id="testframe" src="file_bug541937.html">
</iframe>
<iframe id="testframe2" src="file_bug541937.xhtml">
</iframe>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
function testSerializer () {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
const de = Components.interfaces.nsIDocumentEncoder
var encoder = Components.classes["@mozilla.org/layout/documentEncoder;1?type=text/html"]
.createInstance(Components.interfaces.nsIDocumentEncoder);
var parser = new DOMParser();
var serializer = new XMLSerializer();
// with content
var str = '<?xml version="1.0"?><doc>\n<link xmlns="http://www.w3.org/1999/xhtml"><!-- child nodes --> \n<content xmlns=""/></link>\n</doc>';
var expected = '<?xml version="1.0" encoding="UTF-8"?>\n<doc>\n<link xmlns="http://www.w3.org/1999/xhtml"><!-- child nodes --> \n<content xmlns=""/></link>\n</doc>';
var doc = parser.parseFromString(str,"application/xml");
var result = serializer.serializeToString(doc);
is(result, expected, "serialization of a link element inside an xml document with children");
// with only whitespaces
str = '<?xml version="1.0"?><doc>\n<link xmlns="http://www.w3.org/1999/xhtml"> \n </link>\n</doc>';
expected = '<?xml version="1.0" encoding="UTF-8"?>\n<doc>\n<link xmlns="http://www.w3.org/1999/xhtml"> \n </link>\n</doc>';
doc = parser.parseFromString(str,"application/xml");
result = serializer.serializeToString(doc);
is(result, expected, "serialization of a link element with only whitespaces as content, inside an xml document");
// with only one space as content
str = '<?xml version="1.0"?><doc>\n<link xmlns="http://www.w3.org/1999/xhtml"> </link>\n</doc>';
expected = '<?xml version="1.0" encoding="UTF-8"?>\n<doc>\n<link xmlns="http://www.w3.org/1999/xhtml"> </link>\n</doc>';
doc = parser.parseFromString(str,"application/xml");
result = serializer.serializeToString(doc);
is(result, expected, "serialization of a link element with only one space as content, inside an xml document");
// let's remove the content
str = '<?xml version="1.0"?><doc>\n<link xmlns="http://www.w3.org/1999/xhtml"> <!-- child nodes --> \ndeleted content<content xmlns=""/> </link>\n</doc>';
expected = '<?xml version="1.0" encoding="UTF-8"?>\n<doc>\n<link xmlns="http://www.w3.org/1999/xhtml" />\n</doc>';
doc = parser.parseFromString(str,"application/xml");
doc.documentElement.firstElementChild.textContent = '';
result = serializer.serializeToString(doc);
is(result, expected, "serialization of a link element on which we removed dynamically the content, inside an xml document");
// with no content but an ended tag
str = '<?xml version="1.0"?><doc>\n<link xmlns="http://www.w3.org/1999/xhtml"></link>\n</doc>';
expected = '<?xml version="1.0" encoding="UTF-8"?>\n<doc>\n<link xmlns="http://www.w3.org/1999/xhtml" />\n</doc>';
doc = parser.parseFromString(str,"application/xml");
result = serializer.serializeToString(doc);
is(result, expected, "serialization of a link element with no content but with an ended tag, inside an xml document");
// with no content
str = '<?xml version="1.0"?><doc>\n<link xmlns="http://www.w3.org/1999/xhtml"/>\n</doc>';
expected = '<?xml version="1.0" encoding="UTF-8"?>\n<doc>\n<link xmlns="http://www.w3.org/1999/xhtml" />\n</doc>';
doc = parser.parseFromString(str,"application/xml");
result = serializer.serializeToString(doc);
is(result, expected, "serialization of a link element with no content, inside an xml document");
doc = $("testframe").contentDocument;
encoder.init(doc, "text/html", de.OutputLFLineBreak);
encoder.setCharset("UTF-8");
result = encoder.encodeToString();
expected = '<!DOCTYPE HTML>\n<html><head><meta http-equiv=\"content-type\" content=\"text/html; \ncharset=UTF-8\">\n <title>Test</title>\n';
expected += ' <link rel=\"Top\" href=\"\"> ';
expected += ' </head><body>foo \n\n\n <p>Hello world</p>\n</body></html>';
is(result, expected, "serialization of a link element with content, inside an html document");
doc = $("testframe2").contentDocument;
encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak);
encoder.setCharset("UTF-8");
result = encoder.encodeToString();
expected = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n';
expected += '<html xmlns="http://www.w3.org/1999/xhtml">\n<head>\n <meta http-equiv="content-type" content="text/html; charset=UTF-8" />\n <title>Test</title>\n';
expected += ' <link rel="Top" href=""> foo </link>\n';
expected += '\n</head>\n<body>\n <p>Hello world</p>\n</body>\n</html>';
is(result, expected, "serialization of a link element with content, inside an xhtml document");
SimpleTest.finish();
}
SimpleTest.waitForExplicitFinish();
addLoadEvent(testSerializer);
</script>
</pre>
</body>
</html>