2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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
|
|
|
|
|
|
|
/*
|
|
|
|
* Object that can be used to serialize selections, ranges, or nodes
|
|
|
|
* to strings in a gazillion different ways.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsIDocumentEncoder.h"
|
|
|
|
|
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsIFactory.h"
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIHTMLDocument.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIContentSerializer.h"
|
|
|
|
#include "nsIUnicodeEncoder.h"
|
|
|
|
#include "nsIOutputStream.h"
|
|
|
|
#include "nsIDOMElement.h"
|
|
|
|
#include "nsIDOMText.h"
|
|
|
|
#include "nsIDOMCDATASection.h"
|
|
|
|
#include "nsIDOMComment.h"
|
|
|
|
#include "nsIDOMProcessingInstruction.h"
|
|
|
|
#include "nsIDOMDocumentType.h"
|
|
|
|
#include "nsIDOMNodeList.h"
|
2012-01-10 06:19:54 -08:00
|
|
|
#include "nsRange.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDOMRange.h"
|
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIParserService.h"
|
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
2014-04-10 09:09:40 -07:00
|
|
|
#include "mozilla/dom/Selection.h"
|
2010-05-24 16:24:15 -07:00
|
|
|
#include "nsISelectionPrivate.h"
|
|
|
|
#include "nsITransferable.h" // for kUnicodeMime
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsContentUtils.h"
|
2013-03-26 00:15:23 -07:00
|
|
|
#include "nsNodeUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "nsReadableUtils.h"
|
2009-03-20 01:15:35 -07:00
|
|
|
#include "nsTArray.h"
|
2010-04-10 13:05:58 -07:00
|
|
|
#include "nsIFrame.h"
|
2010-06-26 13:39:56 -07:00
|
|
|
#include "nsStringBuffer.h"
|
2011-07-20 12:18:54 -07:00
|
|
|
#include "mozilla/dom/Element.h"
|
2013-12-02 02:26:11 -08:00
|
|
|
#include "mozilla/dom/ShadowRoot.h"
|
2012-03-31 07:10:34 -07:00
|
|
|
#include "nsIEditor.h"
|
|
|
|
#include "nsIHTMLEditor.h"
|
|
|
|
#include "nsIDocShell.h"
|
2014-05-08 02:32:00 -07:00
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
2014-12-01 03:38:11 -08:00
|
|
|
#include "nsComputedDOMStyle.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-10 02:04:45 -08:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult NS_NewDomSelection(nsISelection **aDomSelection);
|
|
|
|
|
|
|
|
enum nsRangeIterationDirection {
|
|
|
|
kDirectionOut = -1,
|
|
|
|
kDirectionIn = 1
|
|
|
|
};
|
|
|
|
|
|
|
|
class nsDocumentEncoder : public nsIDocumentEncoder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsDocumentEncoder();
|
|
|
|
|
2010-06-26 13:39:56 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS(nsDocumentEncoder)
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_DECL_NSIDOCUMENTENCODER
|
|
|
|
|
|
|
|
protected:
|
2014-06-24 19:09:15 -07:00
|
|
|
virtual ~nsDocumentEncoder();
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
void Initialize(bool aClearCachedSerializer = true);
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult SerializeNodeStart(nsINode* aNode, int32_t aStartOffset,
|
|
|
|
int32_t aEndOffset, nsAString& aStr,
|
2012-07-30 07:20:58 -07:00
|
|
|
nsINode* aOriginalNode = nullptr);
|
2010-04-30 04:15:09 -07:00
|
|
|
nsresult SerializeToStringRecursive(nsINode* aNode,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAString& aStr,
|
2014-04-16 15:01:55 -07:00
|
|
|
bool aDontSerializeRoot,
|
|
|
|
uint32_t aMaxLength = 0);
|
2010-04-30 04:15:09 -07:00
|
|
|
nsresult SerializeNodeEnd(nsINode* aNode, nsAString& aStr);
|
2010-10-15 10:59:00 -07:00
|
|
|
// This serializes the content of aNode.
|
|
|
|
nsresult SerializeToStringIterative(nsINode* aNode,
|
|
|
|
nsAString& aStr);
|
2012-01-10 06:19:54 -08:00
|
|
|
nsresult SerializeRangeToString(nsRange *aRange,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAString& aOutputString);
|
2012-01-10 06:19:54 -08:00
|
|
|
nsresult SerializeRangeNodes(nsRange* aRange,
|
2010-04-30 04:15:09 -07:00
|
|
|
nsINode* aNode,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAString& aString,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aDepth);
|
2010-04-30 04:15:09 -07:00
|
|
|
nsresult SerializeRangeContextStart(const nsTArray<nsINode*>& aAncestorArray,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAString& aString);
|
2010-04-30 04:15:09 -07:00
|
|
|
nsresult SerializeRangeContextEnd(const nsTArray<nsINode*>& aAncestorArray,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAString& aString);
|
2012-08-22 08:56:38 -07:00
|
|
|
virtual int32_t
|
2012-05-16 09:39:54 -07:00
|
|
|
GetImmediateContextCount(const nsTArray<nsINode*>& aAncestorArray)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
nsresult FlushText(nsAString& aString, bool aForce);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsVisibleNode(nsINode* aNode)
|
2010-05-24 16:24:15 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aNode, "");
|
|
|
|
|
|
|
|
if (mFlags & SkipInvisibleContent) {
|
2013-12-02 02:26:11 -08:00
|
|
|
// Treat the visibility of the ShadowRoot as if it were
|
|
|
|
// the host content.
|
|
|
|
nsCOMPtr<nsIContent> content;
|
|
|
|
ShadowRoot* shadowRoot = ShadowRoot::FromNode(aNode);
|
|
|
|
if (shadowRoot) {
|
|
|
|
content = shadowRoot->GetHost();
|
|
|
|
} else {
|
|
|
|
content = do_QueryInterface(aNode);
|
|
|
|
}
|
|
|
|
|
2010-05-24 16:24:15 -07:00
|
|
|
if (content) {
|
|
|
|
nsIFrame* frame = content->GetPrimaryFrame();
|
|
|
|
if (!frame) {
|
|
|
|
if (aNode->IsNodeOfType(nsINode::eTEXT)) {
|
|
|
|
// We have already checked that our parent is visible.
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-05-24 16:24:15 -07:00
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-05-24 16:24:15 -07:00
|
|
|
}
|
2013-02-16 13:51:02 -08:00
|
|
|
bool isVisible = frame->StyleVisibility()->IsVisible();
|
2010-05-24 16:24:15 -07:00
|
|
|
if (!isVisible && aNode->IsNodeOfType(nsINode::eTEXT))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-05-24 16:24:15 -07:00
|
|
|
}
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-05-24 16:24:15 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool IsTag(nsIContent* aContent, nsIAtom* aAtom);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
virtual bool IncludeInContext(nsINode *aNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> mDocument;
|
|
|
|
nsCOMPtr<nsISelection> mSelection;
|
2012-01-10 06:19:54 -08:00
|
|
|
nsRefPtr<nsRange> mRange;
|
2010-04-30 04:15:09 -07:00
|
|
|
nsCOMPtr<nsINode> mNode;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIOutputStream> mStream;
|
|
|
|
nsCOMPtr<nsIContentSerializer> mSerializer;
|
|
|
|
nsCOMPtr<nsIUnicodeEncoder> mUnicodeEncoder;
|
2010-04-30 04:15:09 -07:00
|
|
|
nsCOMPtr<nsINode> mCommonParent;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIDocumentEncoderNodeFixup> mNodeFixup;
|
|
|
|
|
|
|
|
nsString mMimeType;
|
|
|
|
nsCString mCharset;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mFlags;
|
|
|
|
uint32_t mWrapColumn;
|
|
|
|
uint32_t mStartDepth;
|
|
|
|
uint32_t mEndDepth;
|
|
|
|
int32_t mStartRootIndex;
|
|
|
|
int32_t mEndRootIndex;
|
2010-04-30 04:15:09 -07:00
|
|
|
nsAutoTArray<nsINode*, 8> mCommonAncestors;
|
2009-03-20 01:15:35 -07:00
|
|
|
nsAutoTArray<nsIContent*, 8> mStartNodes;
|
2012-08-22 08:56:38 -07:00
|
|
|
nsAutoTArray<int32_t, 8> mStartOffsets;
|
2009-03-20 01:15:35 -07:00
|
|
|
nsAutoTArray<nsIContent*, 8> mEndNodes;
|
2012-08-22 08:56:38 -07:00
|
|
|
nsAutoTArray<int32_t, 8> mEndOffsets;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mHaltRangeHint;
|
2012-05-16 09:39:54 -07:00
|
|
|
// Used when context has already been serialized for
|
2012-05-16 09:39:54 -07:00
|
|
|
// table cell selections (where parent is <tr>)
|
2012-05-16 09:39:54 -07:00
|
|
|
bool mDisableContextSerialize;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mIsCopying; // Set to true only while copying
|
|
|
|
bool mNodeIsContainer;
|
2010-06-26 13:39:56 -07:00
|
|
|
nsStringBuffer* mCachedBuffer;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2010-06-26 13:39:56 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDocumentEncoder)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDocumentEncoder)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-26 13:39:56 -07:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDocumentEncoder)
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDocumentEncoder)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2014-04-25 09:49:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION(nsDocumentEncoder,
|
|
|
|
mDocument, mSelection, mRange, mNode, mCommonParent)
|
2010-06-26 13:39:56 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
nsDocumentEncoder::nsDocumentEncoder() : mCachedBuffer(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
Initialize();
|
|
|
|
mMimeType.AssignLiteral("text/plain");
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
void nsDocumentEncoder::Initialize(bool aClearCachedSerializer)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mFlags = 0;
|
|
|
|
mWrapColumn = 72;
|
|
|
|
mStartDepth = 0;
|
|
|
|
mEndDepth = 0;
|
|
|
|
mStartRootIndex = 0;
|
|
|
|
mEndRootIndex = 0;
|
2011-10-17 07:59:28 -07:00
|
|
|
mHaltRangeHint = false;
|
2012-05-16 09:39:54 -07:00
|
|
|
mDisableContextSerialize = false;
|
2011-10-17 07:59:28 -07:00
|
|
|
mNodeIsContainer = false;
|
2010-06-26 14:59:32 -07:00
|
|
|
if (aClearCachedSerializer) {
|
2012-07-30 07:20:58 -07:00
|
|
|
mSerializer = nullptr;
|
2010-06-26 14:59:32 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsDocumentEncoder::~nsDocumentEncoder()
|
|
|
|
{
|
2010-06-26 13:39:56 -07:00
|
|
|
if (mCachedBuffer) {
|
|
|
|
mCachedBuffer->Release();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDocumentEncoder::Init(nsIDOMDocument* aDocument,
|
|
|
|
const nsAString& aMimeType,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!aDocument)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
2010-06-26 13:39:56 -07:00
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDocument);
|
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
return NativeInit(doc, aMimeType, aFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDocumentEncoder::NativeInit(nsIDocument* aDocument,
|
|
|
|
const nsAString& aMimeType,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aFlags)
|
2010-06-26 13:39:56 -07:00
|
|
|
{
|
|
|
|
if (!aDocument)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
2010-06-26 14:59:32 -07:00
|
|
|
Initialize(!mMimeType.Equals(aMimeType));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-26 13:39:56 -07:00
|
|
|
mDocument = aDocument;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
mMimeType = aMimeType;
|
|
|
|
|
|
|
|
mFlags = aFlags;
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsCopying = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsDocumentEncoder::SetWrapColumn(uint32_t aWC)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mWrapColumn = aWC;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDocumentEncoder::SetSelection(nsISelection* aSelection)
|
|
|
|
{
|
|
|
|
mSelection = aSelection;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDocumentEncoder::SetRange(nsIDOMRange* aRange)
|
|
|
|
{
|
2012-01-10 06:19:54 -08:00
|
|
|
mRange = static_cast<nsRange*>(aRange);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDocumentEncoder::SetNode(nsIDOMNode* aNode)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
mNodeIsContainer = false;
|
2010-04-30 04:15:09 -07:00
|
|
|
mNode = do_QueryInterface(aNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-10 04:02:22 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDocumentEncoder::SetNativeNode(nsINode* aNode)
|
|
|
|
{
|
|
|
|
mNodeIsContainer = false;
|
|
|
|
mNode = aNode;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDocumentEncoder::SetContainerNode(nsIDOMNode *aContainer)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
mNodeIsContainer = true;
|
2010-04-30 04:15:09 -07:00
|
|
|
mNode = do_QueryInterface(aContainer);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-26 13:39:56 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDocumentEncoder::SetNativeContainerNode(nsINode* aContainer)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
mNodeIsContainer = true;
|
2010-06-26 13:39:56 -07:00
|
|
|
mNode = aContainer;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDocumentEncoder::SetCharset(const nsACString& aCharset)
|
|
|
|
{
|
|
|
|
mCharset = aCharset;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDocumentEncoder::GetMimeType(nsAString& aMimeType)
|
|
|
|
{
|
|
|
|
aMimeType = mMimeType;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-04-30 04:15:09 -07:00
|
|
|
nsDocumentEncoder::IncludeInContext(nsINode *aNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-03-31 07:10:34 -07:00
|
|
|
static
|
|
|
|
bool
|
|
|
|
IsInvisibleBreak(nsINode *aNode) {
|
|
|
|
// xxxehsan: we should probably figure out a way to determine
|
|
|
|
// if a BR node is visible without using the editor.
|
|
|
|
Element* elt = aNode->AsElement();
|
|
|
|
if (!elt->IsHTML(nsGkAtoms::br) ||
|
|
|
|
!aNode->IsEditable()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab the editor associated with the document
|
2014-10-02 11:45:44 -07:00
|
|
|
nsIDocument *doc = aNode->GetComposedDoc();
|
2012-03-31 07:10:34 -07:00
|
|
|
if (doc) {
|
|
|
|
nsPIDOMWindow *window = doc->GetWindow();
|
|
|
|
if (window) {
|
|
|
|
nsIDocShell *docShell = window->GetDocShell();
|
|
|
|
if (docShell) {
|
2013-02-13 14:39:30 -08:00
|
|
|
nsCOMPtr<nsIEditor> editor;
|
|
|
|
docShell->GetEditor(getter_AddRefs(editor));
|
|
|
|
nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(editor);
|
|
|
|
if (htmlEditor) {
|
|
|
|
bool isVisible = false;
|
|
|
|
nsCOMPtr<nsIDOMNode> domNode = do_QueryInterface(aNode);
|
|
|
|
htmlEditor->BreakIsVisible(domNode, &isVisible);
|
|
|
|
return !isVisible;
|
2012-03-31 07:10:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
2010-04-30 04:15:09 -07:00
|
|
|
nsDocumentEncoder::SerializeNodeStart(nsINode* aNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aStartOffset,
|
|
|
|
int32_t aEndOffset,
|
2008-07-28 22:59:25 -07:00
|
|
|
nsAString& aStr,
|
2010-04-30 04:15:09 -07:00
|
|
|
nsINode* aOriginalNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-05-24 16:24:15 -07:00
|
|
|
if (!IsVisibleNode(aNode))
|
|
|
|
return NS_OK;
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
nsINode* node = nullptr;
|
2010-04-30 04:15:09 -07:00
|
|
|
nsCOMPtr<nsINode> fixedNodeKungfuDeathGrip;
|
2008-07-28 22:59:25 -07:00
|
|
|
|
|
|
|
// Caller didn't do fixup, so we'll do it ourselves
|
2008-07-30 13:20:25 -07:00
|
|
|
if (!aOriginalNode) {
|
2008-07-28 22:59:25 -07:00
|
|
|
aOriginalNode = aNode;
|
2008-07-30 13:20:25 -07:00
|
|
|
if (mNodeFixup) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool dummy;
|
2010-04-30 04:15:09 -07:00
|
|
|
nsCOMPtr<nsIDOMNode> domNodeIn = do_QueryInterface(aNode);
|
|
|
|
nsCOMPtr<nsIDOMNode> domNodeOut;
|
|
|
|
mNodeFixup->FixupNode(domNodeIn, &dummy, getter_AddRefs(domNodeOut));
|
|
|
|
fixedNodeKungfuDeathGrip = do_QueryInterface(domNodeOut);
|
|
|
|
node = fixedNodeKungfuDeathGrip;
|
2008-07-30 13:20:25 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-07-28 22:59:25 -07:00
|
|
|
|
|
|
|
// Either there was no fixed-up node,
|
|
|
|
// or the caller did fixup themselves and aNode is already fixed
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!node)
|
2008-07-28 22:59:25 -07:00
|
|
|
node = aNode;
|
2010-10-15 10:59:00 -07:00
|
|
|
|
|
|
|
if (node->IsElement()) {
|
2012-03-31 07:10:34 -07:00
|
|
|
if ((mFlags & (nsIDocumentEncoder::OutputPreformatted |
|
|
|
|
nsIDocumentEncoder::OutputDropInvisibleBreak)) &&
|
|
|
|
IsInvisibleBreak(node)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-02-10 02:04:45 -08:00
|
|
|
Element* originalElement =
|
2010-10-15 10:59:00 -07:00
|
|
|
aOriginalNode && aOriginalNode->IsElement() ?
|
2012-07-30 07:20:58 -07:00
|
|
|
aOriginalNode->AsElement() : nullptr;
|
2010-10-15 10:59:00 -07:00
|
|
|
mSerializer->AppendElementStart(node->AsElement(),
|
|
|
|
originalElement, aStr);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-06-14 00:56:48 -07:00
|
|
|
switch (node->NodeType()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
case nsIDOMNode::TEXT_NODE:
|
|
|
|
{
|
2010-05-04 06:19:54 -07:00
|
|
|
mSerializer->AppendText(static_cast<nsIContent*>(node),
|
|
|
|
aStartOffset, aEndOffset, aStr);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case nsIDOMNode::CDATA_SECTION_NODE:
|
|
|
|
{
|
2010-05-04 06:19:54 -07:00
|
|
|
mSerializer->AppendCDATASection(static_cast<nsIContent*>(node),
|
|
|
|
aStartOffset, aEndOffset, aStr);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case nsIDOMNode::PROCESSING_INSTRUCTION_NODE:
|
|
|
|
{
|
2010-05-04 06:19:54 -07:00
|
|
|
mSerializer->AppendProcessingInstruction(static_cast<nsIContent*>(node),
|
|
|
|
aStartOffset, aEndOffset, aStr);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case nsIDOMNode::COMMENT_NODE:
|
|
|
|
{
|
2010-05-04 06:19:54 -07:00
|
|
|
mSerializer->AppendComment(static_cast<nsIContent*>(node),
|
|
|
|
aStartOffset, aEndOffset, aStr);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case nsIDOMNode::DOCUMENT_TYPE_NODE:
|
|
|
|
{
|
2010-05-04 06:19:54 -07:00
|
|
|
mSerializer->AppendDoctype(static_cast<nsIContent*>(node), aStr);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-04-30 04:15:09 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-04-30 04:15:09 -07:00
|
|
|
nsDocumentEncoder::SerializeNodeEnd(nsINode* aNode,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAString& aStr)
|
|
|
|
{
|
2010-05-24 16:24:15 -07:00
|
|
|
if (!IsVisibleNode(aNode))
|
|
|
|
return NS_OK;
|
|
|
|
|
2010-04-30 06:12:05 -07:00
|
|
|
if (aNode->IsElement()) {
|
2010-10-22 14:25:22 -07:00
|
|
|
mSerializer->AppendElementEnd(aNode->AsElement(), aStr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-04-30 04:15:09 -07:00
|
|
|
nsDocumentEncoder::SerializeToStringRecursive(nsINode* aNode,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAString& aStr,
|
2014-04-16 15:01:55 -07:00
|
|
|
bool aDontSerializeRoot,
|
|
|
|
uint32_t aMaxLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-04-16 15:01:55 -07:00
|
|
|
if (aMaxLength > 0 && aStr.Length() >= aMaxLength) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-05-24 16:24:15 -07:00
|
|
|
if (!IsVisibleNode(aNode))
|
|
|
|
return NS_OK;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = NS_OK;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool serializeClonedChildren = false;
|
2012-07-30 07:20:58 -07:00
|
|
|
nsINode* maybeFixedNode = nullptr;
|
2010-04-30 04:15:09 -07:00
|
|
|
|
|
|
|
// Keep the node from FixupNode alive.
|
|
|
|
nsCOMPtr<nsINode> fixedNodeKungfuDeathGrip;
|
|
|
|
if (mNodeFixup) {
|
|
|
|
nsCOMPtr<nsIDOMNode> domNodeIn = do_QueryInterface(aNode);
|
|
|
|
nsCOMPtr<nsIDOMNode> domNodeOut;
|
|
|
|
mNodeFixup->FixupNode(domNodeIn, &serializeClonedChildren, getter_AddRefs(domNodeOut));
|
|
|
|
fixedNodeKungfuDeathGrip = do_QueryInterface(domNodeOut);
|
|
|
|
maybeFixedNode = fixedNodeKungfuDeathGrip;
|
|
|
|
}
|
2008-07-28 22:59:25 -07:00
|
|
|
|
|
|
|
if (!maybeFixedNode)
|
|
|
|
maybeFixedNode = aNode;
|
|
|
|
|
2013-08-06 07:40:38 -07:00
|
|
|
if ((mFlags & SkipInvisibleContent) &&
|
|
|
|
!(mFlags & OutputNonTextContentAsPlaceholder)) {
|
2010-04-30 04:15:09 -07:00
|
|
|
if (aNode->IsNodeOfType(nsINode::eCONTENT)) {
|
|
|
|
nsIFrame* frame = static_cast<nsIContent*>(aNode)->GetPrimaryFrame();
|
2010-04-14 07:55:16 -07:00
|
|
|
if (frame) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isSelectable;
|
2012-07-30 07:20:58 -07:00
|
|
|
frame->IsSelectable(&isSelectable, nullptr);
|
2010-04-14 07:55:16 -07:00
|
|
|
if (!isSelectable){
|
2011-10-17 07:59:28 -07:00
|
|
|
aDontSerializeRoot = true;
|
2010-04-14 07:55:16 -07:00
|
|
|
}
|
2010-04-10 13:05:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!aDontSerializeRoot) {
|
2014-04-16 15:01:55 -07:00
|
|
|
int32_t endOffset = -1;
|
|
|
|
if (aMaxLength > 0) {
|
|
|
|
MOZ_ASSERT(aMaxLength >= aStr.Length());
|
|
|
|
endOffset = aMaxLength - aStr.Length();
|
|
|
|
}
|
|
|
|
rv = SerializeNodeStart(maybeFixedNode, 0, endOffset, aStr, aNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2010-04-30 04:15:09 -07:00
|
|
|
nsINode* node = serializeClonedChildren ? maybeFixedNode : aNode;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-03-26 00:15:23 -07:00
|
|
|
for (nsINode* child = nsNodeUtils::GetFirstChildOfTemplateOrNode(node);
|
|
|
|
child;
|
2010-07-19 03:35:02 -07:00
|
|
|
child = child->GetNextSibling()) {
|
2014-04-16 15:01:55 -07:00
|
|
|
rv = SerializeToStringRecursive(child, aStr, false, aMaxLength);
|
2010-04-30 04:15:09 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!aDontSerializeRoot) {
|
2008-07-28 22:59:25 -07:00
|
|
|
rv = SerializeNodeEnd(node, aStr);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return FlushText(aStr, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-10-15 10:59:00 -07:00
|
|
|
nsresult
|
|
|
|
nsDocumentEncoder::SerializeToStringIterative(nsINode* aNode,
|
|
|
|
nsAString& aStr)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2014-06-04 15:21:23 -07:00
|
|
|
nsINode* node = nsNodeUtils::GetFirstChildOfTemplateOrNode(aNode);
|
2010-10-15 10:59:00 -07:00
|
|
|
while (node) {
|
|
|
|
nsINode* current = node;
|
|
|
|
rv = SerializeNodeStart(current, 0, -1, aStr, current);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-06-04 15:21:23 -07:00
|
|
|
node = nsNodeUtils::GetFirstChildOfTemplateOrNode(current);
|
2010-10-15 10:59:00 -07:00
|
|
|
while (!node && current && current != aNode) {
|
|
|
|
rv = SerializeNodeEnd(current, aStr);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
// Check if we have siblings.
|
|
|
|
node = current->GetNextSibling();
|
|
|
|
if (!node) {
|
|
|
|
// Perhaps parent node has siblings.
|
2012-10-09 05:31:24 -07:00
|
|
|
current = current->GetParentNode();
|
2014-06-04 15:21:23 -07:00
|
|
|
|
|
|
|
// Handle template element. If the parent is a template's content,
|
|
|
|
// then adjust the parent to be the template element.
|
|
|
|
if (current && current != aNode &&
|
|
|
|
current->NodeType() == nsIDOMNode::DOCUMENT_FRAGMENT_NODE) {
|
|
|
|
DocumentFragment* frag = static_cast<DocumentFragment*>(current);
|
|
|
|
nsIContent* host = frag->GetHost();
|
|
|
|
if (host && host->IsHTML(nsGkAtoms::_template)) {
|
|
|
|
current = host;
|
|
|
|
}
|
|
|
|
}
|
2010-10-15 10:59:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-04-30 04:15:09 -07:00
|
|
|
nsDocumentEncoder::IsTag(nsIContent* aContent, nsIAtom* aAtom)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-04-30 04:15:09 -07:00
|
|
|
return aContent && aContent->Tag() == aAtom;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
ConvertAndWrite(const nsAString& aString,
|
|
|
|
nsIOutputStream* aStream,
|
|
|
|
nsIUnicodeEncoder* aEncoder)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aStream);
|
|
|
|
NS_ENSURE_ARG_POINTER(aEncoder);
|
|
|
|
nsresult rv;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t charLength, startCharLength;
|
2007-03-22 10:30:00 -07:00
|
|
|
const nsPromiseFlatString& flat = PromiseFlatString(aString);
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t* unicodeBuf = flat.get();
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t unicodeLength = aString.Length();
|
|
|
|
int32_t startLength = unicodeLength;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
rv = aEncoder->GetMaxLength(unicodeBuf, unicodeLength, &charLength);
|
|
|
|
startCharLength = charLength;
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-02-18 06:57:44 -08:00
|
|
|
if (!charLength) {
|
|
|
|
// Nothing to write. Besides, a length 0 string has an immutable buffer, so
|
|
|
|
// attempts to null-terminate it will crash.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString charXferString;
|
2013-05-07 08:52:52 -07:00
|
|
|
if (!charXferString.SetLength(charLength, fallible_t()))
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
char* charXferBuf = charXferString.BeginWriting();
|
|
|
|
nsresult convert_rv = NS_OK;
|
|
|
|
|
|
|
|
do {
|
|
|
|
unicodeLength = startLength;
|
|
|
|
charLength = startCharLength;
|
|
|
|
|
|
|
|
convert_rv = aEncoder->Convert(unicodeBuf, &unicodeLength, charXferBuf, &charLength);
|
|
|
|
NS_ENSURE_SUCCESS(convert_rv, convert_rv);
|
|
|
|
|
|
|
|
// Make sure charXferBuf is null-terminated before we call
|
|
|
|
// Write().
|
|
|
|
|
|
|
|
charXferBuf[charLength] = '\0';
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t written;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = aStream->Write(charXferBuf, charLength, &written);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// If the converter couldn't convert a chraacer we replace the
|
|
|
|
// character with a characre entity.
|
|
|
|
if (convert_rv == NS_ERROR_UENC_NOMAPPING) {
|
|
|
|
// Finishes the conversion.
|
|
|
|
// The converter has the possibility to write some extra data and flush its final state.
|
|
|
|
char finish_buf[33];
|
|
|
|
charLength = sizeof(finish_buf) - 1;
|
|
|
|
rv = aEncoder->Finish(finish_buf, &charLength);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Make sure finish_buf is null-terminated before we call
|
|
|
|
// Write().
|
|
|
|
|
|
|
|
finish_buf[charLength] = '\0';
|
|
|
|
|
|
|
|
rv = aStream->Write(finish_buf, charLength, &written);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString entString("&#");
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_IS_HIGH_SURROGATE(unicodeBuf[unicodeLength - 1]) &&
|
|
|
|
unicodeLength < startLength && NS_IS_LOW_SURROGATE(unicodeBuf[unicodeLength])) {
|
|
|
|
entString.AppendInt(SURROGATE_TO_UCS4(unicodeBuf[unicodeLength - 1],
|
|
|
|
unicodeBuf[unicodeLength]));
|
|
|
|
unicodeLength += 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
entString.AppendInt(unicodeBuf[unicodeLength - 1]);
|
|
|
|
entString.Append(';');
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
// Since entString is an nsAutoCString we know entString.get()
|
2007-03-22 10:30:00 -07:00
|
|
|
// returns a null-terminated string, so no need for extra
|
|
|
|
// null-termination before calling Write() here.
|
|
|
|
|
|
|
|
rv = aStream->Write(entString.get(), entString.Length(), &written);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
unicodeBuf += unicodeLength;
|
|
|
|
startLength -= unicodeLength;
|
|
|
|
}
|
|
|
|
} while (convert_rv == NS_ERROR_UENC_NOMAPPING);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-09-28 23:19:26 -07:00
|
|
|
nsDocumentEncoder::FlushText(nsAString& aString, bool aForce)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!mStream)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
if (aString.Length() > 1024 || aForce) {
|
|
|
|
rv = ConvertAndWrite(aString, mStream, mUnicodeEncoder);
|
|
|
|
|
|
|
|
aString.Truncate();
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0 // This code is really fast at serializing a range, but unfortunately
|
|
|
|
// there are problems with it so we don't use it now, maybe later...
|
2012-08-22 08:56:38 -07:00
|
|
|
static nsresult ChildAt(nsIDOMNode* aNode, int32_t aIndex, nsIDOMNode*& aChild)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> content(do_QueryInterface(aNode));
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
aChild = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_ENSURE_TRUE(content, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsIContent *child = content->GetChildAt(aIndex);
|
|
|
|
|
|
|
|
if (child)
|
|
|
|
return CallQueryInterface(child, &aChild);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static int32_t IndexOf(nsIDOMNode* aParent, nsIDOMNode* aChild)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> parent(do_QueryInterface(aParent));
|
|
|
|
nsCOMPtr<nsIContent> child(do_QueryInterface(aChild));
|
|
|
|
|
|
|
|
if (!parent)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return parent->IndexOf(child);
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static inline int32_t GetIndex(nsTArray<int32_t>& aIndexArray)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t count = aIndexArray.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (count) {
|
2009-03-20 01:15:35 -07:00
|
|
|
return aIndexArray.ElementAt(count - 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static nsresult GetNextNode(nsIDOMNode* aNode, nsTArray<int32_t>& aIndexArray,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIDOMNode*& aNextNode,
|
|
|
|
nsRangeIterationDirection& aDirection)
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasChildren;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
aNextNode = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
aNode->HasChildNodes(&hasChildren);
|
|
|
|
|
|
|
|
if (hasChildren && aDirection == kDirectionIn) {
|
|
|
|
ChildAt(aNode, 0, aNextNode);
|
|
|
|
NS_ENSURE_TRUE(aNextNode, NS_ERROR_FAILURE);
|
|
|
|
|
2009-03-20 01:15:35 -07:00
|
|
|
aIndexArray.AppendElement(0);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
aDirection = kDirectionIn;
|
|
|
|
} else if (aDirection == kDirectionIn) {
|
|
|
|
aNextNode = aNode;
|
|
|
|
|
|
|
|
NS_ADDREF(aNextNode);
|
|
|
|
|
|
|
|
aDirection = kDirectionOut;
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
|
|
|
|
|
|
|
aNode->GetParentNode(getter_AddRefs(parent));
|
|
|
|
NS_ENSURE_TRUE(parent, NS_ERROR_FAILURE);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t count = aIndexArray.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (count) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t indx = aIndexArray.ElementAt(count - 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
ChildAt(parent, indx + 1, aNextNode);
|
|
|
|
|
|
|
|
if (aNextNode)
|
2009-03-20 01:15:35 -07:00
|
|
|
aIndexArray.ElementAt(count - 1) = indx + 1;
|
2007-03-22 10:30:00 -07:00
|
|
|
else
|
|
|
|
aIndexArray.RemoveElementAt(count - 1);
|
|
|
|
} else {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t indx = IndexOf(parent, aNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (indx >= 0) {
|
|
|
|
ChildAt(parent, indx + 1, aNextNode);
|
|
|
|
|
|
|
|
if (aNextNode)
|
2009-03-20 01:15:35 -07:00
|
|
|
aIndexArray.AppendElement(indx + 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aNextNode) {
|
|
|
|
aDirection = kDirectionIn;
|
|
|
|
} else {
|
|
|
|
aDirection = kDirectionOut;
|
|
|
|
|
|
|
|
aNextNode = parent;
|
|
|
|
|
|
|
|
NS_ADDREF(aNextNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool IsTextNode(nsINode *aNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-04-30 04:15:09 -07:00
|
|
|
return aNode && aNode->IsNodeOfType(nsINode::eTEXT);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-01-10 06:19:54 -08:00
|
|
|
nsDocumentEncoder::SerializeRangeNodes(nsRange* aRange,
|
2010-04-30 04:15:09 -07:00
|
|
|
nsINode* aNode,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAString& aString,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aDepth)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
|
|
|
|
NS_ENSURE_TRUE(content, NS_ERROR_FAILURE);
|
|
|
|
|
2010-05-24 16:24:15 -07:00
|
|
|
if (!IsVisibleNode(aNode))
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// get start and end nodes for this recursion level
|
|
|
|
nsCOMPtr<nsIContent> startNode, endNode;
|
2010-05-24 16:24:15 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t start = mStartRootIndex - aDepth;
|
|
|
|
if (start >= 0 && (uint32_t)start <= mStartNodes.Length())
|
2010-05-24 16:24:15 -07:00
|
|
|
startNode = mStartNodes[start];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t end = mEndRootIndex - aDepth;
|
|
|
|
if (end >= 0 && (uint32_t)end <= mEndNodes.Length())
|
2010-05-24 16:24:15 -07:00
|
|
|
endNode = mEndNodes[end];
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-24 16:24:15 -07:00
|
|
|
if (startNode != content && endNode != content)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// node is completely contained in range. Serialize the whole subtree
|
|
|
|
// rooted by this node.
|
2011-10-17 07:59:28 -07:00
|
|
|
rv = SerializeToStringRecursive(aNode, aString, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// due to implementation it is impossible for text node to be both start and end of
|
|
|
|
// range. We would have handled that case without getting here.
|
2010-04-30 04:15:09 -07:00
|
|
|
//XXXsmaug What does this all mean?
|
2007-03-22 10:30:00 -07:00
|
|
|
if (IsTextNode(aNode))
|
|
|
|
{
|
|
|
|
if (startNode == content)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t startOffset = aRange->StartOffset();
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = SerializeNodeStart(aNode, startOffset, -1, aString);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t endOffset = aRange->EndOffset();
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = SerializeNodeStart(aNode, 0, endOffset, aString);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (aNode != mCommonParent)
|
|
|
|
{
|
|
|
|
if (IncludeInContext(aNode))
|
|
|
|
{
|
|
|
|
// halt the incrementing of mStartDepth/mEndDepth. This is
|
|
|
|
// so paste client will include this node in paste.
|
2011-10-17 07:59:28 -07:00
|
|
|
mHaltRangeHint = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
if ((startNode == content) && !mHaltRangeHint) mStartDepth++;
|
|
|
|
if ((endNode == content) && !mHaltRangeHint) mEndDepth++;
|
|
|
|
|
|
|
|
// serialize the start of this node
|
|
|
|
rv = SerializeNodeStart(aNode, 0, -1, aString);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// do some calculations that will tell us which children of this
|
|
|
|
// node are in the range.
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIContent* childAsNode = nullptr;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t startOffset = 0, endOffset = -1;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (startNode == content && mStartRootIndex >= aDepth)
|
2009-03-20 01:15:35 -07:00
|
|
|
startOffset = mStartOffsets[mStartRootIndex - aDepth];
|
2007-03-22 10:30:00 -07:00
|
|
|
if (endNode == content && mEndRootIndex >= aDepth)
|
2009-03-20 01:15:35 -07:00
|
|
|
endOffset = mEndOffsets[mEndRootIndex - aDepth];
|
2007-03-22 10:30:00 -07:00
|
|
|
// generated content will cause offset values of -1 to be returned.
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t j;
|
|
|
|
uint32_t childCount = content->GetChildCount();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (startOffset == -1) startOffset = 0;
|
|
|
|
if (endOffset == -1) endOffset = childCount;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// if we are at the "tip" of the selection, endOffset is fine.
|
|
|
|
// otherwise, we need to add one. This is because of the semantics
|
|
|
|
// of the offset list created by GetAncestorsAndOffsets(). The
|
|
|
|
// intermediate points on the list use the endOffset of the
|
|
|
|
// location of the ancestor, rather than just past it. So we need
|
|
|
|
// to add one here in order to include it in the children we serialize.
|
2010-04-30 04:15:09 -07:00
|
|
|
if (aNode != aRange->GetEndParent())
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
endOffset++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// serialize the children of this node that are in the range
|
|
|
|
for (j=startOffset; j<endOffset; j++)
|
|
|
|
{
|
2010-04-30 04:15:09 -07:00
|
|
|
childAsNode = content->GetChildAt(j);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if ((j==startOffset) || (j==endOffset-1))
|
|
|
|
rv = SerializeRangeNodes(aRange, childAsNode, aString, aDepth+1);
|
|
|
|
else
|
2011-10-17 07:59:28 -07:00
|
|
|
rv = SerializeToStringRecursive(childAsNode, aString, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// serialize the end of this node
|
|
|
|
if (aNode != mCommonParent)
|
|
|
|
{
|
|
|
|
rv = SerializeNodeEnd(aNode, aString);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-04-30 04:15:09 -07:00
|
|
|
nsDocumentEncoder::SerializeRangeContextStart(const nsTArray<nsINode*>& aAncestorArray,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAString& aString)
|
|
|
|
{
|
2012-05-16 09:39:54 -07:00
|
|
|
if (mDisableContextSerialize) {
|
2012-05-16 09:39:54 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t i = aAncestorArray.Length(), j;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2012-05-16 09:39:54 -07:00
|
|
|
// currently only for table-related elements; see Bug 137450
|
|
|
|
j = GetImmediateContextCount(aAncestorArray);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
while (i > 0) {
|
2010-04-30 04:15:09 -07:00
|
|
|
nsINode *node = aAncestorArray.ElementAt(--i);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!node)
|
|
|
|
break;
|
|
|
|
|
2012-05-16 09:39:54 -07:00
|
|
|
// Either a general inclusion or as immediate context
|
|
|
|
if (IncludeInContext(node) || i < j) {
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = SerializeNodeStart(node, 0, -1, aString);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-04-30 04:15:09 -07:00
|
|
|
nsDocumentEncoder::SerializeRangeContextEnd(const nsTArray<nsINode*>& aAncestorArray,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAString& aString)
|
|
|
|
{
|
2012-05-16 09:39:54 -07:00
|
|
|
if (mDisableContextSerialize) {
|
2012-05-16 09:39:54 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t i = 0, j;
|
|
|
|
int32_t count = aAncestorArray.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2012-05-16 09:39:54 -07:00
|
|
|
// currently only for table-related elements
|
|
|
|
j = GetImmediateContextCount(aAncestorArray);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
while (i < count) {
|
2010-04-30 04:15:09 -07:00
|
|
|
nsINode *node = aAncestorArray.ElementAt(i++);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!node)
|
|
|
|
break;
|
|
|
|
|
2012-05-16 09:39:54 -07:00
|
|
|
// Either a general inclusion or as immediate context
|
|
|
|
if (IncludeInContext(node) || i - 1 < j) {
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = SerializeNodeEnd(node, aString);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-01-10 06:19:54 -08:00
|
|
|
nsDocumentEncoder::SerializeRangeToString(nsRange *aRange,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAString& aOutputString)
|
|
|
|
{
|
2010-04-30 04:15:09 -07:00
|
|
|
if (!aRange || aRange->Collapsed())
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
|
2010-04-30 04:15:09 -07:00
|
|
|
mCommonParent = aRange->GetCommonAncestor();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!mCommonParent)
|
|
|
|
return NS_OK;
|
|
|
|
|
2010-04-30 04:15:09 -07:00
|
|
|
nsINode* startParent = aRange->GetStartParent();
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_TRUE(startParent, NS_ERROR_FAILURE);
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t startOffset = aRange->StartOffset();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-30 04:15:09 -07:00
|
|
|
nsINode* endParent = aRange->GetEndParent();
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_TRUE(endParent, NS_ERROR_FAILURE);
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t endOffset = aRange->EndOffset();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
mCommonAncestors.Clear();
|
|
|
|
mStartNodes.Clear();
|
|
|
|
mStartOffsets.Clear();
|
|
|
|
mEndNodes.Clear();
|
|
|
|
mEndOffsets.Clear();
|
|
|
|
|
2010-04-30 04:15:09 -07:00
|
|
|
nsContentUtils::GetAncestors(mCommonParent, mCommonAncestors);
|
|
|
|
nsCOMPtr<nsIDOMNode> sp = do_QueryInterface(startParent);
|
|
|
|
nsContentUtils::GetAncestorsAndOffsets(sp, startOffset,
|
2007-03-22 10:30:00 -07:00
|
|
|
&mStartNodes, &mStartOffsets);
|
2010-04-30 04:15:09 -07:00
|
|
|
nsCOMPtr<nsIDOMNode> ep = do_QueryInterface(endParent);
|
|
|
|
nsContentUtils::GetAncestorsAndOffsets(ep, endOffset,
|
2007-03-22 10:30:00 -07:00
|
|
|
&mEndNodes, &mEndOffsets);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> commonContent = do_QueryInterface(mCommonParent);
|
|
|
|
mStartRootIndex = mStartNodes.IndexOf(commonContent);
|
|
|
|
mEndRootIndex = mEndNodes.IndexOf(commonContent);
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
rv = SerializeRangeContextStart(mCommonAncestors, aOutputString);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if ((startParent == endParent) && IsTextNode(startParent))
|
|
|
|
{
|
2010-05-24 16:24:15 -07:00
|
|
|
if (mFlags & SkipInvisibleContent) {
|
|
|
|
// Check that the parent is visible if we don't a frame.
|
|
|
|
// IsVisibleNode() will do it when there's a frame.
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(startParent);
|
|
|
|
if (content && !content->GetPrimaryFrame()) {
|
|
|
|
nsIContent* parent = content->GetParent();
|
|
|
|
if (!parent || !IsVisibleNode(parent))
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = SerializeNodeStart(startParent, startOffset, endOffset, aOutputString);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rv = SerializeRangeNodes(aRange, mCommonParent, aOutputString, 0);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
rv = SerializeRangeContextEnd(mCommonAncestors, aOutputString);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDocumentEncoder::EncodeToString(nsAString& aOutputString)
|
2014-04-16 15:01:55 -07:00
|
|
|
{
|
|
|
|
return EncodeToStringWithMaxLength(0, aOutputString);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDocumentEncoder::EncodeToStringWithMaxLength(uint32_t aMaxLength,
|
|
|
|
nsAString& aOutputString)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!mDocument)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
aOutputString.Truncate();
|
|
|
|
|
2010-06-26 13:39:56 -07:00
|
|
|
nsString output;
|
|
|
|
static const size_t bufferSize = 2048;
|
|
|
|
if (!mCachedBuffer) {
|
2014-03-15 12:00:15 -07:00
|
|
|
mCachedBuffer = nsStringBuffer::Alloc(bufferSize).take();
|
2010-06-26 13:39:56 -07:00
|
|
|
}
|
|
|
|
NS_ASSERTION(!mCachedBuffer->IsReadonly(),
|
|
|
|
"DocumentEncoder shouldn't keep reference to non-readonly buffer!");
|
2014-01-04 07:02:17 -08:00
|
|
|
static_cast<char16_t*>(mCachedBuffer->Data())[0] = char16_t(0);
|
2011-10-17 07:59:28 -07:00
|
|
|
mCachedBuffer->ToString(0, output, true);
|
2010-06-26 13:39:56 -07:00
|
|
|
// output owns the buffer now!
|
2012-07-30 07:20:58 -07:00
|
|
|
mCachedBuffer = nullptr;
|
2010-06-26 13:39:56 -07:00
|
|
|
|
|
|
|
|
|
|
|
if (!mSerializer) {
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString progId(NS_CONTENTSERIALIZER_CONTRACTID_PREFIX);
|
2010-06-26 13:39:56 -07:00
|
|
|
AppendUTF16toUTF8(mMimeType, progId);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-26 13:39:56 -07:00
|
|
|
mSerializer = do_CreateInstance(progId.get());
|
|
|
|
NS_ENSURE_TRUE(mSerializer, NS_ERROR_NOT_IMPLEMENTED);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAtom> charsetAtom;
|
2007-08-10 17:38:53 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool rewriteEncodingDeclaration = !(mSelection || mRange || mNode) && !(mFlags & OutputDontRewriteEncodingDeclaration);
|
2009-09-28 00:59:52 -07:00
|
|
|
mSerializer->Init(mFlags, mWrapColumn, mCharset.get(), mIsCopying, rewriteEncodingDeclaration);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (mSelection) {
|
|
|
|
nsCOMPtr<nsIDOMRange> range;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t i, count = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
rv = mSelection->GetRangeCount(&count);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-03-27 08:10:23 -07:00
|
|
|
nsCOMPtr<nsIDOMNode> node, prevNode;
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
mSelection->GetRangeAt(i, getter_AddRefs(range));
|
|
|
|
|
2007-03-27 08:10:23 -07:00
|
|
|
// Bug 236546: newlines not added when copying table cells into clipboard
|
|
|
|
// Each selected cell shows up as a range containing a row with a single cell
|
|
|
|
// get the row, compare it to previous row and emit </tr><tr> as needed
|
2012-05-16 09:39:54 -07:00
|
|
|
// Bug 137450: Problem copying/pasting a table from a web page to Excel.
|
|
|
|
// Each separate block of <tr></tr> produced above will be wrapped by the
|
|
|
|
// immediate context. This assumes that you can't select cells that are
|
|
|
|
// multiple selections from two tables simultaneously.
|
2007-03-27 08:10:23 -07:00
|
|
|
range->GetStartContainer(getter_AddRefs(node));
|
|
|
|
NS_ENSURE_TRUE(node, NS_ERROR_FAILURE);
|
2007-05-25 13:47:30 -07:00
|
|
|
if (node != prevNode) {
|
2012-05-16 09:39:54 -07:00
|
|
|
nsCOMPtr<nsINode> p;
|
2007-05-25 13:47:30 -07:00
|
|
|
if (prevNode) {
|
2012-05-16 09:39:54 -07:00
|
|
|
p = do_QueryInterface(prevNode);
|
2010-06-26 13:39:56 -07:00
|
|
|
rv = SerializeNodeEnd(p, output);
|
2007-05-25 13:47:30 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(node);
|
2012-05-16 09:39:54 -07:00
|
|
|
if (content && content->IsHTML(nsGkAtoms::tr)) {
|
|
|
|
nsINode* n = content;
|
|
|
|
if (!prevNode) {
|
|
|
|
// Went from a non-<tr> to a <tr>
|
|
|
|
mCommonAncestors.Clear();
|
2012-10-09 05:31:24 -07:00
|
|
|
nsContentUtils::GetAncestors(n->GetParentNode(), mCommonAncestors);
|
2012-05-16 09:39:54 -07:00
|
|
|
rv = SerializeRangeContextStart(mCommonAncestors, output);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
// Don't let SerializeRangeToString serialize the context again
|
2012-05-16 09:39:54 -07:00
|
|
|
mDisableContextSerialize = true;
|
2012-05-16 09:39:54 -07:00
|
|
|
}
|
|
|
|
|
2010-06-26 13:39:56 -07:00
|
|
|
rv = SerializeNodeStart(n, 0, -1, output);
|
2007-05-25 13:47:30 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
prevNode = node;
|
2012-05-16 09:39:54 -07:00
|
|
|
} else if (prevNode) {
|
|
|
|
// Went from a <tr> to a non-<tr>
|
|
|
|
mCommonAncestors.Clear();
|
2012-10-09 05:31:24 -07:00
|
|
|
nsContentUtils::GetAncestors(p->GetParentNode(), mCommonAncestors);
|
2012-05-16 09:39:54 -07:00
|
|
|
mDisableContextSerialize = false;
|
2012-05-16 09:39:54 -07:00
|
|
|
rv = SerializeRangeContextEnd(mCommonAncestors, output);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-07-30 07:20:58 -07:00
|
|
|
prevNode = nullptr;
|
2007-03-27 08:10:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 06:19:54 -08:00
|
|
|
nsRange* r = static_cast<nsRange*>(range.get());
|
2010-06-26 13:39:56 -07:00
|
|
|
rv = SerializeRangeToString(r, output);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2012-05-16 09:39:54 -07:00
|
|
|
|
2007-05-25 13:47:30 -07:00
|
|
|
if (prevNode) {
|
2010-04-30 04:15:09 -07:00
|
|
|
nsCOMPtr<nsINode> p = do_QueryInterface(prevNode);
|
2010-06-26 13:39:56 -07:00
|
|
|
rv = SerializeNodeEnd(p, output);
|
2007-05-25 13:47:30 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-05-16 09:39:54 -07:00
|
|
|
mCommonAncestors.Clear();
|
2012-10-09 05:31:24 -07:00
|
|
|
nsContentUtils::GetAncestors(p->GetParentNode(), mCommonAncestors);
|
2012-05-16 09:39:54 -07:00
|
|
|
mDisableContextSerialize = false;
|
2012-05-16 09:39:54 -07:00
|
|
|
rv = SerializeRangeContextEnd(mCommonAncestors, output);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-27 08:10:23 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-05-16 09:39:54 -07:00
|
|
|
// Just to be safe
|
|
|
|
mDisableContextSerialize = false;
|
2012-05-16 09:39:54 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mSelection = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else if (mRange) {
|
2010-06-26 13:39:56 -07:00
|
|
|
rv = SerializeRangeToString(mRange, output);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mRange = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else if (mNode) {
|
2010-10-15 10:59:00 -07:00
|
|
|
if (!mNodeFixup && !(mFlags & SkipInvisibleContent) && !mStream &&
|
|
|
|
mNodeIsContainer) {
|
|
|
|
rv = SerializeToStringIterative(mNode, output);
|
|
|
|
} else {
|
|
|
|
rv = SerializeToStringRecursive(mNode, output, mNodeIsContainer);
|
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
mNode = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2010-06-26 13:39:56 -07:00
|
|
|
rv = mSerializer->AppendDocumentStart(mDocument, output);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-04-16 15:01:55 -07:00
|
|
|
rv = SerializeToStringRecursive(mDocument, output, false, aMaxLength);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-26 13:39:56 -07:00
|
|
|
rv = mSerializer->Flush(output);
|
|
|
|
|
|
|
|
mCachedBuffer = nsStringBuffer::FromString(output);
|
2010-10-06 18:01:24 -07:00
|
|
|
// We have to be careful how we set aOutputString, because we don't
|
|
|
|
// want it to end up sharing mCachedBuffer if we plan to reuse it.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool setOutput = false;
|
2010-06-26 13:39:56 -07:00
|
|
|
// Try to cache the buffer.
|
2010-10-06 18:01:24 -07:00
|
|
|
if (mCachedBuffer) {
|
|
|
|
if (mCachedBuffer->StorageSize() == bufferSize &&
|
|
|
|
!mCachedBuffer->IsReadonly()) {
|
|
|
|
mCachedBuffer->AddRef();
|
|
|
|
} else {
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mCachedBuffer->ToString(output.Length(), aOutputString);
|
2011-10-17 07:59:28 -07:00
|
|
|
setOutput = true;
|
2010-10-06 18:01:24 -07:00
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
mCachedBuffer = nullptr;
|
2010-10-06 18:01:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!setOutput && NS_SUCCEEDED(rv)) {
|
|
|
|
aOutputString.Append(output.get(), output.Length());
|
2010-06-26 13:39:56 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDocumentEncoder::EncodeToStream(nsIOutputStream* aStream)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
if (!mDocument)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2014-05-08 02:32:00 -07:00
|
|
|
nsAutoCString encoding;
|
|
|
|
if (!EncodingUtils::FindEncodingForLabelNoReplacement(mCharset, encoding)) {
|
|
|
|
return NS_ERROR_UCONV_NOCONV;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-08 02:32:00 -07:00
|
|
|
mUnicodeEncoder = EncodingUtils::EncoderForEncoding(encoding);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (mMimeType.LowerCaseEqualsLiteral("text/plain")) {
|
2012-07-30 07:20:58 -07:00
|
|
|
rv = mUnicodeEncoder->SetOutputErrorBehavior(nsIUnicodeEncoder::kOnError_Replace, nullptr, '?');
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2013-05-16 13:11:55 -07:00
|
|
|
mStream = aStream;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAutoString buf;
|
|
|
|
|
|
|
|
rv = EncodeToString(buf);
|
|
|
|
|
|
|
|
// Force a flush of the last chunk of data.
|
2011-10-17 07:59:28 -07:00
|
|
|
FlushText(buf, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mStream = nullptr;
|
|
|
|
mUnicodeEncoder = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDocumentEncoder::EncodeToStringWithContext(nsAString& aContextString,
|
|
|
|
nsAString& aInfoString,
|
|
|
|
nsAString& aEncodedString)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDocumentEncoder::SetNodeFixup(nsIDocumentEncoderNodeFixup *aFixup)
|
|
|
|
{
|
|
|
|
mNodeFixup = aFixup;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult NS_NewTextEncoder(nsIDocumentEncoder** aResult); // make mac compiler happy
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewTextEncoder(nsIDocumentEncoder** aResult)
|
|
|
|
{
|
|
|
|
*aResult = new nsDocumentEncoder;
|
|
|
|
if (!*aResult)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
class nsHTMLCopyEncoder : public nsDocumentEncoder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
nsHTMLCopyEncoder();
|
|
|
|
virtual ~nsHTMLCopyEncoder();
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_IMETHOD Init(nsIDOMDocument* aDocument, const nsAString& aMimeType, uint32_t aFlags);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// overridden methods from nsDocumentEncoder
|
|
|
|
NS_IMETHOD SetSelection(nsISelection* aSelection);
|
|
|
|
NS_IMETHOD EncodeToStringWithContext(nsAString& aContextString,
|
|
|
|
nsAString& aInfoString,
|
|
|
|
nsAString& aEncodedString);
|
2013-04-03 11:39:06 -07:00
|
|
|
NS_IMETHOD EncodeToString(nsAString& aOutputString);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
enum Endpoint
|
|
|
|
{
|
|
|
|
kStart,
|
|
|
|
kEnd
|
|
|
|
};
|
|
|
|
|
|
|
|
nsresult PromoteRange(nsIDOMRange *inRange);
|
|
|
|
nsresult PromoteAncestorChain(nsCOMPtr<nsIDOMNode> *ioNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t *ioStartOffset,
|
|
|
|
int32_t *ioEndOffset);
|
|
|
|
nsresult GetPromotedPoint(Endpoint aWhere, nsIDOMNode *aNode, int32_t aOffset,
|
|
|
|
nsCOMPtr<nsIDOMNode> *outNode, int32_t *outOffset, nsIDOMNode *aCommon);
|
|
|
|
nsCOMPtr<nsIDOMNode> GetChildAt(nsIDOMNode *aParent, int32_t aOffset);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsMozBR(nsIDOMNode* aNode);
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult GetNodeLocation(nsIDOMNode *inChild, nsCOMPtr<nsIDOMNode> *outParent, int32_t *outOffset);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsRoot(nsIDOMNode* aNode);
|
|
|
|
bool IsFirstNode(nsIDOMNode *aNode);
|
|
|
|
bool IsLastNode(nsIDOMNode *aNode);
|
|
|
|
bool IsEmptyTextContent(nsIDOMNode* aNode);
|
|
|
|
virtual bool IncludeInContext(nsINode *aNode);
|
2012-08-22 08:56:38 -07:00
|
|
|
virtual int32_t
|
2012-05-16 09:39:54 -07:00
|
|
|
GetImmediateContextCount(const nsTArray<nsINode*>& aAncestorArray);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mIsTextWidget;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
nsHTMLCopyEncoder::nsHTMLCopyEncoder()
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsTextWidget = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHTMLCopyEncoder::~nsHTMLCopyEncoder()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLCopyEncoder::Init(nsIDOMDocument* aDocument,
|
2010-05-24 16:24:15 -07:00
|
|
|
const nsAString& aMimeType,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!aDocument)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsTextWidget = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
Initialize();
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsCopying = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
mDocument = do_QueryInterface(aDocument);
|
|
|
|
NS_ENSURE_TRUE(mDocument, NS_ERROR_FAILURE);
|
|
|
|
|
2012-03-31 07:10:34 -07:00
|
|
|
// Hack, hack! Traditionally, the caller passes text/unicode, which is
|
|
|
|
// treated as "guess text/html or text/plain" in this context. (It has a
|
|
|
|
// different meaning in other contexts. Sigh.) From now on, "text/plain"
|
|
|
|
// means forcing text/plain instead of guessing.
|
|
|
|
if (aMimeType.EqualsLiteral("text/plain")) {
|
|
|
|
mMimeType.AssignLiteral("text/plain");
|
|
|
|
} else {
|
|
|
|
mMimeType.AssignLiteral("text/html");
|
|
|
|
}
|
2010-05-24 16:24:15 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Make all links absolute when copying
|
|
|
|
// (see related bugs #57296, #41924, #58646, #32768)
|
|
|
|
mFlags = aFlags | OutputAbsoluteLinks;
|
|
|
|
|
|
|
|
if (!mDocument->IsScriptEnabled())
|
|
|
|
mFlags |= OutputNoScriptContent;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLCopyEncoder::SetSelection(nsISelection* aSelection)
|
|
|
|
{
|
|
|
|
// check for text widgets: we need to recognize these so that
|
|
|
|
// we don't tweak the selection to be outside of the magic
|
|
|
|
// div that ender-lite text widgets are embedded in.
|
|
|
|
|
|
|
|
if (!aSelection)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMRange> range;
|
|
|
|
nsCOMPtr<nsIDOMNode> commonParent;
|
2013-02-07 06:17:47 -08:00
|
|
|
Selection* selection = static_cast<Selection*>(aSelection);
|
|
|
|
uint32_t rangeCount = selection->GetRangeCount();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// if selection is uninitialized return
|
2013-02-07 06:17:47 -08:00
|
|
|
if (!rangeCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// we'll just use the common parent of the first range. Implicit assumption
|
|
|
|
// here that multi-range selections are table cell selections, in which case
|
|
|
|
// the common parent is somewhere in the table and we don't really care where.
|
2013-02-07 06:17:47 -08:00
|
|
|
nsresult rv = aSelection->GetRangeAt(0, getter_AddRefs(range));
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!range)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
range->GetCommonAncestorContainer(getter_AddRefs(commonParent));
|
|
|
|
|
|
|
|
for (nsCOMPtr<nsIContent> selContent(do_QueryInterface(commonParent));
|
|
|
|
selContent;
|
|
|
|
selContent = selContent->GetParent())
|
|
|
|
{
|
|
|
|
// checking for selection inside a plaintext form widget
|
|
|
|
nsIAtom *atom = selContent->Tag();
|
|
|
|
if (atom == nsGkAtoms::input ||
|
|
|
|
atom == nsGkAtoms::textarea)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsTextWidget = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
}
|
2014-12-01 03:38:11 -08:00
|
|
|
else if (selContent->IsElement()) {
|
|
|
|
nsRefPtr<nsStyleContext> styleContext =
|
|
|
|
nsComputedDOMStyle::GetStyleContextForElementNoFlush(selContent->AsElement(),
|
|
|
|
nullptr, nullptr);
|
|
|
|
if (styleContext) {
|
|
|
|
const nsStyleText* textStyle = styleContext->StyleText();
|
|
|
|
switch (textStyle->mWhiteSpace) {
|
|
|
|
case NS_STYLE_WHITESPACE_PRE:
|
|
|
|
case NS_STYLE_WHITESPACE_PRE_WRAP:
|
|
|
|
case NS_STYLE_WHITESPACE_PRE_LINE:
|
|
|
|
case NS_STYLE_WHITESPACE_PRE_SPACE:
|
|
|
|
// Copy as plaintext for all preformatted elements
|
|
|
|
mIsTextWidget = true;
|
|
|
|
break;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-12-01 03:38:11 -08:00
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// normalize selection if we are not in a widget
|
|
|
|
if (mIsTextWidget)
|
|
|
|
{
|
|
|
|
mSelection = aSelection;
|
|
|
|
mMimeType.AssignLiteral("text/plain");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-04-03 11:39:06 -07:00
|
|
|
|
|
|
|
// also consider ourselves in a text widget if we can't find an html document
|
|
|
|
nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(mDocument);
|
|
|
|
if (!(htmlDoc && mDocument->IsHTML())) {
|
|
|
|
mIsTextWidget = true;
|
|
|
|
mSelection = aSelection;
|
|
|
|
// mMimeType is set to text/plain when encoding starts.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// there's no Clone() for selection! fix...
|
|
|
|
//nsresult rv = aSelection->Clone(getter_AddRefs(mSelection);
|
|
|
|
//NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_NewDomSelection(getter_AddRefs(mSelection));
|
|
|
|
NS_ENSURE_TRUE(mSelection, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
// loop thru the ranges in the selection
|
2013-02-07 06:17:47 -08:00
|
|
|
for (uint32_t rangeIdx = 0; rangeIdx < rangeCount; ++rangeIdx) {
|
|
|
|
range = selection->GetRangeAt(rangeIdx);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_TRUE(range, NS_ERROR_FAILURE);
|
|
|
|
nsCOMPtr<nsIDOMRange> myRange;
|
|
|
|
range->CloneRange(getter_AddRefs(myRange));
|
|
|
|
NS_ENSURE_TRUE(myRange, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
// adjust range to include any ancestors who's children are entirely selected
|
|
|
|
rv = PromoteRange(myRange);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mSelection->AddRange(myRange);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-03 11:39:06 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLCopyEncoder::EncodeToString(nsAString& aOutputString)
|
|
|
|
{
|
|
|
|
if (mIsTextWidget) {
|
|
|
|
mMimeType.AssignLiteral("text/plain");
|
|
|
|
}
|
|
|
|
return nsDocumentEncoder::EncodeToString(aOutputString);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLCopyEncoder::EncodeToStringWithContext(nsAString& aContextString,
|
|
|
|
nsAString& aInfoString,
|
|
|
|
nsAString& aEncodedString)
|
|
|
|
{
|
|
|
|
nsresult rv = EncodeToString(aEncodedString);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// do not encode any context info or range hints if we are in a text widget.
|
|
|
|
if (mIsTextWidget) return NS_OK;
|
|
|
|
|
|
|
|
// now encode common ancestors into aContextString. Note that the common ancestors
|
|
|
|
// will be for the last range in the selection in the case of multirange selections.
|
|
|
|
// encoding ancestors every range in a multirange selection in a way that could be
|
|
|
|
// understood by the paste code would be a lot more work to do. As a practical matter,
|
|
|
|
// selections are single range, and the ones that aren't are table cell selections
|
|
|
|
// where all the cells are in the same table.
|
|
|
|
|
|
|
|
// leaf of ancestors might be text node. If so discard it.
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t count = mCommonAncestors.Length();
|
|
|
|
int32_t i;
|
2010-04-30 04:15:09 -07:00
|
|
|
nsCOMPtr<nsINode> node;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (count > 0)
|
2009-03-20 01:15:35 -07:00
|
|
|
node = mCommonAncestors.ElementAt(0);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (node && IsTextNode(node))
|
|
|
|
{
|
|
|
|
mCommonAncestors.RemoveElementAt(0);
|
|
|
|
// don't forget to adjust range depth info
|
|
|
|
if (mStartDepth) mStartDepth--;
|
|
|
|
if (mEndDepth) mEndDepth--;
|
|
|
|
// and the count
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = count;
|
|
|
|
while (i > 0)
|
|
|
|
{
|
2009-03-20 01:15:35 -07:00
|
|
|
node = mCommonAncestors.ElementAt(--i);
|
2007-03-22 10:30:00 -07:00
|
|
|
SerializeNodeStart(node, 0, -1, aContextString);
|
|
|
|
}
|
|
|
|
//i = 0; guaranteed by above
|
|
|
|
while (i < count)
|
|
|
|
{
|
2009-03-20 01:15:35 -07:00
|
|
|
node = mCommonAncestors.ElementAt(i++);
|
2007-03-22 10:30:00 -07:00
|
|
|
SerializeNodeEnd(node, aContextString);
|
|
|
|
}
|
|
|
|
|
|
|
|
// encode range info : the start and end depth of the selection, where the depth is
|
|
|
|
// distance down in the parent hierarchy. Later we will need to add leading/trailing
|
|
|
|
// whitespace info to this.
|
|
|
|
nsAutoString infoString;
|
|
|
|
infoString.AppendInt(mStartDepth);
|
2014-01-04 07:02:17 -08:00
|
|
|
infoString.Append(char16_t(','));
|
2007-03-22 10:30:00 -07:00
|
|
|
infoString.AppendInt(mEndDepth);
|
|
|
|
aInfoString = infoString;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-04-30 04:15:09 -07:00
|
|
|
nsHTMLCopyEncoder::IncludeInContext(nsINode *aNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> content(do_QueryInterface(aNode));
|
|
|
|
|
|
|
|
if (!content)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsIAtom *tag = content->Tag();
|
|
|
|
|
|
|
|
return (tag == nsGkAtoms::b ||
|
|
|
|
tag == nsGkAtoms::i ||
|
|
|
|
tag == nsGkAtoms::u ||
|
|
|
|
tag == nsGkAtoms::a ||
|
|
|
|
tag == nsGkAtoms::tt ||
|
|
|
|
tag == nsGkAtoms::s ||
|
|
|
|
tag == nsGkAtoms::big ||
|
|
|
|
tag == nsGkAtoms::small ||
|
|
|
|
tag == nsGkAtoms::strike ||
|
|
|
|
tag == nsGkAtoms::em ||
|
|
|
|
tag == nsGkAtoms::strong ||
|
|
|
|
tag == nsGkAtoms::dfn ||
|
|
|
|
tag == nsGkAtoms::code ||
|
|
|
|
tag == nsGkAtoms::cite ||
|
2011-03-18 14:02:44 -07:00
|
|
|
tag == nsGkAtoms::var ||
|
2007-03-22 10:30:00 -07:00
|
|
|
tag == nsGkAtoms::abbr ||
|
|
|
|
tag == nsGkAtoms::font ||
|
|
|
|
tag == nsGkAtoms::script ||
|
|
|
|
tag == nsGkAtoms::span ||
|
|
|
|
tag == nsGkAtoms::pre ||
|
|
|
|
tag == nsGkAtoms::h1 ||
|
|
|
|
tag == nsGkAtoms::h2 ||
|
|
|
|
tag == nsGkAtoms::h3 ||
|
|
|
|
tag == nsGkAtoms::h4 ||
|
|
|
|
tag == nsGkAtoms::h5 ||
|
|
|
|
tag == nsGkAtoms::h6);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLCopyEncoder::PromoteRange(nsIDOMRange *inRange)
|
|
|
|
{
|
|
|
|
if (!inRange) return NS_ERROR_NULL_POINTER;
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIDOMNode> startNode, endNode, common;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t startOffset, endOffset;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
rv = inRange->GetCommonAncestorContainer(getter_AddRefs(common));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = inRange->GetStartContainer(getter_AddRefs(startNode));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = inRange->GetStartOffset(&startOffset);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = inRange->GetEndContainer(getter_AddRefs(endNode));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = inRange->GetEndOffset(&endOffset);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> opStartNode;
|
|
|
|
nsCOMPtr<nsIDOMNode> opEndNode;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t opStartOffset, opEndOffset;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIDOMRange> opRange;
|
|
|
|
|
|
|
|
// examine range endpoints.
|
|
|
|
rv = GetPromotedPoint( kStart, startNode, startOffset, address_of(opStartNode), &opStartOffset, common);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = GetPromotedPoint( kEnd, endNode, endOffset, address_of(opEndNode), &opEndOffset, common);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// if both range endpoints are at the common ancestor, check for possible inclusion of ancestors
|
|
|
|
if ( (opStartNode == common) && (opEndNode == common) )
|
|
|
|
{
|
|
|
|
rv = PromoteAncestorChain(address_of(opStartNode), &opStartOffset, &opEndOffset);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
opEndNode = opStartNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the range to the new values
|
|
|
|
rv = inRange->SetStart(opStartNode, opStartOffset);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = inRange->SetEnd(opEndNode, opEndOffset);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// PromoteAncestorChain will promote a range represented by [{*ioNode,*ioStartOffset} , {*ioNode,*ioEndOffset}]
|
|
|
|
// The promotion is different from that found in getPromotedPoint: it will only promote one endpoint if it can
|
|
|
|
// promote the other. Thus, instead of having a startnode/endNode, there is just the one ioNode.
|
|
|
|
nsresult
|
|
|
|
nsHTMLCopyEncoder::PromoteAncestorChain(nsCOMPtr<nsIDOMNode> *ioNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t *ioStartOffset,
|
|
|
|
int32_t *ioEndOffset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!ioNode || !ioStartOffset || !ioEndOffset) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool done = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> frontNode, endNode, parent;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t frontOffset, endOffset;
|
2009-11-01 15:43:18 -08:00
|
|
|
|
|
|
|
//save the editable state of the ioNode, so we don't promote an ancestor if it has different editable state
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(*ioNode);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isEditable = node->IsEditable();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// loop for as long as we can promote both endpoints
|
|
|
|
while (!done)
|
|
|
|
{
|
|
|
|
rv = (*ioNode)->GetParentNode(getter_AddRefs(parent));
|
|
|
|
if ((NS_FAILED(rv)) || !parent)
|
2011-10-17 07:59:28 -07:00
|
|
|
done = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// passing parent as last param to GetPromotedPoint() allows it to promote only one level
|
2007-11-09 16:02:23 -08:00
|
|
|
// up the hierarchy.
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = GetPromotedPoint( kStart, *ioNode, *ioStartOffset, address_of(frontNode), &frontOffset, parent);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
// then we make the same attempt with the endpoint
|
|
|
|
rv = GetPromotedPoint( kEnd, *ioNode, *ioEndOffset, address_of(endNode), &endOffset, parent);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-11-01 15:43:18 -08:00
|
|
|
|
|
|
|
nsCOMPtr<nsINode> frontINode = do_QueryInterface(frontNode);
|
|
|
|
// if both endpoints were promoted one level and isEditable is the same as the original node,
|
|
|
|
// keep looping - otherwise we are done.
|
|
|
|
if ( (frontNode != parent) || (endNode != parent) || (frontINode->IsEditable() != isEditable) )
|
2011-10-17 07:59:28 -07:00
|
|
|
done = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
*ioNode = frontNode;
|
|
|
|
*ioStartOffset = frontOffset;
|
|
|
|
*ioEndOffset = endOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsHTMLCopyEncoder::GetPromotedPoint(Endpoint aWhere, nsIDOMNode *aNode, int32_t aOffset,
|
|
|
|
nsCOMPtr<nsIDOMNode> *outNode, int32_t *outOffset, nsIDOMNode *common)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsIDOMNode> node = aNode;
|
|
|
|
nsCOMPtr<nsIDOMNode> parent = aNode;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t offset = aOffset;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool bResetPromotion = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// default values
|
|
|
|
*outNode = node;
|
|
|
|
*outOffset = offset;
|
|
|
|
|
|
|
|
if (common == node)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
if (aWhere == kStart)
|
|
|
|
{
|
|
|
|
// some special casing for text nodes
|
2010-04-30 04:15:09 -07:00
|
|
|
nsCOMPtr<nsINode> t = do_QueryInterface(aNode);
|
|
|
|
if (IsTextNode(t))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// if not at beginning of text node, we are done
|
|
|
|
if (offset > 0)
|
|
|
|
{
|
|
|
|
// unless everything before us in just whitespace. NOTE: we need a more
|
|
|
|
// general solution that truly detects all cases of non-significant
|
|
|
|
// whitesace with no false alarms.
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> nodeAsText = do_QueryInterface(aNode);
|
|
|
|
nsAutoString text;
|
|
|
|
nodeAsText->SubstringData(0, offset, text);
|
|
|
|
text.CompressWhitespace();
|
|
|
|
if (!text.IsEmpty())
|
|
|
|
return NS_OK;
|
2011-10-17 07:59:28 -07:00
|
|
|
bResetPromotion = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
// else
|
|
|
|
rv = GetNodeLocation(aNode, address_of(parent), &offset);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
node = GetChildAt(parent,offset);
|
|
|
|
}
|
|
|
|
if (!node) node = parent;
|
|
|
|
|
|
|
|
// finding the real start for this point. look up the tree for as long as we are the
|
|
|
|
// first node in the container, and as long as we haven't hit the body node.
|
|
|
|
if (!IsRoot(node) && (parent != common))
|
|
|
|
{
|
|
|
|
rv = GetNodeLocation(node, address_of(parent), &offset);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (offset == -1) return NS_OK; // we hit generated content; STOP
|
|
|
|
nsIParserService *parserService = nsContentUtils::GetParserService();
|
|
|
|
if (!parserService)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
while ((IsFirstNode(node)) && (!IsRoot(parent)) && (parent != common))
|
|
|
|
{
|
|
|
|
if (bResetPromotion)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(parent);
|
|
|
|
if (content)
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isBlock = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
parserService->IsBlock(parserService->HTMLAtomTagToId(content->Tag()), isBlock);
|
|
|
|
if (isBlock)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
bResetPromotion = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node = parent;
|
|
|
|
rv = GetNodeLocation(node, address_of(parent), &offset);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (offset == -1) // we hit generated content; STOP
|
|
|
|
{
|
|
|
|
// back up a bit
|
|
|
|
parent = node;
|
|
|
|
offset = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bResetPromotion)
|
|
|
|
{
|
|
|
|
*outNode = aNode;
|
|
|
|
*outOffset = aOffset;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*outNode = parent;
|
|
|
|
*outOffset = offset;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aWhere == kEnd)
|
|
|
|
{
|
|
|
|
// some special casing for text nodes
|
2010-04-30 04:15:09 -07:00
|
|
|
nsCOMPtr<nsINode> n = do_QueryInterface(aNode);
|
|
|
|
if (IsTextNode(n))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// if not at end of text node, we are done
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t len = n->Length();
|
|
|
|
if (offset < (int32_t)len)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// unless everything after us in just whitespace. NOTE: we need a more
|
|
|
|
// general solution that truly detects all cases of non-significant
|
2012-03-29 14:09:04 -07:00
|
|
|
// whitespace with no false alarms.
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIDOMCharacterData> nodeAsText = do_QueryInterface(aNode);
|
|
|
|
nsAutoString text;
|
|
|
|
nodeAsText->SubstringData(offset, len-offset, text);
|
|
|
|
text.CompressWhitespace();
|
|
|
|
if (!text.IsEmpty())
|
|
|
|
return NS_OK;
|
2011-10-17 07:59:28 -07:00
|
|
|
bResetPromotion = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
rv = GetNodeLocation(aNode, address_of(parent), &offset);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (offset) offset--; // we want node _before_ offset
|
|
|
|
node = GetChildAt(parent,offset);
|
|
|
|
}
|
|
|
|
if (!node) node = parent;
|
|
|
|
|
|
|
|
// finding the real end for this point. look up the tree for as long as we are the
|
|
|
|
// last node in the container, and as long as we haven't hit the body node.
|
|
|
|
if (!IsRoot(node) && (parent != common))
|
|
|
|
{
|
|
|
|
rv = GetNodeLocation(node, address_of(parent), &offset);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (offset == -1) return NS_OK; // we hit generated content; STOP
|
|
|
|
nsIParserService *parserService = nsContentUtils::GetParserService();
|
|
|
|
if (!parserService)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
while ((IsLastNode(node)) && (!IsRoot(parent)) && (parent != common))
|
|
|
|
{
|
|
|
|
if (bResetPromotion)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(parent);
|
|
|
|
if (content)
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isBlock = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
parserService->IsBlock(parserService->HTMLAtomTagToId(content->Tag()), isBlock);
|
|
|
|
if (isBlock)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
bResetPromotion = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node = parent;
|
|
|
|
rv = GetNodeLocation(node, address_of(parent), &offset);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (offset == -1) // we hit generated content; STOP
|
|
|
|
{
|
|
|
|
// back up a bit
|
|
|
|
parent = node;
|
|
|
|
offset = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bResetPromotion)
|
|
|
|
{
|
|
|
|
*outNode = aNode;
|
|
|
|
*outOffset = aOffset;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*outNode = parent;
|
|
|
|
offset++; // add one since this in an endpoint - want to be AFTER node.
|
|
|
|
*outOffset = offset;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode>
|
2012-08-22 08:56:38 -07:00
|
|
|
nsHTMLCopyEncoder::GetChildAt(nsIDOMNode *aParent, int32_t aOffset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> resultNode;
|
|
|
|
|
|
|
|
if (!aParent)
|
|
|
|
return resultNode;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aParent);
|
|
|
|
NS_PRECONDITION(content, "null content in nsHTMLCopyEncoder::GetChildAt");
|
|
|
|
|
|
|
|
resultNode = do_QueryInterface(content->GetChildAt(aOffset));
|
|
|
|
|
|
|
|
return resultNode;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsHTMLCopyEncoder::IsMozBR(nsIDOMNode* aNode)
|
|
|
|
{
|
2012-02-10 02:04:45 -08:00
|
|
|
MOZ_ASSERT(aNode);
|
|
|
|
nsCOMPtr<Element> element = do_QueryInterface(aNode);
|
|
|
|
return element &&
|
|
|
|
element->IsHTML(nsGkAtoms::br) &&
|
|
|
|
element->AttrValueIs(kNameSpaceID_None, nsGkAtoms::type,
|
|
|
|
NS_LITERAL_STRING("_moz"), eIgnoreCase);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLCopyEncoder::GetNodeLocation(nsIDOMNode *inChild,
|
|
|
|
nsCOMPtr<nsIDOMNode> *outParent,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t *outOffset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION((inChild && outParent && outOffset), "bad args");
|
|
|
|
nsresult result = NS_ERROR_NULL_POINTER;
|
|
|
|
if (inChild && outParent && outOffset)
|
|
|
|
{
|
|
|
|
result = inChild->GetParentNode(getter_AddRefs(*outParent));
|
|
|
|
if ((NS_SUCCEEDED(result)) && (*outParent))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(*outParent);
|
|
|
|
nsCOMPtr<nsIContent> cChild = do_QueryInterface(inChild);
|
|
|
|
if (!cChild || !content)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
*outOffset = content->IndexOf(cChild);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsHTMLCopyEncoder::IsRoot(nsIDOMNode* aNode)
|
|
|
|
{
|
2010-04-30 04:15:09 -07:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
|
|
|
|
if (content)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mIsTextWidget)
|
2010-04-30 04:15:09 -07:00
|
|
|
return (IsTag(content, nsGkAtoms::div));
|
|
|
|
|
|
|
|
return (IsTag(content, nsGkAtoms::body) ||
|
|
|
|
IsTag(content, nsGkAtoms::td) ||
|
|
|
|
IsTag(content, nsGkAtoms::th));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsHTMLCopyEncoder::IsFirstNode(nsIDOMNode *aNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t offset, j=0;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = GetNodeLocation(aNode, address_of(parent), &offset);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("failure in IsFirstNode");
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
if (offset == 0) // easy case, we are first dom child
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!parent)
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// need to check if any nodes before us are really visible.
|
|
|
|
// Mike wrote something for me along these lines in nsSelectionController,
|
|
|
|
// but I don't think it's ready for use yet - revisit.
|
|
|
|
// HACK: for now, simply consider all whitespace text nodes to be
|
|
|
|
// invisible formatting nodes.
|
|
|
|
nsCOMPtr<nsIDOMNodeList> childList;
|
|
|
|
nsCOMPtr<nsIDOMNode> child;
|
|
|
|
|
|
|
|
rv = parent->GetChildNodes(getter_AddRefs(childList));
|
|
|
|
if (NS_FAILED(rv) || !childList)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("failure in IsFirstNode");
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
while (j < offset)
|
|
|
|
{
|
|
|
|
childList->Item(j, getter_AddRefs(child));
|
|
|
|
if (!IsEmptyTextContent(child))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
j++;
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsHTMLCopyEncoder::IsLastNode(nsIDOMNode *aNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t offset,j;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = GetNodeLocation(aNode, address_of(parent), &offset);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("failure in IsLastNode");
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-03-29 14:09:04 -07:00
|
|
|
nsCOMPtr<nsINode> parentNode = do_QueryInterface(parent);
|
|
|
|
if (!parentNode) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2012-03-29 14:09:04 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t numChildren = parentNode->Length();
|
|
|
|
if (offset+1 == (int32_t)numChildren) // easy case, we are last dom child
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
// need to check if any nodes after us are really visible.
|
|
|
|
// Mike wrote something for me along these lines in nsSelectionController,
|
|
|
|
// but I don't think it's ready for use yet - revisit.
|
|
|
|
// HACK: for now, simply consider all whitespace text nodes to be
|
|
|
|
// invisible formatting nodes.
|
2012-08-22 08:56:38 -07:00
|
|
|
j = (int32_t)numChildren-1;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIDOMNodeList>childList;
|
|
|
|
nsCOMPtr<nsIDOMNode> child;
|
|
|
|
rv = parent->GetChildNodes(getter_AddRefs(childList));
|
|
|
|
if (NS_FAILED(rv) || !childList)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("failure in IsLastNode");
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
while (j > offset)
|
|
|
|
{
|
|
|
|
childList->Item(j, getter_AddRefs(child));
|
|
|
|
j--;
|
|
|
|
if (IsMozBR(child)) // we ignore trailing moz BRs.
|
|
|
|
continue;
|
|
|
|
if (!IsEmptyTextContent(child))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsHTMLCopyEncoder::IsEmptyTextContent(nsIDOMNode* aNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> cont = do_QueryInterface(aNode);
|
|
|
|
return cont && cont->TextIsOnlyWhitespace();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult NS_NewHTMLCopyTextEncoder(nsIDocumentEncoder** aResult); // make mac compiler happy
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewHTMLCopyTextEncoder(nsIDocumentEncoder** aResult)
|
|
|
|
{
|
|
|
|
*aResult = new nsHTMLCopyEncoder;
|
|
|
|
if (!*aResult)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-05-16 09:39:54 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t
|
2012-05-16 09:39:54 -07:00
|
|
|
nsHTMLCopyEncoder::GetImmediateContextCount(const nsTArray<nsINode*>& aAncestorArray)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t i = aAncestorArray.Length(), j = 0;
|
2012-05-16 09:39:54 -07:00
|
|
|
while (j < i) {
|
|
|
|
nsINode *node = aAncestorArray.ElementAt(j);
|
|
|
|
if (!node) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIContent> content(do_QueryInterface(node));
|
2012-10-14 23:04:47 -07:00
|
|
|
if (!content || !content->IsHTML() || (content->Tag() != nsGkAtoms::tr &&
|
|
|
|
content->Tag() != nsGkAtoms::thead &&
|
|
|
|
content->Tag() != nsGkAtoms::tbody &&
|
|
|
|
content->Tag() != nsGkAtoms::tfoot &&
|
|
|
|
content->Tag() != nsGkAtoms::table)) {
|
2012-05-16 09:39:54 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
return j;
|
|
|
|
}
|
|
|
|
|