Permalink
Browse files

Fixed NL->Space, handle BaseURL parameter for HREFs, hide text within…

… VIDEO tag
  • Loading branch information...
1 parent 324c69b commit 5383d6b35ff5523e0a98426f3088dd898e02d98b @odrobnik odrobnik committed Jan 21, 2011
@@ -27,7 +27,7 @@
@property (nonatomic, readonly) CTFramesetterRef framesetter;
@property (nonatomic, readonly) CTFrameRef textFrame;
-@property (nonatomic, retain) NSAttributedString *string;
+@property (retain) NSAttributedString *string;
@property (nonatomic) UIEdgeInsets edgeInsets;
@property (nonatomic, assign) DTAttributedTextView *parentView;
@@ -117,9 +117,14 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
#pragma mark UITableViewDelegate
-- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
+- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
+{
+ NSDictionary *rowSnippet = [_snippets objectAtIndex:indexPath.row];
+
DemoTextViewController *viewController = [[DemoTextViewController alloc] init];
- viewController.fileName = [[_snippets objectAtIndex:indexPath.row] objectForKey:@"File"];
+ viewController.fileName = [rowSnippet objectForKey:@"File"];
+ viewController.baseURL = [NSURL URLWithString:[rowSnippet objectForKey:@"BaseURL"]];
+
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
}
@@ -19,6 +19,8 @@
UITextView *_charsView;
UITextView *_dataView;
+ NSURL *baseURL;
+
// private
NSURL *lastActionLink;
NSMutableSet *mediaPlayers;
@@ -28,4 +30,7 @@
@property (nonatomic, retain) NSURL *lastActionLink;
+@property (nonatomic, retain) NSURL *baseURL;
+
+
@end
@@ -50,6 +50,7 @@ - (void)dealloc
[_rangeView release];
[_charsView release];
[_dataView release];
+ [baseURL release];
[lastActionLink release];
[mediaPlayers release];
@@ -109,7 +110,7 @@ - (void)viewDidLoad {
NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];
// Create attributed string from HTML
- NSAttributedString *string = [[NSAttributedString alloc] initWithHTML:data documentAttributes:NULL];
+ NSAttributedString *string = [[NSAttributedString alloc] initWithHTML:data baseURL:baseURL documentAttributes:NULL];
// Display string
_textView.string = string;
@@ -237,14 +238,14 @@ - (UIView *)attributedTextView:(DTAttributedTextView *)attributedTextView viewFo
- (void)linkPushed:(DTLinkButton *)button
{
- [[UIApplication sharedApplication] openURL:button.url];
+ [[UIApplication sharedApplication] openURL:[button.url absoluteURL]];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != actionSheet.cancelButtonIndex)
{
- [[UIApplication sharedApplication] openURL:self.lastActionLink];
+ [[UIApplication sharedApplication] openURL:[self.lastActionLink absoluteURL]];
}
}
@@ -256,9 +257,9 @@ - (void)linkLongPressed:(UILongPressGestureRecognizer *)gesture
button.highlighted = NO;
self.lastActionLink = button.url;
- if ([[UIApplication sharedApplication] canOpenURL:button.url])
+ if ([[UIApplication sharedApplication] canOpenURL:[button.url absoluteURL]])
{
- UIActionSheet *action = [[[UIActionSheet alloc] initWithTitle:[button.url description] delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Open in Safari", nil] autorelease];
+ UIActionSheet *action = [[[UIActionSheet alloc] initWithTitle:[[button.url absoluteURL] description] delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Open in Safari", nil] autorelease];
[action showFromRect:button.frame inView:button.superview animated:YES];
}
}
@@ -279,6 +280,7 @@ - (NSMutableSet *)mediaPlayers
@synthesize fileName = _fileName;
@synthesize lastActionLink;
@synthesize mediaPlayers;
+@synthesize baseURL;
@end
@@ -7,6 +7,7 @@
//
#import <CoreText/CoreText.h>
+#import <UIKit/UIKit.h>
#import "NSAttributedString+HTML.h"
#import "NSString+HTML.h"
@@ -97,7 +98,13 @@ - (id)initWithHTML:(NSData *)data documentAttributes:(NSDictionary **)dict
- (id)initWithHTML:(NSData *)data baseURL:(NSURL *)base documentAttributes:(NSDictionary **)dict
{
- NSDictionary *optionsDict = [NSDictionary dictionaryWithObject:base forKey:NSBaseURLDocumentOption];
+ NSDictionary *optionsDict = nil;
+
+ if (base)
+ {
+ optionsDict = [NSDictionary dictionaryWithObject:base forKey:NSBaseURLDocumentOption];
+ }
+
return [self initWithHTML:data options:optionsDict documentAttributes:dict];
}
@@ -114,6 +121,10 @@ - (id)initWithHTML:(NSData *)data options:(NSDictionary *)options documentAttrib
encoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding);
}
+ // use baseURL from options if present
+ NSURL *baseURL = [options objectForKey:NSBaseURLDocumentOption];
+
+
// Make it a string
NSString *htmlString = [[NSString alloc] initWithData:data encoding:encoding];
@@ -131,8 +142,10 @@ - (id)initWithHTML:(NSData *)data options:(NSDictionary *)options documentAttrib
BOOL needsListItemStart = NO;
BOOL needsNewLineBefore = NO;
+
+ // we cannot skip any characters, NLs turn into spaces and multi-spaces get compressed to singles
NSScanner *scanner = [NSScanner scannerWithString:htmlString];
- scanner.charactersToBeSkipped = [NSCharacterSet newlineCharacterSet];
+ scanner.charactersToBeSkipped = nil;
NSMutableDictionary *currentTag = [tagStack lastObject];
NSDictionary *previousAttributes = NULL;
@@ -239,6 +252,10 @@ - (id)initWithHTML:(NSData *)data options:(NSDictionary *)options documentAttrib
}
else if ([tagName isEqualToString:@"video"] && tagOpen)
{
+ // hide contents of recognized tag
+ [currentTag setObject:[NSNumber numberWithBool:YES] forKey:@"_tagContentsInvisible"];
+
+
CGFloat width = [[tagAttributesDict objectForKey:@"width"] intValue];
CGFloat height = [[tagAttributesDict objectForKey:@"height"] intValue];
@@ -287,6 +304,13 @@ - (id)initWithHTML:(NSData *)data options:(NSDictionary *)options documentAttrib
NSString *cleanString = [[tagAttributesDict objectForKey:@"href"] stringByReplacingOccurrencesOfString:@"\n" withString:@""];
NSURL *link = [NSURL URLWithString:cleanString];
+ // deal with relative URL
+ if (![link scheme])
+ {
+ link = [NSURL URLWithString:cleanString relativeToURL:baseURL];
+ }
+
+
if (link)
{
[currentTag setObject:link forKey:@"DTLink"];
@@ -545,12 +569,12 @@ - (id)initWithHTML:(NSData *)data options:(NSDictionary *)options documentAttrib
//----------------------------------------- TAG CONTENTS -----------------------------------------
NSString *tagContents = nil;
- if ([scanner scanUpToString:@"<" intoString:&tagContents])
+ if ([scanner scanUpToString:@"<" intoString:&tagContents] && ![[currentTag objectForKey:@"_tagContentsInvisible"] boolValue])
{
-
- tagContents = [tagContents stringByNormalizingWhitespace];
+ tagContents = [tagContents stringByNormalizingWhitespace];
tagContents = [tagContents stringByReplacingHTMLEntities];
+
NSMutableDictionary *fontAttributes = [NSMutableDictionary dictionary];
NSMutableDictionary *fontStyleAttributes = [NSMutableDictionary dictionary];
@@ -86,6 +86,21 @@ - (void)testSpaceNormalization
STAssertEqualObjects(resultOnIOS, resultOnMac, @"Output on List Test differs");
}
+- (void)testSpaceAndNewlines
+{
+ NSString *html = @"<a>bla</a>\nfollows\n<font color=\"blue\">NSString</font> <font color=\"purple\">*</font>str <font color=\"#000000\">=</font> @<font color=\"#E40000\">\"The Quick Brown Fox Brown\"</font>;";
+
+ NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];
+
+ NSAttributedString *string = [[NSAttributedString alloc] initWithHTML:data documentAttributes:NULL];
+ NSData *dump = [[string string] dataUsingEncoding:NSUTF8StringEncoding];
+ NSString *resultOnIOS = [dump description];
+
+ NSString *resultOnMac = @"<626c6120 666f6c6c 6f777320 4e535374 72696e67 202a7374 72203d20 40225468 65205175 69636b20 42726f77 6e20466f 78204272 6f776e22 3b>";
+
+ STAssertEqualObjects(resultOnIOS, resultOnMac, @"Output on List Test differs");
+}
+
View
@@ -6,9 +6,12 @@
// Copyright 2011 Drobnik.com. All rights reserved.
//
+#import <Foundation/Foundation.h>
+
@interface NSScanner (HTML)
- (NSString *)peekNextTagSkippingClosingTags:(BOOL)skipClosingTags;
- (BOOL)scanHTMLTag:(NSString **)tagName attributes:(NSDictionary **)attributes isOpen:(BOOL *)isOpen isClosed:(BOOL *)isClosed;
+- (void)logPosition;
@end
View
@@ -170,5 +170,10 @@ - (BOOL)scanHTMLTag:(NSString **)tagName attributes:(NSDictionary **)attributes
return YES;
}
+// for debugging scanner
+- (void)logPosition
+{
+ NSLog(@"%@", [[self string] substringFromIndex:[self scanLocation]]);
+}
@end
Oops, something went wrong.

0 comments on commit 5383d6b

Please sign in to comment.