2015-05-03 12:32:37 -07: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 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* nsIContentSerializer implementation that can be used with an
|
|
|
|
* nsIDocumentEncoder to convert an HTML (not XHTML!) DOM to an HTML
|
|
|
|
* string that could be parsed into more or less the original DOM.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsHTMLContentSerializer.h"
|
|
|
|
|
|
|
|
#include "nsIDOMElement.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDocument.h"
|
2014-02-27 15:04:46 -08:00
|
|
|
#include "nsNameSpaceManager.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIDocumentEncoder.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsEscape.h"
|
|
|
|
#include "nsITextToSubURI.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsIParserService.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsLWBrkCIID.h"
|
|
|
|
#include "nsIScriptElement.h"
|
|
|
|
#include "nsAttrName.h"
|
2010-11-06 15:49:26 -07:00
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIEditor.h"
|
2011-03-25 08:03:33 -07:00
|
|
|
#include "nsIHTMLEditor.h"
|
2010-10-22 14:25:22 -07:00
|
|
|
#include "mozilla/dom/Element.h"
|
2011-08-01 00:48:24 -07:00
|
|
|
#include "nsParserConstants.h"
|
2010-10-22 14:25:22 -07:00
|
|
|
|
|
|
|
using namespace mozilla::dom;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2015-03-12 10:20:29 -07:00
|
|
|
nsresult
|
|
|
|
NS_NewHTMLContentSerializer(nsIContentSerializer** aSerializer)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<nsHTMLContentSerializer> it = new nsHTMLContentSerializer();
|
2015-03-12 10:20:29 -07:00
|
|
|
it.forget(aSerializer);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHTMLContentSerializer::nsHTMLContentSerializer()
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsHTMLSerializer = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHTMLContentSerializer::~nsHTMLContentSerializer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-05-04 06:19:54 -07:00
|
|
|
nsHTMLContentSerializer::AppendDocumentStart(nsIDocument *aDocument,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAString& aStr)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-17 01:55:22 -07:00
|
|
|
bool
|
2009-09-01 07:31:05 -07:00
|
|
|
nsHTMLContentSerializer::SerializeHTMLAttributes(nsIContent* aContent,
|
2010-05-04 06:19:54 -07:00
|
|
|
nsIContent *aOriginalElement,
|
2009-09-01 07:31:05 -07:00
|
|
|
nsAString& aTagPrefix,
|
|
|
|
const nsAString& aTagNamespaceURI,
|
|
|
|
nsIAtom* aTagName,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aNamespace,
|
2009-09-01 07:31:05 -07:00
|
|
|
nsAString& aStr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t count = aContent->GetAttrCount();
|
2009-06-28 15:44:22 -07:00
|
|
|
if (!count)
|
2015-03-17 01:55:22 -07:00
|
|
|
return true;
|
2009-06-28 15:44:22 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv;
|
2010-03-08 07:45:00 -08:00
|
|
|
nsAutoString valueStr;
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_NAMED_LITERAL_STRING(_mozStr, "_moz");
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t index = count; index > 0;) {
|
2009-12-23 00:32:39 -08:00
|
|
|
--index;
|
2007-03-22 10:30:00 -07:00
|
|
|
const nsAttrName* name = aContent->GetAttrNameAt(index);
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t namespaceID = name->NamespaceID();
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIAtom* attrName = name->LocalName();
|
|
|
|
|
|
|
|
// Filter out any attribute starting with [-|_]moz
|
2010-03-08 07:45:00 -08:00
|
|
|
nsDependentAtomString attrNameStr(attrName);
|
|
|
|
if (StringBeginsWith(attrNameStr, NS_LITERAL_STRING("_moz")) ||
|
|
|
|
StringBeginsWith(attrNameStr, NS_LITERAL_STRING("-moz"))) {
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
aContent->GetAttr(namespaceID, attrName, valueStr);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Filter out special case of <br type="_moz"> or <br _moz*>,
|
|
|
|
// used by the editor. Bug 16988. Yuck.
|
|
|
|
//
|
2011-05-08 23:48:46 -07:00
|
|
|
if (aTagName == nsGkAtoms::br && aNamespace == kNameSpaceID_XHTML &&
|
|
|
|
attrName == nsGkAtoms::type && namespaceID == kNameSpaceID_None &&
|
2007-03-22 10:30:00 -07:00
|
|
|
StringBeginsWith(valueStr, _mozStr)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-05-08 23:48:46 -07:00
|
|
|
if (mIsCopying && mIsFirstChildOfOL &&
|
|
|
|
aTagName == nsGkAtoms::li && aNamespace == kNameSpaceID_XHTML &&
|
|
|
|
attrName == nsGkAtoms::value && namespaceID == kNameSpaceID_None){
|
2007-03-22 10:30:00 -07:00
|
|
|
// This is handled separately in SerializeLIValueAttribute()
|
|
|
|
continue;
|
|
|
|
}
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isJS = IsJavaScript(aContent, attrName, namespaceID, valueStr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-05-08 23:48:46 -07:00
|
|
|
if (((attrName == nsGkAtoms::href &&
|
|
|
|
(namespaceID == kNameSpaceID_None ||
|
|
|
|
namespaceID == kNameSpaceID_XLink)) ||
|
|
|
|
(attrName == nsGkAtoms::src && namespaceID == kNameSpaceID_None))) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// Make all links absolute when converting only the selection:
|
|
|
|
if (mFlags & nsIDocumentEncoder::OutputAbsoluteLinks) {
|
|
|
|
// Would be nice to handle OBJECT and APPLET tags,
|
|
|
|
// but that gets more complicated since we have to
|
|
|
|
// search the tag list for CODEBASE as well.
|
|
|
|
// For now, just leave them relative.
|
|
|
|
nsCOMPtr<nsIURI> uri = aContent->GetBaseURI();
|
|
|
|
if (uri) {
|
|
|
|
nsAutoString absURI;
|
|
|
|
rv = NS_MakeAbsoluteURI(absURI, valueStr, uri);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
valueStr = absURI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Need to escape URI.
|
|
|
|
nsAutoString tempURI(valueStr);
|
2009-04-25 02:00:14 -07:00
|
|
|
if (!isJS && NS_FAILED(EscapeURI(aContent, tempURI, valueStr)))
|
2007-03-22 10:30:00 -07:00
|
|
|
valueStr = tempURI;
|
|
|
|
}
|
|
|
|
|
2009-09-28 00:59:52 -07:00
|
|
|
if (mRewriteEncodingDeclaration && aTagName == nsGkAtoms::meta &&
|
2011-05-08 23:48:46 -07:00
|
|
|
aNamespace == kNameSpaceID_XHTML && attrName == nsGkAtoms::content
|
|
|
|
&& namespaceID == kNameSpaceID_None) {
|
2007-12-28 16:23:58 -08:00
|
|
|
// If we're serializing a <meta http-equiv="content-type">,
|
|
|
|
// use the proper value, rather than what's in the document.
|
|
|
|
nsAutoString header;
|
|
|
|
aContent->GetAttr(kNameSpaceID_None, nsGkAtoms::httpEquiv, header);
|
|
|
|
if (header.LowerCaseEqualsLiteral("content-type")) {
|
|
|
|
valueStr = NS_LITERAL_STRING("text/html; charset=") +
|
|
|
|
NS_ConvertASCIItoUTF16(mCharset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
nsDependentAtomString nameStr(attrName);
|
2011-05-08 23:48:46 -07:00
|
|
|
nsAutoString prefix;
|
|
|
|
if (namespaceID == kNameSpaceID_XML) {
|
2014-05-26 11:54:10 -07:00
|
|
|
prefix.AssignLiteral(MOZ_UTF16("xml"));
|
2011-05-08 23:48:46 -07:00
|
|
|
} else if (namespaceID == kNameSpaceID_XLink) {
|
2014-05-26 11:54:10 -07:00
|
|
|
prefix.AssignLiteral(MOZ_UTF16("xlink"));
|
2011-05-08 23:48:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Expand shorthand attribute.
|
2011-05-08 23:48:46 -07:00
|
|
|
if (aNamespace == kNameSpaceID_XHTML &&
|
|
|
|
namespaceID == kNameSpaceID_None &&
|
|
|
|
IsShorthandAttr(attrName, aTagName) &&
|
|
|
|
valueStr.IsEmpty()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
valueStr = nameStr;
|
|
|
|
}
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(SerializeAttr(prefix, nameStr, valueStr,
|
|
|
|
aStr, !isJS), false);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2015-03-17 01:55:22 -07:00
|
|
|
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-10-22 14:25:22 -07:00
|
|
|
nsHTMLContentSerializer::AppendElementStart(Element* aElement,
|
|
|
|
Element* aOriginalElement,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAString& aStr)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aElement);
|
|
|
|
|
2010-05-04 06:19:54 -07:00
|
|
|
nsIContent* content = aElement;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool forceFormat = false;
|
2015-03-17 01:55:22 -07:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (!CheckElementStart(content, forceFormat, aStr, rv)) {
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-03-03 03:09:00 -08:00
|
|
|
nsIAtom *name = content->NodeInfo()->NameAtom();
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t ns = content->GetNameSpaceID();
|
2010-11-06 15:49:26 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool lineBreakBeforeOpen = LineBreakBeforeOpen(ns, name);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2015-01-12 08:41:10 -08:00
|
|
|
if ((mDoFormat || forceFormat) && !mDoRaw && !PreLevel()) {
|
2009-04-25 02:00:14 -07:00
|
|
|
if (mColPos && lineBreakBeforeOpen) {
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(AppendNewLineToString(aStr), NS_ERROR_OUT_OF_MEMORY);
|
2009-04-25 02:00:14 -07:00
|
|
|
}
|
|
|
|
else {
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(MaybeAddNewlineForRootNode(aStr), NS_ERROR_OUT_OF_MEMORY);
|
2009-04-25 02:00:14 -07:00
|
|
|
}
|
|
|
|
if (!mColPos) {
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(AppendIndentation(aStr), NS_ERROR_OUT_OF_MEMORY);
|
2009-04-25 02:00:14 -07:00
|
|
|
}
|
|
|
|
else if (mAddSpace) {
|
2015-03-17 01:55:22 -07:00
|
|
|
bool result = AppendToString(char16_t(' '), aStr);
|
2011-10-17 07:59:28 -07:00
|
|
|
mAddSpace = false;
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(result, NS_ERROR_OUT_OF_MEMORY);
|
2009-04-25 02:00:14 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else if (mAddSpace) {
|
2015-03-17 01:55:22 -07:00
|
|
|
bool result = AppendToString(char16_t(' '), aStr);
|
2011-10-17 07:59:28 -07:00
|
|
|
mAddSpace = false;
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(result, NS_ERROR_OUT_OF_MEMORY);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(MaybeAddNewlineForRootNode(aStr), NS_ERROR_OUT_OF_MEMORY);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-04-25 02:00:14 -07:00
|
|
|
// Always reset to avoid false newlines in case MaybeAddNewlineForRootNode wasn't
|
2007-03-22 10:30:00 -07:00
|
|
|
// called
|
2011-10-17 07:59:28 -07:00
|
|
|
mAddNewlineForRootNode = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(AppendToString(kLessThan, aStr), NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(AppendToString(nsDependentAtomString(name), aStr), NS_ERROR_OUT_OF_MEMORY);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-25 02:00:14 -07:00
|
|
|
MaybeEnterInPreContent(content);
|
|
|
|
|
|
|
|
// for block elements, we increase the indentation
|
2015-01-12 08:41:10 -08:00
|
|
|
if ((mDoFormat || forceFormat) && !mDoRaw && !PreLevel())
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(IncrIndentation(name), NS_ERROR_OUT_OF_MEMORY);
|
2009-04-25 02:00:14 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Need to keep track of OL and LI elements in order to get ordinal number
|
|
|
|
// for the LI.
|
2011-05-08 23:48:46 -07:00
|
|
|
if (mIsCopying && name == nsGkAtoms::ol && ns == kNameSpaceID_XHTML){
|
2007-03-22 10:30:00 -07:00
|
|
|
// We are copying and current node is an OL;
|
2009-01-23 01:02:09 -08:00
|
|
|
// Store its start attribute value in olState->startVal.
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAutoString start;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t startAttrVal = 0;
|
2010-05-04 06:19:54 -07:00
|
|
|
|
|
|
|
aElement->GetAttr(kNameSpaceID_None, nsGkAtoms::start, start);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!start.IsEmpty()){
|
2012-07-27 06:59:29 -07:00
|
|
|
nsresult rv = NS_OK;
|
2007-03-22 10:30:00 -07: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 07:59:28 -07:00
|
|
|
mOLStateStack.AppendElement(olState(startAttrVal, true));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-05-08 23:48:46 -07:00
|
|
|
if (mIsCopying && name == nsGkAtoms::li && ns == kNameSpaceID_XHTML) {
|
2007-03-22 10:30:00 -07:00
|
|
|
mIsFirstChildOfOL = IsFirstChildOfOL(aOriginalElement);
|
|
|
|
if (mIsFirstChildOfOL){
|
|
|
|
// If OL is parent of this LI, serialize attributes in different manner.
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(SerializeLIValueAttribute(aElement, aStr), NS_ERROR_OUT_OF_MEMORY);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Even LI passed above have to go through this
|
|
|
|
// for serializing attributes other than "value".
|
2009-04-25 02:00:14 -07:00
|
|
|
nsAutoString dummyPrefix;
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(SerializeHTMLAttributes(content,
|
|
|
|
aOriginalElement,
|
|
|
|
dummyPrefix,
|
|
|
|
EmptyString(),
|
|
|
|
name,
|
|
|
|
ns,
|
|
|
|
aStr), NS_ERROR_OUT_OF_MEMORY);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(AppendToString(kGreaterThan, aStr), NS_ERROR_OUT_OF_MEMORY);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-05-08 23:48:46 -07:00
|
|
|
if (ns == kNameSpaceID_XHTML &&
|
|
|
|
(name == nsGkAtoms::script ||
|
|
|
|
name == nsGkAtoms::style ||
|
|
|
|
name == nsGkAtoms::noscript ||
|
|
|
|
name == nsGkAtoms::noframes)) {
|
2009-04-25 02:00:14 -07:00
|
|
|
++mDisableEntityEncoding;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2015-01-12 08:41:10 -08:00
|
|
|
if ((mDoFormat || forceFormat) && !mDoRaw && !PreLevel() &&
|
|
|
|
LineBreakAfterOpen(ns, name)) {
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(AppendNewLineToString(aStr), NS_ERROR_OUT_OF_MEMORY);
|
2007-06-17 04:41:03 -07:00
|
|
|
}
|
2007-05-13 09:44:06 -07:00
|
|
|
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(AfterElementStart(content, aOriginalElement, aStr), NS_ERROR_OUT_OF_MEMORY);
|
2009-04-25 02:00:14 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-10-22 14:25:22 -07:00
|
|
|
nsHTMLContentSerializer::AppendElementEnd(Element* aElement,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAString& aStr)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aElement);
|
|
|
|
|
2010-05-04 06:19:54 -07:00
|
|
|
nsIContent* content = aElement;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2015-03-03 03:09:00 -08:00
|
|
|
nsIAtom *name = content->NodeInfo()->NameAtom();
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t ns = content->GetNameSpaceID();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-05-08 23:48:46 -07:00
|
|
|
if (ns == kNameSpaceID_XHTML &&
|
|
|
|
(name == nsGkAtoms::script ||
|
|
|
|
name == nsGkAtoms::style ||
|
|
|
|
name == nsGkAtoms::noscript ||
|
|
|
|
name == nsGkAtoms::noframes)) {
|
2009-04-25 02:00:14 -07:00
|
|
|
--mDisableEntityEncoding;
|
|
|
|
}
|
|
|
|
|
2012-04-23 23:06:11 -07:00
|
|
|
bool forceFormat = !(mFlags & nsIDocumentEncoder::OutputIgnoreMozDirty) &&
|
|
|
|
content->HasAttr(kNameSpaceID_None, nsGkAtoms::mozdirty);
|
2009-04-25 02:00:14 -07:00
|
|
|
|
2015-01-12 08:41:10 -08:00
|
|
|
if ((mDoFormat || forceFormat) && !mDoRaw && !PreLevel()) {
|
2009-04-25 02:00:14 -07:00
|
|
|
DecrIndentation(name);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (name == nsGkAtoms::script) {
|
|
|
|
nsCOMPtr<nsIScriptElement> script = do_QueryInterface(aElement);
|
|
|
|
|
2015-01-12 08:41:10 -08:00
|
|
|
if (ShouldMaintainPreLevel() && script && script->IsMalformed()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// We're looking at a malformed script tag. This means that the end tag
|
|
|
|
// was missing in the source. Imitate that here by not serializing the end
|
|
|
|
// tag.
|
2015-01-12 08:41:10 -08:00
|
|
|
--PreLevel();
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
2011-05-08 23:48:46 -07:00
|
|
|
else if (mIsCopying && name == nsGkAtoms::ol && ns == kNameSpaceID_XHTML) {
|
2009-04-25 02:00:14 -07:00
|
|
|
NS_ASSERTION((!mOLStateStack.IsEmpty()), "Cannot have an empty OL Stack");
|
2007-03-22 10:30:00 -07:00
|
|
|
/* 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*/
|
2009-03-20 01:15:35 -07:00
|
|
|
if (!mOLStateStack.IsEmpty()) {
|
|
|
|
mOLStateStack.RemoveElementAt(mOLStateStack.Length() -1);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-08 23:48:46 -07:00
|
|
|
if (ns == kNameSpaceID_XHTML) {
|
|
|
|
nsIParserService* parserService = nsContentUtils::GetParserService();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-05-08 23:48:46 -07:00
|
|
|
if (parserService) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isContainer;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-05-08 23:48:46 -07:00
|
|
|
parserService->
|
|
|
|
IsContainer(parserService->HTMLCaseSensitiveAtomTagToId(name),
|
|
|
|
isContainer);
|
|
|
|
if (!isContainer) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2015-01-12 08:41:10 -08:00
|
|
|
if ((mDoFormat || forceFormat) && !mDoRaw && !PreLevel()) {
|
2009-04-25 02:00:14 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool lineBreakBeforeClose = LineBreakBeforeClose(ns, name);
|
2009-04-25 02:00:14 -07:00
|
|
|
|
|
|
|
if (mColPos && lineBreakBeforeClose) {
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(AppendNewLineToString(aStr), NS_ERROR_OUT_OF_MEMORY);
|
2009-04-25 02:00:14 -07:00
|
|
|
}
|
|
|
|
if (!mColPos) {
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(AppendIndentation(aStr), NS_ERROR_OUT_OF_MEMORY);
|
2009-04-25 02:00:14 -07:00
|
|
|
}
|
|
|
|
else if (mAddSpace) {
|
2015-03-17 01:55:22 -07:00
|
|
|
bool result = AppendToString(char16_t(' '), aStr);
|
2011-10-17 07:59:28 -07:00
|
|
|
mAddSpace = false;
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(result, NS_ERROR_OUT_OF_MEMORY);
|
2009-04-25 02:00:14 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else if (mAddSpace) {
|
2015-03-17 01:55:22 -07:00
|
|
|
bool result = AppendToString(char16_t(' '), aStr);
|
2011-10-17 07:59:28 -07:00
|
|
|
mAddSpace = false;
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(result, NS_ERROR_OUT_OF_MEMORY);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(AppendToString(kEndTag, aStr), NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
NS_ENSURE_TRUE(AppendToString(nsDependentAtomString(name), aStr), NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
NS_ENSURE_TRUE(AppendToString(kGreaterThan, aStr), NS_ERROR_OUT_OF_MEMORY);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-25 02:00:14 -07:00
|
|
|
MaybeLeaveFromPreContent(content);
|
|
|
|
|
2015-01-12 08:41:10 -08:00
|
|
|
if ((mDoFormat || forceFormat)&& !mDoRaw && !PreLevel()
|
|
|
|
&& LineBreakAfterClose(ns, name)) {
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(AppendNewLineToString(aStr), NS_ERROR_OUT_OF_MEMORY);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
2009-04-25 02:00:14 -07:00
|
|
|
MaybeFlagNewlineForRootNode(aElement);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-05-08 23:48:46 -07:00
|
|
|
if (name == nsGkAtoms::body && ns == kNameSpaceID_XHTML) {
|
2008-10-21 13:30:09 -07:00
|
|
|
--mInBody;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-03-18 09:07:32 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static const uint16_t kValNBSP = 160;
|
2010-03-18 09:07:32 -07:00
|
|
|
static const char* kEntities[] = {
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, "&", nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
"<", nullptr, ">", nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
2010-10-17 07:15:54 -07:00
|
|
|
" "
|
2010-03-18 09:07:32 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static const char* kAttrEntities[] = {
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, """, nullptr, nullptr, nullptr, "&", nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
"<", nullptr, ">", nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
2010-10-17 07:15:54 -07:00
|
|
|
" "
|
2010-03-18 09:07:32 -07:00
|
|
|
};
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t FindNextBasicEntity(const nsAString& aStr,
|
|
|
|
const uint32_t aLen,
|
|
|
|
uint32_t aIndex,
|
2010-10-17 07:15:54 -07:00
|
|
|
const char** aEntityTable,
|
|
|
|
const char** aEntity)
|
|
|
|
{
|
|
|
|
for (; aIndex < aLen; ++aIndex) {
|
|
|
|
// for each character in this chunk, check if it
|
|
|
|
// needs to be replaced
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t val = aStr[aIndex];
|
2010-10-17 07:15:54 -07:00
|
|
|
if (val <= kValNBSP && aEntityTable[val]) {
|
|
|
|
*aEntity = aEntityTable[val];
|
|
|
|
return aIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return aIndex;
|
|
|
|
}
|
|
|
|
|
2015-03-17 01:55:22 -07:00
|
|
|
bool
|
2010-03-18 09:07:32 -07:00
|
|
|
nsHTMLContentSerializer::AppendAndTranslateEntities(const nsAString& aStr,
|
|
|
|
nsAString& aOutputStr)
|
|
|
|
{
|
|
|
|
if (mBodyOnly && !mInBody) {
|
2015-03-17 01:55:22 -07:00
|
|
|
return true;
|
2010-03-18 09:07:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mDisableEntityEncoding) {
|
2015-03-17 01:55:22 -07:00
|
|
|
return aOutputStr.Append(aStr, mozilla::fallible);
|
2010-03-18 09:07:32 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nonBasicEntities =
|
2010-10-17 07:15:54 -07:00
|
|
|
!!(mFlags & (nsIDocumentEncoder::OutputEncodeLatin1Entities |
|
|
|
|
nsIDocumentEncoder::OutputEncodeHTMLEntities |
|
|
|
|
nsIDocumentEncoder::OutputEncodeW3CEntities));
|
|
|
|
|
|
|
|
if (!nonBasicEntities &&
|
|
|
|
(mFlags & (nsIDocumentEncoder::OutputEncodeBasicEntities))) {
|
|
|
|
const char **entityTable = mInAttribute ? kAttrEntities : kEntities;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t start = 0;
|
|
|
|
const uint32_t len = aStr.Length();
|
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
2012-07-30 07:20:58 -07:00
|
|
|
const char* entity = nullptr;
|
2010-10-17 07:15:54 -07:00
|
|
|
i = FindNextBasicEntity(aStr, len, i, entityTable, &entity);
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t normalTextLen = i - start;
|
2010-10-17 07:15:54 -07:00
|
|
|
if (normalTextLen) {
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(aOutputStr.Append(Substring(aStr, start, normalTextLen),
|
|
|
|
mozilla::fallible), false);
|
2010-10-17 07:15:54 -07:00
|
|
|
}
|
|
|
|
if (entity) {
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(aOutputStr.AppendASCII(entity, mozilla::fallible), false);
|
2010-10-17 07:15:54 -07:00
|
|
|
start = i + 1;
|
|
|
|
}
|
|
|
|
}
|
2015-03-17 01:55:22 -07:00
|
|
|
return true;
|
2010-10-17 07:15:54 -07:00
|
|
|
} else if (nonBasicEntities) {
|
2010-03-18 09:07:32 -07:00
|
|
|
nsIParserService* parserService = nsContentUtils::GetParserService();
|
|
|
|
|
|
|
|
if (!parserService) {
|
|
|
|
NS_ERROR("Can't get parser service");
|
2015-03-17 01:55:22 -07:00
|
|
|
return true;
|
2010-03-18 09:07:32 -07:00
|
|
|
}
|
|
|
|
|
2014-01-04 07:02:17 -08:00
|
|
|
nsReadingIterator<char16_t> done_reading;
|
2010-03-18 09:07:32 -07:00
|
|
|
aStr.EndReading(done_reading);
|
|
|
|
|
|
|
|
// for each chunk of |aString|...
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t advanceLength = 0;
|
2014-01-04 07:02:17 -08:00
|
|
|
nsReadingIterator<char16_t> iter;
|
2010-03-18 09:07:32 -07:00
|
|
|
|
|
|
|
const char **entityTable = mInAttribute ? kAttrEntities : kEntities;
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString entityReplacement;
|
2010-03-18 09:07:32 -07:00
|
|
|
|
|
|
|
for (aStr.BeginReading(iter);
|
|
|
|
iter != done_reading;
|
2012-08-22 08:56:38 -07:00
|
|
|
iter.advance(int32_t(advanceLength))) {
|
|
|
|
uint32_t fragmentLength = iter.size_forward();
|
|
|
|
uint32_t lengthReplaced = 0; // the number of UTF-16 codepoints
|
2010-03-18 09:07:32 -07:00
|
|
|
// replaced by a particular entity
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t* c = iter.get();
|
|
|
|
const char16_t* fragmentStart = c;
|
|
|
|
const char16_t* fragmentEnd = c + fragmentLength;
|
2012-07-30 07:20:58 -07:00
|
|
|
const char* entityText = nullptr;
|
|
|
|
const char* fullConstEntityText = nullptr;
|
|
|
|
char* fullEntityText = nullptr;
|
2010-03-18 09:07:32 -07:00
|
|
|
|
|
|
|
advanceLength = 0;
|
|
|
|
// for each character in this chunk, check if it
|
|
|
|
// needs to be replaced
|
|
|
|
for (; c < fragmentEnd; c++, advanceLength++) {
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t val = *c;
|
2010-10-17 07:15:54 -07:00
|
|
|
if (val <= kValNBSP && entityTable[val]) {
|
2010-05-14 03:18:27 -07:00
|
|
|
fullConstEntityText = entityTable[val];
|
2010-03-18 09:07:32 -07:00
|
|
|
break;
|
|
|
|
} else if (val > 127 &&
|
|
|
|
((val < 256 &&
|
|
|
|
mFlags & nsIDocumentEncoder::OutputEncodeLatin1Entities) ||
|
|
|
|
mFlags & nsIDocumentEncoder::OutputEncodeHTMLEntities)) {
|
2010-05-14 03:18:27 -07:00
|
|
|
entityReplacement.Truncate();
|
2010-03-18 09:07:32 -07:00
|
|
|
parserService->HTMLConvertUnicodeToEntity(val, entityReplacement);
|
|
|
|
|
|
|
|
if (!entityReplacement.IsEmpty()) {
|
|
|
|
entityText = entityReplacement.get();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (val > 127 &&
|
|
|
|
mFlags & nsIDocumentEncoder::OutputEncodeW3CEntities &&
|
|
|
|
mEntityConverter) {
|
|
|
|
if (NS_IS_HIGH_SURROGATE(val) &&
|
|
|
|
c + 1 < fragmentEnd &&
|
|
|
|
NS_IS_LOW_SURROGATE(*(c + 1))) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t valUTF32 = SURROGATE_TO_UCS4(val, *(++c));
|
2010-03-18 09:07:32 -07:00
|
|
|
if (NS_SUCCEEDED(mEntityConverter->ConvertUTF32ToEntity(valUTF32,
|
|
|
|
nsIEntityConverter::entityW3C, &fullEntityText))) {
|
|
|
|
lengthReplaced = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
advanceLength++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (NS_SUCCEEDED(mEntityConverter->ConvertToEntity(val,
|
|
|
|
nsIEntityConverter::entityW3C,
|
|
|
|
&fullEntityText))) {
|
|
|
|
lengthReplaced = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 01:55:22 -07:00
|
|
|
bool result = aOutputStr.Append(fragmentStart, advanceLength, mozilla::fallible);
|
2010-03-18 09:07:32 -07:00
|
|
|
if (entityText) {
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(aOutputStr.Append(char16_t('&'), mozilla::fallible), false);
|
|
|
|
NS_ENSURE_TRUE(AppendASCIItoUTF16(entityText, aOutputStr, mozilla::fallible), false);
|
|
|
|
NS_ENSURE_TRUE(aOutputStr.Append(char16_t(';'), mozilla::fallible), false);
|
2010-03-18 09:07:32 -07:00
|
|
|
advanceLength++;
|
|
|
|
}
|
2010-05-14 03:18:27 -07:00
|
|
|
else if (fullConstEntityText) {
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(aOutputStr.AppendASCII(fullConstEntityText, mozilla::fallible), false);
|
2010-05-14 03:18:27 -07:00
|
|
|
++advanceLength;
|
|
|
|
}
|
2010-03-18 09:07:32 -07:00
|
|
|
// if it comes from nsIEntityConverter, it already has '&' and ';'
|
|
|
|
else if (fullEntityText) {
|
2015-03-17 01:55:22 -07:00
|
|
|
bool ok = AppendASCIItoUTF16(fullEntityText, aOutputStr, mozilla::fallible);
|
2015-03-26 17:01:12 -07:00
|
|
|
free(fullEntityText);
|
2010-03-18 09:07:32 -07:00
|
|
|
advanceLength += lengthReplaced;
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(ok, false);
|
2010-03-18 09:07:32 -07:00
|
|
|
}
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(result, false);
|
2010-03-18 09:07:32 -07:00
|
|
|
}
|
|
|
|
} else {
|
2015-03-17 01:55:22 -07:00
|
|
|
NS_ENSURE_TRUE(nsXMLContentSerializer::AppendAndTranslateEntities(aStr, aOutputStr), false);
|
2010-03-18 09:07:32 -07:00
|
|
|
}
|
2015-03-17 01:55:22 -07:00
|
|
|
|
|
|
|
return true;
|
2010-03-18 09:07:32 -07:00
|
|
|
}
|