/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* * nsIContentSerializer implementation that can be used with an * nsIDocumentEncoder to convert an XHTML (not HTML!) DOM to an XHTML * string that could be parsed into more or less the original DOM.
*/
nsXHTMLContentSerializer::~nsXHTMLContentSerializer() {
NS_ASSERTION(mOLStateStack.IsEmpty(), "Expected OL State stack to be empty");
}
NS_IMETHODIMP
nsXHTMLContentSerializer::Init(uint32_t aFlags, uint32_t aWrapColumn, const Encoding* aEncoding, bool aIsCopying, bool aRewriteEncodingDeclaration, bool* aNeedsPreformatScanning,
nsAString& aOutput) { // The previous version of the HTML serializer did implicit wrapping // when there is no flags, so we keep wrapping in order to keep // compatibility with the existing calling code // XXXLJ perhaps should we remove this default settings later ? if (aFlags & nsIDocumentEncoder::OutputFormatted) {
aFlags = aFlags | nsIDocumentEncoder::OutputWrap;
}
// this method is not called by nsHTMLContentSerializer // so we don't have to check HTML element, just XHTML
if (mIsCopying && kNameSpaceID_XHTML == contentNamespaceID) { // Need to keep track of OL and LI elements in order to get ordinal number // for the LI. if (aTagName == nsGkAtoms::ol) { // We are copying and current node is an OL; // Store its start attribute value in olState->startVal.
nsAutoString start;
int32_t startAttrVal = 0;
aElement->GetAttr(nsGkAtoms::start, start); if (!start.IsEmpty()) {
nsresult rv = NS_OK;
startAttrVal = start.ToInteger(&rv); // If OL has "start" attribute, first LI element has to start with that // value Therefore subtracting 1 as all the LI elements are incrementing // it before using it; In failure of ToInteger(), default StartAttrValue // to 0. if (NS_SUCCEEDED(rv))
--startAttrVal; else
startAttrVal = 0;
}
olState state(startAttrVal, true);
mOLStateStack.AppendElement(state);
} elseif (aTagName == nsGkAtoms::li) {
mIsFirstChildOfOL = IsFirstChildOfOL(aOriginalElement); if (mIsFirstChildOfOL) { // If OL is parent of this LI, serialize attributes in different manner.
NS_ENSURE_TRUE(SerializeLIValueAttribute(aElement, aStr), false);
}
}
}
// If we had to add a new namespace declaration, serialize // and push it on the namespace stack if (aAddNSAttr) { if (aTagPrefix.IsEmpty()) { // Serialize default namespace decl
NS_ENSURE_TRUE(
SerializeAttr(u""_ns, xmlnsStr, aTagNamespaceURI, aStr, true), false);
} else { // Serialize namespace decl
NS_ENSURE_TRUE(
SerializeAttr(xmlnsStr, aTagPrefix, aTagNamespaceURI, aStr, true), false);
}
PushNameSpaceDecl(aTagPrefix, aTagNamespaceURI, aOriginalElement);
}
count = aElement->GetAttrCount();
// Now serialize each of the attributes // XXX Unfortunately we need a namespace manager to get // attribute URIs. for (index = 0; index < count; index++) { if (aSkipAttr == index) { continue;
}
dom::BorrowedAttrInfo info = aElement->GetAttrInfoAt(index); const nsAttrName* name = info.mName;
// Filter out any attribute starting with [-|_]moz
nsDependentAtomString attrNameStr(attrName); if (StringBeginsWith(attrNameStr, u"_moz"_ns) ||
StringBeginsWith(attrNameStr, u"-moz"_ns)) { continue;
}
if (attrPrefix) {
attrPrefix->ToString(prefixStr);
} else {
prefixStr.Truncate();
}
if (namespaceID == kNameSpaceID_None &&
((attrName == nsGkAtoms::href) || (attrName == nsGkAtoms::src))) { // Make all links absolute when converting only the selection: if (mFlags & nsIDocumentEncoder::OutputAbsoluteLinks) { // Would be nice to handle OBJECT tags, // but that gets more complicated since we have to // search the tag list for CODEBASE as well. // For now, just leave them relative.
nsIURI* uri = aElement->GetBaseURI(); if (uri) {
nsAutoString absURI;
rv = NS_MakeAbsoluteURI(absURI, valueStr, uri); if (NS_SUCCEEDED(rv)) {
valueStr = absURI;
}
}
}
}
if (mRewriteEncodingDeclaration && aTagName == nsGkAtoms::meta &&
attrName == nsGkAtoms::content) { // If we're serializing a <meta http-equiv="content-type">, // use the proper value, rather than what's in the document.
nsAutoString header;
aElement->GetAttr(nsGkAtoms::httpEquiv, header); if (header.LowerCaseEqualsLiteral("content-type")) {
valueStr =
u"text/html; charset="_ns + NS_ConvertASCIItoUTF16(mCharset);
}
}
if (addNSAttr) {
NS_ASSERTION(!prefixStr.IsEmpty(), "Namespaced attributes must have a prefix");
NS_ENSURE_TRUE(SerializeAttr(xmlnsStr, prefixStr, uriStr, aStr, true), false);
PushNameSpaceDecl(prefixStr, uriStr, aOriginalElement);
}
}
returntrue;
}
bool nsXHTMLContentSerializer::AfterElementStart(nsIContent* aContent,
nsIContent* aOriginalElement,
nsAString& aStr) { if (mRewriteEncodingDeclaration && aContent->IsHTMLElement(nsGkAtoms::head)) { // Check if there already are any content-type meta children. // If there are, they will be modified to use the correct charset. // If there aren't, we'll insert one here. bool hasMeta = false; for (nsIContent* child = aContent->GetFirstChild(); child;
child = child->GetNextSibling()) { if (child->IsHTMLElement(nsGkAtoms::meta) &&
child->AsElement()->HasAttr(nsGkAtoms::content)) {
nsAutoString header;
child->AsElement()->GetAttr(nsGkAtoms::httpEquiv, header);
if (header.LowerCaseEqualsLiteral("content-type")) {
hasMeta = true; break;
}
}
}
// this method is not called by nsHTMLContentSerializer // so we don't have to check HTML element, just XHTML if (aContent->IsHTMLElement(nsGkAtoms::body)) {
--mInBody;
}
}
NS_IMETHODIMP
nsXHTMLContentSerializer::AppendDocumentStart(Document* aDocument) { if (!mBodyOnly) { return nsXMLContentSerializer::AppendDocumentStart(aDocument);
}
// The _moz_dirty attribute is emitted by the editor to // indicate that this element should be pretty printed // even if we're not in pretty printing mode
aForceFormat = !(mFlags & nsIDocumentEncoder::OutputIgnoreMozDirty) &&
aElement->HasAttr(nsGkAtoms::mozdirty);
if (mIsCopying && aElement->IsHTMLElement(nsGkAtoms::ol)) {
NS_ASSERTION((!mOLStateStack.IsEmpty()), "Cannot have an empty OL Stack"); /* Though at this point we must always have an state to be deleted as all
the OL opening tags are supposed to push an olState object to the stack*/ if (!mOLStateStack.IsEmpty()) {
mOLStateStack.RemoveLastElement();
}
}
bool nsXHTMLContentSerializer::IsElementPreformatted(nsIContent* aNode) {
MOZ_ASSERT(ShouldMaintainPreLevel(), "We should not be calling this needlessly");
bool nsXHTMLContentSerializer::SerializeLIValueAttribute(nsIContent* aElement,
nsAString& aStr) { // We are copying and we are at the "first" LI node of OL in selected range. // It may not be the first LI child of OL but it's first in the selected // range. Note that we get into this condition only once per a OL. bool found = false;
nsAutoString valueStr;
olState state(0, false);
if (!mOLStateStack.IsEmpty()) {
state = mOLStateStack[mOLStateStack.Length() - 1]; // isFirstListItem should be true only before the serialization of the // first item in the list.
state.isFirstListItem = false;
mOLStateStack[mOLStateStack.Length() - 1] = state;
}
// Traverse previous siblings until we find one with "value" attribute. // offset keeps track of how many previous siblings we had to traverse.
nsIContent* currNode = aElement; while (currNode && !found) { if (currNode->IsHTMLElement(nsGkAtoms::li)) {
currNode->AsElement()->GetAttr(nsGkAtoms::value, valueStr); if (valueStr.IsEmpty()) {
offset++;
} else {
found = true;
nsresult rv = NS_OK;
startVal = valueStr.ToInteger(&rv);
}
}
currNode = currNode->GetPreviousSibling();
} // If LI was not having "value", Set the "value" attribute for it. // Note that We are at the first LI in the selected range of OL. if (offset == 0 && found) { // offset = 0 => LI itself has the value attribute and we did not need to // traverse back. Just serialize value attribute like other tags.
NS_ENSURE_TRUE(SerializeAttr(u""_ns, u"value"_ns, valueStr, aStr, false), false);
} elseif (offset == 1 && !found) { /*(offset = 1 && !found) means either LI is the first child node of OL and LI is not having "value" attribute. In that case we would not like to set "value" attribute to reduce the changes.
*/ // do nothing...
} elseif (offset > 0) { // Set value attribute.
nsAutoString valueStr;
// As serializer needs to use this valueAttr we are creating here,
valueStr.AppendInt(startVal + offset);
NS_ENSURE_TRUE(SerializeAttr(u""_ns, u"value"_ns, valueStr, aStr, false), false);
}
returntrue;
}
bool nsXHTMLContentSerializer::IsFirstChildOfOL(nsIContent* aElement) {
nsIContent* parent = aElement->GetParent(); if (parent && parent->NodeName().LowerCaseEqualsLiteral("ol")) { if (!mOLStateStack.IsEmpty()) {
olState state = mOLStateStack[mOLStateStack.Length() - 1]; if (state.isFirstListItem) returntrue;
}
}
returnfalse;
}
bool nsXHTMLContentSerializer::HasNoChildren(nsIContent* aContent) { for (nsIContent* child = aContent->GetFirstChild(); child;
child = child->GetNextSibling()) { if (!child->IsText()) returnfalse;
if (child->TextLength()) returnfalse;
}
returntrue;
}
¤ Dauer der Verarbeitung: 0.17 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.