1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/MPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is the guts of the find algorithm.
17
* The Initial Developer of the Original Code is Akkana Peck.
19
* Portions created by the Initial Developer are Copyright (C) 2002
20
* the Initial Developer. All Rights Reserved.
23
* Akkana Peck <akkana@netscape.com>
24
* Roger B. Sidje <rbs@maths.uq.edu.au> (added find in <textarea> & text <input>)
26
* Alternatively, the contents of this file may be used under the terms of
27
* either the GNU General Public License Version 2 or later (the "GPL"), or
28
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29
* in which case the provisions of the GPL or the LGPL are applicable instead
30
* of those above. If you wish to allow use of your version of this file only
31
* under the terms of either the GPL or the LGPL, and not to allow others to
32
* use your version of this file under the terms of the MPL, indicate your
33
* decision by deleting the provisions above and replace them with the notice
34
* and other provisions required by the GPL or the LGPL. If you do not delete
35
* the provisions above, a recipient may use your version of this file under
36
* the terms of any one of the MPL, the GPL or the LGPL.
38
* ***** END LICENSE BLOCK ***** */
40
//#define DEBUG_FIND 1
43
#include "nsContentCID.h"
44
#include "nsIEnumerator.h"
45
#include "nsITextContent.h"
46
#include "nsIDOMNode.h"
47
#include "nsIDOMNodeList.h"
48
#include "nsIDOMDocumentRange.h"
49
#include "nsIDOMDocumentTraversal.h"
50
#include "nsISelection.h"
51
#include "nsISelectionController.h"
52
#include "nsIPresShell.h"
54
#include "nsITextControlFrame.h"
55
#include "nsIFormControl.h"
56
#include "nsIEditor.h"
57
#include "nsIPlaintextEditor.h"
58
#include "nsIDocument.h"
59
#include "nsTextFragment.h"
62
#include "nsParserCIID.h"
63
#include "nsIServiceManagerUtils.h"
64
#include "nsUnicharUtils.h"
65
#include "nsIDOMElement.h"
68
// Yikes! Casting a char to unichar can fill with ones!
69
#define CHAR_TO_UNICHAR(c) ((PRUnichar)(const unsigned char)c)
71
static NS_DEFINE_CID(kCContentIteratorCID, NS_CONTENTITERATOR_CID);
72
static NS_DEFINE_CID(kCPreContentIteratorCID, NS_PRECONTENTITERATOR_CID);
73
static NS_DEFINE_CID(kParserServiceCID, NS_PARSERSERVICE_CID);
74
static NS_DEFINE_IID(kRangeCID, NS_RANGE_CID);
76
// -----------------------------------------------------------------------
77
// nsFindContentIterator is a special iterator that also goes through
78
// any existing <textarea>'s or text <input>'s editor to lookup the
79
// anonymous DOM content there.
82
// 1) We use two iterators: The "outer-iterator" goes through the
83
// normal DOM. The "inner-iterator" goes through the anonymous DOM
86
// 2) [MaybeSetupInnerIterator] As soon as the outer-iterator's current
87
// node is changed, a check is made to see if the node is a <textarea> or
88
// a text <input> node. If so, an inner-iterator is created to lookup the
89
// anynomous contents of the editor underneath the text control.
91
// 3) When the inner-iterator is created, we position the outer-iterator
92
// 'after' (or 'before' in backward search) the text control to avoid
93
// revisiting that control.
95
// 4) As a consequence of searching through text controls, we can be
96
// called via FindNext with the current selection inside a <textarea>
97
// or a text <input>. This means that we can be given an initial search
98
// range that stretches across the anonymous DOM and the normal DOM. To
99
// cater for this situation, we split the anonymous part into the
100
// inner-iterator and then reposition the outer-iterator outside.
102
// 5) The implementation assumes that First() and Next() are only called
103
// in find-forward mode, while Last() and Prev() are used in find-backward.
105
class nsFindContentIterator : public nsIContentIterator
108
nsFindContentIterator(PRBool aFindBackward)
109
: mOuterIterator(nsnull)
110
, mInnerIterator(nsnull)
112
, mStartOuterNode(nsnull)
113
, mEndOuterNode(nsnull)
114
, mFindBackward(aFindBackward)
118
virtual ~nsFindContentIterator()
125
// nsIContentIterator
126
virtual nsresult Init(nsIContent* aRoot)
128
NS_NOTREACHED("internal error");
129
return NS_ERROR_NOT_IMPLEMENTED;
131
virtual nsresult Init(nsIDOMRange* aRange);
132
virtual void First();
136
virtual nsIContent* GetCurrentNode();
137
virtual PRBool IsDone();
138
virtual nsresult PositionAt(nsIContent* aCurNode);
141
nsCOMPtr<nsIContentIterator> mOuterIterator;
142
nsCOMPtr<nsIContentIterator> mInnerIterator;
143
nsCOMPtr<nsIDOMRange> mRange;
144
nsCOMPtr<nsIDOMNode> mStartOuterNode;
145
nsCOMPtr<nsIDOMNode> mEndOuterNode;
146
PRBool mFindBackward;
149
void MaybeSetupInnerIterator();
150
void SetupInnerIterator(nsIContent* aContent);
153
NS_IMPL_ISUPPORTS1(nsFindContentIterator, nsIContentIterator)
156
nsFindContentIterator::Init(nsIDOMRange* aRange)
158
if (!mOuterIterator) {
160
// Use post-order in the reverse case, so we get parents
161
// before children in case we want to prevent descending
163
mOuterIterator = do_CreateInstance(kCContentIteratorCID);
166
// Use pre-order in the forward case, so we get parents
167
// before children in case we want to prevent descending
169
mOuterIterator = do_CreateInstance(kCPreContentIteratorCID);
171
NS_ENSURE_ARG_POINTER(mOuterIterator);
174
// mRange is the search range that we will examine
175
return aRange->CloneRange(getter_AddRefs(mRange));
179
nsFindContentIterator::First()
185
nsFindContentIterator::Last()
191
nsFindContentIterator::Next()
193
if (mInnerIterator) {
194
mInnerIterator->Next();
195
if (!mInnerIterator->IsDone())
198
// by construction, mOuterIterator is already on the next node
201
mOuterIterator->Next();
203
MaybeSetupInnerIterator();
207
nsFindContentIterator::Prev()
209
if (mInnerIterator) {
210
mInnerIterator->Prev();
211
if (!mInnerIterator->IsDone())
214
// by construction, mOuterIterator is already on the previous node
217
mOuterIterator->Prev();
219
MaybeSetupInnerIterator();
223
nsFindContentIterator::GetCurrentNode()
225
if (mInnerIterator && !mInnerIterator->IsDone()) {
226
return mInnerIterator->GetCurrentNode();
228
return mOuterIterator->GetCurrentNode();
232
nsFindContentIterator::IsDone() {
233
if (mInnerIterator && !mInnerIterator->IsDone()) {
236
return mOuterIterator->IsDone();
240
nsFindContentIterator::PositionAt(nsIContent* aCurNode)
242
nsIContent* oldNode = mOuterIterator->GetCurrentNode();
243
nsresult rv = mOuterIterator->PositionAt(aCurNode);
244
if (NS_SUCCEEDED(rv)) {
245
MaybeSetupInnerIterator();
248
mOuterIterator->PositionAt(oldNode);
250
rv = mInnerIterator->PositionAt(aCurNode);
256
nsFindContentIterator::Reset()
258
mInnerIterator = nsnull;
259
mStartOuterNode = nsnull;
260
mEndOuterNode = nsnull;
262
// As a consequence of searching through text controls, we may have been
263
// initialized with a selection inside a <textarea> or a text <input>.
265
// see if the start node is an anonymous text node inside a text control
266
nsCOMPtr<nsIDOMNode> startNode;
267
mRange->GetStartContainer(getter_AddRefs(startNode));
268
nsCOMPtr<nsIContent> startContent(do_QueryInterface(startNode));
269
for ( ; startContent; startContent = startContent->GetParent()) {
270
if (!startContent->IsNativeAnonymous()) {
271
mStartOuterNode = do_QueryInterface(startContent);
276
// see if the end node is an anonymous text node inside a text control
277
nsCOMPtr<nsIDOMNode> endNode;
278
mRange->GetEndContainer(getter_AddRefs(endNode));
279
nsCOMPtr<nsIContent> endContent(do_QueryInterface(endNode));
280
for ( ; endContent; endContent = endContent->GetParent()) {
281
if (!endContent->IsNativeAnonymous()) {
282
mEndOuterNode = do_QueryInterface(endContent);
287
mOuterIterator->Init(mRange);
289
if (!mFindBackward) {
290
if (mStartOuterNode != startNode) {
291
// the start node was an anonymous text node
292
SetupInnerIterator(startContent);
294
mInnerIterator->First();
296
mOuterIterator->First();
299
if (mEndOuterNode != endNode) {
300
// the end node was an anonymous text node
301
SetupInnerIterator(endContent);
303
mInnerIterator->Last();
305
mOuterIterator->Last();
308
// if we didn't create an inner-iterator, the boundary node could still be
309
// a text control, in which case we also need an inner-iterator straightaway
310
if (!mInnerIterator) {
311
MaybeSetupInnerIterator();
316
nsFindContentIterator::MaybeSetupInnerIterator()
318
mInnerIterator = nsnull;
320
nsIContent* content = mOuterIterator->GetCurrentNode();
321
if (!content || !content->IsContentOfType(nsIContent::eHTML_FORM_CONTROL))
324
nsCOMPtr<nsIFormControl> formControl(do_QueryInterface(content));
325
PRInt32 controlType = formControl->GetType();
326
if (controlType != NS_FORM_TEXTAREA &&
327
controlType != NS_FORM_INPUT_TEXT)
330
SetupInnerIterator(content);
331
if (mInnerIterator) {
332
if (!mFindBackward) {
333
mInnerIterator->First();
334
// finish setup: position mOuterIterator on the actual "next"
335
// node (this completes its re-init, @see SetupInnerIterator)
336
mOuterIterator->First();
339
mInnerIterator->Last();
340
// finish setup: position mOuterIterator on the actual "previous"
341
// node (this completes its re-init, @see SetupInnerIterator)
342
mOuterIterator->Last();
348
nsFindContentIterator::SetupInnerIterator(nsIContent* aContent)
350
NS_ASSERTION(aContent && !aContent->IsNativeAnonymous(), "invalid call");
352
nsIDocument* doc = aContent->GetDocument();
353
nsIPresShell* shell = doc ? doc->GetShellAt(0) : nsnull;
357
nsIFrame* frame = nsnull;
358
shell->GetPrimaryFrameFor(aContent, &frame);
362
nsITextControlFrame* tcFrame = nsnull;
363
CallQueryInterface(frame, &tcFrame);
367
nsCOMPtr<nsIEditor> editor;
368
tcFrame->GetEditor(getter_AddRefs(editor));
372
// don't mess with disabled input fields
373
PRUint32 editorFlags = 0;
374
editor->GetFlags(&editorFlags);
375
if (editorFlags & nsIPlaintextEditor::eEditorDisabledMask)
378
nsCOMPtr<nsIDOMElement> rootElement;
379
editor->GetRootElement(getter_AddRefs(rootElement));
380
nsCOMPtr<nsIContent> rootContent(do_QueryInterface(rootElement));
382
// now create the inner-iterator
383
mInnerIterator = do_CreateInstance(kCPreContentIteratorCID);
385
if (mInnerIterator) {
386
nsCOMPtr<nsIDOMNode> node(do_QueryInterface(rootContent));
387
nsCOMPtr<nsIDOMRange> range(do_CreateInstance(kRangeCID));
388
range->SelectNodeContents(node);
390
// fix up the inner bounds, we may have to only lookup a portion
391
// of the text control if the current node is a boundary point
393
nsCOMPtr<nsIDOMNode> outerNode(do_QueryInterface(aContent));
394
if (outerNode == mStartOuterNode) {
395
mRange->GetStartOffset(&offset);
396
mRange->GetStartContainer(getter_AddRefs(node));
397
range->SetStart(node, offset);
399
if (outerNode == mEndOuterNode) {
400
mRange->GetEndOffset(&offset);
401
mRange->GetEndContainer(getter_AddRefs(node));
402
range->SetEnd(node, offset);
404
// Note: we just init here. We do First() or Last() later.
405
mInnerIterator->Init(range);
407
// make sure to place the outer-iterator outside
408
// the text control so that we don't go there again.
410
mRange->CloneRange(getter_AddRefs(range));
411
if (!mFindBackward) { // find forward
412
// cut the outer-iterator after the current node
413
res = range->SetStartAfter(outerNode);
415
else { // find backward
416
// cut the outer-iterator before the current node
417
res = range->SetEndBefore(outerNode);
419
if (NS_FAILED(res)) {
420
// we are done with the outer-iterator, the
421
// inner-iterator will traverse what we want
422
range->Collapse(PR_TRUE);
424
// Note: we just re-init here, using the segment of mRange that is
425
// yet to be visited. Thus when we later do mOuterIterator->First()
426
// [or mOuterIterator->Last()], we will effectively be on the next
427
// node [or the previous node] _with respect to_ mRange.
428
mOuterIterator->Init(range);
433
NS_NewFindContentIterator(PRBool aFindBackward,
434
nsIContentIterator** aResult)
436
NS_ENSURE_ARG_POINTER(aResult);
438
return NS_ERROR_NULL_POINTER;
441
nsFindContentIterator* it = new nsFindContentIterator(aFindBackward);
443
return NS_ERROR_OUT_OF_MEMORY;
445
return it->QueryInterface(NS_GET_IID(nsIContentIterator), (void **)aResult);
447
// --------------------------------------------------------------------
449
// Sure would be nice if we could just get these from somewhere else!
450
PRInt32 nsFind::sInstanceCount = 0;
451
nsIAtom* nsFind::sTextAtom = nsnull;
452
nsIAtom* nsFind::sImgAtom = nsnull;
453
nsIAtom* nsFind::sHRAtom = nsnull;
454
nsIAtom* nsFind::sCommentAtom = nsnull;
455
nsIAtom* nsFind::sScriptAtom = nsnull;
456
nsIAtom* nsFind::sNoframesAtom = nsnull;
457
nsIAtom* nsFind::sSelectAtom = nsnull;
458
nsIAtom* nsFind::sTextareaAtom = nsnull;
459
nsIAtom* nsFind::sThAtom = nsnull;
460
nsIAtom* nsFind::sTdAtom = nsnull;
462
NS_IMPL_ISUPPORTS1(nsFind, nsIFind)
465
: mFindBackward(PR_FALSE)
466
, mCaseSensitive(PR_FALSE)
469
// Initialize the atoms if they aren't already:
470
if (sInstanceCount <= 0)
472
sTextAtom = NS_NewAtom("__moz_text");
473
sImgAtom = NS_NewAtom("img");
474
sHRAtom = NS_NewAtom("hr");
475
sCommentAtom = NS_NewAtom("__moz_comment");
476
sScriptAtom = NS_NewAtom("script");
477
sNoframesAtom = NS_NewAtom("noframes");
478
sSelectAtom = NS_NewAtom("select");
479
sTextareaAtom = NS_NewAtom("textarea");
480
sThAtom = NS_NewAtom("th");
481
sTdAtom = NS_NewAtom("td");
488
if (sInstanceCount <= 1)
490
NS_IF_RELEASE(sTextAtom);
491
NS_IF_RELEASE(sImgAtom);
492
NS_IF_RELEASE(sHRAtom);
493
NS_IF_RELEASE(sCommentAtom);
494
NS_IF_RELEASE(sScriptAtom);
495
NS_IF_RELEASE(sNoframesAtom);
496
NS_IF_RELEASE(sSelectAtom);
497
NS_IF_RELEASE(sTextareaAtom);
498
NS_IF_RELEASE(sThAtom);
499
NS_IF_RELEASE(sTdAtom);
505
static void DumpNode(nsIDOMNode* aNode)
509
printf(">>>> Node: NULL\n");
512
nsAutoString nodeName;
513
aNode->GetNodeName(nodeName);
514
nsCOMPtr<nsITextContent> textContent (do_QueryInterface(aNode));
517
nsAutoString newText;
518
textContent->CopyText(newText);
519
printf(">>>> Text node (node name %s): '%s'\n",
520
NS_LossyConvertUCS2toASCII(nodeName).get(),
521
NS_LossyConvertUCS2toASCII(newText).get());
524
printf(">>>> Node: %s\n", NS_LossyConvertUCS2toASCII(nodeName).get());
527
static void DumpRange(nsIDOMRange* aRange)
529
nsCOMPtr<nsIDOMNode> startN;
530
nsCOMPtr<nsIDOMNode> endN;
531
PRInt32 startO, endO;
532
aRange->GetStartContainer(getter_AddRefs(startN));
533
aRange->GetStartOffset(&startO);
534
aRange->GetEndContainer(getter_AddRefs(endN));
535
aRange->GetEndOffset(&endO);
536
printf(" -- start %d, ", startO); DumpNode(startN);
537
printf(" -- end %d, ", endO); DumpNode(endN);
542
nsFind::InitIterator(nsIDOMRange* aSearchRange)
547
rv = NS_NewFindContentIterator(mFindBackward, getter_AddRefs(mIterator));
548
NS_ENSURE_SUCCESS(rv, rv);
549
NS_ENSURE_ARG_POINTER(mIterator);
552
NS_ENSURE_ARG_POINTER(aSearchRange);
555
printf("InitIterator search range:\n"); DumpRange(aSearchRange);
558
rv = mIterator->Init(aSearchRange);
559
NS_ENSURE_SUCCESS(rv, rv);
569
/* attribute boolean findBackward; */
571
nsFind::GetFindBackwards(PRBool *aFindBackward)
574
return NS_ERROR_NULL_POINTER;
576
*aFindBackward = mFindBackward;
580
nsFind::SetFindBackwards(PRBool aFindBackward)
582
mFindBackward = aFindBackward;
586
/* attribute boolean caseSensitive; */
588
nsFind::GetCaseSensitive(PRBool *aCaseSensitive)
591
return NS_ERROR_NULL_POINTER;
593
*aCaseSensitive = mCaseSensitive;
597
nsFind::SetCaseSensitive(PRBool aCaseSensitive)
599
mCaseSensitive = aCaseSensitive;
604
nsFind::GetWordBreaker(nsIWordBreaker** aWordBreaker)
606
*aWordBreaker = mWordBreaker;
607
NS_IF_ADDREF(*aWordBreaker);
612
nsFind::SetWordBreaker(nsIWordBreaker* aWordBreaker)
614
mWordBreaker = aWordBreaker;
619
// Here begins the find code.
620
// A ten-thousand-foot view of how it works:
621
// Find needs to be able to compare across inline (but not block) nodes,
622
// e.g. find for "abc" should match a<b>b</b>c.
623
// So after we've searched a node, we're not done with it;
624
// in the case of a partial match we may need to reset the
625
// iterator to go back to a previously visited node,
626
// so we always save the "match anchor" node and offset.
628
// Text nodes store their text in an nsTextFragment, which is
629
// effectively a union of a one-byte string or a two-byte string.
630
// Single and double strings are intermixed in the dom.
631
// We don't have string classes which can deal with intermixed strings,
632
// so all the handling is done explicitly here.
636
nsFind::NextNode(nsIDOMRange* aSearchRange,
637
nsIDOMRange* aStartPoint, nsIDOMRange* aEndPoint,
642
nsIContent *content = nsnull;
643
nsCOMPtr<nsITextContent> tc;
645
if (!mIterator || aContinueOk)
647
// If we are continuing, that means we have a match in progress.
648
// In that case, we want to continue from the end point
649
// (where we are now) to the beginning/end of the search range.
650
nsCOMPtr<nsIDOMRange> newRange (do_CreateInstance(kRangeCID));
654
printf("Match in progress: continuing past endpoint\n");
656
nsCOMPtr<nsIDOMNode> startNode;
657
nsCOMPtr<nsIDOMNode> endNode;
658
PRInt32 startOffset, endOffset;
660
aSearchRange->GetStartContainer(getter_AddRefs(startNode));
661
aSearchRange->GetStartOffset(&startOffset);
662
aEndPoint->GetStartContainer(getter_AddRefs(endNode));
663
aEndPoint->GetStartOffset(&endOffset);
665
aEndPoint->GetEndContainer(getter_AddRefs(startNode));
666
aEndPoint->GetEndOffset(&startOffset);
667
aSearchRange->GetEndContainer(getter_AddRefs(endNode));
668
aSearchRange->GetEndOffset(&endOffset);
670
newRange->SetStart(startNode, startOffset);
671
newRange->SetEnd(endNode, endOffset);
673
else // Normal, not continuing
675
nsCOMPtr<nsIDOMNode> startNode;
676
nsCOMPtr<nsIDOMNode> endNode;
677
PRInt32 startOffset, endOffset;
679
aSearchRange->GetStartContainer(getter_AddRefs(startNode));
680
aSearchRange->GetStartOffset(&startOffset);
681
aStartPoint->GetEndContainer(getter_AddRefs(endNode));
682
aStartPoint->GetEndOffset(&endOffset);
684
// Problem with this approach: if there is a match which starts
685
// just before the current selection and continues into the selection,
686
// we will miss it, because our search algorithm only starts
687
// searching from the end of the word, so we would have to
688
// search the current selection but discount any matches
689
// that fall entirely inside it.
691
aStartPoint->GetStartContainer(getter_AddRefs(startNode));
692
aStartPoint->GetStartOffset(&startOffset);
693
aEndPoint->GetEndContainer(getter_AddRefs(endNode));
694
aEndPoint->GetEndOffset(&endOffset);
696
newRange->SetStart(startNode, startOffset);
697
newRange->SetEnd(endNode, endOffset);
700
rv = InitIterator(newRange);
701
NS_ENSURE_SUCCESS(rv, rv);
703
aStartPoint = aSearchRange;
705
content = mIterator->GetCurrentNode();
707
nsCOMPtr<nsIDOMNode> dnode (do_QueryInterface(content));
708
printf(":::::: Got the first node "); DumpNode(dnode);
710
tc = do_QueryInterface(content);
711
if (tc && !SkipNode(content))
713
mIterNode = do_QueryInterface(content);
714
// Also set mIterOffset if appropriate:
715
nsCOMPtr<nsIDOMNode> node;
717
aStartPoint->GetEndContainer(getter_AddRefs(node));
718
if (mIterNode.get() == node.get())
719
aStartPoint->GetEndOffset(&mIterOffset);
721
mIterOffset = -1; // sign to start from end
725
aStartPoint->GetStartContainer(getter_AddRefs(node));
726
if (mIterNode.get() == node.get())
727
aStartPoint->GetStartOffset(&mIterOffset);
732
printf("Setting initial offset to %d\n", mIterOffset);
745
content = mIterator->GetCurrentNode();
750
nsCOMPtr<nsIDOMNode> dnode (do_QueryInterface(content));
751
printf(":::::: Got another node "); DumpNode(dnode);
754
// If we ever cross a block node, we might want to reset
756
// we don't match patterns extending across block boundaries.
757
// But we can't depend on this test here now, because the iterator
758
// doesn't give us the parent going in and going out, and we
759
// need it both times to depend on this.
760
//if (IsBlockNode(content))
762
// Now see if we need to skip this node --
763
// e.g. is it part of a script or other invisible node?
764
// Note that we don't ask for CSS information;
765
// a node can be invisible due to CSS, and we'd still find it.
766
if (SkipNode(content))
769
tc = do_QueryInterface(content);
773
dnode = do_QueryInterface(content);
774
printf("Not a text node: "); DumpNode(dnode);
779
mIterNode = do_QueryInterface(content);
785
printf("Iterator gave: "); DumpNode(mIterNode);
790
PRBool nsFind::IsBlockNode(nsIContent* aContent)
792
if (!aContent->IsContentOfType(nsIContent::eHTML)) {
796
nsIAtom *atom = aContent->Tag();
798
if (atom == sImgAtom ||
804
if (!mParserService) {
805
mParserService = do_GetService(kParserServiceCID);
811
mParserService->HTMLAtomTagToId(atom, &id);
813
PRBool isBlock = PR_FALSE;
814
mParserService->IsBlock(id, isBlock);
818
PRBool nsFind::IsTextNode(nsIDOMNode* aNode)
820
// Can't just QI for nsITextContent, because nsCommentNode
821
// also implements that interface.
822
nsCOMPtr<nsIContent> content (do_QueryInterface(aNode));
824
return content && content->Tag() == sTextAtom;
827
PRBool nsFind::IsVisibleNode(nsIDOMNode *aDOMNode)
829
nsCOMPtr<nsIContent> content(do_QueryInterface(aDOMNode));
833
nsCOMPtr<nsIDocument> doc = content->GetDocument();
837
nsIPresShell *presShell = doc->GetShellAt(0);
841
nsIFrame *frame = nsnull;
842
presShell->GetPrimaryFrameFor(content, &frame);
844
// No frame! Not visible then.
848
return frame->GetStyleVisibility()->IsVisible();
851
PRBool nsFind::SkipNode(nsIContent* aContent)
855
#ifdef HAVE_BIDI_ITERATOR
856
atom = aContent->Tag();
858
// We may not need to skip comment nodes,
859
// now that IsTextNode distinguishes them from real text nodes.
860
return (atom == sCommentAtom ||
861
(aContent->IsContentOfType(nsIContent::eHTML) &&
862
(atom == sScriptAtom ||
863
atom == sCommentAtom ||
864
atom == sNoframesAtom ||
865
atom == sSelectAtom)));
867
#else /* HAVE_BIDI_ITERATOR */
868
// Temporary: eventually we will have an iterator to do this,
869
// but for now, we have to climb up the tree for each node
870
// and see whether any parent is a skipped node,
871
// and take the performance hit.
873
nsIContent *content = aContent;
876
atom = content->Tag();
878
if (atom == sCommentAtom ||
879
(content->IsContentOfType(nsIContent::eHTML) &&
880
(atom == sScriptAtom ||
881
atom == sNoframesAtom ||
882
atom == sSelectAtom)))
885
printf("Skipping node: ");
886
nsCOMPtr<nsIDOMNode> node (do_QueryInterface(content));
893
// Only climb to the nearest block node
894
if (IsBlockNode(content))
897
content = content->GetParent();
901
#endif /* HAVE_BIDI_ITERATOR */
904
nsresult nsFind::GetBlockParent(nsIDOMNode* aNode, nsIDOMNode** aParent)
908
nsCOMPtr<nsIDOMNode> parent;
909
nsresult rv = aNode->GetParentNode(getter_AddRefs(parent));
910
NS_ENSURE_SUCCESS(rv, rv);
911
nsCOMPtr<nsIContent> content (do_QueryInterface(parent));
912
if (content && IsBlockNode(content))
920
return NS_ERROR_FAILURE;
923
// Call ResetAll before returning,
924
// to remove all references to external objects.
925
void nsFind::ResetAll()
928
mLastBlockParent = nsnull;
931
#define NBSP_CHARCODE (CHAR_TO_UNICHAR(160))
932
#define IsSpace(c) (nsCRT::IsAsciiSpace(c) || (c) == NBSP_CHARCODE)
933
#define OVERFLOW_PINDEX (mFindBackward ? pindex < 0 : pindex > patLen)
934
#define DONE_WITH_PINDEX (mFindBackward ? pindex <= 0 : pindex >= patLen)
935
#define ALMOST_DONE_WITH_PINDEX (mFindBackward ? pindex <= 0 : pindex >= patLen-1)
939
// Take nodes out of the tree with NextNode,
940
// until null (NextNode will return 0 at the end of our range).
943
nsFind::Find(const PRUnichar *aPatText, nsIDOMRange* aSearchRange,
944
nsIDOMRange* aStartPoint, nsIDOMRange* aEndPoint,
945
nsIDOMRange** aRangeRet)
948
printf("============== nsFind::Find('%s'%s, %p, %p, %p)\n",
949
NS_LossyConvertUCS2toASCII(aPatText).get(),
950
mFindBackward ? " (backward)" : " (forward)",
951
(void*)aSearchRange, (void*)aStartPoint, (void*)aEndPoint);
954
NS_ENSURE_ARG_POINTER(aRangeRet);
958
return NS_ERROR_NULL_POINTER;
962
nsAutoString patAutoStr(aPatText);
964
ToLowerCase(patAutoStr);
965
const PRUnichar* patStr = patAutoStr.get();
966
PRInt32 patLen = patAutoStr.Length() - 1;
968
// current offset into the pattern -- reset to beginning/end:
969
PRInt32 pindex = (mFindBackward ? patLen : 0);
971
// Current offset into the fragment
974
// Direction to move pindex and ptr*
975
int incr = (mFindBackward ? -1 : 1);
977
nsCOMPtr<nsITextContent> tc;
978
const nsTextFragment *frag = nsnull;
981
// Pointers into the current fragment:
982
const PRUnichar *t2b = nsnull;
983
const char *t1b = nsnull;
985
// Keep track of when we're in whitespace:
986
// (only matters when we're matching)
987
PRBool inWhitespace = PR_FALSE;
989
// Have we extended a search past the endpoint?
990
PRBool continuing = PR_FALSE;
992
// Place to save the range start point in case we find a match:
993
nsCOMPtr<nsIDOMNode> matchAnchorNode;
994
PRInt32 matchAnchorOffset = 0;
996
// Get the end point, so we know when to end searches:
997
nsCOMPtr<nsIDOMNode> endNode;
999
aEndPoint->GetEndContainer(getter_AddRefs(endNode));
1000
aEndPoint->GetEndOffset(&endOffset);
1005
printf("Loop ...\n");
1008
// If this is our first time on a new node, reset the pointers:
1013
NextNode(aSearchRange, aStartPoint, aEndPoint, PR_FALSE);
1014
if (!mIterNode) // Out of nodes
1016
// Are we in the middle of a match?
1017
// If so, try again with continuation.
1018
if (matchAnchorNode && !continuing)
1019
NextNode(aSearchRange, aStartPoint, aEndPoint, PR_TRUE);
1021
// Reset the iterator, so this nsFind will be usable if
1022
// the user wants to search again (from beginning/end).
1027
// We have a new text content. If its block parent is different
1028
// from the block parent of the last text content, then we
1029
// need to clear the match since we don't want to find
1030
// across block boundaries.
1031
nsCOMPtr<nsIDOMNode> blockParent;
1032
GetBlockParent(mIterNode, getter_AddRefs(blockParent));
1034
printf("New node: old blockparent = %p, new = %p\n",
1035
(void*)mLastBlockParent.get(), (void*)blockParent.get());
1037
if (blockParent != mLastBlockParent)
1040
printf("Different block parent!\n");
1042
mLastBlockParent = blockParent;
1043
// End any pending match:
1044
matchAnchorNode = nsnull;
1045
matchAnchorOffset = 0;
1046
pindex = (mFindBackward ? patLen : 0);
1049
// Get the text content:
1050
tc = do_QueryInterface(mIterNode);
1051
if (!tc) // Out of nodes
1054
mLastBlockParent = 0;
1059
nsresult rv = tc->GetText(&frag);
1060
if (NS_FAILED(rv)) continue;
1061
fragLen = frag->GetLength();
1063
// Set our starting point in this node.
1064
// If we're going back to the anchor node, which means that we
1065
// just ended a partial match, use the saved offset:
1066
if (mIterNode == matchAnchorNode)
1067
findex = matchAnchorOffset + (mFindBackward ? 1 : 0);
1069
// mIterOffset, if set, is the range's idea of an offset,
1070
// and points between characters. But when translated
1071
// to a string index, it points to a character. If we're
1072
// going backward, this is one character too late and
1073
// we'll match part of our previous pattern.
1074
else if (mIterOffset >= 0)
1075
findex = mIterOffset - (mFindBackward ? 1 : 0);
1077
// Otherwise, just start at the appropriate end of the fragment:
1078
else if (mFindBackward)
1079
findex = fragLen - 1;
1083
// Offset can only apply to the first node:
1086
// If this is outside the bounds of the string, then skip this node:
1087
if (findex < 0 || findex > fragLen-1)
1090
printf("At the end of a text node -- skipping to the next\n");
1097
printf("Starting from offset %d\n", findex);
1101
t2b = frag->Get2b();
1104
nsAutoString str2(t2b, fragLen);
1105
printf("2 byte, '%s'\n", NS_LossyConvertUCS2toASCII(str2).get());
1110
t1b = frag->Get1b();
1113
nsCAutoString str1(t1b, fragLen);
1114
printf("1 byte, '%s'\n", str1.get());
1118
else // still on the old node
1120
// Still on the old node. Advance the pointers,
1121
// then see if we need to pull a new node.
1124
printf("Same node -- (%d, %d)\n", pindex, findex);
1126
if (mFindBackward ? (findex < 0) : (findex >= fragLen))
1129
printf("Will need to pull a new node: mAO = %d, frag len=%d\n",
1130
matchAnchorOffset, fragLen);
1132
// Done with this node. Pull a new one.
1138
// Have we gone past the endpoint yet?
1139
// If we have, and we're not in the middle of a match, return.
1140
if (mIterNode == endNode && !continuing &&
1141
((mFindBackward && (findex < endOffset)) ||
1142
(!mFindBackward && (findex > endOffset))))
1148
// The two characters we'll be comparing:
1149
PRUnichar c = (t2b ? t2b[findex] : CHAR_TO_UNICHAR(t1b[findex]));
1150
PRUnichar patc = patStr[pindex];
1152
printf("Comparing '%c'=%x to '%c' (%d of %d), findex=%d%s\n",
1153
(char)c, (int)c, patc, pindex, patLen, findex,
1154
inWhitespace ? " (inWhitespace)" : "");
1157
// Do we need to go back to non-whitespace mode?
1158
// If inWhitespace, then this space in the pat str
1159
// has already matched at least one space in the document.
1160
if (inWhitespace && !IsSpace(c))
1162
inWhitespace = PR_FALSE;
1165
// This shouldn't happen -- if we were still matching, and we
1166
// were at the end of the pat string, then we should have
1167
// caught it in the last iteration and returned success.
1168
if (OVERFLOW_PINDEX)
1169
NS_ASSERTION(PR_FALSE, "Missed a whitespace match\n");
1171
patc = patStr[pindex];
1173
if (!inWhitespace && IsSpace(patc))
1174
inWhitespace = PR_TRUE;
1176
// convert to lower case if necessary
1177
else if (!inWhitespace && !mCaseSensitive && IsUpperCase(c))
1181
if ((c == patc) || (inWhitespace && IsSpace(c)))
1185
printf("YES (whitespace)(%d of %d)\n", pindex, patLen);
1187
printf("YES! '%c' == '%c' (%d of %d)\n", c, patc, pindex, patLen);
1190
// Save the range anchors if we haven't already:
1191
if (!matchAnchorNode) {
1192
matchAnchorNode = mIterNode;
1193
matchAnchorOffset = findex;
1197
if (DONE_WITH_PINDEX)
1198
// Matched the whole string!
1201
printf("Found a match!\n");
1205
nsCOMPtr<nsIDOMNode> startParent;
1206
nsCOMPtr<nsIDOMNode> endParent;
1207
nsCOMPtr<nsIDOMRange> range (do_CreateInstance(kRangeCID));
1210
PRInt32 matchStartOffset, matchEndOffset;
1211
// convert char index to range point:
1212
PRInt32 mao = matchAnchorOffset + (mFindBackward ? 1 : 0);
1215
startParent = do_QueryInterface(tc);
1216
endParent = matchAnchorNode;
1217
matchStartOffset = findex;
1218
matchEndOffset = mao;
1222
startParent = matchAnchorNode;
1223
endParent = do_QueryInterface(tc);
1224
matchStartOffset = mao;
1225
matchEndOffset = findex+1;
1227
if (startParent && endParent &&
1228
IsVisibleNode(startParent) && IsVisibleNode(endParent))
1230
range->SetStart(startParent, matchStartOffset);
1231
range->SetEnd(endParent, matchEndOffset);
1232
*aRangeRet = range.get();
1233
NS_ADDREF(*aRangeRet);
1236
startParent = nsnull; // This match is no good -- invisible or bad range
1241
// If startParent == nsnull, we didn't successfully make range
1242
// or, we didn't make a range because the start or end node were invisible
1243
// Reset the offset to the other end of the found string:
1244
mIterOffset = findex + (mFindBackward ? 1 : 0);
1246
printf("mIterOffset = %d, mIterNode = ", mIterOffset);
1247
DumpNode(mIterNode);
1253
matchAnchorNode = nsnull; // This match is no good, continue on in document
1256
if (matchAnchorNode) {
1257
// Not done, but still matching.
1258
// Advance and loop around for the next characters.
1259
// But don't advance from a space to a non-space:
1260
if (!inWhitespace || DONE_WITH_PINDEX || IsSpace(patStr[pindex+incr]))
1263
inWhitespace = PR_FALSE;
1265
printf("Advancing pindex to %d\n", pindex);
1274
printf("NOT: %c == %c\n", c, patc);
1276
// If we were continuing, then this ends our search.
1282
// If we didn't match, go back to the beginning of patStr,
1283
// and set findex back to the next char after
1284
// we started the current match.
1285
if (matchAnchorNode) // we're ending a partial match
1287
findex = matchAnchorOffset;
1288
mIterOffset = matchAnchorOffset;
1289
// +incr will be added to findex when we continue
1291
// Are we going back to a previous node?
1292
if (matchAnchorNode != mIterNode)
1294
nsCOMPtr<nsIContent> content (do_QueryInterface(matchAnchorNode));
1295
nsresult rv = NS_ERROR_UNEXPECTED;
1297
rv = mIterator->PositionAt(content);
1299
NS_ASSERTION(NS_SUCCEEDED(rv), "Text content wasn't nsIContent!");
1301
printf("Repositioned anchor node\n");
1305
printf("Ending a partial match; findex -> %d, mIterOffset -> %d\n",
1306
findex, mIterOffset);
1309
matchAnchorNode = nsnull;
1310
matchAnchorOffset = 0;
1311
inWhitespace = PR_FALSE;
1312
pindex = (mFindBackward ? patLen : 0);
1314
printf("Setting findex back to %d, pindex to %d\n", findex, pindex);
1319
// Out of nodes, and didn't match.