Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 122 lines (106 sloc) 4.161 kB
c0965e3 @laullon basic search capability on FileVew
laullon authored
1 //
2 // SearchWebView.m
3 // GitX
4 //
5 // Created by German Laullon on 19/03/11.
6 // Copyright 2011 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "SearchWebView.h"
10
11 @implementation WebView (SearchWebView)
12
13 - (NSInteger)highlightAllOccurencesOfString:(NSString*)str inNode:(DOMNode *)_node
14 {
15 NSInteger count=0;
16 DOMDocument *document=[[self mainFrame] DOMDocument];
17
18 DOMNodeList *nodes=[_node childNodes];
19 DOMNode *node=[nodes item:0];
20 while(node!=nil){
21 if([node nodeType]==DOM_TEXT_NODE){
22 NSString *block;
23 if([[node nodeValue] rangeOfString:str options:NSCaseInsensitiveSearch].location!=NSNotFound){
24 NSScanner *scanner=[NSScanner scannerWithString:[node nodeValue]];
25 [scanner setCharactersToBeSkipped:nil];
26 [scanner setCaseSensitive:NO];
27 while([scanner scanUpToString:str intoString:&block]){
28 DOMNode *newNode=[document createTextNode:block];
29 [[node parentNode] appendChild:newNode];
30
31 while([scanner scanString:str intoString:&block]){
32 DOMElement *span=[document createElement:@"span"];
33 [span setAttribute:@"id" value:[NSString stringWithFormat:@"SWVHL_%d",count++]];
34 [span setAttribute:@"class" value:@"SWVHL"];
35 newNode=[document createTextNode:block];
36 [span appendChild:newNode];
37 [[node parentNode] appendChild:span];
38 }
39 }
40 [[node parentNode] removeChild:node];
41 }
42 }else if([node nodeType]==DOM_ELEMENT_NODE){
43 count+=[self highlightAllOccurencesOfString:str inNode:node];
44 }else{
656cb82 @laullon NSLog => Dlog (Log only on debug build)
laullon authored
45 DLog(@"--->%@",node);
c0965e3 @laullon basic search capability on FileVew
laullon authored
46 }
47 node=[node nextSibling];
48 }
49
50 return count;
51 }
52
e109d03 @laullon Better file view search
laullon authored
53 - (DOMRange *)highlightAllOccurencesOfString:(NSString*)str
c0965e3 @laullon basic search capability on FileVew
laullon authored
54 {
55 NSInteger count=0;
e109d03 @laullon Better file view search
laullon authored
56 DOMRange *res=nil;
c0965e3 @laullon basic search capability on FileVew
laullon authored
57
58 if([[[[self mainFrame] DOMDocument] documentElement] isKindOfClass:[DOMHTMLElement class]]){
59 DOMHTMLElement *dom=(DOMHTMLElement *)[[[self mainFrame] DOMDocument] documentElement];
60 if(![str isEqualToString:[dom getAttribute:@"searchStr"]]){
61 [self removeAllHighlights];
62 count=[self highlightAllOccurencesOfString:str inNode:dom];
e109d03 @laullon Better file view search
laullon authored
63 if(count>0){
64 [dom setAttribute:@"searchStr" value:str];
65 }
66 }
67 if([self searchFor:str direction:YES caseSensitive:NO wrap:YES]){
68 res=[self selectedDOMRange];
c0965e3 @laullon basic search capability on FileVew
laullon authored
69 }
70 }
71
e109d03 @laullon Better file view search
laullon authored
72 return res;
73 }
74
75 - (void)updateSearch:(NSSearchField *)sender
76 {
77 NSString *searchString = [sender stringValue];
78 DLog(@"searchString:%@",searchString);
79
80 DOMRange *selection;
81
82 if([searchString length]>0){
83 selection=[self highlightAllOccurencesOfString:searchString];
84 [[sender window] makeFirstResponder:sender];
85 if(selection!=nil)
86 [self setSelectedDOMRange:selection affinity:NSSelectionAffinityDownstream];
87 }else{
88 [self removeAllHighlights];
89 }
c0965e3 @laullon basic search capability on FileVew
laullon authored
90 }
91
92 - (void)removeAllHighlights:(DOMNode *)_node
93 {
94 DOMNode *node=[_node firstChild];
95 while(node!=nil){
96 if ([node nodeType]==DOM_ELEMENT_NODE) {
97 if ([[(DOMElement *)node getAttribute:@"class"] isEqualToString:@"SWVHL"]) {
98 DOMNode *txt=[node firstChild];
99 DOMNode *parent=[node parentNode];
100 [node removeChild:txt];
101 [parent insertBefore:txt refChild:node];
102 [parent removeChild:node];
103 [parent normalize];
104 [self removeAllHighlights:parent];
105 }else{
106 [self removeAllHighlights:node];
107 }
108 }
109 node=[node nextSibling];
110 }
111 }
112
113 - (void)removeAllHighlights
114 {
115 if([[[[self mainFrame] DOMDocument] documentElement] isKindOfClass:[DOMHTMLElement class]]){
116 DOMHTMLElement *dom=(DOMHTMLElement *)[[[self mainFrame] DOMDocument] documentElement];
117 [self removeAllHighlights:dom];
118 }
119 }
120
121 @end
Something went wrong with that request. Please try again.