Made namespace work properly
Still need to add dtd for xhtml, or it will choke on html entities
Showing
1 changed file
with
27 additions
and
11 deletions
@@ -10,7 +10,8 @@ | @@ -10,7 +10,8 @@ | ||
10 | #import "ASIWebPageRequest.h" | 10 | #import "ASIWebPageRequest.h" |
11 | #import "ASINetworkQueue.h" | 11 | #import "ASINetworkQueue.h" |
12 | 12 | ||
13 | -static xmlChar *xpathExpr = (xmlChar *)"//link/@href|//script/@src|//img/@src|//frame/@src|//iframe/@src|//*/@style"; | 13 | +static xmlChar *xpathExpr = (xmlChar *)"//xhtml:link/@href|//xhtml:script/@src|//xhtml:img/@src|//xhtml:frame/@src|//xhtml:iframe/@src|//*/@style"; |
14 | + | ||
14 | 15 | ||
15 | static NSLock *xmlParsingLock = nil; | 16 | static NSLock *xmlParsingLock = nil; |
16 | static NSMutableArray *requestsUsingXMLParser = nil; | 17 | static NSMutableArray *requestsUsingXMLParser = nil; |
@@ -97,6 +98,7 @@ static NSMutableArray *requestsUsingXMLParser = nil; | @@ -97,6 +98,7 @@ static NSMutableArray *requestsUsingXMLParser = nil; | ||
97 | [[self externalResourceQueue] addOperation:externalResourceRequest]; | 98 | [[self externalResourceQueue] addOperation:externalResourceRequest]; |
98 | } | 99 | } |
99 | 100 | ||
101 | + // Remove external resources | ||
100 | [[self externalResourceQueue] go]; | 102 | [[self externalResourceQueue] go]; |
101 | 103 | ||
102 | } | 104 | } |
@@ -128,9 +130,6 @@ static NSMutableArray *requestsUsingXMLParser = nil; | @@ -128,9 +130,6 @@ static NSMutableArray *requestsUsingXMLParser = nil; | ||
128 | } | 130 | } |
129 | [requestsUsingXMLParser addObject:self]; | 131 | [requestsUsingXMLParser addObject:self]; |
130 | 132 | ||
131 | - // Strip the namespace, because it makes the xpath query a pain | ||
132 | - responseHTML = [responseHTML stringByReplacingOccurrencesOfString:@" xmlns=\"http://www.w3.org/1999/xhtml\"" withString:@""]; | ||
133 | - | ||
134 | NSData *data = [responseHTML dataUsingEncoding:NSUTF8StringEncoding]; | 133 | NSData *data = [responseHTML dataUsingEncoding:NSUTF8StringEncoding]; |
135 | 134 | ||
136 | /* Load XML document */ | 135 | /* Load XML document */ |
@@ -243,15 +242,21 @@ static NSMutableArray *requestsUsingXMLParser = nil; | @@ -243,15 +242,21 @@ static NSMutableArray *requestsUsingXMLParser = nil; | ||
243 | 242 | ||
244 | - (void)readResourceURLs | 243 | - (void)readResourceURLs |
245 | { | 244 | { |
246 | - /* Create xpath evaluation context */ | 245 | + // Create xpath evaluation context |
247 | xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc); | 246 | xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc); |
248 | if(xpathCtx == NULL) { | 247 | if(xpathCtx == NULL) { |
249 | - xmlFreeDoc(doc); | ||
250 | [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:101 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Error: unable to create new XPath context",NSLocalizedDescriptionKey,nil]]]; | 248 | [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:101 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Error: unable to create new XPath context",NSLocalizedDescriptionKey,nil]]]; |
251 | return; | 249 | return; |
252 | } | 250 | } |
253 | 251 | ||
254 | - /* Evaluate xpath expression */ | 252 | + // Add the namespace |
253 | + if (xmlXPathRegisterNs(xpathCtx,(xmlChar *)"xhtml",(xmlChar *)"http://www.w3.org/1999/xhtml") != 0) { | ||
254 | + xmlXPathFreeContext(xpathCtx); | ||
255 | + [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:101 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Error: failed to register namespace for XPath",NSLocalizedDescriptionKey,nil]]]; | ||
256 | + return; | ||
257 | + } | ||
258 | + | ||
259 | + // Evaluate xpath expression | ||
255 | xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx); | 260 | xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx); |
256 | if(xpathObj == NULL) { | 261 | if(xpathObj == NULL) { |
257 | xmlXPathFreeContext(xpathCtx); | 262 | xmlXPathFreeContext(xpathCtx); |
@@ -260,6 +265,7 @@ static NSMutableArray *requestsUsingXMLParser = nil; | @@ -260,6 +265,7 @@ static NSMutableArray *requestsUsingXMLParser = nil; | ||
260 | return; | 265 | return; |
261 | } | 266 | } |
262 | 267 | ||
268 | + // Now loop through our matches | ||
263 | xmlNodeSetPtr nodes = xpathObj->nodesetval; | 269 | xmlNodeSetPtr nodes = xpathObj->nodesetval; |
264 | 270 | ||
265 | int size = (nodes) ? nodes->nodeNr : 0; | 271 | int size = (nodes) ? nodes->nodeNr : 0; |
@@ -298,7 +304,7 @@ static NSMutableArray *requestsUsingXMLParser = nil; | @@ -298,7 +304,7 @@ static NSMutableArray *requestsUsingXMLParser = nil; | ||
298 | 304 | ||
299 | - (void)updateResourceURLs | 305 | - (void)updateResourceURLs |
300 | { | 306 | { |
301 | - /* Create xpath evaluation context */ | 307 | + // Create xpath evaluation context |
302 | xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc); | 308 | xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc); |
303 | if(xpathCtx == NULL) { | 309 | if(xpathCtx == NULL) { |
304 | xmlFreeDoc(doc); | 310 | xmlFreeDoc(doc); |
@@ -306,7 +312,14 @@ static NSMutableArray *requestsUsingXMLParser = nil; | @@ -306,7 +312,14 @@ static NSMutableArray *requestsUsingXMLParser = nil; | ||
306 | return; | 312 | return; |
307 | } | 313 | } |
308 | 314 | ||
309 | - /* Evaluate xpath expression */ | 315 | + // <PAIN:xml namespaces="ibet://these/always/caused/more/problems/than/they/solved"> |
316 | + if (xmlXPathRegisterNs(xpathCtx,(xmlChar *)"xhtml",(xmlChar *)"http://www.w3.org/1999/xhtml") != 0) { | ||
317 | + xmlXPathFreeContext(xpathCtx); | ||
318 | + [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:101 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Error: failed to register namespace for XPath",NSLocalizedDescriptionKey,nil]]]; | ||
319 | + return; | ||
320 | + } | ||
321 | + | ||
322 | + // Evaluate xpath expression | ||
310 | xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx); | 323 | xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx); |
311 | if(xpathObj == NULL) { | 324 | if(xpathObj == NULL) { |
312 | xmlXPathFreeContext(xpathCtx); | 325 | xmlXPathFreeContext(xpathCtx); |
@@ -419,8 +432,11 @@ static NSMutableArray *requestsUsingXMLParser = nil; | @@ -419,8 +432,11 @@ static NSMutableArray *requestsUsingXMLParser = nil; | ||
419 | if (!theURL) { | 432 | if (!theURL) { |
420 | break; | 433 | break; |
421 | } | 434 | } |
422 | - // Remove any quotes around the url | 435 | + // Remove any quotes or whitespace around the url |
423 | - [urls addObject:[theURL stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\"'"]]]; | 436 | + theURL = [theURL stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; |
437 | + theURL = [theURL stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\"'"]]; | ||
438 | + theURL = [theURL stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; | ||
439 | + [urls addObject:theURL]; | ||
424 | } | 440 | } |
425 | return urls; | 441 | return urls; |
426 | } | 442 | } |
-
Please register or login to post a comment