Files
palemoon27/dom/base/nsHTMLContentSerializer.cpp
T
roytam1 d67c7f2eca import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1255818. Switch from JS_ClearPendingException to AutoJSAPI::ClearException for cases when we have an AutoJSAPI around. r=bholley (e1e0413493)
- Bug 1253591. HTMLAllCollection should be rooted at the document, not the root element, so it can find the root element. r=peterv (c5f2f253a9)
- Bug 1257270 - Use std::nullptr_t instead of nullptr_t in WMFUtils.h. r=cpearce (539e705876)
- Bug 1251881 - use UniquePtr instead of ScopedDeletePtr in mozglue/linker/; r=glandium (2fe329e32c)
- Bug 1254950 - Remove several media Makefile.ins; r=ted (2a274dd21d)
- Bug 1219068 - Compute size correctly when a character consists entirely of glue. r=karlt (80f2e23268)
- Bug 1246132 - Improve register allocation speed on large functions, r=sunfish. (6e023c252b)
- Bug 1150354: Make nsPluginDirServiceProvider build with MSVC 2015 CTP 6, r=jmathies (0113760730)
- Bug 1197311 - Remove PR_snprintf calls in dom/ r=froydnj (6268400ef0)
- Bug 1255099 - XHR CTOR doesn't need to use implicitJSContext, r=bz (72f08fbc36)
- Bug 1255597 - Part 1: Remove redundent trailing spaces. r=khuey (7f2240cb05)
- Bug 1255597 - Part 2: Follow spec to modify the content-type check conditions which determine parsing XHR body or not. r=khuey (ba6f7fc536)
- Bug 1255597 - Part 3: Synchronize content-type hint of HttpChannelChild to HttpChannelParent. r=jduell (bb97478b01)
- Bug 1255597 - Part 4: Remove 'expected fail' settings and expect test cases in responsexml-media-type.htm should be passed. r=khuey (772884b4fb)
- Bug 1201170 - During message diversion must be possible to suspend them. r=jduell (abb2361b7f)
- Bug 1201174 - For FTP - in case of divertToParent, it myst be possible to delay delivering of OnDataAv./OnStopR. r=jduell (9299ef81be)
- Bug 1247393 - use arrays of UniquePtr in ChannelEventQueue; r=mcmanus (7621205b53)
- Bug 1254730 - ChannelEventQueue must be thread-safe, r=michal (b8e01204e9)
- Bug 1254859 part 2. Switch the AutoEntryScript in nsXULTemplateBuilder::InitHTMLTemplateRoot to take ownership of error reporting. r=bholley (f183ec692c)
- Bug 1254859 part 1. Switch the AutoEntryScript in nsGlobalWindow::FireOnNewGlobalObject to take ownership of error reporting. r=bholley (b0cd891917)
- Bug 1254860. Switch the AutoEntryScript TestShellCommandParent::RunCallback to taking ownership of error reporting. r=bholley (0c9697e60f)
- Bug 1254857. Switch the AutoEntryScript in xpc::EvalInSandbox to take ownership of error reporting. r=bholley (93b6bfc87c)
- Bug 1254847 part 3. Make AutoEntryScript always take ownership of error reporting. r=bholley (3c2929462f)
- Bug 1256688 - Continue using getPropertyDescriptor for has in Sandbox창. r=peterv (be7e50b715)
- Bug 1254730 - patch 2 - Better comments and a better management of lifetime of ChannelEvents, r=michal (8348911e35)
- Bug 1163198 - Remove instances of #ifdef PR_LOGGING in dom/plugins. r=froydnj (ba13039dfa)
- Bug 1253216 - clean up the atomic ops ifdef nest. r=jorendorff (6ca747d3cf)
- Bug 1257055 - Use jit/arm64/Architecture-arm64.h on non-JIT aarch64. r=lth (4f3949cd19)
- Bug 1253379 - Cache timings not send to HttpChannelChild r=mayhemer (395172278f)
- align SetAllowStaleCacheContent to FF52, possible misspatch (9d29d27011)
- Bug 1197901, ensure sensor events dispatching follows the becoming spec change, r=bz (f12fe4497c)
- Bug 1252055 P1 Make equivalent ServiceWorker DOM objects strictly equal in js. r=ehsan (cfd6649aa2)
- Bug 1200884: Make postmessage-to-client.https.html pass. r=jdm (7903e29cd3)
- Bug 1188545 - Make skip-waiting-using-registration.html wpt test pass. a=testonly (653f688ffb)
- Bug 1230164 P1 Ensure all service worker WPT tests remove controlled frames before completion. r=ehsan (0ca838a64d)
- Bug 1230164 P2 Only activate service workers in the activating state. r=ehsan (1a33f147d5)
- Bug 1230164 P3 Disable service worker update wpt tests for now. r=ehsan (51fde469a1)
- Bug 1252055 P2 Update tests to verify ServiceWorker object equality.  r=ehsan (8520f66261)
- Bug 1214293, initialize show focus rings state properly in all child frames, r=smaug (6547290d14)
- Bug 735251 - don't show focusrings on HTML video / audio elements on non-Windows when focused by mouse, r=bz (769932daf3)
- Bug 932080 - Support default values in destructuring; r=jorendorff (97d0f50478)
- Bug 1251480 - Implement Rooted<UniquePtr<T>>, r=terrence (86750eefef)
- Bug 1251480 - Use Rooted<UniquePtr<StateData>> in place of StateData::CustomAutoRooter, r=bz (e1d40cbac2)
- Bug 1248153 - Do not convert fp to int by cast. r=waldo (c0a759bfd5)
- Bug 1254335 - Remove invalid assertion; r=jorendorff (9b9e3f050e)
- Bug 1251667 - Add a measure SLOW_SCRIPT_PAGE_COUNT, which measures the number of pages per session that have slow scripts. r=wmccloskey (6ce4d8e0ca)
- Bug 1245185: Remove dead event handling code from nsPIDOMWindow<T>. r=smaug (01f8e77997)
- Bug 1252268: If the slow script dialog aborts an interval timer callback, don't schedule it again. r=bz (62c0828435)
- Bug 1245554. Window's named properties object should not claim to have duplicates of a given property name if it has multiple iframes with that name. r=peterv (78d5c55972)
- Bug 1255709. Simplify the JSContext/GlobalObject handling in ServiceWorkerRegistrationMainThread::GetPushManager a bit. r=bkelly (d157739571)
- Bug 1255655 - Const-ify and shrink kEntities and kAttrEntities. r=baku. (06dc0615ed)
- Bug 1228950 - Disallow scheme sets on nsHostObjectURI. r=bz (667c190935)
- Bug 1225864 - New flag OutputDisallowLineBreaking to disallow line breaking. r=masayuki. (f0e4d46807)
- Bug 1032979 part 1. Refactor the outerHTML web platform test to make it easier to add an XML version. r=smaug (6643f0678b)
- Bug 1032979 part 2. Getting outerHTML on a node from an XML document should not use the self-closing form of empty container tags from the HTML namespace. r=smaug (e3905a9c02)
- Bug 1225882 - Force XSLT to load async if CORS is required. r=sicking (485703225d)
- Bug 1168115 - Null CSSStyleSheet::mOwningPtr in the nsStyleLinkElement Unlink method. r=heycam (a82d26b778)
- Bug 1252841: Convert HAL daemon socket to |UniquePtr<>|, r=brsun (069171166d)
- Bug 1245901 - Measure counts of browser CPOW usage which is now rejected, r=billm (bf772d7df3)
- Bug 1258555: Fix return type of js::GCPointerPolicy::needsSweep. r=terrence (1e81399b2e)
- Bug 1249107 - Fix assertion failure when reaching start node in JS::ubi::ShortestPaths; r=jimb (1dba7085c2)
- Bug 1242462 - Allow IonMonkey re-enter until we reached the frequent bailout threshold. r=jandem (eaec0ee20d)
- Bug 1249736 - Remove redundant StartType from MStart. r=jandem (f18546e86f)
- Bug 1250964: SharedStubs - Don't add invalid stubs, r=jandem (b02cace7ee)
- Bug 1250964: SharedStubs - Remove bogus assert, r=bogus on CLOSED TREE (d310a60b69)
- Bug 1250031 - IonMonkey: MIPS: Fix ion/bug1233343.js crash. r=h4writer (ca83d03b77)
- Bug 1254808 - IonMonkey: MIPS: Define JS_USE_LINK_REGISTER on MIPS. r=h4writer (fce298a97e)
- Bug 1255352 - Use initial CacheIR infrastructure and use it for some simple Baseline stubs. r=efaust (c5ee30767f)
- Bug 1253170 - Enable clang's -Wimplicit-fallthrough warning to catch (65f53a0ddf)
- Bug 1250935 - Use mozilla::Variant instead of a raw union and manual tag for ScriptSource::data; r=terrence (034592e64c)
- bug 1253268 - allow getting ids of proxied accessibles r=yzen (cc73189ef3)
- Bug 1238555 - Always update the LazyScript's static scope chain when emitting functions. (r=till) (b7293ed553)
- Bug 1257434 - Remove MOZ_DEMANGLE_SYMBOLS and MOZ_COMPONENTS_VERSION_SCRIPT_LDFLAGS from js/src/old-configure.in. r=ted (47928e829b)
- Bug 1256988 - Remove useless Gtk/accessibility check in js/src/old-configure.in. r=chmanchester (474378e87c)
- Bug 1122581 - Fix FormatFrame to not assert on Ion frames in some cases. r=shu (4cf9d53fca)
- Bug 1246605 - Fix getBacktrace assert with debugger breakpoints. r=evilpie (c05c44c085)
- Bug 1254861 - Unsupport building with clang < 3.3. r=froydnj (4e3246fa20)
- Bug 1255211 - Add profiledata to the list of LLVM libraries used by t (eb84825750)
- Bug 1233238 - Compile with play-services-{ads,analytics,appindexing} to support Adjust SDK. r=nalexander (ff35cdbdf7)
- Bug 1254174 - Convert uncaught symbol to a descriptive string. r=jorendorff (6917dd4540)
2024-03-18 23:26:24 +08:00

616 lines
20 KiB
C++

/* -*- 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 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"
#include "nsNameSpaceManager.h"
#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"
#include "nsIDocShell.h"
#include "nsIEditor.h"
#include "nsIHTMLEditor.h"
#include "mozilla/dom/Element.h"
#include "nsParserConstants.h"
using namespace mozilla::dom;
nsresult
NS_NewHTMLContentSerializer(nsIContentSerializer** aSerializer)
{
RefPtr<nsHTMLContentSerializer> it = new nsHTMLContentSerializer();
it.forget(aSerializer);
return NS_OK;
}
nsHTMLContentSerializer::nsHTMLContentSerializer()
{
mIsHTMLSerializer = true;
}
nsHTMLContentSerializer::~nsHTMLContentSerializer()
{
}
NS_IMETHODIMP
nsHTMLContentSerializer::AppendDocumentStart(nsIDocument *aDocument,
nsAString& aStr)
{
return NS_OK;
}
bool
nsHTMLContentSerializer::SerializeHTMLAttributes(nsIContent* aContent,
nsIContent *aOriginalElement,
nsAString& aTagPrefix,
const nsAString& aTagNamespaceURI,
nsIAtom* aTagName,
int32_t aNamespace,
nsAString& aStr)
{
int32_t count = aContent->GetAttrCount();
if (!count)
return true;
nsresult rv;
nsAutoString valueStr;
NS_NAMED_LITERAL_STRING(_mozStr, "_moz");
for (int32_t index = count; index > 0;) {
--index;
const nsAttrName* name = aContent->GetAttrNameAt(index);
int32_t namespaceID = name->NamespaceID();
nsIAtom* attrName = name->LocalName();
// Filter out any attribute starting with [-|_]moz
nsDependentAtomString attrNameStr(attrName);
if (StringBeginsWith(attrNameStr, NS_LITERAL_STRING("_moz")) ||
StringBeginsWith(attrNameStr, NS_LITERAL_STRING("-moz"))) {
continue;
}
aContent->GetAttr(namespaceID, attrName, valueStr);
//
// Filter out special case of <br type="_moz"> or <br _moz*>,
// used by the editor. Bug 16988. Yuck.
//
if (aTagName == nsGkAtoms::br && aNamespace == kNameSpaceID_XHTML &&
attrName == nsGkAtoms::type && namespaceID == kNameSpaceID_None &&
StringBeginsWith(valueStr, _mozStr)) {
continue;
}
if (mIsCopying && mIsFirstChildOfOL &&
aTagName == nsGkAtoms::li && aNamespace == kNameSpaceID_XHTML &&
attrName == nsGkAtoms::value && namespaceID == kNameSpaceID_None){
// This is handled separately in SerializeLIValueAttribute()
continue;
}
bool isJS = IsJavaScript(aContent, attrName, namespaceID, valueStr);
if (((attrName == nsGkAtoms::href &&
(namespaceID == kNameSpaceID_None ||
namespaceID == kNameSpaceID_XLink)) ||
(attrName == nsGkAtoms::src && namespaceID == kNameSpaceID_None))) {
// 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);
if (!isJS && NS_FAILED(EscapeURI(aContent, tempURI, valueStr)))
valueStr = tempURI;
}
if (mRewriteEncodingDeclaration && aTagName == nsGkAtoms::meta &&
aNamespace == kNameSpaceID_XHTML && attrName == nsGkAtoms::content
&& namespaceID == kNameSpaceID_None) {
// 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);
}
}
nsDependentAtomString nameStr(attrName);
nsAutoString prefix;
if (namespaceID == kNameSpaceID_XML) {
prefix.AssignLiteral(MOZ_UTF16("xml"));
} else if (namespaceID == kNameSpaceID_XLink) {
prefix.AssignLiteral(MOZ_UTF16("xlink"));
}
// Expand shorthand attribute.
if (aNamespace == kNameSpaceID_XHTML &&
namespaceID == kNameSpaceID_None &&
IsShorthandAttr(attrName, aTagName) &&
valueStr.IsEmpty()) {
valueStr = nameStr;
}
NS_ENSURE_TRUE(SerializeAttr(prefix, nameStr, valueStr,
aStr, !isJS), false);
}
return true;
}
NS_IMETHODIMP
nsHTMLContentSerializer::AppendElementStart(Element* aElement,
Element* aOriginalElement,
nsAString& aStr)
{
NS_ENSURE_ARG(aElement);
nsIContent* content = aElement;
bool forceFormat = false;
nsresult rv = NS_OK;
if (!CheckElementStart(content, forceFormat, aStr, rv)) {
return rv;
}
NS_ENSURE_SUCCESS(rv, rv);
nsIAtom *name = content->NodeInfo()->NameAtom();
int32_t ns = content->GetNameSpaceID();
bool lineBreakBeforeOpen = LineBreakBeforeOpen(ns, name);
if ((mDoFormat || forceFormat) && !mDoRaw && !PreLevel()) {
if (mColPos && lineBreakBeforeOpen) {
NS_ENSURE_TRUE(AppendNewLineToString(aStr), NS_ERROR_OUT_OF_MEMORY);
}
else {
NS_ENSURE_TRUE(MaybeAddNewlineForRootNode(aStr), NS_ERROR_OUT_OF_MEMORY);
}
if (!mColPos) {
NS_ENSURE_TRUE(AppendIndentation(aStr), NS_ERROR_OUT_OF_MEMORY);
}
else if (mAddSpace) {
bool result = AppendToString(char16_t(' '), aStr);
mAddSpace = false;
NS_ENSURE_TRUE(result, NS_ERROR_OUT_OF_MEMORY);
}
}
else if (mAddSpace) {
bool result = AppendToString(char16_t(' '), aStr);
mAddSpace = false;
NS_ENSURE_TRUE(result, NS_ERROR_OUT_OF_MEMORY);
}
else {
NS_ENSURE_TRUE(MaybeAddNewlineForRootNode(aStr), NS_ERROR_OUT_OF_MEMORY);
}
// Always reset to avoid false newlines in case MaybeAddNewlineForRootNode wasn't
// called
mAddNewlineForRootNode = false;
NS_ENSURE_TRUE(AppendToString(kLessThan, aStr), NS_ERROR_OUT_OF_MEMORY);
NS_ENSURE_TRUE(AppendToString(nsDependentAtomString(name), aStr), NS_ERROR_OUT_OF_MEMORY);
MaybeEnterInPreContent(content);
// for block elements, we increase the indentation
if ((mDoFormat || forceFormat) && !mDoRaw && !PreLevel())
NS_ENSURE_TRUE(IncrIndentation(name), NS_ERROR_OUT_OF_MEMORY);
// Need to keep track of OL and LI elements in order to get ordinal number
// for the LI.
if (mIsCopying && name == nsGkAtoms::ol && ns == kNameSpaceID_XHTML){
// 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(kNameSpaceID_None, 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;
}
mOLStateStack.AppendElement(olState(startAttrVal, true));
}
if (mIsCopying && name == nsGkAtoms::li && ns == kNameSpaceID_XHTML) {
mIsFirstChildOfOL = IsFirstChildOfOL(aOriginalElement);
if (mIsFirstChildOfOL){
// If OL is parent of this LI, serialize attributes in different manner.
NS_ENSURE_TRUE(SerializeLIValueAttribute(aElement, aStr), NS_ERROR_OUT_OF_MEMORY);
}
}
// Even LI passed above have to go through this
// for serializing attributes other than "value".
nsAutoString dummyPrefix;
NS_ENSURE_TRUE(SerializeHTMLAttributes(content,
aOriginalElement,
dummyPrefix,
EmptyString(),
name,
ns,
aStr), NS_ERROR_OUT_OF_MEMORY);
NS_ENSURE_TRUE(AppendToString(kGreaterThan, aStr), NS_ERROR_OUT_OF_MEMORY);
if (ns == kNameSpaceID_XHTML &&
(name == nsGkAtoms::script ||
name == nsGkAtoms::style ||
name == nsGkAtoms::noscript ||
name == nsGkAtoms::noframes)) {
++mDisableEntityEncoding;
}
if ((mDoFormat || forceFormat) && !mDoRaw && !PreLevel() &&
LineBreakAfterOpen(ns, name)) {
NS_ENSURE_TRUE(AppendNewLineToString(aStr), NS_ERROR_OUT_OF_MEMORY);
}
NS_ENSURE_TRUE(AfterElementStart(content, aOriginalElement, aStr), NS_ERROR_OUT_OF_MEMORY);
return NS_OK;
}
NS_IMETHODIMP
nsHTMLContentSerializer::AppendElementEnd(Element* aElement,
nsAString& aStr)
{
NS_ENSURE_ARG(aElement);
nsIContent* content = aElement;
nsIAtom *name = content->NodeInfo()->NameAtom();
int32_t ns = content->GetNameSpaceID();
if (ns == kNameSpaceID_XHTML &&
(name == nsGkAtoms::script ||
name == nsGkAtoms::style ||
name == nsGkAtoms::noscript ||
name == nsGkAtoms::noframes)) {
--mDisableEntityEncoding;
}
bool forceFormat = !(mFlags & nsIDocumentEncoder::OutputIgnoreMozDirty) &&
content->HasAttr(kNameSpaceID_None, nsGkAtoms::mozdirty);
if ((mDoFormat || forceFormat) && !mDoRaw && !PreLevel()) {
DecrIndentation(name);
}
if (name == nsGkAtoms::script) {
nsCOMPtr<nsIScriptElement> script = do_QueryInterface(aElement);
if (ShouldMaintainPreLevel() && script && script->IsMalformed()) {
// 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.
--PreLevel();
return NS_OK;
}
}
else if (mIsCopying && name == nsGkAtoms::ol && ns == kNameSpaceID_XHTML) {
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.RemoveElementAt(mOLStateStack.Length() -1);
}
}
if (ns == kNameSpaceID_XHTML) {
nsIParserService* parserService = nsContentUtils::GetParserService();
if (parserService) {
bool isContainer;
parserService->
IsContainer(parserService->HTMLCaseSensitiveAtomTagToId(name),
isContainer);
if (!isContainer) {
return NS_OK;
}
}
}
if ((mDoFormat || forceFormat) && !mDoRaw && !PreLevel()) {
bool lineBreakBeforeClose = LineBreakBeforeClose(ns, name);
if (mColPos && lineBreakBeforeClose) {
NS_ENSURE_TRUE(AppendNewLineToString(aStr), NS_ERROR_OUT_OF_MEMORY);
}
if (!mColPos) {
NS_ENSURE_TRUE(AppendIndentation(aStr), NS_ERROR_OUT_OF_MEMORY);
}
else if (mAddSpace) {
bool result = AppendToString(char16_t(' '), aStr);
mAddSpace = false;
NS_ENSURE_TRUE(result, NS_ERROR_OUT_OF_MEMORY);
}
}
else if (mAddSpace) {
bool result = AppendToString(char16_t(' '), aStr);
mAddSpace = false;
NS_ENSURE_TRUE(result, NS_ERROR_OUT_OF_MEMORY);
}
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);
MaybeLeaveFromPreContent(content);
if ((mDoFormat || forceFormat)&& !mDoRaw && !PreLevel()
&& LineBreakAfterClose(ns, name)) {
NS_ENSURE_TRUE(AppendNewLineToString(aStr), NS_ERROR_OUT_OF_MEMORY);
}
else {
MaybeFlagNewlineForRootNode(aElement);
}
if (name == nsGkAtoms::body && ns == kNameSpaceID_XHTML) {
--mInBody;
}
return NS_OK;
}
static const uint16_t kValNBSP = 160;
#define _ 0
// This table indexes into kEntityStrings[].
static const uint8_t kEntities[] = {
_, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, 2, _,
_, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _,
3, _, 4, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _,
5
};
// This table indexes into kEntityStrings[].
static const uint8_t kAttrEntities[] = {
_, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _,
_, _, _, _, 1, _, _, _, 2, _,
_, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _,
3, _, 4, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _, _,
5
};
#undef _
static const char* const kEntityStrings[] = {
/* 0 */ nullptr,
/* 1 */ "&quot;",
/* 2 */ "&amp;",
/* 3 */ "&lt;",
/* 4 */ "&gt;",
/* 5 */ "&nbsp;"
};
uint32_t FindNextBasicEntity(const nsAString& aStr,
const uint32_t aLen,
uint32_t aIndex,
const uint8_t* aEntityTable,
const char** aEntity)
{
for (; aIndex < aLen; ++aIndex) {
// for each character in this chunk, check if it
// needs to be replaced
char16_t val = aStr[aIndex];
if (val <= kValNBSP && aEntityTable[val]) {
*aEntity = kEntityStrings[aEntityTable[val]];
return aIndex;
}
}
return aIndex;
}
bool
nsHTMLContentSerializer::AppendAndTranslateEntities(const nsAString& aStr,
nsAString& aOutputStr)
{
if (mBodyOnly && !mInBody) {
return true;
}
if (mDisableEntityEncoding) {
return aOutputStr.Append(aStr, mozilla::fallible);
}
bool nonBasicEntities =
!!(mFlags & (nsIDocumentEncoder::OutputEncodeLatin1Entities |
nsIDocumentEncoder::OutputEncodeHTMLEntities |
nsIDocumentEncoder::OutputEncodeW3CEntities));
if (!nonBasicEntities &&
(mFlags & (nsIDocumentEncoder::OutputEncodeBasicEntities))) {
const uint8_t* entityTable = mInAttribute ? kAttrEntities : kEntities;
uint32_t start = 0;
const uint32_t len = aStr.Length();
for (uint32_t i = 0; i < len; ++i) {
const char* entity = nullptr;
i = FindNextBasicEntity(aStr, len, i, entityTable, &entity);
uint32_t normalTextLen = i - start;
if (normalTextLen) {
NS_ENSURE_TRUE(aOutputStr.Append(Substring(aStr, start, normalTextLen),
mozilla::fallible), false);
}
if (entity) {
NS_ENSURE_TRUE(aOutputStr.AppendASCII(entity, mozilla::fallible), false);
start = i + 1;
}
}
return true;
} else if (nonBasicEntities) {
nsIParserService* parserService = nsContentUtils::GetParserService();
if (!parserService) {
NS_ERROR("Can't get parser service");
return true;
}
nsReadingIterator<char16_t> done_reading;
aStr.EndReading(done_reading);
// for each chunk of |aString|...
uint32_t advanceLength = 0;
nsReadingIterator<char16_t> iter;
const uint8_t* entityTable = mInAttribute ? kAttrEntities : kEntities;
nsAutoCString entityReplacement;
for (aStr.BeginReading(iter);
iter != done_reading;
iter.advance(int32_t(advanceLength))) {
uint32_t fragmentLength = iter.size_forward();
uint32_t lengthReplaced = 0; // the number of UTF-16 codepoints
// replaced by a particular entity
const char16_t* c = iter.get();
const char16_t* fragmentStart = c;
const char16_t* fragmentEnd = c + fragmentLength;
const char* entityText = nullptr;
const char* fullConstEntityText = nullptr;
char* fullEntityText = nullptr;
advanceLength = 0;
// for each character in this chunk, check if it
// needs to be replaced
for (; c < fragmentEnd; c++, advanceLength++) {
char16_t val = *c;
if (val <= kValNBSP && entityTable[val]) {
fullConstEntityText = kEntityStrings[entityTable[val]];
break;
} else if (val > 127 &&
((val < 256 &&
mFlags & nsIDocumentEncoder::OutputEncodeLatin1Entities) ||
mFlags & nsIDocumentEncoder::OutputEncodeHTMLEntities)) {
entityReplacement.Truncate();
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))) {
uint32_t valUTF32 = SURROGATE_TO_UCS4(val, *(++c));
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;
}
}
}
bool result = aOutputStr.Append(fragmentStart, advanceLength, mozilla::fallible);
if (entityText) {
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);
advanceLength++;
}
else if (fullConstEntityText) {
NS_ENSURE_TRUE(aOutputStr.AppendASCII(fullConstEntityText, mozilla::fallible), false);
++advanceLength;
}
// if it comes from nsIEntityConverter, it already has '&' and ';'
else if (fullEntityText) {
bool ok = AppendASCIItoUTF16(fullEntityText, aOutputStr, mozilla::fallible);
free(fullEntityText);
advanceLength += lengthReplaced;
NS_ENSURE_TRUE(ok, false);
}
NS_ENSURE_TRUE(result, false);
}
} else {
NS_ENSURE_TRUE(nsXMLContentSerializer::AppendAndTranslateEntities(aStr, aOutputStr), false);
}
return true;
}