Permalink
Browse files

prune fixes

  • Loading branch information...
1 parent 3feccd8 commit 0d2f6479877af28076773d43f434711b633b77a2 @parisman parisman committed May 29, 2009
Showing with 21 additions and 17 deletions.
  1. +1 −3 Classes/CSSPartMatcher.h
  2. +10 −14 Classes/CSSPartMatcher.m
  3. +1 −0 Test/CSSSelectorTest.m
  4. +1 −0 Test/ElementParserTest.h
  5. +8 −0 Test/ElementParserTest.m
@@ -33,17 +33,15 @@
*/
@interface CSSPartMatcher : NSObject {
CSSSelectorMatcher* selectorMatcher; // not retained
- Element* scopeElement;
Element* matchedElement;
int matchedPartIndex;
NSMutableArray* matchersForNextPart;
}
-@property (nonatomic, retain) Element* scopeElement;
@property (nonatomic, retain) Element* matchedElement;
@property int matchedPartIndex;
-(id)initWithElement:(Element*) anElement selectorMatcher:(CSSSelectorMatcher*)aSelectorMatcher;
--(void)pruneMatchesForElement:(Element*)anElement;
+//-(void)pruneMatchesForElement:(Element*)anElement;
-(BOOL)matchNextElement:(Element*) nextElement forIndex: (int) index;
@end
@@ -28,25 +28,24 @@
@implementation CSSPartMatcher
-@synthesize matchedElement, scopeElement, matchedPartIndex;
+@synthesize matchedElement, matchedPartIndex;
-(id)initWithElement:(Element*) anElement selectorMatcher:(CSSSelectorMatcher*)aSelectorMatcher{
self = [super init];
matchedElement = [anElement retain];
- scopeElement = [anElement retain];
selectorMatcher = aSelectorMatcher;
return self;
}
-(void)dealloc{
// NSLog(@"pruned: %@", [self description]);
[matchedElement release];
- [scopeElement release];
[matchersForNextPart release];
[super dealloc];
}
--(void)pruneMatchesForElement: (Element*)anElement{
+/* we don't do this yet...
+ -(void)pruneMatchesForElement: (Element*)anElement{
if (!matchersForNextPart) return;
for (CSSPartMatcher* match in matchersForNextPart){
if ([match scopeElement] == anElement)
@@ -55,13 +54,11 @@ -(void)pruneMatchesForElement: (Element*)anElement{
[match pruneMatchesForElement: anElement];
}
}
+*/
-(void)addNextMatch:(Element*)nextElement withIndex:(int)index{
CSSPartMatcher* nextMatch = [[CSSPartMatcher alloc] initWithElement: nextElement selectorMatcher: selectorMatcher];
nextMatch.matchedPartIndex = index;
- CSSVerb nextNextVerb = [[selectorMatcher selector] verbAfterIndex: index];
- if (nextNextVerb == CSSVerbSuccessor)
- [nextMatch setScopeElement: self.matchedElement];
if (!matchersForNextPart)
matchersForNextPart = [[NSMutableArray alloc] initWithCapacity: 4];
[matchersForNextPart addObject: nextMatch];
@@ -76,26 +73,25 @@ -(BOOL)matchNextElement:(Element*) nextElement forIndex: (int) index{
if (nextVerb == CSSVerbAny)
verbMatches = YES;
else if (nextVerb == CSSVerbDescendant)
- verbMatches = YES;//because we prune
+ verbMatches = [nextElement hasAncestor: self.matchedElement];//wasteful to not prune matches as they go out of scope
else if (nextVerb == CSSVerbChild)
verbMatches = nextElement.parent == self.matchedElement;
else if (nextVerb == CSSVerbSuccessor)
verbMatches = nextElement == self.matchedElement.nextSybling;
}
BOOL completeMatch = verbMatches && (index == [[selectorMatcher selector] countOfParts] - 1);
+
if (matchersForNextPart){
- for (CSSPartMatcher* match in matchersForNextPart)
+ for (CSSPartMatcher* match in matchersForNextPart){
completeMatch = completeMatch || [match matchNextElement: nextElement forIndex: index + 1];
+ }
}
- if (completeMatch)
- return YES;
-
- if (verbMatches)
+ if (!completeMatch && verbMatches)//actually part and verb match
[self addNextMatch: nextElement withIndex: index];
- return NO;
+ return completeMatch;
}
-(CSSSelectorPart*)matchedPart{
@@ -85,6 +85,7 @@ +(void)testCSSSelector{
[self assertWithCSSSelectorString: @"foo#ids > bar.huh + img[title]"];
}
+
+(void)testAll{
[self testCSSSelector];
}
@@ -28,6 +28,7 @@
@interface ElementParser (Test)
+(void)testElementParser;
++(void)testNestedMatches;
+(void)testAll;
@@ -73,6 +73,14 @@ +(void)testFeedPerf{
NSLog(@"%i runs processing feed: %f", runs, [NSDate timeIntervalSinceReferenceDate] - start);
}
++(void)testNestedMatches{
+ NSString* source = @"<html><body><div class='x'><a href='1' /></div><a href='2' /></body></html>";
+ ElementParser* parser = [[[ElementParser alloc] init] autorelease];
+ DocumentRoot* root = [parser parseHTML: source];
+ NSArray* result = [root selectElements: @"div.x a"];
+ assert([result count] == 1);
+}
+
+(void)testFeed{
NSString* file = [[NSBundle mainBundle] pathForResource: @"gizmodo" ofType: @"xml"];
NSString* source = [NSString stringWithContentsOfFile: file];

0 comments on commit 0d2f647

Please sign in to comment.