Skip to content
This repository
Browse code

prune fixes

  • Loading branch information...
commit 0d2f6479877af28076773d43f434711b633b77a2 1 parent 3feccd8
parisman authored May 28, 2009
4  Classes/CSSPartMatcher.h
@@ -33,17 +33,15 @@
33 33
  */
34 34
 @interface CSSPartMatcher : NSObject {
35 35
 	CSSSelectorMatcher* selectorMatcher; // not retained
36  
-	Element* scopeElement;
37 36
 	Element* matchedElement;
38 37
 	int matchedPartIndex;
39 38
 	NSMutableArray* matchersForNextPart;
40 39
 }
41  
-@property (nonatomic, retain) Element* scopeElement;
42 40
 @property (nonatomic, retain) Element* matchedElement;
43 41
 @property int matchedPartIndex;
44 42
 
45 43
 -(id)initWithElement:(Element*) anElement selectorMatcher:(CSSSelectorMatcher*)aSelectorMatcher;
46  
--(void)pruneMatchesForElement:(Element*)anElement;
  44
+//-(void)pruneMatchesForElement:(Element*)anElement;
47 45
 -(BOOL)matchNextElement:(Element*) nextElement forIndex: (int) index;
48 46
 
49 47
 @end
24  Classes/CSSPartMatcher.m
@@ -28,12 +28,11 @@
28 28
 
29 29
 @implementation CSSPartMatcher
30 30
 
31  
-@synthesize matchedElement, scopeElement, matchedPartIndex;
  31
+@synthesize matchedElement, matchedPartIndex;
32 32
 
33 33
 -(id)initWithElement:(Element*) anElement selectorMatcher:(CSSSelectorMatcher*)aSelectorMatcher{
34 34
 	self = [super init];
35 35
 	matchedElement = [anElement retain];
36  
-	scopeElement = [anElement retain];
37 36
 	selectorMatcher = aSelectorMatcher;
38 37
 	return self;
39 38
 }
@@ -41,12 +40,12 @@ -(id)initWithElement:(Element*) anElement selectorMatcher:(CSSSelectorMatcher*)a
41 40
 -(void)dealloc{
42 41
 //	NSLog(@"pruned: %@", [self description]);
43 42
 	[matchedElement release];
44  
-	[scopeElement release];
45 43
 	[matchersForNextPart release];
46 44
 	[super dealloc];
47 45
 }
48 46
 
49  
--(void)pruneMatchesForElement: (Element*)anElement{
  47
+/* we don't do this yet...
  48
+ -(void)pruneMatchesForElement: (Element*)anElement{
50 49
 	if (!matchersForNextPart) return;
51 50
 	for (CSSPartMatcher* match in matchersForNextPart){
52 51
 		if ([match scopeElement] == anElement)
@@ -55,13 +54,11 @@ -(void)pruneMatchesForElement: (Element*)anElement{
55 54
 			[match pruneMatchesForElement: anElement];
56 55
 	}
57 56
 }
  57
+*/
58 58
 
59 59
 -(void)addNextMatch:(Element*)nextElement withIndex:(int)index{
60 60
 	CSSPartMatcher* nextMatch = [[CSSPartMatcher alloc] initWithElement: nextElement selectorMatcher: selectorMatcher];
61 61
 	nextMatch.matchedPartIndex = index;
62  
-	CSSVerb nextNextVerb = [[selectorMatcher selector] verbAfterIndex: index];
63  
-	if (nextNextVerb == CSSVerbSuccessor)
64  
-		[nextMatch setScopeElement: self.matchedElement];
65 62
 	if (!matchersForNextPart) 
66 63
 		matchersForNextPart = [[NSMutableArray alloc] initWithCapacity: 4];
67 64
 	[matchersForNextPart addObject: nextMatch];
@@ -76,7 +73,7 @@ -(BOOL)matchNextElement:(Element*) nextElement forIndex: (int) index{
76 73
 		if (nextVerb == CSSVerbAny)
77 74
 			verbMatches = YES;
78 75
 		else if (nextVerb == CSSVerbDescendant)
79  
-			verbMatches = YES;//because we prune
  76
+			verbMatches = [nextElement hasAncestor: self.matchedElement];//wasteful to not prune matches as they go out of scope
80 77
 		else if (nextVerb == CSSVerbChild)
81 78
 			verbMatches = nextElement.parent == self.matchedElement; 
82 79
 		else if (nextVerb == CSSVerbSuccessor)
@@ -84,18 +81,17 @@ -(BOOL)matchNextElement:(Element*) nextElement forIndex: (int) index{
84 81
 	}
85 82
 	
86 83
 	BOOL completeMatch = verbMatches && (index == [[selectorMatcher selector] countOfParts] - 1);
  84
+	
87 85
 	if (matchersForNextPart){
88  
-		for (CSSPartMatcher* match in matchersForNextPart)
  86
+		for (CSSPartMatcher* match in matchersForNextPart){
89 87
 			completeMatch = completeMatch || [match matchNextElement: nextElement forIndex: index + 1];
  88
+		}
90 89
 	}
91 90
 
92  
-	if (completeMatch)
93  
-		return YES;
94  
-	
95  
-	if (verbMatches)
  91
+	if (!completeMatch && verbMatches)//actually part and verb match
96 92
 		[self addNextMatch: nextElement withIndex: index];
97 93
 
98  
-	return NO;
  94
+	return completeMatch;
99 95
 }
100 96
 
101 97
 -(CSSSelectorPart*)matchedPart{
1  Test/CSSSelectorTest.m
@@ -85,6 +85,7 @@ +(void)testCSSSelector{
85 85
 	[self assertWithCSSSelectorString: @"foo#ids > bar.huh + img[title]"];
86 86
 	
87 87
 }
  88
+
88 89
 +(void)testAll{
89 90
 	[self testCSSSelector];
90 91
 }
1  Test/ElementParserTest.h
@@ -28,6 +28,7 @@
28 28
 @interface ElementParser (Test)
29 29
 
30 30
 +(void)testElementParser;
  31
++(void)testNestedMatches;
31 32
 +(void)testAll;
32 33
 
33 34
 
8  Test/ElementParserTest.m
@@ -73,6 +73,14 @@ +(void)testFeedPerf{
73 73
 	NSLog(@"%i runs processing feed: %f", runs, [NSDate timeIntervalSinceReferenceDate] - start);	
74 74
 }
75 75
 
  76
++(void)testNestedMatches{
  77
+	NSString* source = @"<html><body><div class='x'><a href='1' /></div><a href='2' /></body></html>";
  78
+	ElementParser* parser = [[[ElementParser alloc] init] autorelease];
  79
+	DocumentRoot* root = [parser parseHTML: source];
  80
+	NSArray* result = [root selectElements: @"div.x a"];
  81
+	assert([result count] == 1);
  82
+}
  83
+
76 84
 +(void)testFeed{
77 85
 	NSString* file = [[NSBundle mainBundle] pathForResource: @"gizmodo" ofType: @"xml"];
78 86
 	NSString* source = [NSString stringWithContentsOfFile: file];

0 notes on commit 0d2f647

Please sign in to comment.
Something went wrong with that request. Please try again.