Bug 222667: Add newlines between root-nodes in serializer rather then encoder.

r=peterv sr=jst
This commit is contained in:
sicking%bigfoot.com 2003-10-23 22:24:21 +00:00
Родитель d39df80ec1
Коммит b3ff999e6a
4 изменённых файлов: 55 добавлений и 15 удалений

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

@ -346,15 +346,6 @@ nsDocumentEncoder::SerializeNodeEnd(nsIDOMNode* aNode,
} }
} }
nsCOMPtr<nsIDOMNode> parent;
aNode->GetParentNode(getter_AddRefs(parent));
if (parent) {
parent->GetNodeType(&type);
if (type == nsIDOMNode::DOCUMENT_NODE) {
aStr.Append((PRUnichar)'\n');
}
}
return NS_OK; return NS_OK;
} }
@ -929,7 +920,6 @@ nsDocumentEncoder::EncodeToString(nsAString& aOutputString)
rv = mSerializer->AppendDocumentStart(domdoc, aOutputString); rv = mSerializer->AppendDocumentStart(domdoc, aOutputString);
if (NS_SUCCEEDED(rv)) { if (NS_SUCCEEDED(rv)) {
aOutputString.Append((PRUnichar)'\n');
nsCOMPtr<nsIDOMNode> doc(do_QueryInterface(mDocument)); nsCOMPtr<nsIDOMNode> doc(do_QueryInterface(mDocument));
rv = SerializeToStringRecursive(doc, aOutputString); rv = SerializeToStringRecursive(doc, aOutputString);

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

@ -702,6 +702,12 @@ nsHTMLContentSerializer::AppendElementStart(nsIDOMElement *aElement,
AppendToString(PRUnichar(' '), aStr); AppendToString(PRUnichar(' '), aStr);
mAddSpace = PR_FALSE; mAddSpace = PR_FALSE;
} }
else {
MaybeAddNewline(aStr);
}
// Always reset to avoid false newlines in case MaybeAddNewline wasn't
// called
mAddNewline = PR_FALSE;
StartIndentation(name, hasDirtyAttr, aStr); StartIndentation(name, hasDirtyAttr, aStr);
@ -847,6 +853,9 @@ nsHTMLContentSerializer::AppendElementEnd(nsIDOMElement *aElement,
mMayIgnoreLineBreakSequence = PR_TRUE; mMayIgnoreLineBreakSequence = PR_TRUE;
mColPos = 0; mColPos = 0;
} }
else {
MaybeFlagNewline(aElement);
}
mInCDATA = PR_FALSE; mInCDATA = PR_FALSE;

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

@ -74,9 +74,10 @@ nsresult NS_NewXMLContentSerializer(nsIContentSerializer** aSerializer)
} }
nsXMLContentSerializer::nsXMLContentSerializer() nsXMLContentSerializer::nsXMLContentSerializer()
: mPrefixIndex(0),
mInAttribute(PR_FALSE),
mAddNewline(PR_FALSE)
{ {
mPrefixIndex = 0;
mInAttribute = PR_FALSE;
} }
nsXMLContentSerializer::~nsXMLContentSerializer() nsXMLContentSerializer::~nsXMLContentSerializer()
@ -174,6 +175,8 @@ nsXMLContentSerializer::AppendProcessingInstruction(nsIDOMProcessingInstruction*
nsresult rv; nsresult rv;
nsAutoString target, data; nsAutoString target, data;
MaybeAddNewline(aStr);
rv = aPI->GetTarget(target); rv = aPI->GetTarget(target);
if (NS_FAILED(rv)) return NS_ERROR_FAILURE; if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
@ -187,6 +190,7 @@ nsXMLContentSerializer::AppendProcessingInstruction(nsIDOMProcessingInstruction*
AppendToString(data, aStr); AppendToString(data, aStr);
} }
AppendToString(NS_LITERAL_STRING("?>"), aStr); AppendToString(NS_LITERAL_STRING("?>"), aStr);
MaybeFlagNewline(aPI);
return NS_OK; return NS_OK;
} }
@ -204,6 +208,8 @@ nsXMLContentSerializer::AppendComment(nsIDOMComment* aComment,
rv = aComment->GetData(data); rv = aComment->GetData(data);
if (NS_FAILED(rv)) return NS_ERROR_FAILURE; if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
MaybeAddNewline(aStr);
AppendToString(NS_LITERAL_STRING("<!--"), aStr); AppendToString(NS_LITERAL_STRING("<!--"), aStr);
if (aStartOffset || (aEndOffset != -1)) { if (aStartOffset || (aEndOffset != -1)) {
PRInt32 length = (aEndOffset == -1) ? data.Length() : aEndOffset; PRInt32 length = (aEndOffset == -1) ? data.Length() : aEndOffset;
@ -217,6 +223,7 @@ nsXMLContentSerializer::AppendComment(nsIDOMComment* aComment,
AppendToString(data, aStr); AppendToString(data, aStr);
} }
AppendToString(NS_LITERAL_STRING("-->"), aStr); AppendToString(NS_LITERAL_STRING("-->"), aStr);
MaybeFlagNewline(aComment);
return NS_OK; return NS_OK;
} }
@ -238,6 +245,8 @@ nsXMLContentSerializer::AppendDoctype(nsIDOMDocumentType *aDoctype,
rv = aDoctype->GetInternalSubset(internalSubset); rv = aDoctype->GetInternalSubset(internalSubset);
if (NS_FAILED(rv)) return NS_ERROR_FAILURE; if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
MaybeAddNewline(aStr);
AppendToString(NS_LITERAL_STRING("<!DOCTYPE "), aStr); AppendToString(NS_LITERAL_STRING("<!DOCTYPE "), aStr);
AppendToString(name, aStr); AppendToString(name, aStr);
PRUnichar quote; PRUnichar quote;
@ -286,6 +295,7 @@ nsXMLContentSerializer::AppendDoctype(nsIDOMDocumentType *aDoctype,
} }
AppendToString(PRUnichar('>'), aStr); AppendToString(PRUnichar('>'), aStr);
MaybeFlagNewline(aDoctype);
return NS_OK; return NS_OK;
} }
@ -516,6 +526,8 @@ nsXMLContentSerializer::AppendElementStart(nsIDOMElement *aElement,
PRBool addNSAttr; PRBool addNSAttr;
MaybeAddNewline(aStr);
addNSAttr = ConfirmPrefix(tagPrefix, tagNamespaceURI); addNSAttr = ConfirmPrefix(tagPrefix, tagNamespaceURI);
// Serialize the qualified name of the element // Serialize the qualified name of the element
AppendToString(NS_LITERAL_STRING("<"), aStr); AppendToString(NS_LITERAL_STRING("<"), aStr);
@ -587,6 +599,7 @@ nsXMLContentSerializer::AppendElementStart(nsIDOMElement *aElement,
// We don't output a separate end tag for empty element // We don't output a separate end tag for empty element
if (!aHasChildren) { if (!aHasChildren) {
AppendToString(NS_LITERAL_STRING("/>"), aStr); AppendToString(NS_LITERAL_STRING("/>"), aStr);
MaybeFlagNewline(aElement);
} else { } else {
AppendToString(NS_LITERAL_STRING(">"), aStr); AppendToString(NS_LITERAL_STRING(">"), aStr);
} }
@ -625,6 +638,7 @@ nsXMLContentSerializer::AppendElementEnd(nsIDOMElement *aElement,
} }
AppendToString(tagLocalName, aStr); AppendToString(tagLocalName, aStr);
AppendToString(NS_LITERAL_STRING(">"), aStr); AppendToString(NS_LITERAL_STRING(">"), aStr);
MaybeFlagNewline(aElement);
PopNameSpaceDeclsFor(aElement); PopNameSpaceDeclsFor(aElement);
@ -809,6 +823,27 @@ nsXMLContentSerializer::IsShorthandAttr(const nsIAtom* aAttrName,
return PR_FALSE; return PR_FALSE;
} }
void
nsXMLContentSerializer::MaybeAddNewline(nsAString& aStr)
{
if (mAddNewline) {
aStr.Append((PRUnichar)'\n');
mAddNewline = PR_FALSE;
}
}
void
nsXMLContentSerializer::MaybeFlagNewline(nsIDOMNode* aNode)
{
nsCOMPtr<nsIDOMNode> parent;
aNode->GetParentNode(getter_AddRefs(parent));
if (parent) {
PRUint16 type;
parent->GetNodeType(&type);
mAddNewline = type == nsIDOMNode::DOCUMENT_NODE;
}
}
NS_IMETHODIMP NS_IMETHODIMP
nsXMLContentSerializer::AppendDocumentStart(nsIDOMDocument *aDocument, nsXMLContentSerializer::AppendDocumentStart(nsIDOMDocument *aDocument,
nsAString& aStr) nsAString& aStr)
@ -839,6 +874,7 @@ nsXMLContentSerializer::AppendDocumentStart(nsIDOMDocument *aDocument,
} }
aStr += NS_LITERAL_STRING("?>"); aStr += NS_LITERAL_STRING("?>");
mAddNewline = PR_TRUE;
return NS_OK; return NS_OK;
} }

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

@ -90,8 +90,7 @@ class nsXMLContentSerializer : public nsIContentSerializer {
virtual void AppendToString(const PRUnichar* aStr, virtual void AppendToString(const PRUnichar* aStr,
PRInt32 aLength, PRInt32 aLength,
nsAString& aOutputStr); nsAString& aOutputStr);
virtual void AppendToString(const PRUnichar aChar, virtual void AppendToString(const PRUnichar aChar, nsAString& aOutputStr);
nsAString& aOutputStr);
virtual void AppendToString(const nsAString& aStr, virtual void AppendToString(const nsAString& aStr,
nsAString& aOutputStr, nsAString& aOutputStr,
PRBool aTranslateEntities = PR_FALSE, PRBool aTranslateEntities = PR_FALSE,
@ -116,9 +115,15 @@ class nsXMLContentSerializer : public nsIContentSerializer {
PRBool IsShorthandAttr(const nsIAtom* aAttrName, PRBool IsShorthandAttr(const nsIAtom* aAttrName,
const nsIAtom* aElementName); const nsIAtom* aElementName);
// Functions to check for newlines that needs to be added between nodes in
// the root of a document.
void MaybeAddNewline(nsAString& aStr);
void MaybeFlagNewline(nsIDOMNode* aNode);
PRInt32 mPrefixIndex; PRInt32 mPrefixIndex;
nsVoidArray mNameSpaceStack; nsVoidArray mNameSpaceStack;
PRBool mInAttribute; PRPackedBool mInAttribute;
PRPackedBool mAddNewline;
}; };
nsresult nsresult