Skip to content

Commit

Permalink
Prevent 2 spaces if a paragraph is continued after invalid inline tag
Browse files Browse the repository at this point in the history
  • Loading branch information
odrobnik committed Jan 22, 2011
1 parent e461f61 commit 160e6ad
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 65 deletions.
40 changes: 24 additions & 16 deletions Classes/NSAttributedString+HTML.m
Expand Up @@ -160,9 +160,9 @@ - (id)initWithHTML:(NSData *)data options:(NSDictionary *)options documentAttrib
NSDictionary *tagAttributesDict = nil;
BOOL tagOpen = YES;
BOOL immediatelyClosed = NO;

[scanner logPosition];


if ([scanner scanHTMLTag:&tagName attributes:&tagAttributesDict isOpen:&tagOpen isClosed:&immediatelyClosed] && tagName)
{
Expand Down Expand Up @@ -230,8 +230,8 @@ - (id)initWithHTML:(NSData *)data options:(NSDictionary *)options documentAttrib
CTParagraphStyleRef paragraphStyle = createParagraphStyle(0, 0, 0, 0);

NSMutableDictionary *localAttributes = [NSMutableDictionary dictionaryWithObjectsAndKeys:attachment, @"DTTextAttachment",
(id)embeddedObjectRunDelegate, kCTRunDelegateAttributeName,
(id)paragraphStyle, kCTParagraphStyleAttributeName, nil];
(id)embeddedObjectRunDelegate, kCTRunDelegateAttributeName,
(id)paragraphStyle, kCTParagraphStyleAttributeName, nil];
CFRelease(embeddedObjectRunDelegate);

id link = [currentTag objectForKey:@"DTLink"];
Expand Down Expand Up @@ -749,24 +749,32 @@ - (id)initWithHTML:(NSData *)data options:(NSDictionary *)options documentAttrib
needsNewLineBefore = NO;
}
else // might be a continuation of a paragraph, then we might need space before it
{
// TODO: Needs better handling of whitespace compression and adding space between tags if there are newlines
if (![tagContents hasPrefix:@" "])
{
NSString *stringSoFar = [tmpString string];

if ([stringSoFar length] && ![stringSoFar hasSuffix:@" "] && ![stringSoFar hasSuffix:@"\n"] && ![stringSoFar hasSuffix:UNICODE_LINE_FEED])
// // TODO: Needs better handling of whitespace compression and adding space between tags if there are newlines
// if (![tagContents hasPrefix:@" "])
// {
//
// if ([stringSoFar length] && ![stringSoFar hasSuffix:@" "] && ![stringSoFar hasSuffix:@"\n"] && ![stringSoFar hasSuffix:UNICODE_LINE_FEED])
// {
// // add space prefix unless punctuation character
// if (![tagContents hasPrefixCharacterFromSet:[NSCharacterSet punctuationCharacterSet]])
// {
// //FIXME: What are the situations where a whitespace needs adding?
// // NOT: </font>text
// //tagContents = [@" " stringByAppendingString:tagContents];
// }
// }
// }

// prevent double spacing
if ([stringSoFar hasSuffix:@" "] && [tagContents hasPrefix:@" "])
{
// add space prefix unless punctuation character
if (![tagContents hasPrefixCharacterFromSet:[NSCharacterSet punctuationCharacterSet]])
{
//FIXME: What are the situations where a whitespace needs adding?
// NOT: </font>text
//tagContents = [@" " stringByAppendingString:tagContents];
}
tagContents = [tagContents substringFromIndex:1];
}
}
}


NSAttributedString *tagString = [[NSAttributedString alloc] initWithString:tagContents attributes:attributes];
[tmpString appendAttributedString:tagString];
Expand Down
16 changes: 15 additions & 1 deletion Classes/NSAttributedStringHTMLTest.m
Expand Up @@ -101,7 +101,21 @@ - (void)testSpaceAndNewlines
STAssertEqualObjects(resultOnIOS, resultOnMac, @"Output on List Test differs");
}


- (void)testMissingClosingTagAndSpacing
{
NSString *html = @" image \n <a href=\"http://sv.wikipedia.org/wiki/Fil:V%C3%A4dersoltavlan_cropped.JPG\"\n late</a> last";

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 = @"<696d6167 65206c61 7374>";

STAssertEqualObjects(resultOnIOS, resultOnMac, @"Output on Invalid Tag Test differs");

}



Expand Down
19 changes: 19 additions & 0 deletions CoreTextExtensions.xcodeproj/project.pbxproj
Expand Up @@ -75,6 +75,16 @@
B235598C12E055BC005EA62F /* LoremIpsum.html in Resources */ = {isa = PBXBuildFile; fileRef = B235598B12E055BC005EA62F /* LoremIpsum.html */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
A7DBFFB412EB35AC00F25897 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 29B97313FDCFA39411CA2CEA /* Project object */;
proxyType = 1;
remoteGlobalIDString = A7DBFBEA12E965DE00F25897 /* UnitTest */;
remoteInfo = UnitTest;
};
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
1D30AB110D05D00D00671497 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
1D3623240D0F684500981E51 /* DemoAppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DemoAppDelegate.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -317,6 +327,7 @@
buildRules = (
);
dependencies = (
A7DBFFB512EB35AC00F25897 /* PBXTargetDependency */,
);
name = CoreTextExtensions;
productName = CoreTextExtensions;
Expand Down Expand Up @@ -471,6 +482,14 @@
};
/* End PBXSourcesBuildPhase section */

/* Begin PBXTargetDependency section */
A7DBFFB512EB35AC00F25897 /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = A7DBFBEA12E965DE00F25897 /* UnitTest */;
targetProxy = A7DBFFB412EB35AC00F25897 /* PBXContainerItemProxy */;
};
/* End PBXTargetDependency section */

/* Begin XCBuildConfiguration section */
1D6058940D05DD3E006BFB54 /* Debug */ = {
isa = XCBuildConfiguration;
Expand Down
50 changes: 2 additions & 48 deletions Resources/CurrentTest.html
@@ -1,49 +1,3 @@
What's happened to the Sun?

Sometimes it looks like the Sun is being viewed through a large
<a href="http://www.opticalres.com/optics_for_kids/kidoptx_p2.html"
>lens</a>.

In the above case, however, there are actually millions of lenses:
<a href="http://www.its.caltech.edu/~atomic/snowcrystals/halos/halos.htm"
>ice crystals</a>.

As water freezes in the upper
<a href="http://csep10.phys.utk.edu/astr161/lect/earth/atmosphere.html"
>atmosphere</a>, small, flat, six-sided, ice crystals might be formed.

As these <a href="http://en.wikipedia.org/wiki/Ice_crystal"
>crystals</a> flutter to the ground, much time is spent
with their faces flat, parallel to the ground.

An <a href="http://www.youtube.com/watch?v=RLYg5jptvCc">observe</a>r may pass through the same plane as
many of the falling ice crystals near sunrise or sunset.

During this alignment, each crystal can act like a miniature lens,
refracting sunlight into our
<a href="ap100208.html">view</a> and creating
<a href="http://www.atoptics.co.uk/halo/parhelia.htm"
>phenomena like parhelia</a>, the technical term for
<a href="ap990823.html">sundogs</a>.

The above image was taken
invalid tag
<a href="http://sv.wikipedia.org/wiki/Fil:V%C3%A4dersoltavlan_cropped.JPG"
late</a> last year in
<a href="http://en.wikipedia.org/wiki/Stockholm">Stockholm</a>,
<a href="http://en.wikipedia.org/wiki/Sweden">Sweden</a>.

Visible in the image center is the Sun, while two bright
<a href="http://ww2010.atmos.uiuc.edu/(Gh)/guides/mtr/opt/ice/sd.rxml"
>sundogs</a> glow prominently from both the left and the right.

<a href="http://hyperphysics.phy-astr.gsu.edu/hbase/atmos/halo.html"
>Also visible</a> is the bright
<a href="http://hyperphysics.phy-astr.gsu.edu/hbase/atmos/halo22.html"
>22 degree halo</a> -- as well as the rarer and much fainter
<a href="http://www.atoptics.co.uk/halo/46hal.htm">46 degree halo</a> --
also created by
<a href="http://www.atoptics.co.uk/halo/circular.htm"
>sunlight reflecting</a> off of atmospheric ice
<a href="ap080618.html">crystals</a>.

</font></b>
late</a> no double space

0 comments on commit 160e6ad

Please sign in to comment.