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
|
|
|
|
2013-04-02 17:13:10 -07:00
|
|
|
#include <stddef.h> // for nullptr
|
2012-07-12 23:33:42 -07:00
|
|
|
|
|
|
|
#include "mozilla/Assertions.h" // for MOZ_ASSERT, etc
|
2014-11-02 04:04:13 -08:00
|
|
|
#include "mozilla/dom/Selection.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "mozilla/mozalloc.h" // for operator new, etc
|
|
|
|
#include "nsAString.h" // for nsAString_internal::Length, etc
|
|
|
|
#include "nsAutoPtr.h" // for nsRefPtr
|
|
|
|
#include "nsContentUtils.h" // for nsContentUtils
|
|
|
|
#include "nsDebug.h" // for NS_ENSURE_TRUE, etc
|
|
|
|
#include "nsDependentSubstring.h" // for Substring
|
|
|
|
#include "nsError.h" // for NS_OK, NS_ERROR_FAILURE, etc
|
|
|
|
#include "nsFilteredContentIterator.h" // for nsFilteredContentIterator
|
|
|
|
#include "nsIContent.h" // for nsIContent, etc
|
|
|
|
#include "nsIContentIterator.h" // for nsIContentIterator
|
|
|
|
#include "nsID.h" // for NS_GET_IID
|
|
|
|
#include "nsIDOMDocument.h" // for nsIDOMDocument
|
|
|
|
#include "nsIDOMElement.h" // for nsIDOMElement
|
|
|
|
#include "nsIDOMHTMLDocument.h" // for nsIDOMHTMLDocument
|
|
|
|
#include "nsIDOMHTMLElement.h" // for nsIDOMHTMLElement
|
|
|
|
#include "nsIDOMNode.h" // for nsIDOMNode, etc
|
|
|
|
#include "nsIDOMRange.h" // for nsIDOMRange, etc
|
|
|
|
#include "nsIEditor.h" // for nsIEditor, etc
|
|
|
|
#include "nsINode.h" // for nsINode
|
|
|
|
#include "nsIPlaintextEditor.h" // for nsIPlaintextEditor
|
|
|
|
#include "nsISelection.h" // for nsISelection
|
|
|
|
#include "nsISelectionController.h" // for nsISelectionController, etc
|
|
|
|
#include "nsISupportsBase.h" // for nsISupports
|
|
|
|
#include "nsISupportsUtils.h" // for NS_IF_ADDREF, NS_ADDREF, etc
|
|
|
|
#include "nsITextServicesFilter.h" // for nsITextServicesFilter
|
|
|
|
#include "nsIWordBreaker.h" // for nsWordRange, nsIWordBreaker
|
|
|
|
#include "nsRange.h" // for nsRange
|
|
|
|
#include "nsStaticAtom.h" // for NS_STATIC_ATOM, etc
|
|
|
|
#include "nsString.h" // for nsString, nsAutoString
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsTextServicesDocument.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nscore.h" // for nsresult, NS_IMETHODIMP, etc
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#define LOCK_DOC(doc)
|
|
|
|
#define UNLOCK_DOC(doc)
|
|
|
|
|
2011-10-10 22:50:08 -07:00
|
|
|
using namespace mozilla;
|
2014-11-02 04:04:13 -08:00
|
|
|
using namespace mozilla::dom;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
class OffsetEntry
|
|
|
|
{
|
|
|
|
public:
|
2012-08-22 08:56:38 -07:00
|
|
|
OffsetEntry(nsIDOMNode *aNode, int32_t aOffset, int32_t aLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
: mNode(aNode), mNodeOffset(0), mStrOffset(aOffset), mLength(aLength),
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsInsertedText(false), mIsValid(true)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mStrOffset < 1)
|
|
|
|
mStrOffset = 0;
|
|
|
|
|
|
|
|
if (mLength < 1)
|
|
|
|
mLength = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~OffsetEntry()
|
|
|
|
{
|
|
|
|
mNode = 0;
|
|
|
|
mNodeOffset = 0;
|
|
|
|
mStrOffset = 0;
|
|
|
|
mLength = 0;
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsValid = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIDOMNode *mNode;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mNodeOffset;
|
|
|
|
int32_t mStrOffset;
|
|
|
|
int32_t mLength;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mIsInsertedText;
|
|
|
|
bool mIsValid;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#define TS_ATOM(name_, value_) nsIAtom* nsTextServicesDocument::name_ = 0;
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsTSAtomList.h" // IWYU pragma: keep
|
2007-03-22 10:30:00 -07:00
|
|
|
#undef TS_ATOM
|
|
|
|
|
|
|
|
nsTextServicesDocument::nsTextServicesDocument()
|
|
|
|
{
|
|
|
|
mSelStartIndex = -1;
|
|
|
|
mSelStartOffset = -1;
|
|
|
|
mSelEndIndex = -1;
|
|
|
|
mSelEndOffset = -1;
|
|
|
|
|
|
|
|
mIteratorStatus = eIsDone;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTextServicesDocument::~nsTextServicesDocument()
|
|
|
|
{
|
|
|
|
ClearOffsetTable(&mOffsetTable);
|
|
|
|
}
|
|
|
|
|
2010-03-08 07:44:59 -08:00
|
|
|
#define TS_ATOM(name_, value_) NS_STATIC_ATOM_BUFFER(name_##_buffer, value_)
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsTSAtomList.h" // IWYU pragma: keep
|
2010-03-08 07:44:59 -08:00
|
|
|
#undef TS_ATOM
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* static */
|
|
|
|
void
|
|
|
|
nsTextServicesDocument::RegisterAtoms()
|
|
|
|
{
|
|
|
|
static const nsStaticAtom ts_atoms[] = {
|
2010-03-08 07:44:59 -08:00
|
|
|
#define TS_ATOM(name_, value_) NS_STATIC_ATOM(name_##_buffer, &name_),
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsTSAtomList.h" // IWYU pragma: keep
|
2007-03-22 10:30:00 -07:00
|
|
|
#undef TS_ATOM
|
|
|
|
};
|
|
|
|
|
2012-03-08 18:22:57 -08:00
|
|
|
NS_RegisterStaticAtoms(ts_atoms);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-06-02 11:33:47 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsTextServicesDocument)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsTextServicesDocument)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsTextServicesDocument)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsITextServicesDocument)
|
2010-06-07 21:20:23 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIEditActionListener)
|
2010-06-02 11:33:47 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsITextServicesDocument)
|
|
|
|
NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(nsTextServicesDocument)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2014-04-25 09:49:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION(nsTextServicesDocument,
|
|
|
|
mDOMDocument,
|
|
|
|
mSelCon,
|
|
|
|
mIterator,
|
|
|
|
mPrevTextBlock,
|
|
|
|
mNextTextBlock,
|
|
|
|
mExtent,
|
|
|
|
mTxtSvcFilter)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextServicesDocument::InitWithEditor(nsIEditor *aEditor)
|
|
|
|
{
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
nsCOMPtr<nsISelectionController> selCon;
|
|
|
|
nsCOMPtr<nsIDOMDocument> doc;
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aEditor, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
LOCK_DOC(this);
|
|
|
|
|
|
|
|
// Check to see if we already have an mSelCon. If we do, it
|
|
|
|
// better be the same one the editor uses!
|
|
|
|
|
|
|
|
result = aEditor->GetSelectionController(getter_AddRefs(selCon));
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!selCon || (mSelCon && selCon != mSelCon))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mSelCon)
|
|
|
|
mSelCon = selCon;
|
|
|
|
|
|
|
|
// Check to see if we already have an mDOMDocument. If we do, it
|
|
|
|
// better be the same one the editor uses!
|
|
|
|
|
|
|
|
result = aEditor->GetDocument(getter_AddRefs(doc));
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!doc || (mDOMDocument && doc != mDOMDocument))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mDOMDocument)
|
|
|
|
{
|
|
|
|
mDOMDocument = doc;
|
|
|
|
|
|
|
|
result = CreateDocumentContentIterator(getter_AddRefs(mIterator));
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIteratorStatus = nsTextServicesDocument::eIsDone;
|
|
|
|
|
|
|
|
result = FirstBlock();
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mEditor = do_GetWeakReference(aEditor);
|
|
|
|
|
2010-06-07 21:20:23 -07:00
|
|
|
result = aEditor->AddEditActionListener(this);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextServicesDocument::GetDocument(nsIDOMDocument **aDoc)
|
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aDoc, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
*aDoc = nullptr; // init out param
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(mDOMDocument, NS_ERROR_NOT_INITIALIZED);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
*aDoc = mDOMDocument;
|
|
|
|
NS_ADDREF(*aDoc);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextServicesDocument::SetExtent(nsIDOMRange* aDOMRange)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aDOMRange);
|
|
|
|
NS_ENSURE_TRUE(mDOMDocument, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
LOCK_DOC(this);
|
|
|
|
|
|
|
|
// We need to store a copy of aDOMRange since we don't
|
|
|
|
// know where it came from.
|
|
|
|
|
2014-11-02 04:04:13 -08:00
|
|
|
mExtent = static_cast<nsRange*>(aDOMRange)->CloneRange();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Create a new iterator based on our new extent range.
|
|
|
|
|
2014-11-02 04:04:13 -08:00
|
|
|
nsresult result = CreateContentIterator(mExtent, getter_AddRefs(mIterator));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now position the iterator at the start of the first block
|
|
|
|
// in the range.
|
|
|
|
|
|
|
|
mIteratorStatus = nsTextServicesDocument::eIsDone;
|
|
|
|
|
|
|
|
result = FirstBlock();
|
|
|
|
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextServicesDocument::ExpandRangeToWordBoundaries(nsIDOMRange *aRange)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aRange);
|
2014-11-02 04:04:13 -08:00
|
|
|
nsRefPtr<nsRange> range = static_cast<nsRange*>(aRange);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Get the end points of the range.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> rngStartNode, rngEndNode;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t rngStartOffset, rngEndOffset;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-11-02 04:04:13 -08:00
|
|
|
nsresult result = GetRangeEndPoints(range, getter_AddRefs(rngStartNode),
|
2007-03-22 10:30:00 -07:00
|
|
|
&rngStartOffset,
|
|
|
|
getter_AddRefs(rngEndNode),
|
|
|
|
&rngEndOffset);
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
|
|
|
|
|
|
|
// Create a content iterator based on the range.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContentIterator> iter;
|
2014-11-02 04:04:13 -08:00
|
|
|
result = CreateContentIterator(range, getter_AddRefs(iter));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
|
|
|
|
|
|
|
// Find the first text node in the range.
|
|
|
|
|
|
|
|
TSDIteratorStatus iterStatus;
|
|
|
|
|
|
|
|
result = FirstTextNode(iter, &iterStatus);
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
|
|
|
|
|
|
|
if (iterStatus == nsTextServicesDocument::eIsDone)
|
2009-04-08 05:59:06 -07:00
|
|
|
{
|
|
|
|
// No text was found so there's no adjustment necessary!
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
nsINode *firstText = iter->GetCurrentNode();
|
|
|
|
NS_ENSURE_TRUE(firstText, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
// Find the last text node in the range.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
result = LastTextNode(iter, &iterStatus);
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
if (iterStatus == nsTextServicesDocument::eIsDone)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-08 05:59:06 -07:00
|
|
|
// We should never get here because a first text block
|
|
|
|
// was found above.
|
2011-10-17 07:59:28 -07:00
|
|
|
NS_ASSERTION(false, "Found a first without a last!");
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
nsINode *lastText = iter->GetCurrentNode();
|
|
|
|
NS_ENSURE_TRUE(lastText, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
// Now make sure our end points are in terms of text nodes in the range!
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
nsCOMPtr<nsIDOMNode> firstTextNode = do_QueryInterface(firstText);
|
|
|
|
NS_ENSURE_TRUE(firstTextNode, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
if (rngStartNode != firstTextNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-08 05:59:06 -07:00
|
|
|
// The range includes the start of the first text node!
|
|
|
|
rngStartNode = firstTextNode;
|
|
|
|
rngStartOffset = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
nsCOMPtr<nsIDOMNode> lastTextNode = do_QueryInterface(lastText);
|
|
|
|
NS_ENSURE_TRUE(lastTextNode, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
if (rngEndNode != lastTextNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-08 05:59:06 -07:00
|
|
|
// The range includes the end of the last text node!
|
|
|
|
rngEndNode = lastTextNode;
|
|
|
|
nsAutoString str;
|
|
|
|
result = lastTextNode->GetNodeValue(str);
|
|
|
|
rngEndOffset = str.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
// Create a doc iterator so that we can scan beyond
|
|
|
|
// the bounds of the extent range.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContentIterator> docIter;
|
|
|
|
result = CreateDocumentContentIterator(getter_AddRefs(docIter));
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
|
|
|
|
|
|
|
// Grab all the text in the block containing our
|
|
|
|
// first text node.
|
|
|
|
|
|
|
|
result = docIter->PositionAt(firstText);
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
|
|
|
|
|
|
|
iterStatus = nsTextServicesDocument::eValid;
|
|
|
|
|
2009-04-22 01:43:15 -07:00
|
|
|
nsTArray<OffsetEntry*> offsetTable;
|
2009-04-08 05:59:06 -07:00
|
|
|
nsAutoString blockStr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
result = CreateOffsetTable(&offsetTable, docIter, &iterStatus,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, &blockStr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
2009-04-08 05:59:06 -07:00
|
|
|
ClearOffsetTable(&offsetTable);
|
2007-03-22 10:30:00 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
nsCOMPtr<nsIDOMNode> wordStartNode, wordEndNode;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t wordStartOffset, wordEndOffset;
|
2009-04-08 05:59:06 -07:00
|
|
|
|
|
|
|
result = FindWordBounds(&offsetTable, &blockStr,
|
|
|
|
rngStartNode, rngStartOffset,
|
|
|
|
getter_AddRefs(wordStartNode), &wordStartOffset,
|
|
|
|
getter_AddRefs(wordEndNode), &wordEndOffset);
|
|
|
|
|
|
|
|
ClearOffsetTable(&offsetTable);
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
|
|
|
|
|
|
|
rngStartNode = wordStartNode;
|
|
|
|
rngStartOffset = wordStartOffset;
|
|
|
|
|
|
|
|
// Grab all the text in the block containing our
|
|
|
|
// last text node.
|
|
|
|
|
|
|
|
result = docIter->PositionAt(lastText);
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
iterStatus = nsTextServicesDocument::eValid;
|
|
|
|
|
|
|
|
result = CreateOffsetTable(&offsetTable, docIter, &iterStatus,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, &blockStr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
2009-04-08 05:59:06 -07:00
|
|
|
ClearOffsetTable(&offsetTable);
|
2007-03-22 10:30:00 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
result = FindWordBounds(&offsetTable, &blockStr,
|
|
|
|
rngEndNode, rngEndOffset,
|
|
|
|
getter_AddRefs(wordStartNode), &wordStartOffset,
|
|
|
|
getter_AddRefs(wordEndNode), &wordEndOffset);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
ClearOffsetTable(&offsetTable);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
// To prevent expanding the range too much, we only change
|
|
|
|
// rngEndNode and rngEndOffset if it isn't already at the start of the
|
|
|
|
// word and isn't equivalent to rngStartNode and rngStartOffset.
|
|
|
|
|
|
|
|
if (rngEndNode != wordStartNode || rngEndOffset != wordStartOffset ||
|
|
|
|
(rngEndNode == rngStartNode && rngEndOffset == rngStartOffset))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-08 05:59:06 -07:00
|
|
|
rngEndNode = wordEndNode;
|
|
|
|
rngEndOffset = wordEndOffset;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
// Now adjust the range so that it uses our new
|
|
|
|
// end points.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-11-02 04:04:13 -08:00
|
|
|
result = range->SetEnd(rngEndNode, rngEndOffset);
|
2009-04-08 05:59:06 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-11-02 04:04:13 -08:00
|
|
|
return range->SetStart(rngStartNode, rngStartOffset);
|
2009-04-08 05:59:06 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextServicesDocument::SetFilter(nsITextServicesFilter *aFilter)
|
|
|
|
{
|
|
|
|
// Hang on to the filter so we can set it into the filtered iterator.
|
|
|
|
mTxtSvcFilter = aFilter;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextServicesDocument::GetCurrentTextBlock(nsString *aStr)
|
|
|
|
{
|
|
|
|
nsresult result;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aStr, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
aStr->Truncate();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(mIterator, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
LOCK_DOC(this);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
result = CreateOffsetTable(&mOffsetTable, mIterator, &mIteratorStatus,
|
|
|
|
mExtent, aStr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
UNLOCK_DOC(this);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextServicesDocument::FirstBlock()
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mIterator, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
LOCK_DOC(this);
|
|
|
|
|
|
|
|
nsresult result = FirstTextNode(mIterator, &mIteratorStatus);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
// Keep track of prev and next blocks, just in case
|
|
|
|
// the text service blows away the current block.
|
|
|
|
|
|
|
|
if (mIteratorStatus == nsTextServicesDocument::eValid)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
mPrevTextBlock = nullptr;
|
2009-04-08 05:59:06 -07:00
|
|
|
result = GetFirstTextNodeInNextBlock(getter_AddRefs(mNextTextBlock));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-04-08 05:59:06 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// There's no text block in the document!
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mPrevTextBlock = nullptr;
|
|
|
|
mNextTextBlock = nullptr;
|
2009-04-08 05:59:06 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
|
2009-04-08 05:59:06 -07:00
|
|
|
return result;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextServicesDocument::LastSelectedBlock(TSDBlockSelectionStatus *aSelStatus,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t *aSelOffset,
|
|
|
|
int32_t *aSelLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aSelStatus && aSelOffset && aSelLength, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
LOCK_DOC(this);
|
|
|
|
|
|
|
|
mIteratorStatus = nsTextServicesDocument::eIsDone;
|
|
|
|
|
|
|
|
*aSelStatus = nsITextServicesDocument::eBlockNotFound;
|
|
|
|
*aSelOffset = *aSelLength = -1;
|
|
|
|
|
|
|
|
if (!mSelCon || !mIterator)
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-11-02 04:04:13 -08:00
|
|
|
nsCOMPtr<nsISelection> domSelection;
|
|
|
|
result = mSelCon->GetSelection(nsISelectionController::SELECTION_NORMAL,
|
|
|
|
getter_AddRefs(domSelection));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-11-02 04:04:13 -08:00
|
|
|
nsRefPtr<Selection> selection = static_cast<Selection*>(domSelection.get());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-11-02 04:04:13 -08:00
|
|
|
bool isCollapsed = selection->IsCollapsed();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContentIterator> iter;
|
2014-11-02 04:04:13 -08:00
|
|
|
nsRefPtr<nsRange> range;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t i, rangeCount, offset;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (isCollapsed)
|
|
|
|
{
|
|
|
|
// We have a caret. Check if the caret is in a text node.
|
|
|
|
// If it is, make the text node's block the current block.
|
|
|
|
// If the caret isn't in a text node, search forwards in
|
|
|
|
// the document, till we find a text node.
|
|
|
|
|
2014-11-02 04:04:13 -08:00
|
|
|
range = selection->GetRangeAt(0);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!range)
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = range->GetStartContainer(getter_AddRefs(parent));
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!parent)
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = range->GetStartOffset(&offset);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsTextNode(parent))
|
|
|
|
{
|
|
|
|
// The caret is in a text node. Find the beginning
|
|
|
|
// of the text block containing this text node and
|
|
|
|
// return.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> content(do_QueryInterface(parent));
|
|
|
|
|
|
|
|
if (!content)
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = mIterator->PositionAt(content);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = FirstTextNodeInCurrentBlock(mIterator);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIteratorStatus = nsTextServicesDocument::eValid;
|
|
|
|
|
|
|
|
result = CreateOffsetTable(&mOffsetTable, mIterator, &mIteratorStatus,
|
2012-07-30 07:20:58 -07:00
|
|
|
mExtent, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = GetSelection(aSelStatus, aSelOffset, aSelLength);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*aSelStatus == nsITextServicesDocument::eBlockContains)
|
2011-10-17 07:59:28 -07:00
|
|
|
result = SetSelectionInternal(*aSelOffset, *aSelLength, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The caret isn't in a text node. Create an iterator
|
|
|
|
// based on a range that extends from the current caret
|
|
|
|
// position to the end of the document, then walk forwards
|
|
|
|
// till you find a text node, then find the beginning of it's block.
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
result = CreateDocumentContentRootToNodeOffsetRange(parent, offset, false, getter_AddRefs(range));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = range->GetCollapsed(&isCollapsed);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isCollapsed)
|
|
|
|
{
|
|
|
|
// If we get here, the range is collapsed because there is nothing after
|
|
|
|
// the caret! Just return NS_OK;
|
|
|
|
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = CreateContentIterator(range, getter_AddRefs(iter));
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
iter->First();
|
|
|
|
|
2008-10-15 02:40:28 -07:00
|
|
|
nsCOMPtr<nsIContent> content;
|
2007-03-22 10:30:00 -07:00
|
|
|
while (!iter->IsDone())
|
|
|
|
{
|
2008-10-15 02:40:28 -07:00
|
|
|
content = do_QueryInterface(iter->GetCurrentNode());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (IsTextNode(content))
|
|
|
|
break;
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
content = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
iter->Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!content)
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = mIterator->PositionAt(content);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = FirstTextNodeInCurrentBlock(mIterator);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIteratorStatus = nsTextServicesDocument::eValid;
|
|
|
|
|
|
|
|
result = CreateOffsetTable(&mOffsetTable, mIterator, &mIteratorStatus,
|
2012-07-30 07:20:58 -07:00
|
|
|
mExtent, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = GetSelection(aSelStatus, aSelOffset, aSelLength);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we get here, we have an uncollapsed selection!
|
|
|
|
// Look backwards through each range in the selection till you
|
|
|
|
// find the first text node. If you find one, find the
|
2012-02-15 01:18:24 -08:00
|
|
|
// beginning of its text block, and make it the current
|
2007-03-22 10:30:00 -07:00
|
|
|
// block.
|
|
|
|
|
|
|
|
result = selection->GetRangeCount(&rangeCount);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(rangeCount > 0, "Unexpected range count!");
|
|
|
|
|
|
|
|
if (rangeCount <= 0)
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: We may need to add some code here to make sure
|
|
|
|
// the ranges are sorted in document appearance order!
|
|
|
|
|
|
|
|
for (i = rangeCount - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
// Get the i'th range from the selection.
|
|
|
|
|
2014-11-02 04:04:13 -08:00
|
|
|
range = selection->GetRangeAt(i);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-11-02 04:04:13 -08:00
|
|
|
if (!range) {
|
2007-03-22 10:30:00 -07:00
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an iterator for the range.
|
|
|
|
|
|
|
|
result = CreateContentIterator(range, getter_AddRefs(iter));
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
iter->Last();
|
|
|
|
|
|
|
|
// Now walk through the range till we find a text node.
|
|
|
|
|
|
|
|
while (!iter->IsDone())
|
|
|
|
{
|
2012-06-10 05:39:21 -07:00
|
|
|
if (iter->GetCurrentNode()->NodeType() == nsIDOMNode::TEXT_NODE) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// We found a text node, so position the document's
|
|
|
|
// iterator at the beginning of the block, then get
|
|
|
|
// the selection in terms of the string offset.
|
2012-06-10 05:39:21 -07:00
|
|
|
nsCOMPtr<nsIContent> content = iter->GetCurrentNode()->AsContent();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = mIterator->PositionAt(content);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = FirstTextNodeInCurrentBlock(mIterator);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIteratorStatus = nsTextServicesDocument::eValid;
|
|
|
|
|
|
|
|
result = CreateOffsetTable(&mOffsetTable, mIterator, &mIteratorStatus,
|
2012-07-30 07:20:58 -07:00
|
|
|
mExtent, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = GetSelection(aSelStatus, aSelOffset, aSelLength);
|
|
|
|
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
iter->Prev();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we get here, we didn't find any text node in the selection!
|
|
|
|
// Create a range that extends from the end of the selection,
|
|
|
|
// to the end of the document, then iterate forwards through
|
|
|
|
// it till you find a text node!
|
|
|
|
|
2014-11-02 04:04:13 -08:00
|
|
|
range = selection->GetRangeAt(rangeCount - 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!range)
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = range->GetEndContainer(getter_AddRefs(parent));
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!parent)
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = range->GetEndOffset(&offset);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
result = CreateDocumentContentRootToNodeOffsetRange(parent, offset, false, getter_AddRefs(range));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = range->GetCollapsed(&isCollapsed);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isCollapsed)
|
|
|
|
{
|
|
|
|
// If we get here, the range is collapsed because there is nothing after
|
|
|
|
// the current selection! Just return NS_OK;
|
|
|
|
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = CreateContentIterator(range, getter_AddRefs(iter));
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
iter->First();
|
|
|
|
|
|
|
|
while (!iter->IsDone())
|
|
|
|
{
|
2012-06-10 05:39:21 -07:00
|
|
|
if (iter->GetCurrentNode()->NodeType() == nsIDOMNode::TEXT_NODE) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// We found a text node! Adjust the document's iterator to point
|
2012-02-15 01:18:24 -08:00
|
|
|
// to the beginning of its text block, then get the current selection.
|
2012-06-10 05:39:21 -07:00
|
|
|
nsCOMPtr<nsIContent> content = iter->GetCurrentNode()->AsContent();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = mIterator->PositionAt(content);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = FirstTextNodeInCurrentBlock(mIterator);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mIteratorStatus = nsTextServicesDocument::eValid;
|
|
|
|
|
|
|
|
result = CreateOffsetTable(&mOffsetTable, mIterator, &mIteratorStatus,
|
2012-07-30 07:20:58 -07:00
|
|
|
mExtent, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = GetSelection(aSelStatus, aSelOffset, aSelLength);
|
|
|
|
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
iter->Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we get here, we didn't find any block before or inside
|
|
|
|
// the selection! Just return OK.
|
|
|
|
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextServicesDocument::PrevBlock()
|
|
|
|
{
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(mIterator, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
LOCK_DOC(this);
|
|
|
|
|
|
|
|
if (mIteratorStatus == nsTextServicesDocument::eIsDone)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
switch (mIteratorStatus)
|
|
|
|
{
|
|
|
|
case nsTextServicesDocument::eValid:
|
|
|
|
case nsTextServicesDocument::eNext:
|
|
|
|
|
|
|
|
result = FirstTextNodeInPrevBlock(mIterator);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
mIteratorStatus = nsTextServicesDocument::eIsDone;
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mIterator->IsDone())
|
|
|
|
{
|
|
|
|
mIteratorStatus = nsTextServicesDocument::eIsDone;
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIteratorStatus = nsTextServicesDocument::eValid;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsTextServicesDocument::ePrev:
|
|
|
|
|
|
|
|
// The iterator already points to the previous
|
|
|
|
// block, so don't do anything.
|
|
|
|
|
|
|
|
mIteratorStatus = nsTextServicesDocument::eValid;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
mIteratorStatus = nsTextServicesDocument::eIsDone;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep track of prev and next blocks, just in case
|
|
|
|
// the text service blows away the current block.
|
|
|
|
|
|
|
|
if (mIteratorStatus == nsTextServicesDocument::eValid)
|
|
|
|
{
|
|
|
|
result = GetFirstTextNodeInPrevBlock(getter_AddRefs(mPrevTextBlock));
|
|
|
|
result = GetFirstTextNodeInNextBlock(getter_AddRefs(mNextTextBlock));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We must be done!
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mPrevTextBlock = nullptr;
|
|
|
|
mNextTextBlock = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextServicesDocument::NextBlock()
|
|
|
|
{
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(mIterator, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
LOCK_DOC(this);
|
|
|
|
|
|
|
|
if (mIteratorStatus == nsTextServicesDocument::eIsDone)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
switch (mIteratorStatus)
|
|
|
|
{
|
|
|
|
case nsTextServicesDocument::eValid:
|
|
|
|
|
|
|
|
// Advance the iterator to the next text block.
|
|
|
|
|
|
|
|
result = FirstTextNodeInNextBlock(mIterator);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
mIteratorStatus = nsTextServicesDocument::eIsDone;
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mIterator->IsDone())
|
|
|
|
{
|
|
|
|
mIteratorStatus = nsTextServicesDocument::eIsDone;
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIteratorStatus = nsTextServicesDocument::eValid;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsTextServicesDocument::eNext:
|
|
|
|
|
|
|
|
// The iterator already points to the next block,
|
|
|
|
// so don't do anything to it!
|
|
|
|
|
|
|
|
mIteratorStatus = nsTextServicesDocument::eValid;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsTextServicesDocument::ePrev:
|
|
|
|
|
|
|
|
// If the iterator is pointing to the previous block,
|
|
|
|
// we know that there is no next text block! Just
|
|
|
|
// fall through to the default case!
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
mIteratorStatus = nsTextServicesDocument::eIsDone;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep track of prev and next blocks, just in case
|
|
|
|
// the text service blows away the current block.
|
|
|
|
|
|
|
|
if (mIteratorStatus == nsTextServicesDocument::eValid)
|
|
|
|
{
|
|
|
|
result = GetFirstTextNodeInPrevBlock(getter_AddRefs(mPrevTextBlock));
|
|
|
|
result = GetFirstTextNodeInNextBlock(getter_AddRefs(mNextTextBlock));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We must be done.
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mPrevTextBlock = nullptr;
|
|
|
|
mNextTextBlock = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsTextServicesDocument::IsDone(bool *aIsDone)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aIsDone, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
*aIsDone = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(mIterator, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
LOCK_DOC(this);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
*aIsDone = (mIteratorStatus == nsTextServicesDocument::eIsDone) ? true : false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTextServicesDocument::SetSelection(int32_t aOffset, int32_t aLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult result;
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(mSelCon && aOffset >= 0 && aLength >= 0, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
LOCK_DOC(this);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
result = SetSelectionInternal(aOffset, aLength, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
|
|
|
|
//**** KDEBUG ****
|
|
|
|
// printf("\n * Sel: (%2d, %4d) (%2d, %4d)\n", mSelStartIndex, mSelStartOffset, mSelEndIndex, mSelEndOffset);
|
|
|
|
//**** KDEBUG ****
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextServicesDocument::ScrollSelectionIntoView()
|
|
|
|
{
|
|
|
|
nsresult result;
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(mSelCon, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
LOCK_DOC(this);
|
|
|
|
|
2008-02-28 07:28:37 -08:00
|
|
|
// After ScrollSelectionIntoView(), the pending notifications might be flushed
|
|
|
|
// and PresShell/PresContext/Frames may be dead. See bug 418470.
|
2010-10-21 13:56:20 -07:00
|
|
|
result = mSelCon->ScrollSelectionIntoView(nsISelectionController::SELECTION_NORMAL, nsISelectionController::SELECTION_FOCUS_REGION,
|
|
|
|
nsISelectionController::SCROLL_SYNCHRONOUS);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextServicesDocument::DeleteSelection()
|
|
|
|
{
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
// We don't allow deletion during a collapsed selection!
|
|
|
|
nsCOMPtr<nsIEditor> editor (do_QueryReferent(mEditor));
|
|
|
|
NS_ASSERTION(editor, "DeleteSelection called without an editor present!");
|
|
|
|
NS_ASSERTION(SelectionIsValid(), "DeleteSelection called without a valid selection!");
|
|
|
|
|
|
|
|
if (!editor || !SelectionIsValid())
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
if (SelectionIsCollapsed())
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
LOCK_DOC(this);
|
|
|
|
|
|
|
|
//**** KDEBUG ****
|
|
|
|
// printf("\n---- Before Delete\n");
|
|
|
|
// printf("Sel: (%2d, %4d) (%2d, %4d)\n", mSelStartIndex, mSelStartOffset, mSelEndIndex, mSelEndOffset);
|
|
|
|
// PrintOffsetTable();
|
|
|
|
//**** KDEBUG ****
|
|
|
|
|
2012-02-15 01:18:24 -08:00
|
|
|
// If we have an mExtent, save off its current set of
|
2007-03-22 10:30:00 -07:00
|
|
|
// end points so we can compare them against mExtent's
|
|
|
|
// set after the deletion of the content.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> origStartNode, origEndNode;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t origStartOffset = 0, origEndOffset = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (mExtent)
|
|
|
|
{
|
|
|
|
result = GetRangeEndPoints(mExtent,
|
|
|
|
getter_AddRefs(origStartNode), &origStartOffset,
|
|
|
|
getter_AddRefs(origEndNode), &origEndOffset);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t i, selLength;
|
2007-03-22 10:30:00 -07:00
|
|
|
OffsetEntry *entry, *newEntry;
|
|
|
|
|
|
|
|
for (i = mSelStartIndex; i <= mSelEndIndex; i++)
|
|
|
|
{
|
2009-04-22 01:43:15 -07:00
|
|
|
entry = mOffsetTable[i];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (i == mSelStartIndex)
|
|
|
|
{
|
|
|
|
// Calculate the length of the selection. Note that the
|
|
|
|
// selection length can be zero if the start of the selection
|
|
|
|
// is at the very end of a text node entry.
|
|
|
|
|
|
|
|
if (entry->mIsInsertedText)
|
|
|
|
{
|
|
|
|
// Inserted text offset entries have no width when
|
|
|
|
// talking in terms of string offsets! If the beginning
|
|
|
|
// of the selection is in an inserted text offset entry,
|
|
|
|
// the caret is always at the end of the entry!
|
|
|
|
|
|
|
|
selLength = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
selLength = entry->mLength - (mSelStartOffset - entry->mStrOffset);
|
|
|
|
|
|
|
|
if (selLength > 0 && mSelStartOffset > entry->mStrOffset)
|
|
|
|
{
|
|
|
|
// Selection doesn't start at the beginning of the
|
|
|
|
// text node entry. We need to split this entry into
|
|
|
|
// two pieces, the piece before the selection, and
|
|
|
|
// the piece inside the selection.
|
|
|
|
|
|
|
|
result = SplitOffsetEntry(i, selLength);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust selection indexes to account for new entry:
|
|
|
|
|
|
|
|
++mSelStartIndex;
|
|
|
|
++mSelEndIndex;
|
|
|
|
++i;
|
|
|
|
|
2009-04-22 01:43:15 -07:00
|
|
|
entry = mOffsetTable[i];
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (selLength > 0 && mSelStartIndex < mSelEndIndex)
|
|
|
|
{
|
|
|
|
// The entire entry is contained in the selection. Mark the
|
|
|
|
// entry invalid.
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
entry->mIsValid = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//**** KDEBUG ****
|
|
|
|
// printf("\n---- Middle Delete\n");
|
|
|
|
// printf("Sel: (%2d, %4d) (%2d, %4d)\n", mSelStartIndex, mSelStartOffset, mSelEndIndex, mSelEndOffset);
|
|
|
|
// PrintOffsetTable();
|
|
|
|
//**** KDEBUG ****
|
|
|
|
|
|
|
|
if (i == mSelEndIndex)
|
|
|
|
{
|
|
|
|
if (entry->mIsInsertedText)
|
|
|
|
{
|
|
|
|
// Inserted text offset entries have no width when
|
|
|
|
// talking in terms of string offsets! If the end
|
|
|
|
// of the selection is in an inserted text offset entry,
|
|
|
|
// the selection includes the entire entry!
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
entry->mIsValid = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Calculate the length of the selection. Note that the
|
|
|
|
// selection length can be zero if the end of the selection
|
|
|
|
// is at the very beginning of a text node entry.
|
|
|
|
|
|
|
|
selLength = mSelEndOffset - entry->mStrOffset;
|
|
|
|
|
|
|
|
if (selLength > 0 && mSelEndOffset < entry->mStrOffset + entry->mLength)
|
|
|
|
{
|
|
|
|
// mStrOffset is guaranteed to be inside the selection, even
|
|
|
|
// when mSelStartIndex == mSelEndIndex.
|
|
|
|
|
|
|
|
result = SplitOffsetEntry(i, entry->mLength - selLength);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the entry fields:
|
|
|
|
|
2009-04-22 01:43:15 -07:00
|
|
|
newEntry = mOffsetTable[i+1];
|
2007-03-22 10:30:00 -07:00
|
|
|
newEntry->mNodeOffset = entry->mNodeOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (selLength > 0 && mSelEndOffset == entry->mStrOffset + entry->mLength)
|
|
|
|
{
|
|
|
|
// The entire entry is contained in the selection. Mark the
|
|
|
|
// entry invalid.
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
entry->mIsValid = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i != mSelStartIndex && i != mSelEndIndex)
|
|
|
|
{
|
|
|
|
// The entire entry is contained in the selection. Mark the
|
|
|
|
// entry invalid.
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
entry->mIsValid = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure mIterator always points to something valid!
|
|
|
|
|
|
|
|
AdjustContentIterator();
|
|
|
|
|
|
|
|
// Now delete the actual content!
|
|
|
|
|
2012-05-10 07:54:33 -07:00
|
|
|
result = editor->DeleteSelection(nsIEditor::ePrevious, nsIEditor::eStrip);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we've actually deleted the selected content,
|
|
|
|
// check to see if our mExtent has changed, if so, then
|
|
|
|
// we have to create a new content iterator!
|
|
|
|
|
|
|
|
if (origStartNode && origEndNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> curStartNode, curEndNode;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t curStartOffset = 0, curEndOffset = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = GetRangeEndPoints(mExtent,
|
|
|
|
getter_AddRefs(curStartNode), &curStartOffset,
|
|
|
|
getter_AddRefs(curEndNode), &curEndOffset);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (origStartNode != curStartNode || origEndNode != curEndNode)
|
|
|
|
{
|
|
|
|
// The range has changed, so we need to create a new content
|
|
|
|
// iterator based on the new range.
|
|
|
|
|
2008-10-15 02:40:28 -07:00
|
|
|
nsCOMPtr<nsIContent> curContent;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-10-25 05:36:23 -07:00
|
|
|
if (mIteratorStatus != nsTextServicesDocument::eIsDone) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// The old iterator is still pointing to something valid,
|
2012-02-15 01:18:24 -08:00
|
|
|
// so get its current node so we can restore it after we
|
2007-03-22 10:30:00 -07:00
|
|
|
// create the new iterator!
|
|
|
|
|
2012-10-25 05:36:23 -07:00
|
|
|
curContent = mIterator->GetCurrentNode()
|
|
|
|
? mIterator->GetCurrentNode()->AsContent()
|
|
|
|
: nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the new iterator.
|
|
|
|
|
|
|
|
result = CreateContentIterator(mExtent, getter_AddRefs(mIterator));
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now make the new iterator point to the content node
|
|
|
|
// the old one was pointing at.
|
|
|
|
|
|
|
|
if (curContent)
|
|
|
|
{
|
|
|
|
result = mIterator->PositionAt(curContent);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
mIteratorStatus = eIsDone;
|
|
|
|
else
|
|
|
|
mIteratorStatus = eValid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
entry = 0;
|
|
|
|
|
|
|
|
// Move the caret to the end of the first valid entry.
|
|
|
|
// Start with mSelStartIndex since it may still be valid.
|
|
|
|
|
|
|
|
for (i = mSelStartIndex; !entry && i >= 0; i--)
|
|
|
|
{
|
2009-04-22 01:43:15 -07:00
|
|
|
entry = mOffsetTable[i];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!entry->mIsValid)
|
|
|
|
entry = 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mSelStartIndex = mSelEndIndex = i;
|
|
|
|
mSelStartOffset = mSelEndOffset = entry->mStrOffset + entry->mLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we still don't have a valid entry, move the caret
|
|
|
|
// to the next valid entry after the selection:
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (i = mSelEndIndex; !entry && i < int32_t(mOffsetTable.Length()); i++)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-22 01:43:15 -07:00
|
|
|
entry = mOffsetTable[i];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!entry->mIsValid)
|
|
|
|
entry = 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mSelStartIndex = mSelEndIndex = i;
|
|
|
|
mSelStartOffset = mSelEndOffset = entry->mStrOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
result = SetSelection(mSelStartOffset, 0);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Uuughh we have no valid offset entry to place our
|
|
|
|
// caret ... just mark the selection invalid.
|
|
|
|
|
|
|
|
mSelStartIndex = mSelEndIndex = -1;
|
|
|
|
mSelStartOffset = mSelEndOffset = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now remove any invalid entries from the offset table.
|
|
|
|
|
|
|
|
result = RemoveInvalidOffsetEntries();
|
|
|
|
|
|
|
|
//**** KDEBUG ****
|
|
|
|
// printf("\n---- After Delete\n");
|
|
|
|
// printf("Sel: (%2d, %4d) (%2d, %4d)\n", mSelStartIndex, mSelStartOffset, mSelEndIndex, mSelEndOffset);
|
|
|
|
// PrintOffsetTable();
|
|
|
|
//**** KDEBUG ****
|
|
|
|
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextServicesDocument::InsertText(const nsString *aText)
|
|
|
|
{
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIEditor> editor (do_QueryReferent(mEditor));
|
|
|
|
NS_ASSERTION(editor, "InsertText called without an editor present!");
|
|
|
|
|
|
|
|
if (!editor || !SelectionIsValid())
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aText, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// If the selection is not collapsed, we need to save
|
|
|
|
// off the selection offsets so we can restore the
|
|
|
|
// selection and delete the selected content after we've
|
|
|
|
// inserted the new text. This is necessary to try and
|
|
|
|
// retain as much of the original style of the content
|
|
|
|
// being deleted.
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool collapsedSelection = SelectionIsCollapsed();
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t savedSelOffset = mSelStartOffset;
|
|
|
|
int32_t savedSelLength = mSelEndOffset - mSelStartOffset;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!collapsedSelection)
|
|
|
|
{
|
|
|
|
// Collapse to the start of the current selection
|
|
|
|
// for the insert!
|
|
|
|
|
|
|
|
result = SetSelection(mSelStartOffset, 0);
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LOCK_DOC(this);
|
|
|
|
|
|
|
|
result = editor->BeginTransaction();
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPlaintextEditor> textEditor (do_QueryInterface(editor, &result));
|
|
|
|
if (textEditor)
|
|
|
|
result = textEditor->InsertText(*aText);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
editor->EndTransaction();
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//**** KDEBUG ****
|
|
|
|
// printf("\n---- Before Insert\n");
|
|
|
|
// printf("Sel: (%2d, %4d) (%2d, %4d)\n", mSelStartIndex, mSelStartOffset, mSelEndIndex, mSelEndOffset);
|
|
|
|
// PrintOffsetTable();
|
|
|
|
//**** KDEBUG ****
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t strLength = aText->Length();
|
|
|
|
uint32_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
OffsetEntry *itEntry;
|
2009-04-22 01:43:15 -07:00
|
|
|
OffsetEntry *entry = mOffsetTable[mSelStartIndex];
|
2007-03-22 10:30:00 -07:00
|
|
|
void *node = entry->mNode;
|
|
|
|
|
|
|
|
NS_ASSERTION((entry->mIsValid), "Invalid insertion point!");
|
|
|
|
|
|
|
|
if (entry->mStrOffset == mSelStartOffset)
|
|
|
|
{
|
|
|
|
if (entry->mIsInsertedText)
|
|
|
|
{
|
|
|
|
// If the caret is in an inserted text offset entry,
|
|
|
|
// we simply insert the text at the end of the entry.
|
|
|
|
|
|
|
|
entry->mLength += strLength;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Insert an inserted text offset entry before the current
|
|
|
|
// entry!
|
|
|
|
|
|
|
|
itEntry = new OffsetEntry(entry->mNode, entry->mStrOffset, strLength);
|
|
|
|
|
|
|
|
if (!itEntry)
|
|
|
|
{
|
|
|
|
editor->EndTransaction();
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
itEntry->mIsInsertedText = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
itEntry->mNodeOffset = entry->mNodeOffset;
|
|
|
|
|
2009-04-22 01:43:15 -07:00
|
|
|
if (!mOffsetTable.InsertElementAt(mSelStartIndex, itEntry))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
editor->EndTransaction();
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((entry->mStrOffset + entry->mLength) == mSelStartOffset)
|
|
|
|
{
|
|
|
|
// We are inserting text at the end of the current offset entry.
|
|
|
|
// Look at the next valid entry in the table. If it's an inserted
|
2012-02-15 01:18:24 -08:00
|
|
|
// text entry, add to its length and adjust its node offset. If
|
2007-03-22 10:30:00 -07:00
|
|
|
// it isn't, add a new inserted text entry.
|
|
|
|
|
|
|
|
i = mSelStartIndex + 1;
|
|
|
|
itEntry = 0;
|
|
|
|
|
2009-04-22 01:43:15 -07:00
|
|
|
if (mOffsetTable.Length() > i)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-22 01:43:15 -07:00
|
|
|
itEntry = mOffsetTable[i];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!itEntry)
|
|
|
|
{
|
|
|
|
editor->EndTransaction();
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the entry is a match. If it isn't, set
|
|
|
|
// iEntry to zero.
|
|
|
|
|
|
|
|
if (!itEntry->mIsInsertedText || itEntry->mStrOffset != mSelStartOffset)
|
|
|
|
itEntry = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!itEntry)
|
|
|
|
{
|
|
|
|
// We didn't find an inserted text offset entry, so
|
|
|
|
// create one.
|
|
|
|
|
|
|
|
itEntry = new OffsetEntry(entry->mNode, mSelStartOffset, 0);
|
|
|
|
|
|
|
|
if (!itEntry)
|
|
|
|
{
|
|
|
|
editor->EndTransaction();
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
itEntry->mNodeOffset = entry->mNodeOffset + entry->mLength;
|
2011-10-17 07:59:28 -07:00
|
|
|
itEntry->mIsInsertedText = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-22 01:43:15 -07:00
|
|
|
if (!mOffsetTable.InsertElementAt(i, itEntry))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
delete itEntry;
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-15 01:18:24 -08:00
|
|
|
// We have a valid inserted text offset entry. Update its
|
2007-03-22 10:30:00 -07:00
|
|
|
// length, adjust the selection indexes, and make sure the
|
|
|
|
// caret is properly placed!
|
|
|
|
|
|
|
|
itEntry->mLength += strLength;
|
|
|
|
|
|
|
|
mSelStartIndex = mSelEndIndex = i;
|
|
|
|
|
|
|
|
result = mSelCon->GetSelection(nsISelectionController::SELECTION_NORMAL, getter_AddRefs(selection));
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
editor->EndTransaction();
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = selection->Collapse(itEntry->mNode, itEntry->mNodeOffset + itEntry->mLength);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
editor->EndTransaction();
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((entry->mStrOffset + entry->mLength) > mSelStartOffset)
|
|
|
|
{
|
|
|
|
// We are inserting text into the middle of the current offset entry.
|
|
|
|
// split the current entry into two parts, then insert an inserted text
|
|
|
|
// entry between them!
|
|
|
|
|
|
|
|
i = entry->mLength - (mSelStartOffset - entry->mStrOffset);
|
|
|
|
|
|
|
|
result = SplitOffsetEntry(mSelStartIndex, i);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
editor->EndTransaction();
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
itEntry = new OffsetEntry(entry->mNode, mSelStartOffset, strLength);
|
|
|
|
|
|
|
|
if (!itEntry)
|
|
|
|
{
|
|
|
|
editor->EndTransaction();
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
itEntry->mIsInsertedText = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
itEntry->mNodeOffset = entry->mNodeOffset + entry->mLength;
|
|
|
|
|
2009-04-22 01:43:15 -07:00
|
|
|
if (!mOffsetTable.InsertElementAt(mSelStartIndex + 1, itEntry))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
editor->EndTransaction();
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSelEndIndex = ++mSelStartIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've just finished inserting an inserted text offset entry.
|
|
|
|
// update all entries with the same mNode pointer that follow
|
|
|
|
// it in the table!
|
|
|
|
|
2009-04-22 01:43:15 -07:00
|
|
|
for (i = mSelStartIndex + 1; i < mOffsetTable.Length(); i++)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-22 01:43:15 -07:00
|
|
|
entry = mOffsetTable[i];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (entry->mNode == node)
|
|
|
|
{
|
|
|
|
if (entry->mIsValid)
|
|
|
|
entry->mNodeOffset += strLength;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//**** KDEBUG ****
|
|
|
|
// printf("\n---- After Insert\n");
|
|
|
|
// printf("Sel: (%2d, %4d) (%2d, %4d)\n", mSelStartIndex, mSelStartOffset, mSelEndIndex, mSelEndOffset);
|
|
|
|
// PrintOffsetTable();
|
|
|
|
//**** KDEBUG ****
|
|
|
|
|
|
|
|
if (!collapsedSelection)
|
|
|
|
{
|
|
|
|
result = SetSelection(savedSelOffset, savedSelLength);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
editor->EndTransaction();
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = DeleteSelection();
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
editor->EndTransaction();
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result = editor->EndTransaction();
|
|
|
|
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-06-07 21:20:23 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextServicesDocument::DidInsertNode(nsIDOMNode *aNode,
|
|
|
|
nsIDOMNode *aParent,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aPosition,
|
2010-06-07 21:20:23 -07:00
|
|
|
nsresult aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-07 21:20:23 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextServicesDocument::DidDeleteNode(nsIDOMNode *aChild, nsresult aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(aResult, NS_OK);
|
2010-06-07 21:20:23 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_TRUE(mIterator, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
//**** KDEBUG ****
|
|
|
|
// printf("** DeleteNode: 0x%.8x\n", aChild);
|
|
|
|
// fflush(stdout);
|
|
|
|
//**** KDEBUG ****
|
|
|
|
|
|
|
|
LOCK_DOC(this);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t nodeIndex = 0;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasEntry = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
OffsetEntry *entry;
|
|
|
|
|
|
|
|
nsresult result = NodeHasOffsetEntry(&mOffsetTable, aChild, &hasEntry, &nodeIndex);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasEntry)
|
|
|
|
{
|
|
|
|
// It's okay if the node isn't in the offset table, the
|
|
|
|
// editor could be cleaning house.
|
|
|
|
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> node = do_QueryInterface(mIterator->GetCurrentNode());
|
|
|
|
|
|
|
|
if (node && node == aChild &&
|
|
|
|
mIteratorStatus != nsTextServicesDocument::eIsDone)
|
|
|
|
{
|
|
|
|
// XXX: This should never really happen because
|
|
|
|
// AdjustContentIterator() should have been called prior
|
|
|
|
// to the delete to try and position the iterator on the
|
|
|
|
// next valid text node in the offset table, and if there
|
|
|
|
// wasn't a next, it would've set mIteratorStatus to eIsDone.
|
|
|
|
|
2009-08-14 07:09:00 -07:00
|
|
|
NS_ERROR("DeleteNode called for current iterator node.");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t tcount = mOffsetTable.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
while (nodeIndex < tcount)
|
|
|
|
{
|
2009-04-22 01:43:15 -07:00
|
|
|
entry = mOffsetTable[nodeIndex];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!entry)
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry->mNode == aChild)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
entry->mIsValid = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nodeIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-07 21:20:23 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextServicesDocument::DidSplitNode(nsIDOMNode *aExistingRightNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aOffset,
|
2010-06-07 21:20:23 -07:00
|
|
|
nsIDOMNode *aNewLeftNode,
|
|
|
|
nsresult aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
//**** KDEBUG ****
|
|
|
|
// printf("** SplitNode: 0x%.8x %d 0x%.8x\n", aExistingRightNode, aOffset, aNewLeftNode);
|
|
|
|
// fflush(stdout);
|
|
|
|
//**** KDEBUG ****
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-07 21:20:23 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextServicesDocument::DidJoinNodes(nsIDOMNode *aLeftNode,
|
|
|
|
nsIDOMNode *aRightNode,
|
|
|
|
nsIDOMNode *aParent,
|
|
|
|
nsresult aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(aResult, NS_OK);
|
2010-06-07 21:20:23 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t i;
|
|
|
|
uint16_t type;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult result;
|
|
|
|
|
|
|
|
//**** KDEBUG ****
|
|
|
|
// printf("** JoinNodes: 0x%.8x 0x%.8x 0x%.8x\n", aLeftNode, aRightNode, aParent);
|
|
|
|
// fflush(stdout);
|
|
|
|
//**** KDEBUG ****
|
|
|
|
|
2012-04-17 03:18:08 -07:00
|
|
|
// Make sure that both nodes are text nodes -- otherwise we don't care.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = aLeftNode->GetNodeType(&type);
|
2012-07-27 06:59:29 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, NS_OK);
|
2012-04-17 03:18:08 -07:00
|
|
|
if (nsIDOMNode::TEXT_NODE != type) {
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
result = aRightNode->GetNodeType(&type);
|
2012-07-27 06:59:29 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, NS_OK);
|
2012-04-17 03:18:08 -07:00
|
|
|
if (nsIDOMNode::TEXT_NODE != type) {
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note: The editor merges the contents of the left node into the
|
|
|
|
// contents of the right.
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t leftIndex = 0;
|
|
|
|
int32_t rightIndex = 0;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool leftHasEntry = false;
|
|
|
|
bool rightHasEntry = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = NodeHasOffsetEntry(&mOffsetTable, aLeftNode, &leftHasEntry, &leftIndex);
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!leftHasEntry)
|
|
|
|
{
|
2007-12-19 03:32:20 -08:00
|
|
|
// It's okay if the node isn't in the offset table, the
|
|
|
|
// editor could be cleaning house.
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
result = NodeHasOffsetEntry(&mOffsetTable, aRightNode, &rightHasEntry, &rightIndex);
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!rightHasEntry)
|
|
|
|
{
|
2007-12-19 03:32:20 -08:00
|
|
|
// It's okay if the node isn't in the offset table, the
|
|
|
|
// editor could be cleaning house.
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(leftIndex < rightIndex, "Indexes out of order.");
|
|
|
|
|
|
|
|
if (leftIndex > rightIndex)
|
|
|
|
{
|
|
|
|
// Don't know how to handle this situation.
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOCK_DOC(this);
|
|
|
|
|
2009-04-22 01:43:15 -07:00
|
|
|
OffsetEntry *entry = mOffsetTable[rightIndex];
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(entry->mNodeOffset == 0, "Unexpected offset value for rightIndex.");
|
|
|
|
|
|
|
|
// Run through the table and change all entries referring to
|
|
|
|
// the left node so that they now refer to the right node:
|
|
|
|
|
|
|
|
nsAutoString str;
|
|
|
|
result = aLeftNode->GetNodeValue(str);
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t nodeLength = str.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
for (i = leftIndex; i < rightIndex; i++)
|
|
|
|
{
|
2009-04-22 01:43:15 -07:00
|
|
|
entry = mOffsetTable[i];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (entry->mNode == aLeftNode)
|
|
|
|
{
|
|
|
|
if (entry->mIsValid)
|
|
|
|
entry->mNode = aRightNode;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run through the table and adjust the node offsets
|
|
|
|
// for all entries referring to the right node.
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (i = rightIndex; i < int32_t(mOffsetTable.Length()); i++)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-22 01:43:15 -07:00
|
|
|
entry = mOffsetTable[i];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (entry->mNode == aRightNode)
|
|
|
|
{
|
|
|
|
if (entry->mIsValid)
|
|
|
|
entry->mNodeOffset += nodeLength;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now check to see if the iterator is pointing to the
|
|
|
|
// left node. If it is, make it point to the right node!
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> leftContent = do_QueryInterface(aLeftNode);
|
|
|
|
nsCOMPtr<nsIContent> rightContent = do_QueryInterface(aRightNode);
|
|
|
|
|
|
|
|
if (!leftContent || !rightContent)
|
|
|
|
{
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mIterator->GetCurrentNode() == leftContent)
|
|
|
|
result = mIterator->PositionAt(rightContent);
|
|
|
|
|
|
|
|
UNLOCK_DOC(this);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-11-02 04:04:13 -08:00
|
|
|
nsTextServicesDocument::CreateContentIterator(nsRange* aRange,
|
|
|
|
nsIContentIterator** aIterator)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aRange && aIterator, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-08 13:46:24 -07:00
|
|
|
*aIterator = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Create a nsFilteredContentIterator
|
2014-07-08 13:46:24 -07:00
|
|
|
// This class wraps the ContentIterator in order to give itself a chance
|
2007-03-22 10:30:00 -07:00
|
|
|
// to filter out certain content nodes
|
2014-07-08 13:46:24 -07:00
|
|
|
nsRefPtr<nsFilteredContentIterator> filter = new nsFilteredContentIterator(mTxtSvcFilter);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-08 13:46:24 -07:00
|
|
|
nsresult result = filter->Init(aRange);
|
|
|
|
if (NS_FAILED(result)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-07-08 13:46:24 -07:00
|
|
|
filter.forget(aIterator);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsTextServicesDocument::GetDocumentContentRootNode(nsIDOMNode **aNode)
|
|
|
|
{
|
|
|
|
nsresult result;
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aNode, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
*aNode = 0;
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(mDOMDocument, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMHTMLDocument> htmlDoc = do_QueryInterface(mDOMDocument);
|
|
|
|
|
|
|
|
if (htmlDoc)
|
|
|
|
{
|
|
|
|
// For HTML documents, the content root node is the body.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMHTMLElement> bodyElement;
|
|
|
|
|
|
|
|
result = htmlDoc->GetBody(getter_AddRefs(bodyElement));
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(bodyElement, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = bodyElement->QueryInterface(NS_GET_IID(nsIDOMNode), (void **)aNode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// For non-HTML documents, the content root node will be the document element.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMElement> docElement;
|
|
|
|
|
|
|
|
result = mDOMDocument->GetDocumentElement(getter_AddRefs(docElement));
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(docElement, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = docElement->QueryInterface(NS_GET_IID(nsIDOMNode), (void **)aNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-11-02 04:04:13 -08:00
|
|
|
nsTextServicesDocument::CreateDocumentContentRange(nsRange** aRange)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-04-02 17:13:10 -07:00
|
|
|
*aRange = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-01-15 00:13:12 -08:00
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
nsresult rv = GetDocumentContentRootNode(getter_AddRefs(node));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(node, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-02-07 04:09:41 -08:00
|
|
|
nsCOMPtr<nsINode> nativeNode = do_QueryInterface(node);
|
|
|
|
NS_ENSURE_STATE(nativeNode);
|
|
|
|
|
|
|
|
nsRefPtr<nsRange> range = new nsRange(nativeNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-01-15 00:13:12 -08:00
|
|
|
rv = range->SelectNodeContents(node);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-01-15 00:13:12 -08:00
|
|
|
range.forget(aRange);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-11-02 04:04:13 -08:00
|
|
|
nsTextServicesDocument::CreateDocumentContentRootToNodeOffsetRange(
|
|
|
|
nsIDOMNode* aParent, int32_t aOffset, bool aToStart, nsRange** aRange)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aParent && aRange, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
*aRange = 0;
|
|
|
|
|
|
|
|
NS_ASSERTION(aOffset >= 0, "Invalid offset!");
|
|
|
|
|
|
|
|
if (aOffset < 0)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> bodyNode;
|
2012-01-15 00:13:12 -08:00
|
|
|
nsresult rv = GetDocumentContentRootNode(getter_AddRefs(bodyNode));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(bodyNode, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> startNode;
|
|
|
|
nsCOMPtr<nsIDOMNode> endNode;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t startOffset, endOffset;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-01-15 00:13:12 -08:00
|
|
|
if (aToStart) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// The range should begin at the start of the document
|
|
|
|
// and extend up until (aParent, aOffset).
|
|
|
|
|
|
|
|
startNode = bodyNode;
|
|
|
|
startOffset = 0;
|
2012-01-15 00:13:12 -08:00
|
|
|
endNode = aParent;
|
2007-03-22 10:30:00 -07:00
|
|
|
endOffset = aOffset;
|
2012-01-15 00:13:12 -08:00
|
|
|
} else {
|
2007-03-22 10:30:00 -07:00
|
|
|
// The range should begin at (aParent, aOffset) and
|
|
|
|
// extend to the end of the document.
|
|
|
|
|
2012-01-15 00:13:12 -08:00
|
|
|
startNode = aParent;
|
2007-03-22 10:30:00 -07:00
|
|
|
startOffset = aOffset;
|
|
|
|
endNode = bodyNode;
|
|
|
|
|
2012-05-05 02:00:06 -07:00
|
|
|
nsCOMPtr<nsINode> body = do_QueryInterface(bodyNode);
|
2012-08-22 08:56:38 -07:00
|
|
|
endOffset = body ? int32_t(body->GetChildCount()) : 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-01-15 00:13:12 -08:00
|
|
|
return nsRange::CreateRange(startNode, startOffset, endNode, endOffset,
|
|
|
|
aRange);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsTextServicesDocument::CreateDocumentContentIterator(nsIContentIterator **aIterator)
|
|
|
|
{
|
|
|
|
nsresult result;
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aIterator, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-11-02 04:04:13 -08:00
|
|
|
nsRefPtr<nsRange> range;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = CreateDocumentContentRange(getter_AddRefs(range));
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = CreateContentIterator(range, aIterator);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsTextServicesDocument::AdjustContentIterator()
|
|
|
|
{
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(mIterator, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> node(do_QueryInterface(mIterator->GetCurrentNode()));
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(node, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsIDOMNode *nodePtr = node.get();
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t tcount = mOffsetTable.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsIDOMNode *prevValidNode = 0;
|
|
|
|
nsIDOMNode *nextValidNode = 0;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool foundEntry = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
OffsetEntry *entry;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0; i < tcount && !nextValidNode; i++)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-22 01:43:15 -07:00
|
|
|
entry = mOffsetTable[i];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(entry, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (entry->mNode == nodePtr)
|
|
|
|
{
|
|
|
|
if (entry->mIsValid)
|
|
|
|
{
|
|
|
|
// The iterator is still pointing to something valid!
|
|
|
|
// Do nothing!
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We found an invalid entry that points to
|
|
|
|
// the current iterator node. Stop looking for
|
|
|
|
// a previous valid node!
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
foundEntry = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry->mIsValid)
|
|
|
|
{
|
|
|
|
if (!foundEntry)
|
|
|
|
prevValidNode = entry->mNode;
|
|
|
|
else
|
|
|
|
nextValidNode = entry->mNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> content;
|
|
|
|
|
|
|
|
if (prevValidNode)
|
|
|
|
content = do_QueryInterface(prevValidNode);
|
|
|
|
else if (nextValidNode)
|
|
|
|
content = do_QueryInterface(nextValidNode);
|
|
|
|
|
|
|
|
if (content)
|
|
|
|
{
|
|
|
|
result = mIterator->PositionAt(content);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
mIteratorStatus = eIsDone;
|
|
|
|
else
|
|
|
|
mIteratorStatus = eValid;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we get here, there aren't any valid entries
|
|
|
|
// in the offset table! Try to position the iterator
|
|
|
|
// on the next text block first, then previous if
|
|
|
|
// one doesn't exist!
|
|
|
|
|
|
|
|
if (mNextTextBlock)
|
|
|
|
{
|
|
|
|
result = mIterator->PositionAt(mNextTextBlock);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
mIteratorStatus = eIsDone;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIteratorStatus = eNext;
|
|
|
|
}
|
|
|
|
else if (mPrevTextBlock)
|
|
|
|
{
|
|
|
|
result = mIterator->PositionAt(mPrevTextBlock);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
mIteratorStatus = eIsDone;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIteratorStatus = ePrev;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mIteratorStatus = eIsDone;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsTextServicesDocument::DidSkip(nsIContentIterator* aFilteredIter)
|
|
|
|
{
|
|
|
|
// We can assume here that the Iterator is a nsFilteredContentIterator because
|
|
|
|
// all the iterator are created in CreateContentIterator which create a
|
|
|
|
// nsFilteredContentIterator
|
|
|
|
// So if the iterator bailed on one of the "filtered" content nodes then we
|
2011-10-17 07:59:28 -07:00
|
|
|
// consider that to be a block and bail with true
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aFilteredIter) {
|
2007-07-08 00:08:04 -07:00
|
|
|
nsFilteredContentIterator* filter = static_cast<nsFilteredContentIterator *>(aFilteredIter);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (filter && filter->DidSkip()) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsTextServicesDocument::ClearDidSkip(nsIContentIterator* aFilteredIter)
|
|
|
|
{
|
|
|
|
// Clear filter's skip flag
|
|
|
|
if (aFilteredIter) {
|
2007-07-08 00:08:04 -07:00
|
|
|
nsFilteredContentIterator* filter = static_cast<nsFilteredContentIterator *>(aFilteredIter);
|
2007-03-22 10:30:00 -07:00
|
|
|
filter->ClearDidSkip();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsTextServicesDocument::IsBlockNode(nsIContent *aContent)
|
|
|
|
{
|
2010-08-24 22:44:19 -07:00
|
|
|
if (!aContent) {
|
|
|
|
NS_ERROR("How did a null pointer get passed to IsBlockNode?");
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-08-24 22:44:19 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIAtom *atom = aContent->Tag();
|
|
|
|
|
|
|
|
return (sAAtom != atom &&
|
|
|
|
sAddressAtom != atom &&
|
|
|
|
sBigAtom != atom &&
|
|
|
|
sBAtom != atom &&
|
|
|
|
sCiteAtom != atom &&
|
|
|
|
sCodeAtom != atom &&
|
|
|
|
sDfnAtom != atom &&
|
|
|
|
sEmAtom != atom &&
|
|
|
|
sFontAtom != atom &&
|
|
|
|
sIAtom != atom &&
|
|
|
|
sKbdAtom != atom &&
|
|
|
|
sKeygenAtom != atom &&
|
|
|
|
sNobrAtom != atom &&
|
|
|
|
sSAtom != atom &&
|
|
|
|
sSampAtom != atom &&
|
|
|
|
sSmallAtom != atom &&
|
|
|
|
sSpacerAtom != atom &&
|
|
|
|
sSpanAtom != atom &&
|
|
|
|
sStrikeAtom != atom &&
|
|
|
|
sStrongAtom != atom &&
|
|
|
|
sSubAtom != atom &&
|
|
|
|
sSupAtom != atom &&
|
|
|
|
sTtAtom != atom &&
|
|
|
|
sUAtom != atom &&
|
|
|
|
sVarAtom != atom &&
|
|
|
|
sWbrAtom != atom);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsTextServicesDocument::HasSameBlockNodeParent(nsIContent *aContent1, nsIContent *aContent2)
|
|
|
|
{
|
|
|
|
nsIContent* p1 = aContent1->GetParent();
|
|
|
|
nsIContent* p2 = aContent2->GetParent();
|
|
|
|
|
|
|
|
// Quick test:
|
|
|
|
|
|
|
|
if (p1 == p2)
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Walk up the parent hierarchy looking for closest block boundary node:
|
|
|
|
|
|
|
|
while (p1 && !IsBlockNode(p1))
|
|
|
|
{
|
|
|
|
p1 = p1->GetParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
while (p2 && !IsBlockNode(p2))
|
|
|
|
{
|
|
|
|
p2 = p2->GetParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return p1 == p2;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsTextServicesDocument::IsTextNode(nsIContent *aContent)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
NS_ENSURE_TRUE(aContent, false);
|
2012-01-15 00:13:12 -08:00
|
|
|
return nsIDOMNode::TEXT_NODE == aContent->NodeType();
|
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
|
|
|
nsTextServicesDocument::IsTextNode(nsIDOMNode *aNode)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
NS_ENSURE_TRUE(aNode, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-01-15 00:13:12 -08:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
|
|
|
|
return IsTextNode(content);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTextServicesDocument::SetSelectionInternal(int32_t aOffset, int32_t aLength, bool aDoUpdate)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(mSelCon && aOffset >= 0 && aLength >= 0, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsIDOMNode *sNode = 0, *eNode = 0;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t i, sOffset = 0, eOffset = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
OffsetEntry *entry;
|
|
|
|
|
|
|
|
// Find start of selection in node offset terms:
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (i = 0; !sNode && i < int32_t(mOffsetTable.Length()); i++)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-22 01:43:15 -07:00
|
|
|
entry = mOffsetTable[i];
|
2007-03-22 10:30:00 -07:00
|
|
|
if (entry->mIsValid)
|
|
|
|
{
|
|
|
|
if (entry->mIsInsertedText)
|
|
|
|
{
|
|
|
|
// Caret can only be placed at the end of an
|
|
|
|
// inserted text offset entry, if the offsets
|
|
|
|
// match exactly!
|
|
|
|
|
|
|
|
if (entry->mStrOffset == aOffset)
|
|
|
|
{
|
|
|
|
sNode = entry->mNode;
|
|
|
|
sOffset = entry->mNodeOffset + entry->mLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (aOffset >= entry->mStrOffset)
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool foundEntry = false;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t strEndOffset = entry->mStrOffset + entry->mLength;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (aOffset < strEndOffset)
|
2011-10-17 07:59:28 -07:00
|
|
|
foundEntry = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
else if (aOffset == strEndOffset)
|
|
|
|
{
|
|
|
|
// Peek after this entry to see if we have any
|
|
|
|
// inserted text entries belonging to the same
|
|
|
|
// entry->mNode. If so, we have to place the selection
|
|
|
|
// after it!
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
if ((i+1) < int32_t(mOffsetTable.Length()))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-22 01:43:15 -07:00
|
|
|
OffsetEntry *nextEntry = mOffsetTable[i+1];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!nextEntry->mIsValid || nextEntry->mStrOffset != aOffset)
|
|
|
|
{
|
|
|
|
// Next offset entry isn't an exact match, so we'll
|
|
|
|
// just use the current entry.
|
2011-10-17 07:59:28 -07:00
|
|
|
foundEntry = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foundEntry)
|
|
|
|
{
|
|
|
|
sNode = entry->mNode;
|
|
|
|
sOffset = entry->mNodeOffset + aOffset - entry->mStrOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sNode)
|
|
|
|
{
|
|
|
|
mSelStartIndex = i;
|
|
|
|
mSelStartOffset = aOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(sNode, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// XXX: If we ever get a SetSelection() method in nsIEditor, we should
|
|
|
|
// use it.
|
|
|
|
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
|
|
|
|
if (aDoUpdate)
|
|
|
|
{
|
|
|
|
result = mSelCon->GetSelection(nsISelectionController::SELECTION_NORMAL, getter_AddRefs(selection));
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = selection->Collapse(sNode, sOffset);
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aLength <= 0)
|
|
|
|
{
|
|
|
|
// We have a collapsed selection. (Caret)
|
|
|
|
|
|
|
|
mSelEndIndex = mSelStartIndex;
|
|
|
|
mSelEndOffset = mSelStartOffset;
|
|
|
|
|
|
|
|
//**** KDEBUG ****
|
|
|
|
// printf("\n* Sel: (%2d, %4d) (%2d, %4d)\n", mSelStartIndex, mSelStartOffset, mSelEndIndex, mSelEndOffset);
|
|
|
|
//**** KDEBUG ****
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the end of the selection in node offset terms:
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t endOffset = aOffset + aLength;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-22 01:43:15 -07:00
|
|
|
for (i = mOffsetTable.Length() - 1; !eNode && i >= 0; i--)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-22 01:43:15 -07:00
|
|
|
entry = mOffsetTable[i];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (entry->mIsValid)
|
|
|
|
{
|
|
|
|
if (entry->mIsInsertedText)
|
|
|
|
{
|
|
|
|
if (entry->mStrOffset == eOffset)
|
|
|
|
{
|
|
|
|
// If the selection ends on an inserted text offset entry,
|
|
|
|
// the selection includes the entire entry!
|
|
|
|
|
|
|
|
eNode = entry->mNode;
|
|
|
|
eOffset = entry->mNodeOffset + entry->mLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (endOffset >= entry->mStrOffset && endOffset <= entry->mStrOffset + entry->mLength)
|
|
|
|
{
|
|
|
|
eNode = entry->mNode;
|
|
|
|
eOffset = entry->mNodeOffset + endOffset - entry->mStrOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eNode)
|
|
|
|
{
|
|
|
|
mSelEndIndex = i;
|
|
|
|
mSelEndOffset = endOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aDoUpdate && eNode)
|
|
|
|
{
|
|
|
|
result = selection->Extend(eNode, eOffset);
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//**** KDEBUG ****
|
|
|
|
// printf("\n * Sel: (%2d, %4d) (%2d, %4d)\n", mSelStartIndex, mSelStartOffset, mSelEndIndex, mSelEndOffset);
|
|
|
|
//**** KDEBUG ****
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTextServicesDocument::GetSelection(nsITextServicesDocument::TSDBlockSelectionStatus *aSelStatus, int32_t *aSelOffset, int32_t *aSelLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult result;
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aSelStatus && aSelOffset && aSelLength, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
*aSelStatus = nsITextServicesDocument::eBlockNotFound;
|
|
|
|
*aSelOffset = -1;
|
|
|
|
*aSelLength = -1;
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(mDOMDocument && mSelCon, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (mIteratorStatus == nsTextServicesDocument::eIsDone)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isCollapsed;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = mSelCon->GetSelection(nsISelectionController::SELECTION_NORMAL, getter_AddRefs(selection));
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = selection->GetIsCollapsed(&isCollapsed);
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// XXX: If we expose this method publicly, we need to
|
|
|
|
// add LOCK_DOC/UNLOCK_DOC calls!
|
|
|
|
|
|
|
|
// LOCK_DOC(this);
|
|
|
|
|
|
|
|
if (isCollapsed)
|
|
|
|
result = GetCollapsedSelection(aSelStatus, aSelOffset, aSelLength);
|
|
|
|
else
|
|
|
|
result = GetUncollapsedSelection(aSelStatus, aSelOffset, aSelLength);
|
|
|
|
|
|
|
|
// UNLOCK_DOC(this);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTextServicesDocument::GetCollapsedSelection(nsITextServicesDocument::TSDBlockSelectionStatus *aSelStatus, int32_t *aSelOffset, int32_t *aSelLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-11-02 04:04:13 -08:00
|
|
|
nsCOMPtr<nsISelection> domSelection;
|
|
|
|
nsresult result =
|
|
|
|
mSelCon->GetSelection(nsISelectionController::SELECTION_NORMAL,
|
|
|
|
getter_AddRefs(domSelection));
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2014-11-02 04:04:13 -08:00
|
|
|
NS_ENSURE_TRUE(domSelection, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsRefPtr<Selection> selection = static_cast<Selection*>(domSelection.get());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// The calling function should have done the GetIsCollapsed()
|
|
|
|
// check already. Just assume it's collapsed!
|
|
|
|
*aSelStatus = nsITextServicesDocument::eBlockOutside;
|
|
|
|
*aSelOffset = *aSelLength = -1;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t tableCount = mOffsetTable.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (tableCount == 0)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// Get pointers to the first and last offset entries
|
|
|
|
// in the table.
|
|
|
|
|
2012-06-06 00:36:23 -07:00
|
|
|
OffsetEntry* eStart = mOffsetTable[0];
|
|
|
|
OffsetEntry* eEnd;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (tableCount > 1)
|
2009-04-22 01:43:15 -07:00
|
|
|
eEnd = mOffsetTable[tableCount - 1];
|
2007-03-22 10:30:00 -07:00
|
|
|
else
|
|
|
|
eEnd = eStart;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t eStartOffset = eStart->mNodeOffset;
|
|
|
|
int32_t eEndOffset = eEnd->mNodeOffset + eEnd->mLength;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-11-02 04:04:13 -08:00
|
|
|
nsRefPtr<nsRange> range = selection->GetRangeAt(0);
|
|
|
|
NS_ENSURE_STATE(range);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-06 00:36:23 -07:00
|
|
|
nsCOMPtr<nsIDOMNode> domParent;
|
|
|
|
result = range->GetStartContainer(getter_AddRefs(domParent));
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-06 00:36:23 -07:00
|
|
|
nsCOMPtr<nsINode> parent = do_QueryInterface(domParent);
|
|
|
|
MOZ_ASSERT(parent);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t offset;
|
2012-06-06 00:36:23 -07:00
|
|
|
result = range->GetStartOffset(&offset);
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t e1s1 = nsContentUtils::ComparePoints(eStart->mNode, eStartOffset,
|
2012-06-06 00:36:23 -07:00
|
|
|
domParent, offset);
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t e2s1 = nsContentUtils::ComparePoints(eEnd->mNode, eEndOffset,
|
2012-06-06 00:36:23 -07:00
|
|
|
domParent, offset);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-06 00:36:23 -07:00
|
|
|
if (e1s1 > 0 || e2s1 < 0) {
|
|
|
|
// We're done if the caret is outside the current text block.
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-06 00:36:23 -07:00
|
|
|
if (parent->NodeType() == nsIDOMNode::TEXT_NODE) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// Good news, the caret is in a text node. Look
|
|
|
|
// through the offset table for the entry that
|
2012-02-15 01:18:24 -08:00
|
|
|
// matches its parent and offset.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0; i < tableCount; i++) {
|
2012-06-06 00:36:23 -07:00
|
|
|
OffsetEntry* entry = mOffsetTable[i];
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(entry, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-06 00:36:23 -07:00
|
|
|
if (entry->mNode == domParent.get() &&
|
2007-03-22 10:30:00 -07:00
|
|
|
entry->mNodeOffset <= offset && offset <= (entry->mNodeOffset + entry->mLength))
|
|
|
|
{
|
|
|
|
*aSelStatus = nsITextServicesDocument::eBlockContains;
|
|
|
|
*aSelOffset = entry->mStrOffset + (offset - entry->mNodeOffset);
|
|
|
|
*aSelLength = 0;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we get here, we didn't find a text node entry
|
|
|
|
// in our offset table that matched.
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The caret is in our text block, but it's positioned in some
|
|
|
|
// non-text node (ex. <b>). Create a range based on the start
|
|
|
|
// and end of the text block, then create an iterator based on
|
2012-02-15 01:18:24 -08:00
|
|
|
// this range, with its initial position set to the closest
|
2007-03-22 10:30:00 -07:00
|
|
|
// child of this non-text node. Then look for the closest text
|
|
|
|
// node.
|
|
|
|
|
|
|
|
result = CreateRange(eStart->mNode, eStartOffset, eEnd->mNode, eEndOffset, getter_AddRefs(range));
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-06 00:36:23 -07:00
|
|
|
nsCOMPtr<nsIContentIterator> iter;
|
2007-03-22 10:30:00 -07:00
|
|
|
result = CreateContentIterator(range, getter_AddRefs(iter));
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-06 00:36:23 -07:00
|
|
|
nsIContent* saveNode;
|
|
|
|
if (parent->HasChildren()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// XXX: We need to make sure that all of parent's
|
|
|
|
// children are in the text block.
|
|
|
|
|
|
|
|
// If the parent has children, position the iterator
|
|
|
|
// on the child that is to the left of the offset.
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t childIndex = (uint32_t)offset;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-06 00:36:23 -07:00
|
|
|
if (childIndex > 0) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t numChildren = parent->GetChildCount();
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(childIndex <= numChildren, "Invalid selection offset!");
|
|
|
|
|
2012-06-06 00:36:23 -07:00
|
|
|
if (childIndex > numChildren) {
|
2007-03-22 10:30:00 -07:00
|
|
|
childIndex = numChildren;
|
2012-06-06 00:36:23 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
childIndex -= 1;
|
|
|
|
}
|
|
|
|
|
2012-06-06 00:36:23 -07:00
|
|
|
nsIContent* content = parent->GetChildAt(childIndex);
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(content, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = iter->PositionAt(content);
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2012-06-06 00:36:23 -07:00
|
|
|
|
|
|
|
saveNode = content;
|
|
|
|
} else {
|
2007-03-22 10:30:00 -07:00
|
|
|
// The parent has no children, so position the iterator
|
|
|
|
// on the parent.
|
2012-06-10 05:39:21 -07:00
|
|
|
NS_ENSURE_TRUE(parent->IsContent(), NS_ERROR_FAILURE);
|
|
|
|
nsCOMPtr<nsIContent> content = parent->AsContent();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = iter->PositionAt(content);
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-06 00:36:23 -07:00
|
|
|
saveNode = content;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now iterate to the left, towards the beginning of
|
|
|
|
// the text block, to find the first text node you
|
|
|
|
// come across.
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIContent* node = nullptr;
|
2012-06-06 00:36:23 -07:00
|
|
|
while (!iter->IsDone()) {
|
|
|
|
nsINode* current = iter->GetCurrentNode();
|
|
|
|
if (current->NodeType() == nsIDOMNode::TEXT_NODE) {
|
|
|
|
node = static_cast<nsIContent*>(current);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
iter->Prev();
|
|
|
|
}
|
|
|
|
|
2012-06-06 00:36:23 -07:00
|
|
|
if (node) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// We found a node, now set the offset to the end
|
|
|
|
// of the text node.
|
2012-06-06 00:36:23 -07:00
|
|
|
offset = node->TextLength();
|
|
|
|
} else {
|
2007-03-22 10:30:00 -07:00
|
|
|
// We should never really get here, but I'm paranoid.
|
|
|
|
|
|
|
|
// We didn't find a text node above, so iterate to
|
|
|
|
// the right, towards the end of the text block, looking
|
|
|
|
// for a text node.
|
|
|
|
|
2012-06-06 00:36:23 -07:00
|
|
|
result = iter->PositionAt(saveNode);
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
node = nullptr;
|
2012-06-06 00:36:23 -07:00
|
|
|
while (!iter->IsDone()) {
|
|
|
|
nsINode* current = iter->GetCurrentNode();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-06 00:36:23 -07:00
|
|
|
if (current->NodeType() == nsIDOMNode::TEXT_NODE) {
|
|
|
|
node = static_cast<nsIContent*>(current);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
iter->Next();
|
|
|
|
}
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(node, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// We found a text node, so set the offset to
|
2012-06-06 00:36:23 -07:00
|
|
|
// the beginning of the node.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0; i < tableCount; i++) {
|
2012-06-06 00:36:23 -07:00
|
|
|
OffsetEntry* entry = mOffsetTable[i];
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(entry, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-06 00:36:23 -07:00
|
|
|
if (entry->mNode == node->AsDOMNode() &&
|
2007-03-22 10:30:00 -07:00
|
|
|
entry->mNodeOffset <= offset && offset <= (entry->mNodeOffset + entry->mLength))
|
|
|
|
{
|
|
|
|
*aSelStatus = nsITextServicesDocument::eBlockContains;
|
|
|
|
*aSelOffset = entry->mStrOffset + (offset - entry->mNodeOffset);
|
|
|
|
*aSelLength = 0;
|
|
|
|
|
|
|
|
// Now move the caret so that it is actually in the text node.
|
|
|
|
// We do this to keep things in sync.
|
|
|
|
//
|
|
|
|
// In most cases, the user shouldn't see any movement in the caret
|
|
|
|
// on screen.
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
result = SetSelectionInternal(*aSelOffset, *aSelLength, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTextServicesDocument::GetUncollapsedSelection(nsITextServicesDocument::TSDBlockSelectionStatus *aSelStatus, int32_t *aSelOffset, int32_t *aSelLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult result;
|
|
|
|
|
2014-11-02 04:04:13 -08:00
|
|
|
nsRefPtr<nsRange> range;
|
2007-03-22 10:30:00 -07:00
|
|
|
OffsetEntry *entry;
|
|
|
|
|
2014-11-02 04:04:13 -08:00
|
|
|
nsCOMPtr<nsISelection> domSelection;
|
|
|
|
result = mSelCon->GetSelection(nsISelectionController::SELECTION_NORMAL,
|
|
|
|
getter_AddRefs(domSelection));
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2014-11-02 04:04:13 -08:00
|
|
|
NS_ENSURE_TRUE(domSelection, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-11-02 04:04:13 -08:00
|
|
|
nsRefPtr<Selection> selection = static_cast<Selection*>(domSelection.get());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// It is assumed that the calling function has made sure that the
|
|
|
|
// selection is not collapsed, and that the input params to this
|
|
|
|
// method are initialized to some defaults.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> startParent, endParent;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t startOffset, endOffset;
|
|
|
|
int32_t rangeCount, tableCount, i;
|
|
|
|
int32_t e1s1, e1s2, e2s1, e2s2;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
OffsetEntry *eStart, *eEnd;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t eStartOffset, eEndOffset;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-22 01:43:15 -07:00
|
|
|
tableCount = mOffsetTable.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Get pointers to the first and last offset entries
|
|
|
|
// in the table.
|
|
|
|
|
2009-04-22 01:43:15 -07:00
|
|
|
eStart = mOffsetTable[0];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (tableCount > 1)
|
2009-04-22 01:43:15 -07:00
|
|
|
eEnd = mOffsetTable[tableCount - 1];
|
2007-03-22 10:30:00 -07:00
|
|
|
else
|
|
|
|
eEnd = eStart;
|
|
|
|
|
|
|
|
eStartOffset = eStart->mNodeOffset;
|
|
|
|
eEndOffset = eEnd->mNodeOffset + eEnd->mLength;
|
|
|
|
|
|
|
|
result = selection->GetRangeCount(&rangeCount);
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Find the first range in the selection that intersects
|
|
|
|
// the current text block.
|
|
|
|
|
|
|
|
for (i = 0; i < rangeCount; i++)
|
|
|
|
{
|
2014-11-02 04:04:13 -08:00
|
|
|
range = selection->GetRangeAt(i);
|
|
|
|
NS_ENSURE_STATE(range);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = GetRangeEndPoints(range,
|
|
|
|
getter_AddRefs(startParent), &startOffset,
|
|
|
|
getter_AddRefs(endParent), &endOffset);
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-01-28 07:42:14 -08:00
|
|
|
e1s2 = nsContentUtils::ComparePoints(eStart->mNode, eStartOffset,
|
|
|
|
endParent, endOffset);
|
|
|
|
e2s1 = nsContentUtils::ComparePoints(eEnd->mNode, eEndOffset,
|
|
|
|
startParent, startOffset);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Break out of the loop if the text block intersects the current range.
|
|
|
|
|
|
|
|
if (e1s2 <= 0 && e2s1 >= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're done if we didn't find an intersecting range.
|
|
|
|
|
|
|
|
if (rangeCount < 1 || e1s2 > 0 || e2s1 < 0)
|
|
|
|
{
|
|
|
|
*aSelStatus = nsITextServicesDocument::eBlockOutside;
|
|
|
|
*aSelOffset = *aSelLength = -1;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have an intersecting range, find out more info:
|
|
|
|
|
2012-01-28 07:42:14 -08:00
|
|
|
e1s1 = nsContentUtils::ComparePoints(eStart->mNode, eStartOffset,
|
|
|
|
startParent, startOffset);
|
|
|
|
e2s2 = nsContentUtils::ComparePoints(eEnd->mNode, eEndOffset,
|
|
|
|
endParent, endOffset);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (rangeCount > 1)
|
|
|
|
{
|
|
|
|
// There are multiple selection ranges, we only deal
|
|
|
|
// with the first one that intersects the current,
|
|
|
|
// text block, so mark this a as a partial.
|
|
|
|
|
|
|
|
*aSelStatus = nsITextServicesDocument::eBlockPartial;
|
|
|
|
}
|
|
|
|
else if (e1s1 > 0 && e2s2 < 0)
|
|
|
|
{
|
|
|
|
// The range extends beyond the start and
|
|
|
|
// end of the current text block.
|
|
|
|
|
|
|
|
*aSelStatus = nsITextServicesDocument::eBlockInside;
|
|
|
|
}
|
|
|
|
else if (e1s1 <= 0 && e2s2 >= 0)
|
|
|
|
{
|
|
|
|
// The current text block contains the entire
|
|
|
|
// range.
|
|
|
|
|
|
|
|
*aSelStatus = nsITextServicesDocument::eBlockContains;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The range partially intersects the block.
|
|
|
|
|
|
|
|
*aSelStatus = nsITextServicesDocument::eBlockPartial;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now create a range based on the intersection of the
|
|
|
|
// text block and range:
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> p1, p2;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t o1, o2;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// The start of the range will be the rightmost
|
|
|
|
// start node.
|
|
|
|
|
|
|
|
if (e1s1 >= 0)
|
|
|
|
{
|
|
|
|
p1 = do_QueryInterface(eStart->mNode);
|
|
|
|
o1 = eStartOffset;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p1 = startParent;
|
|
|
|
o1 = startOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The end of the range will be the leftmost
|
|
|
|
// end node.
|
|
|
|
|
|
|
|
if (e2s2 <= 0)
|
|
|
|
{
|
|
|
|
p2 = do_QueryInterface(eEnd->mNode);
|
|
|
|
o2 = eEndOffset;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p2 = endParent;
|
|
|
|
o2 = endOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = CreateRange(p1, o1, p2, o2, getter_AddRefs(range));
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Now iterate over this range to figure out the selection's
|
|
|
|
// block offset and length.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContentIterator> iter;
|
|
|
|
|
|
|
|
result = CreateContentIterator(range, getter_AddRefs(iter));
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Find the first text node in the range.
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool found;
|
2008-10-15 02:40:28 -07:00
|
|
|
nsCOMPtr<nsIContent> content;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
iter->First();
|
|
|
|
|
|
|
|
if (!IsTextNode(p1))
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
found = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
while (!iter->IsDone())
|
|
|
|
{
|
2008-10-15 02:40:28 -07:00
|
|
|
content = do_QueryInterface(iter->GetCurrentNode());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (IsTextNode(content))
|
|
|
|
{
|
|
|
|
p1 = do_QueryInterface(content);
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(p1, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
o1 = 0;
|
2011-10-17 07:59:28 -07:00
|
|
|
found = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
iter->Next();
|
|
|
|
}
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(found, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the last text node in the range.
|
|
|
|
|
|
|
|
iter->Last();
|
|
|
|
|
|
|
|
if (! IsTextNode(p2))
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
found = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
while (!iter->IsDone())
|
|
|
|
{
|
2008-10-15 02:40:28 -07:00
|
|
|
content = do_QueryInterface(iter->GetCurrentNode());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (IsTextNode(content))
|
|
|
|
{
|
|
|
|
p2 = do_QueryInterface(content);
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(p2, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsString str;
|
|
|
|
|
|
|
|
result = p2->GetNodeValue(str);
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
o2 = str.Length();
|
2011-10-17 07:59:28 -07:00
|
|
|
found = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
iter->Prev();
|
|
|
|
}
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(found, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
found = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
*aSelLength = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < tableCount; i++)
|
|
|
|
{
|
2009-04-22 01:43:15 -07:00
|
|
|
entry = mOffsetTable[i];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(entry, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
if (entry->mNode == p1.get() &&
|
|
|
|
entry->mNodeOffset <= o1 && o1 <= (entry->mNodeOffset + entry->mLength))
|
|
|
|
{
|
|
|
|
*aSelOffset = entry->mStrOffset + (o1 - entry->mNodeOffset);
|
|
|
|
|
|
|
|
if (p1 == p2 &&
|
|
|
|
entry->mNodeOffset <= o2 && o2 <= (entry->mNodeOffset + entry->mLength))
|
|
|
|
{
|
|
|
|
// The start and end of the range are in the same offset
|
|
|
|
// entry. Calculate the length of the range then we're done.
|
|
|
|
|
|
|
|
*aSelLength = o2 - o1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Add the length of the sub string in this offset entry
|
|
|
|
// that follows the start of the range.
|
|
|
|
|
|
|
|
*aSelLength = entry->mLength - (o1 - entry->mNodeOffset);
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
found = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else // found
|
|
|
|
{
|
|
|
|
if (entry->mNode == p2.get() &&
|
|
|
|
entry->mNodeOffset <= o2 && o2 <= (entry->mNodeOffset + entry->mLength))
|
|
|
|
{
|
|
|
|
// We found the end of the range. Calculate the length of the
|
|
|
|
// sub string that is before the end of the range, then we're done.
|
|
|
|
|
|
|
|
*aSelLength += o2 - entry->mNodeOffset;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The entire entry must be in the range.
|
|
|
|
|
|
|
|
*aSelLength += entry->mLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsTextServicesDocument::SelectionIsCollapsed()
|
|
|
|
{
|
|
|
|
return(mSelStartIndex == mSelEndIndex && mSelStartOffset == mSelEndOffset);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsTextServicesDocument::SelectionIsValid()
|
|
|
|
{
|
|
|
|
return(mSelStartIndex >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-11-02 04:04:13 -08:00
|
|
|
nsTextServicesDocument::GetRangeEndPoints(nsRange* aRange,
|
2012-08-22 08:56:38 -07:00
|
|
|
nsIDOMNode **aStartParent, int32_t *aStartOffset,
|
|
|
|
nsIDOMNode **aEndParent, int32_t *aEndOffset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult result;
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aRange && aStartParent && aStartOffset && aEndParent && aEndOffset, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = aRange->GetStartContainer(aStartParent);
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aStartParent, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = aRange->GetStartOffset(aStartOffset);
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = aRange->GetEndContainer(aEndParent);
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aEndParent, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = aRange->GetEndOffset(aEndOffset);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTextServicesDocument::CreateRange(nsIDOMNode *aStartParent, int32_t aStartOffset,
|
|
|
|
nsIDOMNode *aEndParent, int32_t aEndOffset,
|
2014-11-02 04:04:13 -08:00
|
|
|
nsRange** aRange)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-01-15 00:13:12 -08:00
|
|
|
return nsRange::CreateRange(aStartParent, aStartOffset, aEndParent,
|
|
|
|
aEndOffset, aRange);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsTextServicesDocument::FirstTextNode(nsIContentIterator *aIterator,
|
|
|
|
TSDIteratorStatus *aIteratorStatus)
|
|
|
|
{
|
|
|
|
if (aIteratorStatus)
|
|
|
|
*aIteratorStatus = nsTextServicesDocument::eIsDone;
|
|
|
|
|
|
|
|
aIterator->First();
|
|
|
|
|
2012-01-15 00:13:12 -08:00
|
|
|
while (!aIterator->IsDone()) {
|
|
|
|
if (aIterator->GetCurrentNode()->NodeType() == nsIDOMNode::TEXT_NODE) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aIteratorStatus)
|
|
|
|
*aIteratorStatus = nsTextServicesDocument::eValid;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
aIterator->Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsTextServicesDocument::LastTextNode(nsIContentIterator *aIterator,
|
|
|
|
TSDIteratorStatus *aIteratorStatus)
|
|
|
|
{
|
|
|
|
if (aIteratorStatus)
|
|
|
|
*aIteratorStatus = nsTextServicesDocument::eIsDone;
|
|
|
|
|
|
|
|
aIterator->Last();
|
|
|
|
|
2012-01-15 00:13:12 -08:00
|
|
|
while (!aIterator->IsDone()) {
|
|
|
|
if (aIterator->GetCurrentNode()->NodeType() == nsIDOMNode::TEXT_NODE) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aIteratorStatus)
|
|
|
|
*aIteratorStatus = nsTextServicesDocument::eValid;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
aIterator->Prev();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsTextServicesDocument::FirstTextNodeInCurrentBlock(nsIContentIterator *iter)
|
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(iter, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
ClearDidSkip(iter);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> last;
|
|
|
|
|
|
|
|
// Walk backwards over adjacent text nodes until
|
|
|
|
// we hit a block boundary:
|
|
|
|
|
|
|
|
while (!iter->IsDone())
|
|
|
|
{
|
2012-06-10 05:39:21 -07:00
|
|
|
nsCOMPtr<nsIContent> content = iter->GetCurrentNode()->IsContent()
|
|
|
|
? iter->GetCurrentNode()->AsContent()
|
2012-07-30 07:20:58 -07:00
|
|
|
: nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (IsTextNode(content))
|
|
|
|
{
|
|
|
|
if (!last || HasSameBlockNodeParent(content, last))
|
|
|
|
last = content;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We're done, the current text node is in a
|
|
|
|
// different block.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (last && IsBlockNode(content))
|
|
|
|
break;
|
|
|
|
|
|
|
|
iter->Prev();
|
|
|
|
|
|
|
|
if (DidSkip(iter))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last)
|
|
|
|
iter->PositionAt(last);
|
|
|
|
|
|
|
|
// XXX: What should we return if last is null?
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsTextServicesDocument::FirstTextNodeInPrevBlock(nsIContentIterator *aIterator)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> content;
|
|
|
|
nsresult result;
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aIterator, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// XXX: What if mIterator is not currently on a text node?
|
|
|
|
|
|
|
|
// Make sure mIterator is pointing to the first text node in the
|
|
|
|
// current block:
|
|
|
|
|
|
|
|
result = FirstTextNodeInCurrentBlock(aIterator);
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Point mIterator to the first node before the first text node:
|
|
|
|
|
|
|
|
aIterator->Prev();
|
|
|
|
|
|
|
|
if (aIterator->IsDone())
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Now find the first text node of the next block:
|
|
|
|
|
|
|
|
return FirstTextNodeInCurrentBlock(aIterator);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsTextServicesDocument::FirstTextNodeInNextBlock(nsIContentIterator *aIterator)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> prev;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool crossedBlockBoundary = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aIterator, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
ClearDidSkip(aIterator);
|
|
|
|
|
|
|
|
while (!aIterator->IsDone())
|
|
|
|
{
|
2012-06-10 05:39:21 -07:00
|
|
|
nsCOMPtr<nsIContent> content = aIterator->GetCurrentNode()->IsContent()
|
|
|
|
? aIterator->GetCurrentNode()->AsContent()
|
2012-07-30 07:20:58 -07:00
|
|
|
: nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (IsTextNode(content))
|
|
|
|
{
|
|
|
|
if (!crossedBlockBoundary && (!prev || HasSameBlockNodeParent(prev, content)))
|
|
|
|
prev = content;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (!crossedBlockBoundary && IsBlockNode(content))
|
2011-10-17 07:59:28 -07:00
|
|
|
crossedBlockBoundary = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
aIterator->Next();
|
|
|
|
|
|
|
|
if (!crossedBlockBoundary && DidSkip(aIterator))
|
2011-10-17 07:59:28 -07:00
|
|
|
crossedBlockBoundary = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsTextServicesDocument::GetFirstTextNodeInPrevBlock(nsIContent **aContent)
|
|
|
|
{
|
|
|
|
nsresult result;
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aContent, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
*aContent = 0;
|
|
|
|
|
|
|
|
// Save the iterator's current content node so we can restore
|
|
|
|
// it when we are done:
|
|
|
|
|
2008-10-15 02:40:28 -07:00
|
|
|
nsINode* node = mIterator->GetCurrentNode();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = FirstTextNodeInPrevBlock(mIterator);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
// Try to restore the iterator before returning.
|
2008-10-15 02:40:28 -07:00
|
|
|
mIterator->PositionAt(node);
|
2007-03-22 10:30:00 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mIterator->IsDone())
|
|
|
|
{
|
2012-06-10 05:39:21 -07:00
|
|
|
nsCOMPtr<nsIContent> current = mIterator->GetCurrentNode()->IsContent()
|
|
|
|
? mIterator->GetCurrentNode()->AsContent()
|
2012-07-30 07:20:58 -07:00
|
|
|
: nullptr;
|
2012-01-15 00:13:12 -08:00
|
|
|
current.forget(aContent);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Restore the iterator:
|
|
|
|
|
2008-10-15 02:40:28 -07:00
|
|
|
return mIterator->PositionAt(node);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsTextServicesDocument::GetFirstTextNodeInNextBlock(nsIContent **aContent)
|
|
|
|
{
|
|
|
|
nsresult result;
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aContent, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
*aContent = 0;
|
|
|
|
|
|
|
|
// Save the iterator's current content node so we can restore
|
|
|
|
// it when we are done:
|
|
|
|
|
2008-10-15 02:40:28 -07:00
|
|
|
nsINode* node = mIterator->GetCurrentNode();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = FirstTextNodeInNextBlock(mIterator);
|
|
|
|
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
{
|
|
|
|
// Try to restore the iterator before returning.
|
2008-10-15 02:40:28 -07:00
|
|
|
mIterator->PositionAt(node);
|
2007-03-22 10:30:00 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mIterator->IsDone())
|
|
|
|
{
|
2012-06-10 05:39:21 -07:00
|
|
|
nsCOMPtr<nsIContent> current = mIterator->GetCurrentNode()->IsContent()
|
|
|
|
? mIterator->GetCurrentNode()->AsContent()
|
2012-07-30 07:20:58 -07:00
|
|
|
: nullptr;
|
2012-01-15 00:13:12 -08:00
|
|
|
current.forget(aContent);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Restore the iterator:
|
2008-10-15 02:40:28 -07:00
|
|
|
return mIterator->PositionAt(node);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2009-04-22 01:43:15 -07:00
|
|
|
nsTextServicesDocument::CreateOffsetTable(nsTArray<OffsetEntry*> *aOffsetTable,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIContentIterator *aIterator,
|
|
|
|
TSDIteratorStatus *aIteratorStatus,
|
2014-11-02 04:04:13 -08:00
|
|
|
nsRange* aIterRange, nsString* aStr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> first;
|
|
|
|
nsCOMPtr<nsIContent> prev;
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aIterator, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
ClearOffsetTable(aOffsetTable);
|
|
|
|
|
|
|
|
if (aStr)
|
|
|
|
aStr->Truncate();
|
|
|
|
|
|
|
|
if (*aIteratorStatus == nsTextServicesDocument::eIsDone)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// If we have an aIterRange, retrieve the endpoints so
|
|
|
|
// they can be used in the while loop below to trim entries
|
|
|
|
// for text nodes that are partially selected by aIterRange.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> rngStartNode, rngEndNode;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t rngStartOffset = 0, rngEndOffset = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (aIterRange)
|
|
|
|
{
|
|
|
|
result = GetRangeEndPoints(aIterRange,
|
|
|
|
getter_AddRefs(rngStartNode), &rngStartOffset,
|
|
|
|
getter_AddRefs(rngEndNode), &rngEndOffset);
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The text service could have added text nodes to the beginning
|
|
|
|
// of the current block and called this method again. Make sure
|
|
|
|
// we really are at the beginning of the current block:
|
|
|
|
|
|
|
|
result = FirstTextNodeInCurrentBlock(aIterator);
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t offset = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
ClearDidSkip(aIterator);
|
|
|
|
|
|
|
|
while (!aIterator->IsDone())
|
|
|
|
{
|
2012-06-10 05:39:21 -07:00
|
|
|
nsCOMPtr<nsIContent> content = aIterator->GetCurrentNode()->IsContent()
|
|
|
|
? aIterator->GetCurrentNode()->AsContent()
|
2012-07-30 07:20:58 -07:00
|
|
|
: nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (IsTextNode(content))
|
|
|
|
{
|
|
|
|
if (!prev || HasSameBlockNodeParent(prev, content))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> node = do_QueryInterface(content);
|
|
|
|
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
nsString str;
|
|
|
|
|
|
|
|
result = node->GetNodeValue(str);
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Add an entry for this text node into the offset table:
|
|
|
|
|
|
|
|
OffsetEntry *entry = new OffsetEntry(node, offset, str.Length());
|
2009-04-22 01:43:15 -07:00
|
|
|
aOffsetTable->AppendElement(entry);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// If one or both of the endpoints of the iteration range
|
|
|
|
// are in the text node for this entry, make sure the entry
|
|
|
|
// only accounts for the portion of the text node that is
|
|
|
|
// in the range.
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t startOffset = 0;
|
|
|
|
int32_t endOffset = str.Length();
|
2011-09-28 23:19:26 -07:00
|
|
|
bool adjustStr = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (entry->mNode == rngStartNode)
|
|
|
|
{
|
|
|
|
entry->mNodeOffset = startOffset = rngStartOffset;
|
2011-10-17 07:59:28 -07:00
|
|
|
adjustStr = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (entry->mNode == rngEndNode)
|
|
|
|
{
|
|
|
|
endOffset = rngEndOffset;
|
2011-10-17 07:59:28 -07:00
|
|
|
adjustStr = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (adjustStr)
|
|
|
|
{
|
|
|
|
entry->mLength = endOffset - startOffset;
|
|
|
|
str = Substring(str, startOffset, entry->mLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += str.Length();
|
|
|
|
|
|
|
|
if (aStr)
|
|
|
|
{
|
|
|
|
// Append the text node's string to the output string:
|
|
|
|
|
|
|
|
if (!first)
|
|
|
|
*aStr = str;
|
|
|
|
else
|
|
|
|
*aStr += str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prev = content;
|
|
|
|
|
|
|
|
if (!first)
|
|
|
|
first = content;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (IsBlockNode(content))
|
|
|
|
break;
|
|
|
|
|
|
|
|
aIterator->Next();
|
|
|
|
|
|
|
|
if (DidSkip(aIterator))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
// Always leave the iterator pointing at the first
|
|
|
|
// text node of the current block!
|
|
|
|
|
|
|
|
aIterator->PositionAt(first);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If we never ran across a text node, the iterator
|
|
|
|
// might have been pointing to something invalid to
|
|
|
|
// begin with.
|
|
|
|
|
|
|
|
*aIteratorStatus = nsTextServicesDocument::eIsDone;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsTextServicesDocument::RemoveInvalidOffsetEntries()
|
|
|
|
{
|
|
|
|
OffsetEntry *entry;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t i = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
while (uint32_t(i) < mOffsetTable.Length())
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-22 01:43:15 -07:00
|
|
|
entry = mOffsetTable[i];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!entry->mIsValid)
|
|
|
|
{
|
2009-04-22 01:43:15 -07:00
|
|
|
mOffsetTable.RemoveElementAt(i);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (mSelStartIndex >= 0 && mSelStartIndex >= i)
|
|
|
|
{
|
|
|
|
// We are deleting an entry that comes before
|
|
|
|
// mSelStartIndex, decrement mSelStartIndex so
|
|
|
|
// that it points to the correct entry!
|
|
|
|
|
|
|
|
NS_ASSERTION(i != mSelStartIndex, "Invalid selection index.");
|
|
|
|
|
|
|
|
--mSelStartIndex;
|
|
|
|
--mSelEndIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2009-04-22 01:43:15 -07:00
|
|
|
nsTextServicesDocument::ClearOffsetTable(nsTArray<OffsetEntry*> *aOffsetTable)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-22 01:43:15 -07:00
|
|
|
for (i = 0; i < aOffsetTable->Length(); i++)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-22 01:43:15 -07:00
|
|
|
delete aOffsetTable->ElementAt(i);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
aOffsetTable->Clear();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTextServicesDocument::SplitOffsetEntry(int32_t aTableIndex, int32_t aNewEntryLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-22 01:43:15 -07:00
|
|
|
OffsetEntry *entry = mOffsetTable[aTableIndex];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_ASSERTION((aNewEntryLength > 0), "aNewEntryLength <= 0");
|
|
|
|
NS_ASSERTION((aNewEntryLength < entry->mLength), "aNewEntryLength >= mLength");
|
|
|
|
|
|
|
|
if (aNewEntryLength < 1 || aNewEntryLength >= entry->mLength)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t oldLength = entry->mLength - aNewEntryLength;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
OffsetEntry *newEntry = new OffsetEntry(entry->mNode,
|
|
|
|
entry->mStrOffset + oldLength,
|
|
|
|
aNewEntryLength);
|
|
|
|
|
2009-04-22 01:43:15 -07:00
|
|
|
if (!mOffsetTable.InsertElementAt(aTableIndex + 1, newEntry))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
delete newEntry;
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust entry fields:
|
|
|
|
|
|
|
|
entry->mLength = oldLength;
|
|
|
|
newEntry->mNodeOffset = entry->mNodeOffset + oldLength;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTextServicesDocument::NodeHasOffsetEntry(nsTArray<OffsetEntry*> *aOffsetTable, nsIDOMNode *aNode, bool *aHasEntry, int32_t *aEntryIndex)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
OffsetEntry *entry;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aNode && aHasEntry && aEntryIndex, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-22 01:43:15 -07:00
|
|
|
for (i = 0; i < aOffsetTable->Length(); i++)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-22 01:43:15 -07:00
|
|
|
entry = (*aOffsetTable)[i];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(entry, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (entry->mNode == aNode)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
*aHasEntry = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
*aEntryIndex = i;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
*aHasEntry = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
*aEntryIndex = -1;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Spellchecker code has this. See bug 211343
|
|
|
|
#define IS_NBSP_CHAR(c) (((unsigned char)0xa0)==(c))
|
|
|
|
|
|
|
|
nsresult
|
2009-04-22 01:43:15 -07:00
|
|
|
nsTextServicesDocument::FindWordBounds(nsTArray<OffsetEntry*> *aOffsetTable,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsString *aBlockStr,
|
|
|
|
nsIDOMNode *aNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aNodeOffset,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIDOMNode **aWordStartNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t *aWordStartOffset,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIDOMNode **aWordEndNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t *aWordEndOffset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Initialize return values.
|
|
|
|
|
|
|
|
if (aWordStartNode)
|
2012-07-30 07:20:58 -07:00
|
|
|
*aWordStartNode = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aWordStartOffset)
|
|
|
|
*aWordStartOffset = 0;
|
|
|
|
if (aWordEndNode)
|
2012-07-30 07:20:58 -07:00
|
|
|
*aWordEndNode = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aWordEndOffset)
|
|
|
|
*aWordEndOffset = 0;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t entryIndex = 0;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasEntry = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// It's assumed that aNode is a text node. The first thing
|
2012-02-15 01:18:24 -08:00
|
|
|
// we do is get its index in the offset table so we can
|
2007-03-22 10:30:00 -07:00
|
|
|
// calculate the dom point's string offset.
|
|
|
|
|
|
|
|
nsresult result = NodeHasOffsetEntry(aOffsetTable, aNode, &hasEntry, &entryIndex);
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
|
|
|
NS_ENSURE_TRUE(hasEntry, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
// Next we map aNodeOffset into a string offset.
|
|
|
|
|
2009-04-22 01:43:15 -07:00
|
|
|
OffsetEntry *entry = (*aOffsetTable)[entryIndex];
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t strOffset = entry->mStrOffset + aNodeOffset - entry->mNodeOffset;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Now we use the word breaker to find the beginning and end
|
|
|
|
// of the word from our calculated string offset.
|
|
|
|
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t *str = aBlockStr->get();
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t strLen = aBlockStr->Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-05-05 02:00:05 -07:00
|
|
|
nsIWordBreaker* wordBreaker = nsContentUtils::WordBreaker();
|
|
|
|
nsWordRange res = wordBreaker->FindWord(str, strLen, strOffset);
|
|
|
|
if (res.mBegin > strLen) {
|
|
|
|
return str ? NS_ERROR_ILLEGAL_VALUE : NS_ERROR_NULL_POINTER;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Strip out the NBSPs at the ends
|
|
|
|
while ((res.mBegin <= res.mEnd) && (IS_NBSP_CHAR(str[res.mBegin])))
|
|
|
|
res.mBegin++;
|
|
|
|
if (str[res.mEnd] == (unsigned char)0x20)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t realEndWord = res.mEnd - 1;
|
2007-03-22 10:30:00 -07:00
|
|
|
while ((realEndWord > res.mBegin) && (IS_NBSP_CHAR(str[realEndWord])))
|
|
|
|
realEndWord--;
|
|
|
|
if (realEndWord < res.mEnd - 1)
|
|
|
|
res.mEnd = realEndWord + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have the string offsets for the beginning
|
|
|
|
// and end of the word, run through the offset table and
|
|
|
|
// convert them back into dom points.
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t i, lastIndex = aOffsetTable->Length() - 1;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
for (i=0; i <= lastIndex; i++)
|
|
|
|
{
|
2009-04-22 01:43:15 -07:00
|
|
|
entry = (*aOffsetTable)[i];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t strEndOffset = entry->mStrOffset + entry->mLength;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Check to see if res.mBegin is within the range covered
|
|
|
|
// by this entry. Note that if res.mBegin is after the last
|
|
|
|
// character covered by this entry, we will use the next
|
|
|
|
// entry if there is one.
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
if (uint32_t(entry->mStrOffset) <= res.mBegin &&
|
|
|
|
(res.mBegin < uint32_t(strEndOffset) ||
|
|
|
|
(res.mBegin == uint32_t(strEndOffset) && i == lastIndex)))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (aWordStartNode)
|
|
|
|
{
|
|
|
|
*aWordStartNode = entry->mNode;
|
|
|
|
NS_IF_ADDREF(*aWordStartNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aWordStartOffset)
|
|
|
|
*aWordStartOffset = entry->mNodeOffset + res.mBegin - entry->mStrOffset;
|
|
|
|
|
|
|
|
if (!aWordEndNode && !aWordEndOffset)
|
|
|
|
{
|
|
|
|
// We've found our start entry, but if we're not looking
|
|
|
|
// for end entries, we're done.
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if res.mEnd is within the range covered
|
|
|
|
// by this entry.
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
if (uint32_t(entry->mStrOffset) <= res.mEnd && res.mEnd <= uint32_t(strEndOffset))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
if (res.mBegin == res.mEnd && res.mEnd == uint32_t(strEndOffset) && i != lastIndex)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Wait for the next round so that we use the same entry
|
|
|
|
// we did for aWordStartNode.
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aWordEndNode)
|
|
|
|
{
|
|
|
|
*aWordEndNode = entry->mNode;
|
|
|
|
NS_IF_ADDREF(*aWordEndNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aWordEndOffset)
|
|
|
|
*aWordEndOffset = entry->mNodeOffset + res.mEnd - entry->mStrOffset;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-07 21:20:23 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextServicesDocument::WillInsertNode(nsIDOMNode *aNode,
|
|
|
|
nsIDOMNode *aParent,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aPosition)
|
2010-06-07 21:20:23 -07:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextServicesDocument::WillDeleteNode(nsIDOMNode *aChild)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextServicesDocument::WillSplitNode(nsIDOMNode *aExistingRightNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aOffset)
|
2010-06-07 21:20:23 -07:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextServicesDocument::WillJoinNodes(nsIDOMNode *aLeftNode,
|
|
|
|
nsIDOMNode *aRightNode,
|
|
|
|
nsIDOMNode *aParent)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -------------------------------
|
|
|
|
// stubs for unused listen methods
|
|
|
|
// -------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTextServicesDocument::WillCreateNode(const nsAString& aTag, nsIDOMNode *aParent, int32_t aPosition)
|
2010-06-07 21:20:23 -07:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTextServicesDocument::DidCreateNode(const nsAString& aTag, nsIDOMNode *aNode, nsIDOMNode *aParent, int32_t aPosition, nsresult aResult)
|
2010-06-07 21:20:23 -07:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTextServicesDocument::WillInsertText(nsIDOMCharacterData *aTextNode, int32_t aOffset, const nsAString &aString)
|
2010-06-07 21:20:23 -07:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTextServicesDocument::DidInsertText(nsIDOMCharacterData *aTextNode, int32_t aOffset, const nsAString &aString, nsresult aResult)
|
2010-06-07 21:20:23 -07:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTextServicesDocument::WillDeleteText(nsIDOMCharacterData *aTextNode, int32_t aOffset, int32_t aLength)
|
2010-06-07 21:20:23 -07:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTextServicesDocument::DidDeleteText(nsIDOMCharacterData *aTextNode, int32_t aOffset, int32_t aLength, nsresult aResult)
|
2010-06-07 21:20:23 -07:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextServicesDocument::WillDeleteSelection(nsISelection *aSelection)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextServicesDocument::DidDeleteSelection(nsISelection *aSelection)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|