Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes
nsFind Class Reference

#include <nsFind.h>

Inheritance diagram for nsFind:
Inheritance graph
[legend]
Collaboration diagram for nsFind:
Collaboration graph
[legend]

List of all members.

Public Member Functions

NS_DECL_ISUPPORTS NS_DECL_NSIFIND nsFind ()
virtual ~nsFind ()
nsIDOMRange Find (in wstring aPatText, in nsIDOMRange aSearchRange, in nsIDOMRange aStartPoint, in nsIDOMRange aEndPoint)
 Find some text in the current context.

Public Attributes

attribute boolean findBackwards
attribute boolean caseSensitive
attribute nsIWordBreaker wordBreaker
 Use "find entire words" mode by setting to a word breaker or null, to disable "entire words" mode.

Protected Member Functions

nsresult GetBlockParent (nsIDOMNode *aNode, nsIDOMNode **aParent)
PRBool IsTextNode (nsIDOMNode *aNode)
PRBool IsBlockNode (nsIContent *aNode)
PRBool SkipNode (nsIContent *aNode)
PRBool IsVisibleNode (nsIDOMNode *aNode)
nsresult NextNode (nsIDOMRange *aSearchRange, nsIDOMRange *aStartPoint, nsIDOMRange *aEndPoint, PRBool aContinueOk)
void ResetAll ()
nsresult InitIterator (nsIDOMRange *aSearchRange)

Protected Attributes

PRPackedBool mFindBackward
PRPackedBool mCaseSensitive
nsCOMPtr< nsIWordBreakermWordBreaker
nsCOMPtr< nsIParserServicemParserService
PRInt32 mIterOffset
nsCOMPtr< nsIDOMNodemIterNode
nsCOMPtr< nsIDOMNodemLastBlockParent
nsCOMPtr< nsIContentIteratormIterator

Static Protected Attributes

static PRInt32 sInstanceCount = 0
static nsIAtomsImgAtom = nsnull
static nsIAtomsHRAtom = nsnull
static nsIAtomsScriptAtom = nsnull
static nsIAtomsNoframesAtom = nsnull
static nsIAtomsSelectAtom = nsnull
static nsIAtomsTextareaAtom = nsnull
static nsIAtomsThAtom = nsnull
static nsIAtomsTdAtom = nsnull

Detailed Description

Definition at line 59 of file nsFind.h.


Constructor & Destructor Documentation

Definition at line 461 of file nsFind.cpp.

  : mFindBackward(PR_FALSE)
  , mCaseSensitive(PR_FALSE)
  , mIterOffset(0)
{
  // Initialize the atoms if they aren't already:
  if (sInstanceCount <= 0)
  {
    sImgAtom = NS_NewAtom("img");
    sHRAtom = NS_NewAtom("hr");
    sScriptAtom = NS_NewAtom("script");
    sNoframesAtom = NS_NewAtom("noframes");
    sSelectAtom = NS_NewAtom("select");
    sTextareaAtom = NS_NewAtom("textarea");
    sThAtom = NS_NewAtom("th");
    sTdAtom = NS_NewAtom("td");
  }
  ++sInstanceCount;
}

Here is the call graph for this function:

nsFind::~nsFind ( ) [virtual]

Member Function Documentation

nsIDOMRange nsIFind::Find ( in wstring  aPatText,
in nsIDOMRange  aSearchRange,
in nsIDOMRange  aStartPoint,
in nsIDOMRange  aEndPoint 
) [inherited]

Find some text in the current context.

The implementation is responsible for performing the find and highlighting the text.

Parameters:
aPatTextThe text to search for.
aSearchRangeA Range specifying domain of search.
aStartPointA Range specifying search start point. If not collapsed, we'll start from end (forward) or start (backward). May be null; if so, we'll start at the start (forward) or end (back) of aSearchRange.
aEndPointA Range specifying search end point. If not collapsed, we'll end at end (forward) or start (backward). May be null; if so, we'll end at the end (forward) or start (back) of aSearchRange.
Return values:
Arange spanning the match that was found (or null).
nsresult nsFind::GetBlockParent ( nsIDOMNode aNode,
nsIDOMNode **  aParent 
) [protected]

Definition at line 893 of file nsFind.cpp.

{
  while (aNode)
  {
    nsCOMPtr<nsIDOMNode> parent;
    nsresult rv = aNode->GetParentNode(getter_AddRefs(parent));
    NS_ENSURE_SUCCESS(rv, rv);
    nsCOMPtr<nsIContent> content (do_QueryInterface(parent));
    if (content && IsBlockNode(content))
    {
      *aParent = parent;
      NS_ADDREF(*aParent);
      return NS_OK;
    }
    aNode = parent;
  }
  return NS_ERROR_FAILURE;
}

Here is the call graph for this function:

nsresult nsFind::InitIterator ( nsIDOMRange aSearchRange) [protected]

Definition at line 535 of file nsFind.cpp.

{
  nsresult rv;
  if (!mIterator)
  {
    rv = NS_NewFindContentIterator(mFindBackward, getter_AddRefs(mIterator));
    NS_ENSURE_SUCCESS(rv, rv);
    NS_ENSURE_ARG_POINTER(mIterator);
  }

  NS_ENSURE_ARG_POINTER(aSearchRange);

#ifdef DEBUG_FIND
  printf("InitIterator search range:\n"); DumpRange(aSearchRange);
#endif

  rv = mIterator->Init(aSearchRange);
  NS_ENSURE_SUCCESS(rv, rv);
  if (mFindBackward) {
    mIterator->Last();
  }
  else {
    mIterator->First();
  }
  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PRBool nsFind::IsBlockNode ( nsIContent aNode) [protected]

Definition at line 783 of file nsFind.cpp.

{
  if (!aContent->IsContentOfType(nsIContent::eHTML)) {
    return PR_FALSE;
  }

  nsIAtom *atom = aContent->Tag();

  if (atom == sImgAtom ||
      atom == sHRAtom ||
      atom == sThAtom ||
      atom == sTdAtom)
    return PR_TRUE;

  if (!mParserService) {
    mParserService = do_GetService(NS_PARSERSERVICE_CONTRACTID);
    if (!mParserService)
      return PR_FALSE;
  }

  PRBool isBlock = PR_FALSE;
  mParserService->IsBlock(mParserService->HTMLAtomTagToId(atom), isBlock);
  return isBlock;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PRBool nsFind::IsTextNode ( nsIDOMNode aNode) [protected]

Definition at line 808 of file nsFind.cpp.

{
  // Can't just QI for nsITextContent, because nsCommentNode
  // also implements that interface.
  nsCOMPtr<nsIContent> content (do_QueryInterface(aNode));

  return content && content->IsContentOfType(nsIContent::eTEXT);
}

Here is the call graph for this function:

PRBool nsFind::IsVisibleNode ( nsIDOMNode aNode) [protected]

Definition at line 817 of file nsFind.cpp.

{
  nsCOMPtr<nsIContent> content(do_QueryInterface(aDOMNode));
  if (!content)
    return PR_FALSE;

  nsCOMPtr<nsIDocument> doc = content->GetDocument();
  if (!doc)
    return PR_FALSE;

  nsIPresShell *presShell = doc->GetShellAt(0);
  if (!presShell)
    return PR_FALSE;

  nsIFrame *frame = nsnull;
  presShell->GetPrimaryFrameFor(content, &frame);
  if (!frame) {
    // No frame! Not visible then.
    return PR_FALSE;
  }

  return frame->GetStyleVisibility()->IsVisible();
}

Here is the call graph for this function:

nsresult nsFind::NextNode ( nsIDOMRange aSearchRange,
nsIDOMRange aStartPoint,
nsIDOMRange aEndPoint,
PRBool  aContinueOk 
) [protected]

Definition at line 629 of file nsFind.cpp.

{
  nsresult rv;

  nsIContent *content = nsnull;
  nsCOMPtr<nsITextContent> tc;

  if (!mIterator || aContinueOk)
  {
      // If we are continuing, that means we have a match in progress.
      // In that case, we want to continue from the end point
      // (where we are now) to the beginning/end of the search range.
    nsCOMPtr<nsIDOMRange> newRange (do_CreateInstance(kRangeCID));
    if (aContinueOk)
    {
#ifdef DEBUG_FIND
      printf("Match in progress: continuing past endpoint\n");
#endif
      nsCOMPtr<nsIDOMNode> startNode;
      nsCOMPtr<nsIDOMNode> endNode;
      PRInt32 startOffset, endOffset;
      if (mFindBackward) {
        aSearchRange->GetStartContainer(getter_AddRefs(startNode));
        aSearchRange->GetStartOffset(&startOffset);
        aEndPoint->GetStartContainer(getter_AddRefs(endNode));
        aEndPoint->GetStartOffset(&endOffset);
      } else {     // forward
        aEndPoint->GetEndContainer(getter_AddRefs(startNode));
        aEndPoint->GetEndOffset(&startOffset);
        aSearchRange->GetEndContainer(getter_AddRefs(endNode));
        aSearchRange->GetEndOffset(&endOffset);
      }
      newRange->SetStart(startNode, startOffset);
      newRange->SetEnd(endNode, endOffset);
    }
    else  // Normal, not continuing
    {
      nsCOMPtr<nsIDOMNode> startNode;
      nsCOMPtr<nsIDOMNode> endNode;
      PRInt32 startOffset, endOffset;
      if (mFindBackward) {
        aSearchRange->GetStartContainer(getter_AddRefs(startNode));
        aSearchRange->GetStartOffset(&startOffset);
        aStartPoint->GetEndContainer(getter_AddRefs(endNode));
        aStartPoint->GetEndOffset(&endOffset);
        // XXX Needs work:
        // Problem with this approach: if there is a match which starts
        // just before the current selection and continues into the selection,
        // we will miss it, because our search algorithm only starts
        // searching from the end of the word, so we would have to
        // search the current selection but discount any matches
        // that fall entirely inside it.
      } else {     // forward
        aStartPoint->GetStartContainer(getter_AddRefs(startNode));
        aStartPoint->GetStartOffset(&startOffset);
        aEndPoint->GetEndContainer(getter_AddRefs(endNode));
        aEndPoint->GetEndOffset(&endOffset);
      }
      newRange->SetStart(startNode, startOffset);
      newRange->SetEnd(endNode, endOffset);
    }

    rv = InitIterator(newRange);
    NS_ENSURE_SUCCESS(rv, rv);
    if (!aStartPoint)
      aStartPoint = aSearchRange;

    content = mIterator->GetCurrentNode();
#ifdef DEBUG_FIND
    nsCOMPtr<nsIDOMNode> dnode (do_QueryInterface(content));
    printf(":::::: Got the first node "); DumpNode(dnode);
#endif
    tc = do_QueryInterface(content);
    if (tc && !SkipNode(content))
    {
      mIterNode = do_QueryInterface(content);
      // Also set mIterOffset if appropriate:
      nsCOMPtr<nsIDOMNode> node;
      if (mFindBackward) {
        aStartPoint->GetEndContainer(getter_AddRefs(node));
        if (mIterNode.get() == node.get())
          aStartPoint->GetEndOffset(&mIterOffset);
        else
          mIterOffset = -1;   // sign to start from end
      }
      else
      {
        aStartPoint->GetStartContainer(getter_AddRefs(node));
        if (mIterNode.get() == node.get())
          aStartPoint->GetStartOffset(&mIterOffset);
        else
          mIterOffset = 0;
      }
#ifdef DEBUG_FIND
      printf("Setting initial offset to %d\n", mIterOffset);
#endif
      return NS_OK;
    }
  }

  while (1)
  {
    if (mFindBackward)
      mIterator->Prev();
    else
      mIterator->Next();

    content = mIterator->GetCurrentNode();
    if (!content)
      break;

#ifdef DEBUG_FIND
    nsCOMPtr<nsIDOMNode> dnode (do_QueryInterface(content));
    printf(":::::: Got another node "); DumpNode(dnode);
#endif

    // If we ever cross a block node, we might want to reset
    // the match anchor:
    // we don't match patterns extending across block boundaries.
    // But we can't depend on this test here now, because the iterator
    // doesn't give us the parent going in and going out, and we
    // need it both times to depend on this.
    //if (IsBlockNode(content))

    // Now see if we need to skip this node --
    // e.g. is it part of a script or other invisible node?
    // Note that we don't ask for CSS information;
    // a node can be invisible due to CSS, and we'd still find it.
    if (SkipNode(content))
      continue;

    tc = do_QueryInterface(content);
    if (tc)
      break;
#ifdef DEBUG_FIND
    dnode = do_QueryInterface(content);
    printf("Not a text node: "); DumpNode(dnode);
#endif
  }

  if (content)
    mIterNode = do_QueryInterface(content);
  else
    mIterNode = nsnull;
  mIterOffset = -1;

#ifdef DEBUG_FIND
  printf("Iterator gave: "); DumpNode(mIterNode);
#endif
  return NS_OK;
}

Here is the call graph for this function:

void nsFind::ResetAll ( ) [protected]

Definition at line 914 of file nsFind.cpp.

PRBool nsFind::SkipNode ( nsIContent aNode) [protected]

Definition at line 841 of file nsFind.cpp.

{
  nsIAtom *atom;

#ifdef HAVE_BIDI_ITERATOR
  atom = aContent->Tag();

  // We may not need to skip comment nodes,
  // now that IsTextNode distinguishes them from real text nodes.
  return (aContent->IsContentOfType(nsIContent::eCOMMENT) ||
          (aContent->IsContentOfType(nsIContent::eHTML) &&
           (atom == sScriptAtom ||
            atom == sNoframesAtom ||
            atom == sSelectAtom)));

#else /* HAVE_BIDI_ITERATOR */
  // Temporary: eventually we will have an iterator to do this,
  // but for now, we have to climb up the tree for each node
  // and see whether any parent is a skipped node,
  // and take the performance hit.

  nsIContent *content = aContent;
  while (content)
  {
    atom = content->Tag();

    if (aContent->IsContentOfType(nsIContent::eCOMMENT) ||
        (content->IsContentOfType(nsIContent::eHTML) &&
         (atom == sScriptAtom ||
          atom == sNoframesAtom ||
          atom == sSelectAtom)))
    {
#ifdef DEBUG_FIND
      printf("Skipping node: ");
      nsCOMPtr<nsIDOMNode> node (do_QueryInterface(content));
      DumpNode(node);
#endif

      return PR_TRUE;
    }

    // Only climb to the nearest block node
    if (IsBlockNode(content))
      return PR_FALSE;

    content = content->GetParent();
  }

  return PR_FALSE;
#endif /* HAVE_BIDI_ITERATOR */
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 48 of file nsIFind.idl.

Definition at line 47 of file nsIFind.idl.

Definition at line 85 of file nsFind.h.

Definition at line 84 of file nsFind.h.

Definition at line 113 of file nsFind.h.

Definition at line 91 of file nsFind.h.

Definition at line 90 of file nsFind.h.

Definition at line 94 of file nsFind.h.

Definition at line 88 of file nsFind.h.

Definition at line 87 of file nsFind.h.

nsIAtom * nsFind::sHRAtom = nsnull [static, protected]

Definition at line 73 of file nsFind.h.

nsIAtom * nsFind::sImgAtom = nsnull [static, protected]

Definition at line 72 of file nsFind.h.

PRInt32 nsFind::sInstanceCount = 0 [static, protected]

Definition at line 69 of file nsFind.h.

nsIAtom * nsFind::sNoframesAtom = nsnull [static, protected]

Definition at line 76 of file nsFind.h.

nsIAtom * nsFind::sScriptAtom = nsnull [static, protected]

Definition at line 75 of file nsFind.h.

nsIAtom * nsFind::sSelectAtom = nsnull [static, protected]

Definition at line 77 of file nsFind.h.

nsIAtom * nsFind::sTdAtom = nsnull [static, protected]

Definition at line 80 of file nsFind.h.

nsIAtom * nsFind::sTextareaAtom = nsnull [static, protected]

Definition at line 78 of file nsFind.h.

nsIAtom * nsFind::sThAtom = nsnull [static, protected]

Definition at line 79 of file nsFind.h.

Use "find entire words" mode by setting to a word breaker or null, to disable "entire words" mode.

Definition at line 54 of file nsIFind.idl.


The documentation for this class was generated from the following files: