Merge pull request #73 from github/html-view

HTML view
This commit is contained in:
Danny Greg 2013-03-14 13:36:59 -07:00
Родитель e4a5fd8e80 a0a9144b4b
Коммит b2612e1cab
5 изменённых файлов: 178 добавлений и 0 удалений

Просмотреть файл

@ -16,6 +16,10 @@
306A81E81601E92000BF45F3 /* CAAnimation+RBLBlockAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 306A81E61601E92000BF45F3 /* CAAnimation+RBLBlockAdditions.h */; settings = {ATTRIBUTES = (Public, ); }; };
306A81E91601E92000BF45F3 /* CAAnimation+RBLBlockAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 306A81E71601E92000BF45F3 /* CAAnimation+RBLBlockAdditions.m */; };
306A81EB1601EAE700BF45F3 /* CAAnimation+RBLBlockAdditionsSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 306A81EA1601EAE700BF45F3 /* CAAnimation+RBLBlockAdditionsSpec.m */; };
88347E6A16F213C500872A0A /* RBLHTMLView.h in Headers */ = {isa = PBXBuildFile; fileRef = 88347E6816F213C500872A0A /* RBLHTMLView.h */; settings = {ATTRIBUTES = (Public, ); }; };
88347E6B16F213C500872A0A /* RBLHTMLView.m in Sources */ = {isa = PBXBuildFile; fileRef = 88347E6916F213C500872A0A /* RBLHTMLView.m */; };
88347E7316F2145900872A0A /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 88347E7216F2145900872A0A /* WebKit.framework */; };
884675FF16F2574200016B56 /* RBLHTMLViewSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 884675FE16F2574200016B56 /* RBLHTMLViewSpec.m */; };
AB10DB4D166EA9CA00AB7AB1 /* RBLScrollView.h in Headers */ = {isa = PBXBuildFile; fileRef = AB10DB4B166EA9CA00AB7AB1 /* RBLScrollView.h */; settings = {ATTRIBUTES = (Public, ); }; };
AB10DB4E166EA9CA00AB7AB1 /* RBLScrollView.m in Sources */ = {isa = PBXBuildFile; fileRef = AB10DB4C166EA9CA00AB7AB1 /* RBLScrollView.m */; };
AB10DB53166EADD000AB7AB1 /* RBLScrollViewSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = AB10DB52166EADD000AB7AB1 /* RBLScrollViewSpec.m */; };
@ -154,6 +158,10 @@
306A81E71601E92000BF45F3 /* CAAnimation+RBLBlockAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "CAAnimation+RBLBlockAdditions.m"; sourceTree = "<group>"; };
306A81EA1601EAE700BF45F3 /* CAAnimation+RBLBlockAdditionsSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "CAAnimation+RBLBlockAdditionsSpec.m"; sourceTree = "<group>"; };
306A81EC1601F44200BF45F3 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
88347E6816F213C500872A0A /* RBLHTMLView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RBLHTMLView.h; sourceTree = "<group>"; };
88347E6916F213C500872A0A /* RBLHTMLView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RBLHTMLView.m; sourceTree = "<group>"; };
88347E7216F2145900872A0A /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = System/Library/Frameworks/WebKit.framework; sourceTree = SDKROOT; };
884675FE16F2574200016B56 /* RBLHTMLViewSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RBLHTMLViewSpec.m; sourceTree = "<group>"; };
AB10DB4B166EA9CA00AB7AB1 /* RBLScrollView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RBLScrollView.h; sourceTree = "<group>"; };
AB10DB4C166EA9CA00AB7AB1 /* RBLScrollView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RBLScrollView.m; sourceTree = "<group>"; };
AB10DB52166EADD000AB7AB1 /* RBLScrollViewSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RBLScrollViewSpec.m; sourceTree = "<group>"; };
@ -232,6 +240,7 @@
files = (
D0E91C5B1603CDC300D23E93 /* QuartzCore.framework in Frameworks */,
D09AE4E315C5F45200ECAD10 /* Cocoa.framework in Frameworks */,
88347E7316F2145900872A0A /* WebKit.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -301,6 +310,8 @@
AB10DB4C166EA9CA00AB7AB1 /* RBLScrollView.m */,
303DEF4216D2B88700BD65C4 /* RBLShadowedTextFieldCell.h */,
303DEF4316D2B88700BD65C4 /* RBLShadowedTextFieldCell.m */,
88347E6816F213C500872A0A /* RBLHTMLView.h */,
88347E6916F213C500872A0A /* RBLHTMLView.m */,
);
name = Classes;
sourceTree = "<group>";
@ -342,6 +353,7 @@
D078B7DD15F6DB420063DDD5 /* NSView+RBLAnimationAdditionsSpec.m */,
AB10DB52166EADD000AB7AB1 /* RBLScrollViewSpec.m */,
D0723FDF15C60E94004DBDC7 /* RBLViewSpec.m */,
884675FE16F2574200016B56 /* RBLHTMLViewSpec.m */,
);
name = Classes;
sourceTree = "<group>";
@ -439,6 +451,7 @@
D09AE4E215C5F45200ECAD10 /* Cocoa.framework */,
D09AE4F815C5F45300ECAD10 /* SenTestingKit.framework */,
D0E91C5A1603CDC300D23E93 /* QuartzCore.framework */,
88347E7216F2145900872A0A /* WebKit.framework */,
D09AE4E415C5F45200ECAD10 /* Other Frameworks */,
);
name = Frameworks;
@ -517,6 +530,7 @@
B85EE8E9163ECF4C00265870 /* NSView+RBLViewControllerAdditions.h in Headers */,
B85EE8ED163EE15800265870 /* NSObject+RBObjectSizzlingAdditions.h in Headers */,
AB61F6D41638370900DD5D2C /* NSApplication+RBLBlockAdditions.h in Headers */,
88347E6A16F213C500872A0A /* RBLHTMLView.h in Headers */,
AB10DB4D166EA9CA00AB7AB1 /* RBLScrollView.h in Headers */,
303DEF4416D2B88700BD65C4 /* RBLShadowedTextFieldCell.h in Headers */,
D088BEF916747D8F004B2665 /* NSFont+RBLFallbackAdditions.h in Headers */,
@ -721,6 +735,7 @@
D088BEFA16747D8F004B2665 /* NSFont+RBLFallbackAdditions.m in Sources */,
D03D67191677E13F0041DDF2 /* NSAttributedString+RBLHTMLAdditions.m in Sources */,
303DEF4516D2B88700BD65C4 /* RBLShadowedTextFieldCell.m in Sources */,
88347E6B16F213C500872A0A /* RBLHTMLView.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -735,6 +750,7 @@
AB10DB53166EADD000AB7AB1 /* RBLScrollViewSpec.m in Sources */,
D088BEFC16747EE1004B2665 /* NSFont+RBLFallbackAdditionsSpec.m in Sources */,
D03D671B1677E2950041DDF2 /* NSAttributedString+RBLHTMLAdditionsSpec.m in Sources */,
884675FF16F2574200016B56 /* RBLHTMLViewSpec.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

22
Rebel/RBLHTMLView.h Normal file
Просмотреть файл

@ -0,0 +1,22 @@
//
// RBLHTMLView.h
// Rebel
//
// Created by Josh Abernathy on 3/13/13.
// Copyright (c) 2013 GitHub. All rights reserved.
//
#import <WebKit/WebKit.h>
// A view for displaying HTML-styled text.
@interface RBLHTMLView : WebView
// The CSS to use to style the HTML.
//
// Reasonable defaults are used when this is not set.
@property (nonatomic, copy) NSString *CSS;
// The HTML to display.
@property (nonatomic, copy) NSString *HTML;
@end

106
Rebel/RBLHTMLView.m Normal file
Просмотреть файл

@ -0,0 +1,106 @@
//
// RBLHTMLView.m
// Rebel
//
// Created by Josh Abernathy on 3/13/13.
// Copyright (c) 2013 GitHub. All rights reserved.
//
#import "RBLHTMLView.h"
@implementation RBLHTMLView
#pragma mark Lifecycle
static void CommonInit(RBLHTMLView *self) {
self.drawsBackground = NO;
self.maintainsBackForwardList = NO;
self.mainFrame.frameView.allowsScrolling = NO;
self.policyDelegate = self;
}
- (id)initWithFrame:(NSRect)frameRect {
self = [super initWithFrame:frameRect];
if (self == nil) return nil;
CommonInit(self);
return self;
}
- (id)initWithCoder:(NSCoder *)decoder {
self = [super initWithCoder:decoder];
if (self == nil) return nil;
CommonInit(self);
return self;
}
#pragma mark WebPolicyDelegate
- (void)webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
// WebNavigationTypeOther is when we do our HTML string load. Otherwise, we
// shunt it off to a Real Browser®.
if ([actionInformation[WebActionNavigationTypeKey] integerValue] == WebNavigationTypeOther) {
[listener use];
} else {
[listener ignore];
[NSWorkspace.sharedWorkspace openURL:request.URL];
}
}
#pragma mark HTML
- (void)setHTML:(NSString *)HTML {
if ([_HTML isEqual:HTML]) return;
_HTML = [HTML copy];
[self reloadConstructedContent];
}
- (void)setCSS:(NSString *)CSS {
if ([_CSS isEqual:CSS]) return;
_CSS = [CSS copy];
[self reloadConstructedContent];
}
- (void)reloadConstructedContent {
if (self.HTML == nil) return;
static NSString * const template = @""
"<!DOCTYPE html>"
"<style> "
" body { "
" font: 11px Lucida Grande, sans-serif; "
" color: #262626; "
" line-height: 16px; "
" vertical-align: baseline; "
" margin: 0px 0px 0px 0px; "
" } "
" a { "
" text-decoration: none; "
" color: #06C; "
" } "
" a:visited { "
" text-decoration: none; "
" color: #06C; "
" } "
" .error { "
" color: #911; "
" } "
"%@"
"</style> "
"<body>%@</body> ";
NSString *constructedHTML = [NSString stringWithFormat:template, self.CSS, self.HTML];
[self.mainFrame loadHTMLString:constructedHTML baseURL:nil];
while (self.isLoading) {
[NSRunLoop.currentRunLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate date]];
}
}
@end

Просмотреть файл

@ -21,3 +21,4 @@
#import <Rebel/RBLTableCellView.h>
#import <Rebel/RBLScrollView.h>
#import <Rebel/RBLShadowedTextFieldCell.h>
#import <Rebel/RBLHTMLView.h>

Просмотреть файл

@ -0,0 +1,33 @@
//
// RBLHTMLViewSpec.m
// Rebel
//
// Created by Josh Abernathy on 3/14/13.
// Copyright (c) 2013 GitHub. All rights reserved.
//
SpecBegin(RBLHTMLView)
static NSString * const HTML = @"<span>hey brother</span>";
__block RBLHTMLView *view;
beforeEach(^{
view = [[RBLHTMLView alloc] initWithFrame:NSZeroRect];
view.HTML = HTML;
});
it(@"should contain the set HTML", ^{
expect([view.mainFrame.DOMDocument.body.innerHTML rangeOfString:HTML].length).to.beGreaterThan(0);
});
it(@"shouldn't be loading after setting the HTML", ^{
expect(view.isLoading).to.beFalsy();
});
it(@"shouldn't be loading after setting the CSS", ^{
view.CSS = @"body { color: red; }";
expect(view.isLoading).to.beFalsy();
});
SpecEnd