@@ -63,6 +63,7 @@ @implementation DTHTMLAttributedStringBuilder
6363 DTHTMLElement *_rootNode;
6464 DTHTMLElement *_bodyElement;
6565 DTHTMLElement *_currentTag;
66+ BOOL _ignoreParseEvents; // ignores events from parser after first HTML tag was finished
6667}
6768
6869- (id )initWithHTML : (NSData *)data options : (NSDictionary *)options documentAttributes : (NSDictionary **)docAttributes
@@ -75,7 +76,7 @@ - (id)initWithHTML:(NSData *)data options:(NSDictionary *)options documentAttrib
7576
7677 // documentAttributes ignored for now
7778
78- // GCD setup
79+ // GCD setup
7980 _stringAssemblyQueue = dispatch_queue_create (" DTHTMLAttributedStringBuilder" , 0 );
8081 _stringAssemblyGroup = dispatch_group_create ();
8182 _dataParsingQueue = dispatch_queue_create (" DTHTMLAttributedStringBuilderParser" , 0 );
@@ -273,12 +274,6 @@ - (BOOL)_buildString
273274 _defaultParagraphStyle.headIndent = [defaultHeadIndent integerValue ];
274275 }
275276
276- NSNumber *defaultListIndent = [_options objectForKey: DTDefaultListIndent];
277- if (defaultListIndent)
278- {
279- _defaultParagraphStyle.listIndent = [defaultListIndent integerValue ];
280- }
281-
282277 _defaultTag = [[DTHTMLElement alloc ] init ];
283278 _defaultTag.fontDescriptor = _defaultFontDescriptor;
284279 _defaultTag.paragraphStyle = _defaultParagraphStyle;
@@ -402,30 +397,28 @@ - (void)_registerTagStartHandlers
402397 [_tagStartHandlers setObject: [aBlock copy ] forKey: @" a" ];
403398
404399
405- void (^liBlock)(void ) = ^
406- {
407- _currentTag.paragraphStyle .paragraphSpacing = 0 ;
408-
409- DTCSSListStyle *listStyle = [_currentTag.paragraphStyle.textLists lastObject ];
410-
411- if (listStyle.type != DTCSSListStyleTypeNone)
412- {
413- // first tab is to right-align bullet, numbering against
414- CGFloat tabOffset = _currentTag.paragraphStyle .headIndent - 5 .0f *_textScale;
415- [_currentTag.paragraphStyle addTabStopAtPosition: tabOffset alignment: kCTRightTextAlignment ];
416- }
417-
418- // second tab is for the beginning of first line after bullet
419- [_currentTag.paragraphStyle addTabStopAtPosition: _currentTag.paragraphStyle.headIndent alignment: kCTLeftTextAlignment ];
420- };
421-
422- [_tagStartHandlers setObject: [liBlock copy ] forKey: @" li" ];
400+ // void (^liBlock)(void) = ^
401+ // {
402+ // DTCSSListStyle *listStyle = [_currentTag.paragraphStyle.textLists lastObject];
403+ //
404+ // if (listStyle.type != DTCSSListStyleTypeNone)
405+ // {
406+ // // first tab is to right-align bullet, numbering against
407+ // CGFloat tabOffset = _currentTag.paragraphStyle.headIndent - 5.0f*_textScale;
408+ // [_currentTag.paragraphStyle addTabStopAtPosition:tabOffset alignment:kCTRightTextAlignment];
409+ // }
410+ //
411+ // // second tab is for the beginning of first line after bullet
412+ // [_currentTag.paragraphStyle addTabStopAtPosition:_currentTag.paragraphStyle.headIndent alignment: kCTLeftTextAlignment];
413+ // };
414+ //
415+ // [_tagStartHandlers setObject:[liBlock copy] forKey:@"li"];
423416
424417
425418 void (^listBlock)(void ) = ^
426419 {
427420 _currentTag.paragraphStyle .firstLineHeadIndent = _currentTag.paragraphStyle .headIndent ;
428- _currentTag.paragraphStyle .headIndent += _currentTag.paragraphStyle .listIndent ;
421+ // _currentTag.paragraphStyle.headIndent += _currentTag.paragraphStyle.listIndent;
429422
430423 // create the appropriate list style from CSS
431424 DTCSSListStyle *newListStyle = [_currentTag listStyle ];
@@ -646,9 +639,15 @@ - (void)_registerTagEndHandlers
646639
647640- (void )parser : (DTHTMLParser *)parser didStartElement : (NSString *)elementName attributes : (NSDictionary *)attributeDict
648641{
642+
649643 dispatch_group_async (_treeBuildingGroup, _treeBuildingQueue, ^{
650- DTHTMLElement *newNode = [DTHTMLElement elementWithName: elementName attributes: attributeDict options: _options];
651644
645+ if (_ignoreParseEvents)
646+ {
647+ return ;
648+ }
649+
650+ DTHTMLElement *newNode = [DTHTMLElement elementWithName: elementName attributes: attributeDict options: _options];
652651 DTHTMLElement *previousLastChild = nil ;
653652
654653 if (_currentTag)
@@ -670,6 +669,8 @@ - (void)parser:(DTHTMLParser *)parser didStartElement:(NSString *)elementName at
670669 }
671670 else
672671 {
672+ NSAssert (!_rootNode, @" Something went wrong, second root node found in document and not ignored." );
673+
673674 // might be first node ever
674675 if (!_rootNode)
675676 {
@@ -716,7 +717,14 @@ - (void)parser:(DTHTMLParser *)parser didStartElement:(NSString *)elementName at
716717
717718- (void )parser : (DTHTMLParser *)parser didEndElement : (NSString *)elementName
718719{
720+
719721 dispatch_group_async (_treeBuildingGroup, _treeBuildingQueue, ^{
722+
723+ if (_ignoreParseEvents)
724+ {
725+ return ;
726+ }
727+
720728 // output the element if it is direct descendant of body tag, or close of body in case there are direct text nodes
721729
722730 // find block to execute for this tag if any
@@ -783,6 +791,12 @@ - (void)parser:(DTHTMLParser *)parser didEndElement:(NSString *)elementName
783791 // missing end of element, attempt to recover
784792 _currentTag = [_currentTag parentElement ];
785793 }
794+
795+ // closing the root node, ignore everything afterwards
796+ if (_currentTag == _rootNode)
797+ {
798+ _ignoreParseEvents = YES ;
799+ }
786800
787801 // go back up a level
788802 _currentTag = [_currentTag parentElement ];
@@ -795,6 +809,11 @@ - (void)parser:(DTHTMLParser *)parser foundCharacters:(NSString *)string
795809 dispatch_group_async (_treeBuildingGroup, _treeBuildingQueue, ^{
796810 NSAssert (_currentTag, @" Cannot add text node without a current node" );
797811
812+ if (_ignoreParseEvents)
813+ {
814+ return ;
815+ }
816+
798817 if ([string isIgnorableWhitespace ])
799818 {
800819 // ignore whitespace as first element of block element
@@ -853,6 +872,12 @@ - (void)parser:(DTHTMLParser *)parser foundCharacters:(NSString *)string
853872- (void )parser : (DTHTMLParser *)parser foundCDATA : (NSData *)CDATABlock
854873{
855874 dispatch_group_async (_treeBuildingGroup, _treeBuildingQueue, ^{
875+
876+ if (_ignoreParseEvents)
877+ {
878+ return ;
879+ }
880+
856881 NSAssert (_currentTag, @" Cannot add text node without a current node" );
857882
858883 NSString *styleBlock = [[NSString alloc ] initWithData: CDATABlock encoding: NSUTF8StringEncoding];
@@ -866,9 +891,10 @@ - (void)parser:(DTHTMLParser *)parser foundCDATA:(NSData *)CDATABlock
866891
867892- (void )parserDidEndDocument : (DTHTMLParser *)parser
868893{
894+
869895 dispatch_group_async (_treeBuildingGroup, _treeBuildingQueue, ^{
870896 NSAssert (!_currentTag, @" Something went wrong, at end of document there is still an open node" );
871-
897+
872898 dispatch_group_async (_stringAssemblyGroup, _stringAssemblyQueue, ^{
873899 // trim off white space at end
874900 while ([[_tmpString string ] hasSuffixCharacterFromSet: [NSCharacterSet whitespaceCharacterSet ]])
0 commit comments