forked from laullon/gitx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchWebView.m
97 lines (85 loc) · 3.33 KB
/
SearchWebView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//
// SearchWebView.m
// GitX
//
// Created by German Laullon on 19/03/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "SearchWebView.h"
@implementation WebView (SearchWebView)
- (NSInteger)highlightAllOccurencesOfString:(NSString*)str inNode:(DOMNode *)_node
{
NSInteger count=0;
DOMDocument *document=[[self mainFrame] DOMDocument];
DOMNodeList *nodes=[_node childNodes];
DOMNode *node=[nodes item:0];
while(node!=nil){
if([node nodeType]==DOM_TEXT_NODE){
NSString *block;
if([[node nodeValue] rangeOfString:str options:NSCaseInsensitiveSearch].location!=NSNotFound){
NSScanner *scanner=[NSScanner scannerWithString:[node nodeValue]];
[scanner setCharactersToBeSkipped:nil];
[scanner setCaseSensitive:NO];
while([scanner scanUpToString:str intoString:&block]){
DOMNode *newNode=[document createTextNode:block];
[[node parentNode] appendChild:newNode];
while([scanner scanString:str intoString:&block]){
DOMElement *span=[document createElement:@"span"];
[span setAttribute:@"id" value:[NSString stringWithFormat:@"SWVHL_%d",count++]];
[span setAttribute:@"class" value:@"SWVHL"];
newNode=[document createTextNode:block];
[span appendChild:newNode];
[[node parentNode] appendChild:span];
}
}
[[node parentNode] removeChild:node];
}
}else if([node nodeType]==DOM_ELEMENT_NODE){
count+=[self highlightAllOccurencesOfString:str inNode:node];
}else{
DLog(@"--->%@",node);
}
node=[node nextSibling];
}
return count;
}
- (NSInteger)highlightAllOccurencesOfString:(NSString*)str
{
NSInteger count=0;
if([[[[self mainFrame] DOMDocument] documentElement] isKindOfClass:[DOMHTMLElement class]]){
DOMHTMLElement *dom=(DOMHTMLElement *)[[[self mainFrame] DOMDocument] documentElement];
if(![str isEqualToString:[dom getAttribute:@"searchStr"]]){
[self removeAllHighlights];
count=[self highlightAllOccurencesOfString:str inNode:dom];
}
}
return count;
}
- (void)removeAllHighlights:(DOMNode *)_node
{
DOMNode *node=[_node firstChild];
while(node!=nil){
if ([node nodeType]==DOM_ELEMENT_NODE) {
if ([[(DOMElement *)node getAttribute:@"class"] isEqualToString:@"SWVHL"]) {
DOMNode *txt=[node firstChild];
DOMNode *parent=[node parentNode];
[node removeChild:txt];
[parent insertBefore:txt refChild:node];
[parent removeChild:node];
[parent normalize];
[self removeAllHighlights:parent];
}else{
[self removeAllHighlights:node];
}
}
node=[node nextSibling];
}
}
- (void)removeAllHighlights
{
if([[[[self mainFrame] DOMDocument] documentElement] isKindOfClass:[DOMHTMLElement class]]){
DOMHTMLElement *dom=(DOMHTMLElement *)[[[self mainFrame] DOMDocument] documentElement];
[self removeAllHighlights:dom];
}
}
@end