Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for css files reference in html #360

Merged
merged 4 commits into from
Mar 29, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Core/Source/DTHTMLAttributedStringBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,28 @@ - (void)_registerTagEndHandlers
};

[_tagEndHandlers setObject:[styleBlock copy] forKey:@"style"];


void (^linkBlock)(void) = ^
{
NSString *href = [_currentTag attributeForKey:@"href"];
NSString *type = [[_currentTag attributeForKey:@"type"] lowercaseString];
if ([type isEqualToString:@"text/css"]) {
NSURL *stylesheetURL = [NSURL URLWithString:href relativeToURL:_baseURL];
if ([stylesheetURL isFileURL]) {
NSString *stylesheetContent = [NSString stringWithContentsOfURL:stylesheetURL encoding:NSUTF8StringEncoding error:nil];
if (stylesheetContent) {
DTCSSStylesheet *localSheet = [[DTCSSStylesheet alloc] initWithStyleBlock:stylesheetContent];
[_globalStyleSheet mergeStylesheet:localSheet];
}
}
else {
NSLog(@"WARNING: css link referencing a non-local target, ignored");
}
}
};

[ _tagEndHandlers setObject:[linkBlock copy] forKey:@"link"];
}

#pragma mark DTHTMLParser Delegate
Expand Down
4 changes: 4 additions & 0 deletions DTCoreText.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@
A7F7EFC61573603100F5A4D0 /* DTImage+HTML.h in Headers */ = {isa = PBXBuildFile; fileRef = A7C5D01314D7E3BB00AF1D75 /* DTImage+HTML.h */; settings = {ATTRIBUTES = (Public, ); }; };
A7F7EFC81573603100F5A4D0 /* DTCompatibility.h in Headers */ = {isa = PBXBuildFile; fileRef = A7A672B91532F2D100920A18 /* DTCompatibility.h */; settings = {ATTRIBUTES = (Public, ); }; };
A7F842F416D38BC300BCD63F /* FontSizes.html in Resources */ = {isa = PBXBuildFile; fileRef = A7EB84FB168ED63A00D686B7 /* FontSizes.html */; };
DDCCEB1E17005A4F009FF923 /* css.css in Resources */ = {isa = PBXBuildFile; fileRef = DDCCEB1D17005A4F009FF923 /* css.css */; };
DE08BAB5148808B20054DC84 /* DTCoreTextFontOverrides.plist in Resources */ = {isa = PBXBuildFile; fileRef = A788CA2114863EF100E1AFD9 /* DTCoreTextFontOverrides.plist */; };
E2262DC71638331500BFDAD7 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A7F5671C14D8469000D1F167 /* Cocoa.framework */; };
E2262DD51638336700BFDAD7 /* DTColor+HTML.m in Sources */ = {isa = PBXBuildFile; fileRef = A788C95314863E8700E1AFD9 /* DTColor+HTML.m */; };
Expand Down Expand Up @@ -968,6 +969,7 @@
A7F5672514D8506B00D1F167 /* NSAttributedString+SmallCaps.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSAttributedString+SmallCaps.h"; sourceTree = "<group>"; };
A7F5672614D8506C00D1F167 /* NSAttributedString+SmallCaps.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSAttributedString+SmallCaps.m"; sourceTree = "<group>"; };
A7F7EFCD1573603100F5A4D0 /* libDTCoreText_no_DTFoundation.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libDTCoreText_no_DTFoundation.a; sourceTree = BUILT_PRODUCTS_DIR; };
DDCCEB1D17005A4F009FF923 /* css.css */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.css; path = css.css; sourceTree = "<group>"; };
E2262DC61638331500BFDAD7 /* DTCoreText.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = DTCoreText.framework; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -1269,6 +1271,7 @@
A73885A41606E9B2001D60C4 /* EmojiTest.html */,
A730BCD616D29588003B849F /* Languages.html */,
A740369016E51C9800ECCDE0 /* About.html */,
DDCCEB1D17005A4F009FF923 /* css.css */,
);
path = Resources;
sourceTree = "<group>";
Expand Down Expand Up @@ -1970,6 +1973,7 @@
A7CF230A16DE1DF7005A4A6E /* AppledocSettings.plist in Resources */,
A740368916E51B3A00ECCDE0 /* DemoAboutViewController.xib in Resources */,
A740369116E51C9800ECCDE0 /* About.html in Resources */,
DDCCEB1E17005A4F009FF923 /* css.css in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
28 changes: 28 additions & 0 deletions Demo/Resources/css.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
h1{
margin: 0px;
direction:rtl;
text-align:right;
}
.s2{
color: #000000;
font-size: 68.7500%;
font-style: normal;
font-variant: normal;
font-weight: normal;
letter-spacing: 0.0000em;
margin-bottom: 2.3148%;
margin-top: 0.0000%;
padding-left: 0.0000%;
padding-right: 0.0000%;
text-align: left;
text-decoration: none;
text-indent: 0.0000%;
text-transform: none;
}
.s18{
font-size: 81.2500%;
text-align: center;
}
.c10{
background: #FFFFFF;
}
10 changes: 10 additions & 0 deletions Demo/Resources/styles.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<html>
<head>
<link rel="stylesheet" href="css.css" type="text/css"/>
</head>
<body>
<h1>CSS Styles Support</h1>
<p>Currently CSS styling is supported inline in tags, and via simple rules.</p>
<p>For example:</p>
Expand Down Expand Up @@ -51,3 +56,8 @@ <h2>To Do</h2>
<li>More robust mapping of font family to available fonts, support for fallback</li>
<li>text-decoration - blink, overline</li>
<li>font-variant - to support smallcaps</li></ul>

<h1 class="s2 s18"><span class="c10">This text should be centered</span></h1>

</body>
</html>
2 changes: 2 additions & 0 deletions Demo/Source/DemoTextViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ - (NSAttributedString *)_attributedStringForSnippetUsingiOS6Attributes:(BOOL)use
[options setObject:[NSNumber numberWithBool:YES] forKey:DTUseiOS6Attributes];
}

[options setObject:[NSURL fileURLWithPath:readmePath] forKey:NSBaseURLDocumentOption];

NSAttributedString *string = [[NSAttributedString alloc] initWithHTMLData:data options:options documentAttributes:NULL];

return string;
Expand Down