2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2009-04-25 13:00:14 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsXHTMLContentSerializer.h"
|
|
|
|
|
|
|
|
#include "nsIDOMElement.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDocument.h"
|
2017-09-05 13:19:06 +03:00
|
|
|
#include "nsElementTable.h"
|
2014-02-28 03:04:46 +04:00
|
|
|
#include "nsNameSpaceManager.h"
|
2009-04-25 13:00:14 +04:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIDocumentEncoder.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsEscape.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIScriptElement.h"
|
2015-09-29 07:31:56 +03:00
|
|
|
#include "nsStubMutationObserver.h"
|
2009-04-25 13:00:14 +04:00
|
|
|
#include "nsAttrName.h"
|
2014-12-19 20:45:50 +03:00
|
|
|
#include "nsComputedDOMStyle.h"
|
|
|
|
#include "mozilla/dom/Element.h"
|
2009-04-25 13:00:14 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static const int32_t kLongLineLen = 128;
|
2009-04-25 13:00:14 +04:00
|
|
|
|
|
|
|
#define kXMLNS "xmlns"
|
|
|
|
|
2015-03-12 20:20:29 +03:00
|
|
|
nsresult
|
|
|
|
NS_NewXHTMLContentSerializer(nsIContentSerializer** aSerializer)
|
2009-04-25 13:00:14 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsXHTMLContentSerializer> it = new nsXHTMLContentSerializer();
|
2015-03-12 20:20:29 +03:00
|
|
|
it.forget(aSerializer);
|
|
|
|
return NS_OK;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsXHTMLContentSerializer::nsXHTMLContentSerializer()
|
2011-10-17 18:59:28 +04:00
|
|
|
: mIsHTMLSerializer(false)
|
2009-04-25 13:00:14 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsXHTMLContentSerializer::~nsXHTMLContentSerializer()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mOLStateStack.IsEmpty(), "Expected OL State stack to be empty");
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-04-27 13:27:03 +03:00
|
|
|
nsXHTMLContentSerializer::Init(uint32_t aFlags,
|
|
|
|
uint32_t aWrapColumn,
|
|
|
|
const mozilla::Encoding* aEncoding,
|
|
|
|
bool aIsCopying,
|
2017-06-12 22:20:44 +03:00
|
|
|
bool aRewriteEncodingDeclaration,
|
|
|
|
bool* aNeedsPreformatScanning)
|
2009-04-25 13:00:14 +04:00
|
|
|
{
|
|
|
|
// 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
|
2010-04-03 16:51:17 +04:00
|
|
|
// XXXLJ perhaps should we remove this default settings later ?
|
2009-04-25 13:00:14 +04:00
|
|
|
if (aFlags & nsIDocumentEncoder::OutputFormatted ) {
|
|
|
|
aFlags = aFlags | nsIDocumentEncoder::OutputWrap;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
2017-04-27 13:27:03 +03:00
|
|
|
rv = nsXMLContentSerializer::Init(
|
|
|
|
aFlags, aWrapColumn, aEncoding, aIsCopying, aRewriteEncodingDeclaration, aNeedsPreformatScanning);
|
2009-04-25 13:00:14 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2009-09-28 11:59:52 +04:00
|
|
|
mRewriteEncodingDeclaration = aRewriteEncodingDeclaration;
|
2009-04-25 13:00:14 +04:00
|
|
|
mIsCopying = aIsCopying;
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsFirstChildOfOL = false;
|
2009-04-25 13:00:14 +04:00
|
|
|
mInBody = 0;
|
|
|
|
mDisableEntityEncoding = 0;
|
2011-10-17 18:59:28 +04:00
|
|
|
mBodyOnly = (mFlags & nsIDocumentEncoder::OutputBodyOnly) ? true
|
|
|
|
: false;
|
2009-04-25 13:00:14 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// See if the string has any lines longer than longLineLen:
|
|
|
|
// if so, we presume formatting is wonky (e.g. the node has been edited)
|
|
|
|
// and we'd better rewrap the whole text node.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2012-08-22 19:56:38 +04:00
|
|
|
nsXHTMLContentSerializer::HasLongLines(const nsString& text, int32_t& aLastNewlineOffset)
|
2009-04-25 13:00:14 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t start=0;
|
|
|
|
uint32_t theLen = text.Length();
|
2011-09-29 10:19:26 +04:00
|
|
|
bool rv = false;
|
2009-04-25 13:00:14 +04:00
|
|
|
aLastNewlineOffset = kNotFound;
|
|
|
|
for (start = 0; start < theLen; ) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t eol = text.FindChar('\n', start);
|
2009-04-25 13:00:14 +04:00
|
|
|
if (eol < 0) {
|
|
|
|
eol = text.Length();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aLastNewlineOffset = eol;
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
if (int32_t(eol - start) > kLongLineLen)
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = true;
|
2009-04-25 13:00:14 +04:00
|
|
|
start = eol + 1;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-05-04 17:19:54 +04:00
|
|
|
nsXHTMLContentSerializer::AppendText(nsIContent* aText,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aStartOffset,
|
|
|
|
int32_t aEndOffset,
|
2009-04-25 13:00:14 +04:00
|
|
|
nsAString& aStr)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aText);
|
|
|
|
|
|
|
|
nsAutoString data;
|
|
|
|
nsresult rv;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = AppendTextData(aText, aStartOffset, aEndOffset, data, true);
|
2009-04-25 13:00:14 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2015-01-12 19:41:10 +03:00
|
|
|
if (mDoRaw || PreLevel() > 0) {
|
2015-03-17 11:55:22 +03:00
|
|
|
NS_ENSURE_TRUE(AppendToStringConvertLF(data, aStr), NS_ERROR_OUT_OF_MEMORY);
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
else if (mDoFormat) {
|
2015-03-17 11:55:22 +03:00
|
|
|
NS_ENSURE_TRUE(AppendToStringFormatedWrapped(data, aStr), NS_ERROR_OUT_OF_MEMORY);
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
else if (mDoWrap) {
|
2015-03-17 11:55:22 +03:00
|
|
|
NS_ENSURE_TRUE(AppendToStringWrapped(data, aStr), NS_ERROR_OUT_OF_MEMORY);
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
else {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t lastNewlineOffset = kNotFound;
|
2009-04-25 13:00:14 +04:00
|
|
|
if (HasLongLines(data, lastNewlineOffset)) {
|
|
|
|
// We have long lines, rewrap
|
2011-10-17 18:59:28 +04:00
|
|
|
mDoWrap = true;
|
2015-03-17 11:55:22 +03:00
|
|
|
bool result = AppendToStringWrapped(data, aStr);
|
2011-10-17 18:59:28 +04:00
|
|
|
mDoWrap = false;
|
2015-03-17 11:55:22 +03:00
|
|
|
NS_ENSURE_TRUE(result, NS_ERROR_OUT_OF_MEMORY);
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
else {
|
2015-03-17 11:55:22 +03:00
|
|
|
NS_ENSURE_TRUE(AppendToStringConvertLF(data, aStr), NS_ERROR_OUT_OF_MEMORY);
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-17 11:55:22 +03:00
|
|
|
bool
|
2017-12-05 20:05:51 +03:00
|
|
|
nsXHTMLContentSerializer::SerializeAttributes(Element* aElement,
|
|
|
|
Element* aOriginalElement,
|
2009-04-25 13:00:14 +04:00
|
|
|
nsAString& aTagPrefix,
|
|
|
|
const nsAString& aTagNamespaceURI,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* aTagName,
|
2009-04-25 13:00:14 +04:00
|
|
|
nsAString& aStr,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aSkipAttr,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aAddNSAttr)
|
2009-04-25 13:00:14 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t index, count;
|
2010-03-08 18:45:00 +03:00
|
|
|
nsAutoString prefixStr, uriStr, valueStr;
|
2009-04-25 13:00:14 +04:00
|
|
|
nsAutoString xmlnsStr;
|
|
|
|
xmlnsStr.AssignLiteral(kXMLNS);
|
|
|
|
|
2017-12-05 20:05:51 +03:00
|
|
|
int32_t contentNamespaceID = aElement->GetNameSpaceID();
|
2009-04-25 13:00:14 +04:00
|
|
|
|
|
|
|
// this method is not called by nsHTMLContentSerializer
|
|
|
|
// so we don't have to check HTML element, just XHTML
|
|
|
|
|
|
|
|
if (mIsCopying && kNameSpaceID_XHTML == contentNamespaceID) {
|
|
|
|
|
2016-07-22 05:13:46 +03:00
|
|
|
// Need to keep track of OL and LI elements in order to get ordinal number
|
2009-04-25 13:00:14 +04:00
|
|
|
// 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;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startAttrVal = 0;
|
2017-12-05 20:05:51 +03:00
|
|
|
aElement->GetAttr(kNameSpaceID_None, nsGkAtoms::start, start);
|
2009-04-25 13:00:14 +04:00
|
|
|
if (!start.IsEmpty()) {
|
2012-07-27 17:59:29 +04:00
|
|
|
nsresult rv = NS_OK;
|
2009-04-25 13:00:14 +04:00
|
|
|
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;
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
olState state (startAttrVal, true);
|
2009-04-25 13:00:14 +04:00
|
|
|
mOLStateStack.AppendElement(state);
|
|
|
|
}
|
|
|
|
else if (aTagName == nsGkAtoms::li) {
|
|
|
|
mIsFirstChildOfOL = IsFirstChildOfOL(aOriginalElement);
|
|
|
|
if (mIsFirstChildOfOL) {
|
|
|
|
// If OL is parent of this LI, serialize attributes in different manner.
|
2017-12-05 20:05:51 +03:00
|
|
|
NS_ENSURE_TRUE(SerializeLIValueAttribute(aElement, aStr), false);
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2015-03-17 11:55:22 +03:00
|
|
|
NS_ENSURE_TRUE(SerializeAttr(EmptyString(), xmlnsStr,
|
|
|
|
aTagNamespaceURI,
|
|
|
|
aStr, true), false);
|
2009-04-25 13:00:14 +04:00
|
|
|
} else {
|
|
|
|
// Serialize namespace decl
|
2015-03-17 11:55:22 +03:00
|
|
|
NS_ENSURE_TRUE(SerializeAttr(xmlnsStr, aTagPrefix,
|
|
|
|
aTagNamespaceURI,
|
|
|
|
aStr, true), false);
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
PushNameSpaceDecl(aTagPrefix, aTagNamespaceURI, aOriginalElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_STRING(_mozStr, "_moz");
|
|
|
|
|
2017-12-05 20:05:51 +03:00
|
|
|
count = aElement->GetAttrCount();
|
2009-04-25 13:00:14 +04:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2017-12-05 20:05:51 +03:00
|
|
|
mozilla::dom::BorrowedAttrInfo info = aElement->GetAttrInfoAt(index);
|
2016-07-22 05:13:46 +03:00
|
|
|
const nsAttrName* name = info.mName;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t namespaceID = name->NamespaceID();
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* attrName = name->LocalName();
|
|
|
|
nsAtom* attrPrefix = name->GetPrefix();
|
2009-04-25 13:00:14 +04:00
|
|
|
|
|
|
|
// Filter out any attribute starting with [-|_]moz
|
2010-03-08 18:45:00 +03:00
|
|
|
nsDependentAtomString attrNameStr(attrName);
|
|
|
|
if (StringBeginsWith(attrNameStr, NS_LITERAL_STRING("_moz")) ||
|
|
|
|
StringBeginsWith(attrNameStr, NS_LITERAL_STRING("-moz"))) {
|
2009-04-25 13:00:14 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attrPrefix) {
|
|
|
|
attrPrefix->ToString(prefixStr);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
prefixStr.Truncate();
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool addNSAttr = false;
|
2009-04-25 13:00:14 +04:00
|
|
|
if (kNameSpaceID_XMLNS != namespaceID) {
|
|
|
|
nsContentUtils::NameSpaceManager()->GetNameSpaceURI(namespaceID, uriStr);
|
2011-10-17 18:59:28 +04:00
|
|
|
addNSAttr = ConfirmPrefix(prefixStr, uriStr, aOriginalElement, true);
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
2016-07-22 05:13:46 +03:00
|
|
|
info.mValue->ToString(valueStr);
|
2009-04-25 13:00:14 +04:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
nsDependentAtomString nameStr(attrName);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isJS = false;
|
2009-04-25 13:00:14 +04:00
|
|
|
|
|
|
|
if (kNameSpaceID_XHTML == contentNamespaceID) {
|
|
|
|
//
|
|
|
|
// Filter out special case of <br type="_moz"> or <br _moz*>,
|
|
|
|
// used by the editor. Bug 16988. Yuck.
|
|
|
|
//
|
|
|
|
if (namespaceID == kNameSpaceID_None && aTagName == nsGkAtoms::br && attrName == nsGkAtoms::type
|
|
|
|
&& StringBeginsWith(valueStr, _mozStr)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mIsCopying && mIsFirstChildOfOL && (aTagName == nsGkAtoms::li)
|
|
|
|
&& (attrName == nsGkAtoms::value)) {
|
|
|
|
// This is handled separately in SerializeLIValueAttribute()
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-12-05 20:05:51 +03:00
|
|
|
isJS = IsJavaScript(aElement, attrName, namespaceID, valueStr);
|
2009-04-25 13:00:14 +04:00
|
|
|
|
2016-07-22 05:13:46 +03:00
|
|
|
if (namespaceID == kNameSpaceID_None &&
|
2009-04-25 13:00:14 +04:00
|
|
|
((attrName == nsGkAtoms::href) ||
|
|
|
|
(attrName == nsGkAtoms::src))) {
|
|
|
|
// Make all links absolute when converting only the selection:
|
|
|
|
if (mFlags & nsIDocumentEncoder::OutputAbsoluteLinks) {
|
2017-07-29 02:44:39 +03:00
|
|
|
// Would be nice to handle OBJECT tags,
|
2009-04-25 13:00:14 +04:00
|
|
|
// but that gets more complicated since we have to
|
|
|
|
// search the tag list for CODEBASE as well.
|
|
|
|
// For now, just leave them relative.
|
2017-12-05 20:05:51 +03:00
|
|
|
nsCOMPtr<nsIURI> uri = aElement->GetBaseURI();
|
2009-04-25 13:00:14 +04:00
|
|
|
if (uri) {
|
|
|
|
nsAutoString absURI;
|
|
|
|
rv = NS_MakeAbsoluteURI(absURI, valueStr, uri);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
valueStr = absURI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-28 11:59:52 +04:00
|
|
|
if (mRewriteEncodingDeclaration && aTagName == nsGkAtoms::meta &&
|
2009-04-25 13:00:14 +04:00
|
|
|
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;
|
2017-12-05 20:05:51 +03:00
|
|
|
aElement->GetAttr(kNameSpaceID_None, nsGkAtoms::httpEquiv, header);
|
2009-04-25 13:00:14 +04:00
|
|
|
if (header.LowerCaseEqualsLiteral("content-type")) {
|
|
|
|
valueStr = NS_LITERAL_STRING("text/html; charset=") +
|
|
|
|
NS_ConvertASCIItoUTF16(mCharset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expand shorthand attribute.
|
|
|
|
if (namespaceID == kNameSpaceID_None && IsShorthandAttr(attrName, aTagName) && valueStr.IsEmpty()) {
|
|
|
|
valueStr = nameStr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2017-12-05 20:05:51 +03:00
|
|
|
isJS = IsJavaScript(aElement, attrName, namespaceID, valueStr);
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
2015-03-17 11:55:22 +03:00
|
|
|
NS_ENSURE_TRUE(SerializeAttr(prefixStr, nameStr, valueStr, aStr, !isJS), false);
|
2009-04-25 13:00:14 +04:00
|
|
|
|
|
|
|
if (addNSAttr) {
|
|
|
|
NS_ASSERTION(!prefixStr.IsEmpty(),
|
|
|
|
"Namespaced attributes must have a prefix");
|
2015-03-17 11:55:22 +03:00
|
|
|
NS_ENSURE_TRUE(SerializeAttr(xmlnsStr, prefixStr, uriStr, aStr, true), false);
|
2009-04-25 13:00:14 +04:00
|
|
|
PushNameSpaceDecl(prefixStr, uriStr, aOriginalElement);
|
|
|
|
}
|
|
|
|
}
|
2015-03-17 11:55:22 +03:00
|
|
|
|
|
|
|
return true;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
2015-03-17 11:55:22 +03:00
|
|
|
bool
|
|
|
|
nsXHTMLContentSerializer::AfterElementStart(nsIContent* aContent,
|
|
|
|
nsIContent* aOriginalElement,
|
2009-04-25 13:00:14 +04:00
|
|
|
nsAString& aStr)
|
|
|
|
{
|
2015-03-03 14:09:00 +03:00
|
|
|
if (mRewriteEncodingDeclaration &&
|
|
|
|
aContent->IsHTMLElement(nsGkAtoms::head)) {
|
2009-04-25 13:00:14 +04:00
|
|
|
|
|
|
|
// 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.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasMeta = false;
|
2011-09-27 11:54:58 +04:00
|
|
|
for (nsIContent* child = aContent->GetFirstChild();
|
|
|
|
child;
|
|
|
|
child = child->GetNextSibling()) {
|
2015-03-03 14:08:59 +03:00
|
|
|
if (child->IsHTMLElement(nsGkAtoms::meta) &&
|
2017-12-07 21:13:50 +03:00
|
|
|
child->AsElement()->HasAttr(kNameSpaceID_None, nsGkAtoms::content)) {
|
2009-04-25 13:00:14 +04:00
|
|
|
nsAutoString header;
|
2017-12-07 21:13:50 +03:00
|
|
|
child->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::httpEquiv, header);
|
2009-04-25 13:00:14 +04:00
|
|
|
|
|
|
|
if (header.LowerCaseEqualsLiteral("content-type")) {
|
2011-10-17 18:59:28 +04:00
|
|
|
hasMeta = true;
|
2009-04-25 13:00:14 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasMeta) {
|
2015-03-17 11:55:22 +03:00
|
|
|
NS_ENSURE_TRUE(AppendNewLineToString(aStr), false);
|
2009-04-25 13:00:14 +04:00
|
|
|
if (mDoFormat) {
|
2015-03-17 11:55:22 +03:00
|
|
|
NS_ENSURE_TRUE(AppendIndentation(aStr), false);
|
|
|
|
}
|
|
|
|
NS_ENSURE_TRUE(AppendToString(NS_LITERAL_STRING("<meta http-equiv=\"content-type\""), aStr), false);
|
|
|
|
NS_ENSURE_TRUE(AppendToString(NS_LITERAL_STRING(" content=\"text/html; charset="), aStr), false);
|
|
|
|
NS_ENSURE_TRUE(AppendToString(NS_ConvertASCIItoUTF16(mCharset), aStr), false);
|
|
|
|
if (mIsHTMLSerializer) {
|
|
|
|
NS_ENSURE_TRUE(AppendToString(NS_LITERAL_STRING("\">"), aStr), false);
|
|
|
|
} else {
|
|
|
|
NS_ENSURE_TRUE(AppendToString(NS_LITERAL_STRING("\" />"), aStr), false);
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-17 11:55:22 +03:00
|
|
|
|
|
|
|
return true;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsXHTMLContentSerializer::AfterElementEnd(nsIContent * aContent,
|
|
|
|
nsAString& aStr)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mIsHTMLSerializer, "nsHTMLContentSerializer shouldn't call this method !");
|
|
|
|
|
|
|
|
// this method is not called by nsHTMLContentSerializer
|
|
|
|
// so we don't have to check HTML element, just XHTML
|
2015-03-03 14:09:00 +03:00
|
|
|
if (aContent->IsHTMLElement(nsGkAtoms::body)) {
|
2009-04-25 13:00:14 +04:00
|
|
|
--mInBody;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-05-04 17:19:54 +04:00
|
|
|
nsXHTMLContentSerializer::AppendDocumentStart(nsIDocument *aDocument,
|
2009-04-25 13:00:14 +04:00
|
|
|
nsAString& aStr)
|
|
|
|
{
|
|
|
|
if (!mBodyOnly)
|
|
|
|
return nsXMLContentSerializer::AppendDocumentStart(aDocument, aStr);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2017-12-05 20:05:51 +03:00
|
|
|
nsXHTMLContentSerializer::CheckElementStart(Element* aElement,
|
|
|
|
bool& aForceFormat,
|
2015-03-17 11:55:22 +03:00
|
|
|
nsAString& aStr,
|
|
|
|
nsresult& aResult)
|
2009-04-25 13:00:14 +04:00
|
|
|
{
|
2015-03-17 11:55:22 +03:00
|
|
|
aResult = NS_OK;
|
|
|
|
|
2009-04-25 13:00:14 +04:00
|
|
|
// 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
|
2012-04-24 10:06:11 +04:00
|
|
|
aForceFormat = !(mFlags & nsIDocumentEncoder::OutputIgnoreMozDirty) &&
|
2017-12-05 20:05:51 +03:00
|
|
|
aElement->HasAttr(kNameSpaceID_None, nsGkAtoms::mozdirty);
|
2009-04-25 13:00:14 +04:00
|
|
|
|
2017-12-05 20:05:51 +03:00
|
|
|
if (aElement->IsHTMLElement(nsGkAtoms::br) &&
|
2015-03-03 14:09:00 +03:00
|
|
|
(mFlags & nsIDocumentEncoder::OutputNoFormattingInPre) &&
|
|
|
|
PreLevel() > 0) {
|
2015-03-17 11:55:22 +03:00
|
|
|
aResult = AppendNewLineToString(aStr) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
2015-03-03 14:09:00 +03:00
|
|
|
return false;
|
|
|
|
}
|
2009-04-25 13:00:14 +04:00
|
|
|
|
2017-12-05 20:05:51 +03:00
|
|
|
if (aElement->IsHTMLElement(nsGkAtoms::body)) {
|
2015-03-03 14:09:00 +03:00
|
|
|
++mInBody;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
2015-03-03 14:09:00 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2016-02-03 19:39:18 +03:00
|
|
|
nsXHTMLContentSerializer::CheckElementEnd(mozilla::dom::Element* aElement,
|
2016-01-14 05:36:25 +03:00
|
|
|
bool& aForceFormat,
|
2009-04-25 13:00:14 +04:00
|
|
|
nsAString& aStr)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mIsHTMLSerializer, "nsHTMLContentSerializer shouldn't call this method !");
|
|
|
|
|
2012-04-24 10:06:11 +04:00
|
|
|
aForceFormat = !(mFlags & nsIDocumentEncoder::OutputIgnoreMozDirty) &&
|
2016-01-14 05:36:25 +03:00
|
|
|
aElement->HasAttr(kNameSpaceID_None, nsGkAtoms::mozdirty);
|
2009-04-25 13:00:14 +04:00
|
|
|
|
2016-01-14 05:36:25 +03:00
|
|
|
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()) {
|
2009-04-25 13:00:14 +04:00
|
|
|
mOLStateStack.RemoveElementAt(mOLStateStack.Length() -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool dummyFormat;
|
2016-01-14 05:36:25 +03:00
|
|
|
return nsXMLContentSerializer::CheckElementEnd(aElement, dummyFormat, aStr);
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
2015-03-17 11:55:22 +03:00
|
|
|
bool
|
2009-04-25 13:00:14 +04:00
|
|
|
nsXHTMLContentSerializer::AppendAndTranslateEntities(const nsAString& aStr,
|
|
|
|
nsAString& aOutputStr)
|
|
|
|
{
|
|
|
|
if (mBodyOnly && !mInBody) {
|
2015-03-17 11:55:22 +03:00
|
|
|
return true;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mDisableEntityEncoding) {
|
2015-03-17 11:55:22 +03:00
|
|
|
return aOutputStr.Append(aStr, mozilla::fallible);
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
2015-03-17 11:55:22 +03:00
|
|
|
|
|
|
|
return nsXMLContentSerializer::AppendAndTranslateEntities(aStr, aOutputStr);
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2017-10-03 01:05:19 +03:00
|
|
|
nsXHTMLContentSerializer::IsShorthandAttr(const nsAtom* aAttrName,
|
|
|
|
const nsAtom* aElementName)
|
2009-04-25 13:00:14 +04:00
|
|
|
{
|
|
|
|
// checked
|
|
|
|
if ((aAttrName == nsGkAtoms::checked) &&
|
|
|
|
(aElementName == nsGkAtoms::input)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// compact
|
|
|
|
if ((aAttrName == nsGkAtoms::compact) &&
|
2016-07-22 05:13:46 +03:00
|
|
|
(aElementName == nsGkAtoms::dir ||
|
2009-04-25 13:00:14 +04:00
|
|
|
aElementName == nsGkAtoms::dl ||
|
|
|
|
aElementName == nsGkAtoms::menu ||
|
|
|
|
aElementName == nsGkAtoms::ol ||
|
|
|
|
aElementName == nsGkAtoms::ul)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// declare
|
|
|
|
if ((aAttrName == nsGkAtoms::declare) &&
|
|
|
|
(aElementName == nsGkAtoms::object)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// defer
|
|
|
|
if ((aAttrName == nsGkAtoms::defer) &&
|
|
|
|
(aElementName == nsGkAtoms::script)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// disabled
|
|
|
|
if ((aAttrName == nsGkAtoms::disabled) &&
|
|
|
|
(aElementName == nsGkAtoms::button ||
|
|
|
|
aElementName == nsGkAtoms::input ||
|
|
|
|
aElementName == nsGkAtoms::optgroup ||
|
|
|
|
aElementName == nsGkAtoms::option ||
|
|
|
|
aElementName == nsGkAtoms::select ||
|
|
|
|
aElementName == nsGkAtoms::textarea)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ismap
|
|
|
|
if ((aAttrName == nsGkAtoms::ismap) &&
|
|
|
|
(aElementName == nsGkAtoms::img ||
|
|
|
|
aElementName == nsGkAtoms::input)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// multiple
|
|
|
|
if ((aAttrName == nsGkAtoms::multiple) &&
|
|
|
|
(aElementName == nsGkAtoms::select)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// noresize
|
|
|
|
if ((aAttrName == nsGkAtoms::noresize) &&
|
|
|
|
(aElementName == nsGkAtoms::frame)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// noshade
|
|
|
|
if ((aAttrName == nsGkAtoms::noshade) &&
|
|
|
|
(aElementName == nsGkAtoms::hr)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// nowrap
|
|
|
|
if ((aAttrName == nsGkAtoms::nowrap) &&
|
|
|
|
(aElementName == nsGkAtoms::td ||
|
|
|
|
aElementName == nsGkAtoms::th)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// readonly
|
|
|
|
if ((aAttrName == nsGkAtoms::readonly) &&
|
|
|
|
(aElementName == nsGkAtoms::input ||
|
|
|
|
aElementName == nsGkAtoms::textarea)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// selected
|
|
|
|
if ((aAttrName == nsGkAtoms::selected) &&
|
|
|
|
(aElementName == nsGkAtoms::option)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// autoplay and controls
|
|
|
|
if ((aElementName == nsGkAtoms::video || aElementName == nsGkAtoms::audio) &&
|
2011-12-03 16:04:03 +04:00
|
|
|
(aAttrName == nsGkAtoms::autoplay || aAttrName == nsGkAtoms::muted ||
|
2009-04-25 13:00:14 +04:00
|
|
|
aAttrName == nsGkAtoms::controls)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2017-10-03 01:05:19 +03:00
|
|
|
nsXHTMLContentSerializer::LineBreakBeforeOpen(int32_t aNamespaceID, nsAtom* aName)
|
2009-04-25 13:00:14 +04:00
|
|
|
{
|
|
|
|
|
2009-06-09 11:41:19 +04:00
|
|
|
if (aNamespaceID != kNameSpaceID_XHTML) {
|
2009-04-25 13:00:14 +04:00
|
|
|
return mAddSpace;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aName == nsGkAtoms::title ||
|
|
|
|
aName == nsGkAtoms::meta ||
|
|
|
|
aName == nsGkAtoms::link ||
|
|
|
|
aName == nsGkAtoms::style ||
|
|
|
|
aName == nsGkAtoms::select ||
|
|
|
|
aName == nsGkAtoms::option ||
|
|
|
|
aName == nsGkAtoms::script ||
|
|
|
|
aName == nsGkAtoms::html) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
2017-09-05 13:19:06 +03:00
|
|
|
return nsHTMLElement::IsBlock(nsHTMLTags::CaseSensitiveAtomTagToId(aName));
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
2016-07-22 05:13:46 +03:00
|
|
|
bool
|
2017-10-03 01:05:19 +03:00
|
|
|
nsXHTMLContentSerializer::LineBreakAfterOpen(int32_t aNamespaceID, nsAtom* aName)
|
2009-04-25 13:00:14 +04:00
|
|
|
{
|
|
|
|
|
2009-06-09 11:41:19 +04:00
|
|
|
if (aNamespaceID != kNameSpaceID_XHTML) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((aName == nsGkAtoms::html) ||
|
|
|
|
(aName == nsGkAtoms::head) ||
|
|
|
|
(aName == nsGkAtoms::body) ||
|
|
|
|
(aName == nsGkAtoms::ul) ||
|
|
|
|
(aName == nsGkAtoms::ol) ||
|
|
|
|
(aName == nsGkAtoms::dl) ||
|
|
|
|
(aName == nsGkAtoms::table) ||
|
|
|
|
(aName == nsGkAtoms::tbody) ||
|
|
|
|
(aName == nsGkAtoms::tr) ||
|
|
|
|
(aName == nsGkAtoms::br) ||
|
|
|
|
(aName == nsGkAtoms::meta) ||
|
|
|
|
(aName == nsGkAtoms::link) ||
|
|
|
|
(aName == nsGkAtoms::script) ||
|
|
|
|
(aName == nsGkAtoms::select) ||
|
|
|
|
(aName == nsGkAtoms::map) ||
|
|
|
|
(aName == nsGkAtoms::area) ||
|
|
|
|
(aName == nsGkAtoms::style)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
2016-07-22 05:13:46 +03:00
|
|
|
bool
|
2017-10-03 01:05:19 +03:00
|
|
|
nsXHTMLContentSerializer::LineBreakBeforeClose(int32_t aNamespaceID, nsAtom* aName)
|
2009-04-25 13:00:14 +04:00
|
|
|
{
|
|
|
|
|
2009-06-09 11:41:19 +04:00
|
|
|
if (aNamespaceID != kNameSpaceID_XHTML) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((aName == nsGkAtoms::html) ||
|
|
|
|
(aName == nsGkAtoms::head) ||
|
|
|
|
(aName == nsGkAtoms::body) ||
|
|
|
|
(aName == nsGkAtoms::ul) ||
|
|
|
|
(aName == nsGkAtoms::ol) ||
|
|
|
|
(aName == nsGkAtoms::dl) ||
|
|
|
|
(aName == nsGkAtoms::select) ||
|
|
|
|
(aName == nsGkAtoms::table) ||
|
|
|
|
(aName == nsGkAtoms::tbody)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
2016-07-22 05:13:46 +03:00
|
|
|
bool
|
2017-10-03 01:05:19 +03:00
|
|
|
nsXHTMLContentSerializer::LineBreakAfterClose(int32_t aNamespaceID, nsAtom* aName)
|
2009-04-25 13:00:14 +04:00
|
|
|
{
|
|
|
|
|
2009-06-09 11:41:19 +04:00
|
|
|
if (aNamespaceID != kNameSpaceID_XHTML) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((aName == nsGkAtoms::html) ||
|
|
|
|
(aName == nsGkAtoms::head) ||
|
|
|
|
(aName == nsGkAtoms::body) ||
|
|
|
|
(aName == nsGkAtoms::tr) ||
|
|
|
|
(aName == nsGkAtoms::th) ||
|
|
|
|
(aName == nsGkAtoms::td) ||
|
|
|
|
(aName == nsGkAtoms::title) ||
|
|
|
|
(aName == nsGkAtoms::dt) ||
|
|
|
|
(aName == nsGkAtoms::dd) ||
|
|
|
|
(aName == nsGkAtoms::select) ||
|
|
|
|
(aName == nsGkAtoms::option) ||
|
2017-09-25 03:02:17 +03:00
|
|
|
(aName == nsGkAtoms::map)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
2017-09-05 13:19:06 +03:00
|
|
|
return nsHTMLElement::IsBlock(nsHTMLTags::CaseSensitiveAtomTagToId(aName));
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
nsXHTMLContentSerializer::MaybeEnterInPreContent(nsIContent* aNode)
|
|
|
|
{
|
2015-01-12 19:41:10 +03:00
|
|
|
if (!ShouldMaintainPreLevel() ||
|
2015-03-03 14:09:00 +03:00
|
|
|
!aNode->IsHTMLElement()) {
|
2009-04-25 13:00:14 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-19 20:45:50 +03:00
|
|
|
if (IsElementPreformatted(aNode) ||
|
2015-03-03 14:09:00 +03:00
|
|
|
aNode->IsAnyOfHTMLElements(nsGkAtoms::script,
|
|
|
|
nsGkAtoms::style,
|
|
|
|
nsGkAtoms::noscript,
|
|
|
|
nsGkAtoms::noframes)) {
|
2015-01-12 19:41:10 +03:00
|
|
|
PreLevel()++;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsXHTMLContentSerializer::MaybeLeaveFromPreContent(nsIContent* aNode)
|
|
|
|
{
|
2015-01-12 19:41:10 +03:00
|
|
|
if (!ShouldMaintainPreLevel() ||
|
2015-03-03 14:09:00 +03:00
|
|
|
!aNode->IsHTMLElement()) {
|
2009-04-25 13:00:14 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-19 20:45:50 +03:00
|
|
|
if (IsElementPreformatted(aNode) ||
|
2015-03-03 14:09:00 +03:00
|
|
|
aNode->IsAnyOfHTMLElements(nsGkAtoms::script,
|
|
|
|
nsGkAtoms::style,
|
|
|
|
nsGkAtoms::noscript,
|
|
|
|
nsGkAtoms::noframes)) {
|
2015-01-12 19:41:10 +03:00
|
|
|
--PreLevel();
|
2014-12-19 20:45:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-19 20:45:50 +03:00
|
|
|
bool
|
|
|
|
nsXHTMLContentSerializer::IsElementPreformatted(nsIContent* aNode)
|
|
|
|
{
|
2015-01-12 19:41:10 +03:00
|
|
|
MOZ_ASSERT(ShouldMaintainPreLevel(), "We should not be calling this needlessly");
|
|
|
|
|
2014-12-19 20:45:50 +03:00
|
|
|
if (!aNode->IsElement()) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsStyleContext> styleContext =
|
2018-01-23 17:49:00 +03:00
|
|
|
nsComputedDOMStyle::GetStyleContextNoFlush(aNode->AsElement(), nullptr);
|
2014-12-19 20:45:50 +03:00
|
|
|
if (styleContext) {
|
|
|
|
const nsStyleText* textStyle = styleContext->StyleText();
|
|
|
|
return textStyle->WhiteSpaceOrNewlineIsSignificant();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-17 11:55:22 +03:00
|
|
|
bool
|
2010-05-04 17:19:54 +04:00
|
|
|
nsXHTMLContentSerializer::SerializeLIValueAttribute(nsIContent* aElement,
|
2009-04-25 13:00:14 +04:00
|
|
|
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.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool found = false;
|
2009-04-25 13:00:14 +04:00
|
|
|
nsAutoString valueStr;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
olState state (0, false);
|
2009-04-25 13:00:14 +04:00
|
|
|
|
|
|
|
if (!mOLStateStack.IsEmpty()) {
|
|
|
|
state = mOLStateStack[mOLStateStack.Length()-1];
|
|
|
|
// isFirstListItem should be true only before the serialization of the
|
|
|
|
// first item in the list.
|
2011-10-17 18:59:28 +04:00
|
|
|
state.isFirstListItem = false;
|
2009-04-25 13:00:14 +04:00
|
|
|
mOLStateStack[mOLStateStack.Length()-1] = state;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startVal = state.startVal;
|
|
|
|
int32_t offset = 0;
|
2009-04-25 13:00:14 +04:00
|
|
|
|
|
|
|
// Traverse previous siblings until we find one with "value" attribute.
|
2018-01-30 07:10:51 +03:00
|
|
|
// offset keeps track of how many previous siblings we had to traverse.
|
|
|
|
nsIContent* currNode = aElement;
|
2009-04-25 13:00:14 +04:00
|
|
|
while (currNode && !found) {
|
2018-01-30 07:10:51 +03:00
|
|
|
if (currNode->IsHTMLElement(nsGkAtoms::li)) {
|
|
|
|
currNode->AsElement()->GetAttr(kNameSpaceID_None,
|
|
|
|
nsGkAtoms::value, valueStr);
|
|
|
|
if (valueStr.IsEmpty()) {
|
|
|
|
offset++;
|
|
|
|
} else {
|
|
|
|
found = true;
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
startVal = valueStr.ToInteger(&rv);
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
}
|
2018-01-30 07:10:51 +03:00
|
|
|
currNode = currNode->GetPreviousSibling();
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
// 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.
|
2015-03-17 11:55:22 +03:00
|
|
|
NS_ENSURE_TRUE(SerializeAttr(EmptyString(), NS_LITERAL_STRING("value"),
|
|
|
|
valueStr, aStr, false), false);
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
else if (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...
|
|
|
|
}
|
|
|
|
else if (offset > 0) {
|
|
|
|
// Set value attribute.
|
|
|
|
nsAutoString valueStr;
|
|
|
|
|
2016-07-22 05:13:46 +03:00
|
|
|
//As serializer needs to use this valueAttr we are creating here,
|
2009-04-25 13:00:14 +04:00
|
|
|
valueStr.AppendInt(startVal + offset);
|
2015-03-17 11:55:22 +03:00
|
|
|
NS_ENSURE_TRUE(SerializeAttr(EmptyString(), NS_LITERAL_STRING("value"),
|
|
|
|
valueStr, aStr, false), false);
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
2015-03-17 11:55:22 +03:00
|
|
|
|
|
|
|
return true;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2010-05-04 17:19:54 +04:00
|
|
|
nsXHTMLContentSerializer::IsFirstChildOfOL(nsIContent* aElement)
|
2009-04-25 13:00:14 +04:00
|
|
|
{
|
2018-01-30 07:10:50 +03:00
|
|
|
nsIContent* parent = aElement->GetParent();
|
|
|
|
if (parent && parent->NodeName().LowerCaseEqualsLiteral("ol")) {
|
2009-04-25 13:00:14 +04:00
|
|
|
if (!mOLStateStack.IsEmpty()) {
|
|
|
|
olState state = mOLStateStack[mOLStateStack.Length()-1];
|
|
|
|
if (state.isFirstListItem)
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
|
|
|
}
|
2018-01-30 07:10:50 +03:00
|
|
|
|
|
|
|
return false;
|
2009-04-25 13:00:14 +04:00
|
|
|
}
|
2010-03-23 23:55:39 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2017-12-05 20:05:51 +03:00
|
|
|
nsXHTMLContentSerializer::HasNoChildren(nsIContent* aContent) {
|
2010-03-23 23:55:39 +03:00
|
|
|
|
2011-09-27 11:54:58 +04:00
|
|
|
for (nsIContent* child = aContent->GetFirstChild();
|
|
|
|
child;
|
|
|
|
child = child->GetNextSibling()) {
|
2016-07-22 05:13:46 +03:00
|
|
|
|
2010-03-23 23:55:39 +03:00
|
|
|
if (!child->IsNodeOfType(nsINode::eTEXT))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-03-23 23:55:39 +03:00
|
|
|
|
|
|
|
if (child->TextLength())
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-03-23 23:55:39 +03:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-03-23 23:55:39 +03:00
|
|
|
}
|