Don't play games with the <meta> tag when not serializing an entire document. Bug 390735, patch by Ryan Jones <sciguyryan@gmail.com>, r+sr=bzbarsky, a=jst

This commit is contained in:
bzbarsky@mit.edu 2007-08-10 17:38:53 -07:00
Родитель 00de428dd3
Коммит 4560e08469
12 изменённых файлов: 63 добавлений и 17 удалений

Просмотреть файл

@ -52,8 +52,8 @@ class nsAString;
/* starting interface: nsIContentSerializer */
#define NS_ICONTENTSERIALIZER_IID \
{ 0x0921afbc, 0x4c6f, 0x4249, \
{ 0xa7, 0xf5, 0x32, 0xe4, 0x91, 0xbf, 0x6e, 0x32 } }
{ 0x34769de0, 0x30d0, 0x4cef, \
{ 0x89, 0x4a, 0xfc, 0xd8, 0xbb, 0x27, 0xc4, 0xb4 } }
class nsIContentSerializer : public nsISupports {
public:
@ -61,7 +61,8 @@ class nsIContentSerializer : public nsISupports {
NS_DECLARE_STATIC_IID_ACCESSOR(NS_ICONTENTSERIALIZER_IID)
NS_IMETHOD Init(PRUint32 flags, PRUint32 aWrapColumn,
const char* aCharSet, PRBool aIsCopying) = 0;
const char* aCharSet, PRBool aIsCopying,
PRBool aIsWholeDocument) = 0;
NS_IMETHOD AppendText(nsIDOMText* aText, PRInt32 aStartOffset,
PRInt32 aEndOffset, nsAString& aStr) = 0;

Просмотреть файл

@ -126,7 +126,8 @@ NS_IMPL_ISUPPORTS4(mozSanitizingHTMLSerializer,
NS_IMETHODIMP
mozSanitizingHTMLSerializer::Init(PRUint32 aFlags, PRUint32 dummy,
const char* aCharSet, PRBool aIsCopying)
const char* aCharSet, PRBool aIsCopying,
PRBool aIsWholeDocument)
{
NS_ENSURE_TRUE(nsContentUtils::GetParserService(), NS_ERROR_UNEXPECTED);
@ -138,7 +139,7 @@ mozSanitizingHTMLSerializer::Initialize(nsAString* aOutString,
PRUint32 aFlags,
const nsAString& allowedTags)
{
nsresult rv = Init(aFlags, 0, nsnull, PR_FALSE);
nsresult rv = Init(aFlags, 0, nsnull, PR_FALSE, PR_FALSE);
NS_ENSURE_SUCCESS(rv, rv);
// XXX This is wrong. It violates XPCOM string ownership rules.

Просмотреть файл

@ -72,7 +72,7 @@ public:
// nsIContentSerializer
NS_IMETHOD Init(PRUint32 flags, PRUint32 dummy, const char* aCharSet,
PRBool aIsCopying);
PRBool aIsCopying, PRBool aIsWholeDocument);
NS_IMETHOD AppendText(nsIDOMText* aText, PRInt32 aStartOffset,
PRInt32 aEndOffset, nsAString& aStr);

Просмотреть файл

@ -893,7 +893,9 @@ nsDocumentEncoder::EncodeToString(nsAString& aOutputString)
NS_ENSURE_SUCCESS(rv, rv);
}
}
mSerializer->Init(mFlags, mWrapColumn, mCharset.get(), mIsCopying);
PRBool isWholeDocument = !(mSelection || mRange || mNode);
mSerializer->Init(mFlags, mWrapColumn, mCharset.get(), mIsCopying, isWholeDocument);
if (mSelection) {
nsCOMPtr<nsIDOMRange> range;

Просмотреть файл

@ -93,6 +93,7 @@ nsHTMLContentSerializer::nsHTMLContentSerializer()
mInBody(PR_FALSE),
mAddSpace(PR_FALSE),
mMayIgnoreLineBreakSequence(PR_FALSE),
mIsWholeDocument(PR_FALSE),
mInCDATA(PR_FALSE),
mNeedLineBreaker(PR_TRUE)
{
@ -112,7 +113,8 @@ nsHTMLContentSerializer::~nsHTMLContentSerializer()
NS_IMETHODIMP
nsHTMLContentSerializer::Init(PRUint32 aFlags, PRUint32 aWrapColumn,
const char* aCharSet, PRBool aIsCopying)
const char* aCharSet, PRBool aIsCopying,
PRBool aIsWholeDocument)
{
mFlags = aFlags;
if (!aWrapColumn) {
@ -122,6 +124,7 @@ nsHTMLContentSerializer::Init(PRUint32 aFlags, PRUint32 aWrapColumn,
mMaxColumn = aWrapColumn;
}
mIsWholeDocument = aIsWholeDocument;
mIsCopying = aIsCopying;
mIsFirstChildOfOL = PR_FALSE;
mDoFormat = (mFlags & nsIDocumentEncoder::OutputFormatted) ? PR_TRUE
@ -636,7 +639,7 @@ nsHTMLContentSerializer::AppendElementStart(nsIDOMElement *aElement,
// We need too skip any meta tags that set the content type
// becase we set our own later.
if (name == nsGkAtoms::meta) {
if (mIsWholeDocument && name == nsGkAtoms::meta) {
nsAutoString header;
content->GetAttr(kNameSpaceID_None, nsGkAtoms::httpEquiv, header);
if (header.LowerCaseEqualsLiteral("content-type")) {
@ -738,7 +741,7 @@ nsHTMLContentSerializer::AppendElementStart(nsIDOMElement *aElement,
mInCDATA = PR_TRUE;
}
if (name == nsGkAtoms::head) {
if (mIsWholeDocument && name == nsGkAtoms::head) {
AppendToString(mLineBreak, aStr);
AppendToString(NS_LITERAL_STRING("<meta http-equiv=\"content-type\""),
aStr);
@ -765,7 +768,7 @@ nsHTMLContentSerializer::AppendElementEnd(nsIDOMElement *aElement,
nsIAtom *name = content->Tag();
// So that we don't mess up the line breaks.
if (name == nsGkAtoms::meta) {
if (mIsWholeDocument && name == nsGkAtoms::meta) {
nsAutoString header;
content->GetAttr(kNameSpaceID_None, nsGkAtoms::httpEquiv, header);
if (header.LowerCaseEqualsLiteral("content-type")) {

Просмотреть файл

@ -58,7 +58,8 @@ class nsHTMLContentSerializer : public nsXMLContentSerializer {
virtual ~nsHTMLContentSerializer();
NS_IMETHOD Init(PRUint32 flags, PRUint32 aWrapColumn,
const char* aCharSet, PRBool aIsCopying);
const char* aCharSet, PRBool aIsCopying,
PRBool aIsWholeDocument);
NS_IMETHOD AppendText(nsIDOMText* aText,
PRInt32 aStartOffset,
@ -139,6 +140,10 @@ class nsHTMLContentSerializer : public nsXMLContentSerializer {
PRPackedBool mAddSpace;
PRPackedBool mMayIgnoreLineBreakSequence;
// This is to ensure that we only do meta tag fixups when dealing with
// whole documents.
PRPackedBool mIsWholeDocument;
// To keep track of First LI child of OL in selected range
PRPackedBool mIsFirstChildOfOL;
PRInt32 mPreLevel;

Просмотреть файл

@ -149,7 +149,8 @@ NS_IMPL_ISUPPORTS4(nsPlainTextSerializer,
NS_IMETHODIMP
nsPlainTextSerializer::Init(PRUint32 aFlags, PRUint32 aWrapColumn,
const char* aCharSet, PRBool aIsCopying)
const char* aCharSet, PRBool aIsCopying,
PRBool aIsWholeDocument)
{
#ifdef DEBUG
// Check if the major control flags are set correctly.
@ -274,7 +275,7 @@ NS_IMETHODIMP
nsPlainTextSerializer::Initialize(nsAString* aOutString,
PRUint32 aFlags, PRUint32 aWrapCol)
{
nsresult rv = Init(aFlags, aWrapCol, nsnull, PR_FALSE);
nsresult rv = Init(aFlags, aWrapCol, nsnull, PR_FALSE, PR_FALSE);
NS_ENSURE_SUCCESS(rv, rv);
// XXX This is wrong. It violates XPCOM string ownership rules.

Просмотреть файл

@ -69,7 +69,8 @@ public:
// nsIContentSerializer
NS_IMETHOD Init(PRUint32 flags, PRUint32 aWrapColumn,
const char* aCharSet, PRBool aIsCopying);
const char* aCharSet, PRBool aIsCopying,
PRBool aIsWholeDocument);
NS_IMETHOD AppendText(nsIDOMText* aText, PRInt32 aStartOffset,
PRInt32 aEndOffset, nsAString& aStr);

Просмотреть файл

@ -93,7 +93,8 @@ NS_IMPL_ISUPPORTS1(nsXMLContentSerializer, nsIContentSerializer)
NS_IMETHODIMP
nsXMLContentSerializer::Init(PRUint32 flags, PRUint32 aWrapColumn,
const char* aCharSet, PRBool aIsCopying)
const char* aCharSet, PRBool aIsCopying,
PRBool aIsWholeDocument)
{
mCharset = aCharSet;
return NS_OK;

Просмотреть файл

@ -60,7 +60,8 @@ class nsXMLContentSerializer : public nsIContentSerializer {
NS_DECL_ISUPPORTS
NS_IMETHOD Init(PRUint32 flags, PRUint32 aWrapColumn,
const char* aCharSet, PRBool aIsCopying);
const char* aCharSet, PRBool aIsCopying,
PRBool aIsWholeDocument);
NS_IMETHOD AppendText(nsIDOMText* aText, PRInt32 aStartOffset,
PRInt32 aEndOffset, nsAString& aStr);

Просмотреть файл

@ -86,6 +86,7 @@ _TEST_FILES = test_bug5141.html \
test_bug373181.xhtml \
test_bug375314.html \
test_bug382113.html \
test_bug390735.html \
bug382113_object.html \
test_CrossSiteXHR.html \
file_CrossSiteXHR_fail1.xml \

Просмотреть файл

@ -0,0 +1,29 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=390735
-->
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Test for Bug 390735</title>
<script type="text/javascript" src="/MochiKit/MochiKit.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>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
/** Test for Bug 390735 **/
var contents = document.getElementsByTagName("head")[0].innerHTML;
var expectedFind = "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">";
ok(contents.indexOf(expectedFind) > -1, "The meta tag element was not found");
</script>
</pre>
</body>
</html>