Skip to content

Commit

Permalink
Implemented option to ignore inline styles closes #649
Browse files Browse the repository at this point in the history
  • Loading branch information
odrobnik committed Nov 15, 2013
1 parent c53bb51 commit 82f9063
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 15 deletions.
3 changes: 1 addition & 2 deletions Core/Source/DTCSSStylesheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@
@param matchedSelectors The CSS selectors that caused a match
@returns The merged style dictionary containing only styles which selector matches the element
*/
- (NSDictionary *)mergedStyleDictionaryForElement:(DTHTMLElement *)element matchedSelectors:(NSSet **)matchedSelectors;

- (NSDictionary *)mergedStyleDictionaryForElement:(DTHTMLElement *)element matchedSelectors:(NSSet **)matchedSelectors ignoreInlineStyle:(BOOL)ignoreInlineStyle;

/**
Returns a dictionary of the styles of the receiver
Expand Down
23 changes: 13 additions & 10 deletions Core/Source/DTCSSStylesheet.m
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ - (void)_addStyles:(NSDictionary *)styles withSelector:(NSString *)selector {

#pragma mark Accessing Style Information

- (NSDictionary *)mergedStyleDictionaryForElement:(DTHTMLElement *)element matchedSelectors:(NSSet **)matchedSelectors
- (NSDictionary *)mergedStyleDictionaryForElement:(DTHTMLElement *)element matchedSelectors:(NSSet **)matchedSelectors ignoreInlineStyle:(BOOL)ignoreInlineStyle
{
// We are going to combine all the relevant styles for this tag.
// (Note that when styles are applied, the later styles take precedence,
Expand Down Expand Up @@ -753,17 +753,20 @@ - (NSDictionary *)mergedStyleDictionaryForElement:(DTHTMLElement *)element match
[tmpMatchedSelectors addObject:idRule];
}

// Get tag's local style attribute
NSString *styleString = [element.attributes objectForKey:@"style"];

if ([styleString length])
if (!ignoreInlineStyle)
{
NSMutableDictionary *localStyles = [[styleString dictionaryOfCSSStyles] mutableCopy];

// need to uncompress because otherwise we might get shorthands and non-shorthands together
[self _uncompressShorthands:localStyles];
// Get tag's local style attribute
NSString *styleString = [element.attributes objectForKey:@"style"];

[tmpDict addEntriesFromDictionary:localStyles];
if ([styleString length])
{
NSMutableDictionary *localStyles = [[styleString dictionaryOfCSSStyles] mutableCopy];

// need to uncompress because otherwise we might get shorthands and non-shorthands together
[self _uncompressShorthands:localStyles];

[tmpDict addEntriesFromDictionary:localStyles];
}
}

if ([tmpDict count])
Expand Down
2 changes: 2 additions & 0 deletions Core/Source/DTCoreTextConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ extern NSString * const DTDefaultStyleSheet;
extern NSString * const DTUseiOS6Attributes;
extern NSString * const DTWillFlushBlockCallBack;
extern NSString * const DTProcessCustomHTMLAttributes;
extern NSString * const DTIgnoreInlineStylesOption;


// attributed string attribute constants

Expand Down
1 change: 1 addition & 0 deletions Core/Source/DTCoreTextConstants.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
NSString * const DTUseiOS6Attributes = @"DTUseiOS6Attributes";
NSString * const DTWillFlushBlockCallBack = @"DTWillFlushBlockCallBack";
NSString * const DTProcessCustomHTMLAttributes = @"DTProcessCustomHTMLAttributes";
NSString * const DTIgnoreInlineStylesOption = @"DTIgnoreInlineStyles";

// attributed string attribute constants

Expand Down
1 change: 1 addition & 0 deletions Core/Source/DTHTMLAttributedStringBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ typedef void(^DTHTMLAttributedStringBuilderWillFlushCallback)(DTHTMLElement *);
- DTDefaultStyleSheet: The default style sheet to use
- DTUseiOS6Attributes: use iOS 6 attributes for building (UITextView compatible)
- DTWillFlushBlockCallBack: a block to be executed whenever content is flushed to the output string
- DTIgnoreInlineStylesOption: All inline style information is being ignored and only style blocks used
@param data The data in HTML format from which to create the attributed string.
@param options Specifies how the document should be loaded. Contains values described in NSAttributedString(HTML).
Expand Down
6 changes: 5 additions & 1 deletion Core/Source/DTHTMLAttributedStringBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ @implementation DTHTMLAttributedStringBuilder
DTHTMLElement *_bodyElement;
DTHTMLElement *_currentTag;
BOOL _ignoreParseEvents; // ignores events from parser after first HTML tag was finished
BOOL _ignoreInlineStyles; // ignores style blocks attached on elements
}

- (id)initWithHTML:(NSData *)data options:(NSDictionary *)options documentAttributes:(NSDictionary **)docAttributes
Expand Down Expand Up @@ -311,6 +312,9 @@ - (BOOL)_buildString

_shouldProcessCustomHTMLAttributes = [[_options objectForKey:DTProcessCustomHTMLAttributes] boolValue];

// ignore inline styles if option is passed
_ignoreInlineStyles = [[_options objectForKey:DTIgnoreInlineStylesOption] boolValue];

// create a parser
DTHTMLParser *parser = [[DTHTMLParser alloc] initWithData:_data encoding:encoding];
parser.delegate = (id)self;
Expand Down Expand Up @@ -712,7 +716,7 @@ - (void)parser:(DTHTMLParser *)parser didStartElement:(NSString *)elementName at

// apply style from merged style sheet
NSSet *matchedSelectors;
NSDictionary *mergedStyles = [_globalStyleSheet mergedStyleDictionaryForElement:newNode matchedSelectors:&matchedSelectors];
NSDictionary *mergedStyles = [_globalStyleSheet mergedStyleDictionaryForElement:newNode matchedSelectors:&matchedSelectors ignoreInlineStyle:_ignoreInlineStyles];

if (mergedStyles)
{
Expand Down
4 changes: 2 additions & 2 deletions Test/Source/DTCSSStylesheetTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ - (void)testMerging
element.fontDescriptor = [[DTCoreTextFontDescriptor alloc] init]; // need to have just any font descriptor
element.textScale = 1.0;

NSDictionary *styles = [stylesheet mergedStyleDictionaryForElement:element matchedSelectors:NULL];
NSDictionary *styles = [stylesheet mergedStyleDictionaryForElement:element matchedSelectors:NULL ignoreInlineStyle:NO];
[element applyStyleDictionary:styles];

STAssertEquals(element.displayStyle, DTHTMLElementDisplayStyleBlock, @"Style merging lost block display style");
Expand Down Expand Up @@ -125,7 +125,7 @@ - (void)testMergeByID
DTHTMLElement *element = [[DTHTMLElement alloc] initWithName:@"dummy" attributes:attributes];

NSSet *matchedSelectors;
NSDictionary *styles = [stylesheet mergedStyleDictionaryForElement:element matchedSelectors:&matchedSelectors];
NSDictionary *styles = [stylesheet mergedStyleDictionaryForElement:element matchedSelectors:&matchedSelectors ignoreInlineStyle:NO];

STAssertTrue([styles count]==1, @"There should be exactly one style");
STAssertTrue([matchedSelectors count]==1, @"There should be exactly one matched selector");
Expand Down

0 comments on commit 82f9063

Please sign in to comment.