Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add ability to decode Apple Converted Space tags.

- Apple uses spans with apple-converted-space class to represent blocks of spaces of longer than 1. They alternate nbsp; and " ".
- This adds the capability of converting the nbsp; back to normal spaces on parsing
  • Loading branch information...
commit 018b0113c41f49b311c2a9780ec6296e7a9d5fd9 1 parent 1699125
@Cocoanetics authored
View
3  Core/Source/DTHTMLAttributedStringBuilder.m
@@ -694,6 +694,9 @@ - (void)parser:(DTHTMLParser *)parser foundCharacters:(NSString *)string
[textNode inheritAttributesFromElement:_currentTag];
+ // need to transfer Apple Converted Space tag to text node
+ textNode.containsAppleConvertedSpace = _currentTag.containsAppleConvertedSpace;
+
// text directly contained in body needs to be output right away
if (_currentTag == _bodyElement)
{
View
2  Core/Source/DTHTMLElement.h
@@ -60,6 +60,7 @@
BOOL _isColorInherited;
BOOL _preserveNewlines;
+ BOOL _containsAppleConvertedSpace;
DTHTMLElementFontVariant _fontVariant;
@@ -94,6 +95,7 @@
@property (nonatomic, assign) DTHTMLElementFontVariant fontVariant;
@property (nonatomic, assign) CGFloat textScale;
@property (nonatomic, assign) CGSize size;
+@property (nonatomic, assign) BOOL containsAppleConvertedSpace;
@property (nonatomic, assign) BOOL didOutput;
View
18 Core/Source/DTHTMLElement.m
@@ -72,7 +72,11 @@ - (id)initWithName:(NSString *)name attributes:(NSDictionary *)attributes option
self = [super initWithName:name attributes:attributes];
if (self)
{
-
+ // transfer Apple Converted Space tag
+ if ([[self attributeForKey:@"class"] isEqualToString:@"Apple-converted-space"])
+ {
+ _containsAppleConvertedSpace = YES;
+ }
}
return self;
@@ -400,12 +404,15 @@ - (NSAttributedString *)attributedString
if (nodeString)
{
- // we already have a white space in the string so far
- if ([[tmpString string] hasSuffixCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]])
+ if (!oneChild.containsAppleConvertedSpace)
{
- while ([[nodeString string] hasPrefix:@" "])
+ // we already have a white space in the string so far
+ if ([[tmpString string] hasSuffixCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]])
{
- nodeString = [nodeString attributedSubstringFromRange:NSMakeRange(1, [nodeString length]-1)];
+ while ([[nodeString string] hasPrefix:@" "])
+ {
+ nodeString = [nodeString attributedSubstringFromRange:NSMakeRange(1, [nodeString length]-1)];
+ }
}
}
@@ -1252,6 +1259,7 @@ - (void)setLink:(NSURL *)link
@synthesize textScale = _textScale;
@synthesize size = _size;
@synthesize linkGUID = _linkGUID;
+@synthesize containsAppleConvertedSpace = _containsAppleConvertedSpace;
@end
View
7 Core/Source/DTHTMLElementText.m
@@ -35,7 +35,7 @@ - (NSAttributedString *)attributedString
{
NSString *text;
- if (self.preserveNewlines)
+ if (_preserveNewlines)
{
text = _text;
@@ -55,6 +55,11 @@ - (NSAttributedString *)attributedString
// useing \r as to not confuse this with line feeds, but still get a single paragraph
text = [text stringByReplacingOccurrencesOfString:@"\n" withString:@"\r"];
}
+ else if (_containsAppleConvertedSpace)
+ {
+ // replace nbsp; with regular space
+ text = [_text stringByReplacingOccurrencesOfString:UNICODE_NON_BREAKING_SPACE withString:@" "];
+ }
else
{
text = [_text stringByNormalizingWhitespace];
View
50 Demo/Resources/CurrentTest.html
@@ -1,40 +1,16 @@
-<?xml version="1.0" encoding="UTF-8" ?>
-<!DOCTYPE html
-PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
-"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml">
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<html>
<head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <meta content="TX16_HTM 16.0.305.501" name="GENERATOR" />
- <title></title>
- </head>
- <body style="font-family:'Arial';font-size:12pt;text-align:left;">
- <p><span style="font-size:xx-small;">Default font, xx-small</span><span style="font-size:9px;"> = Default font, 9 px</span></p>
- <p><span style="font-size:x-small;">Default font, x-small</span><span style="font-size:10px;"> = Default font, 10 px</span></p>
- <p><span style="font-size:small;">Default font, small</span><span style="font-size:13px;"> = Default font, 13 px</span></p>
- <p><span style="font-size:medium;">Default font, medium</span><span style="font-size:16px;"> = Default font, 16 px</span></p>
- <p><span style="font-size:large;">Default font, large</span><span style="font-size:18px;"> = Default font, 18 px</span></p>
- <p><span style="font-size:x-large;">Default font, x-large</span><span style="font-size:24px;"> = Default font, 24 px</span></p>
- <p><span style="font-size:xx-large;">Default font, xx-large</span><span style="font-size:32px;"> = Default font, 32 px</span></p>
- <p><span style="font-size:8pt;">Default font, 8 pt</span></p>
- <p><span style="font-size:9pt;">Default font, 9 pt</span></p>
- <p><span style="font-size:10pt;">Default font, 10 pt</span></p>
- <p><span style="font-size:11pt;">Default font, 11 pt</span></p>
- <p>Default font, 12 pt</p>
- <p><span style="font-size:14pt;">Default font, 14 pt</span></p>
- <p><span style="font-size:16pt;">Default font, 16 pt</span></p>
- <p><span style="font-size:18pt;">Default font, 18 pt</span></p>
- <p><span style="font-size:24pt;">Default font, 24 pt</span></p>
- <p><span style="font-size:36pt;">Default font, 36 pt</span></p>
- <p><span style="font-family:'Georgia';font-size:8pt;">Georgia font, 8 pt</span></p>
- <p><span style="font-family:'Georgia';font-size:9pt;">Georgia font, 9 pt</span></p>
- <p><span style="font-family:'Georgia';font-size:10pt;">Georgia font, 10 pt</span></p>
- <p><span style="font-family:'Georgia';font-size:11pt;">Georgia font, 11 pt</span></p>
- <p><span style="font-family:'Georgia';">Georgia font, 12 pt</span></p>
- <p><span style="font-family:'Georgia';font-size:14pt;">Georgia font, 14 pt</span></p>
- <p><span style="font-family:'Georgia';font-size:16pt;">Georgia font, 16 pt</span></p>
- <p><span style="font-family:'Georgia';font-size:18pt;">Georgia font, 18 pt</span></p>
- <p><span style="font-family:'Georgia';font-size:24pt;">Georgia font, 24 pt</span></p>
- <p><span style="font-family:'Georgia';font-size:36pt;">Georgia font, 36 pt</span></p>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+ <meta http-equiv="Content-Style-Type" content="text/css">
+ <title></title>
+ <meta name="Generator" content="Cocoa HTML Writer">
+ <style type="text/css">
+ p.p1 {margin: 0.0px 0.0px 0.0px 0.0px}
+ span.s1 {font-family: 'Helvetica'; font-weight: normal; font-style: normal; font-size: 12px}
+ </style>
+ </head>
+ <body>
+ <p class="p1"><span class="s1">many <span class="Apple-converted-space">    </span>spaces</span></p>
</body>
</html>
Please sign in to comment.
Something went wrong with that request. Please try again.