Skip to content

Commit

Permalink
Update DTHTMLElement.m
Browse files Browse the repository at this point in the history
Add type safety determination, fixes Cocoanetics#1243
  • Loading branch information
卢荣华 committed Oct 26, 2021
1 parent a5fdc85 commit 83dfa41
Showing 1 changed file with 47 additions and 26 deletions.
73 changes: 47 additions & 26 deletions Core/Source/DTHTMLElement.m
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ - (void)applyStyleDictionary:(NSDictionary *)styles
// writing direction
NSString *directionStr = [_styles objectForKey:@"direction"];

if (directionStr)
if (directionStr && [directionStr isKindOfClass:[NSString class]])
{
if ([directionStr isEqualToString:@"rtl"])
{
Expand All @@ -779,8 +779,24 @@ - (void)applyStyleDictionary:(NSDictionary *)styles
// register pseudo-selector contents
self.beforeContent = [[_styles objectForKey:@"before:content"] stringByDecodingCSSContentAttribute];

NSString *fontSize = [styles objectForKey:@"font-size"];
if (fontSize)
id fontSizeObj = [styles objectForKey:@"font-size"];
NSString *fontSize = nil;
if (fontSizeObj)
{
if ([fontSizeObj isKindOfClass:[NSString class]])
{
fontSize = fontSizeObj;
}
else if ([fontSizeObj isKindOfClass:[NSArray class]])
{
id fontSizeLastObj = ((NSArray *)fontSizeObj).lastObject;
if ([fontSizeLastObj isKindOfClass:[NSString class]])
{
fontSize = fontSizeLastObj;
}
}
}
if (fontSize && [fontSize isKindOfClass:[NSString class]])
{
// absolute sizes based on 12.0 CoreText default size, Safari has 16.0

Expand Down Expand Up @@ -835,20 +851,20 @@ - (void)applyStyleDictionary:(NSDictionary *)styles
}

NSString *color = [styles objectForKey:@"color"];
if (color)
if (color && [color isKindOfClass:[NSString class]])
{
self.textColor = DTColorCreateWithHTMLName(color);
}

NSString *bgColor = [styles objectForKey:@"background-color"];
if (bgColor)
if (bgColor && [bgColor isKindOfClass:[NSString class]])
{
self.backgroundColor = DTColorCreateWithHTMLName(bgColor);
}

NSString *floatString = [styles objectForKey:@"float"];

if (floatString)
if (floatString && [floatString isKindOfClass:[NSString class]])
{
if ([floatString isEqualToString:@"left"])
{
Expand Down Expand Up @@ -883,6 +899,10 @@ - (void)applyStyleDictionary:(NSDictionary *)styles

for (NSString *fontFamily in fontFamilies)
{
if (![fontFamily isKindOfClass:[NSString class]]) {
continue;
}

_fontDescriptor.fontFamily = fontFamily;

// check if this is a known font family
Expand Down Expand Up @@ -972,7 +992,7 @@ - (void)applyStyleDictionary:(NSDictionary *)styles
}

NSString *fontStyle = [[styles objectForKey:@"font-style"] lowercaseString];
if (fontStyle)
if (fontStyle && [fontStyle isKindOfClass:[NSString class]])
{
// remove font name since this would cause font creation to ignore the trait
_fontDescriptor.fontName = nil;
Expand All @@ -992,7 +1012,7 @@ - (void)applyStyleDictionary:(NSDictionary *)styles
}

NSString *fontWeight = [[styles objectForKey:@"font-weight"] lowercaseString];
if (fontWeight)
if (fontWeight && [fontWeight isKindOfClass:[NSString class]])
{
// remove font name since this would cause font creation to ignore the trait
_fontDescriptor.fontName = nil;
Expand Down Expand Up @@ -1031,7 +1051,7 @@ - (void)applyStyleDictionary:(NSDictionary *)styles
}

NSString *decoration = [[styles objectForKey:@"text-decoration"] lowercaseString];
if (decoration)
if (decoration && [decoration isKindOfClass:[NSString class]])
{
if ([decoration isEqualToString:@"underline"])
{
Expand Down Expand Up @@ -1062,7 +1082,7 @@ - (void)applyStyleDictionary:(NSDictionary *)styles
}

NSString *alignment = [[styles objectForKey:@"text-align"] lowercaseString];
if (alignment)
if (alignment && [alignment isKindOfClass:[NSString class]])
{
if ([alignment isEqualToString:@"left"])
{
Expand Down Expand Up @@ -1103,7 +1123,7 @@ - (void)applyStyleDictionary:(NSDictionary *)styles
}

NSString *verticalAlignment = [[styles objectForKey:@"vertical-align"] lowercaseString];
if (verticalAlignment)
if (verticalAlignment && [verticalAlignment isKindOfClass:[NSString class]])
{
if ([verticalAlignment isEqualToString:@"sub"])
{
Expand Down Expand Up @@ -1140,7 +1160,7 @@ - (void)applyStyleDictionary:(NSDictionary *)styles
}

NSString *letterSpacing = [[styles objectForKey:@"letter-spacing"] lowercaseString];
if (letterSpacing)
if (letterSpacing && [letterSpacing isKindOfClass:[NSString class]])
{
if ([letterSpacing isEqualToString:@"normal"])
{
Expand All @@ -1167,7 +1187,7 @@ - (void)applyStyleDictionary:(NSDictionary *)styles
}

NSString *lineHeight = [[styles objectForKey:@"line-height"] lowercaseString];
if (lineHeight)
if (lineHeight && [lineHeight isKindOfClass:[NSString class]])
{
if ([lineHeight isEqualToString:@"normal"])
{
Expand All @@ -1194,7 +1214,7 @@ - (void)applyStyleDictionary:(NSDictionary *)styles
// Specializations on line-height:

NSString *minimumLineHeight = [[styles objectForKey:@"minimum-line-height"] lowercaseString];
if (minimumLineHeight)
if (minimumLineHeight && [minimumLineHeight isKindOfClass:[NSString class]])
{
if ([minimumLineHeight isEqualToString:@"normal"])
{
Expand All @@ -1216,7 +1236,7 @@ - (void)applyStyleDictionary:(NSDictionary *)styles
}

NSString *maximumLineHeight = [[styles objectForKey:@"maximum-line-height"] lowercaseString];
if (maximumLineHeight)
if (maximumLineHeight && [maximumLineHeight isKindOfClass:[NSString class]])
{
if ([maximumLineHeight isEqualToString:@"normal"])
{
Expand All @@ -1242,7 +1262,7 @@ - (void)applyStyleDictionary:(NSDictionary *)styles


NSString *fontVariantStr = [[styles objectForKey:@"font-variant"] lowercaseString];
if (fontVariantStr)
if (fontVariantStr && [fontVariantStr isKindOfClass:[NSString class]])
{
if ([fontVariantStr isEqualToString:@"small-caps"])
{
Expand All @@ -1259,19 +1279,19 @@ - (void)applyStyleDictionary:(NSDictionary *)styles
}

NSString *widthString = [styles objectForKey:@"width"];
if (widthString && ![widthString isEqualToString:@"auto"])
if (widthString && [widthString isKindOfClass:[NSString class]] && ![widthString isEqualToString:@"auto"])
{
_size.width = [widthString pixelSizeOfCSSMeasureRelativeToCurrentTextSize:self.fontDescriptor.pointSize textScale:_textScale];
}

NSString *heightString = [styles objectForKey:@"height"];
if (heightString && ![heightString isEqualToString:@"auto"])
if (heightString && [heightString isKindOfClass:[NSString class]] && ![heightString isEqualToString:@"auto"])
{
_size.height = [heightString pixelSizeOfCSSMeasureRelativeToCurrentTextSize:self.fontDescriptor.pointSize textScale:_textScale];
}

NSString *whitespaceString = [styles objectForKey:@"white-space"];
if ([whitespaceString hasPrefix:@"pre"])
if ([whitespaceString isKindOfClass:[NSString class]] && [whitespaceString hasPrefix:@"pre"])
{
_preserveNewlines = YES;
}
Expand All @@ -1281,7 +1301,7 @@ - (void)applyStyleDictionary:(NSDictionary *)styles
}

NSString *displayString = [styles objectForKey:@"display"];
if (displayString)
if (displayString && [displayString isKindOfClass:[NSString class]])
{
if ([displayString isEqualToString:@"none"])
{
Expand Down Expand Up @@ -1310,20 +1330,20 @@ - (void)applyStyleDictionary:(NSDictionary *)styles
}

NSString *borderColor = [styles objectForKey:@"border-color"];
if (borderColor)
if (borderColor && [borderColor isKindOfClass:[NSString class]])
{
self.backgroundStrokeColor = DTColorCreateWithHTMLName(borderColor);
}
NSString *borderWidth = [[styles objectForKey:@"border-width"] lowercaseString];
if (borderWidth)
if (borderWidth && [borderWidth isKindOfClass:[NSString class]])
{
_backgroundStrokeWidth = [borderWidth floatValue];
}
else {
_backgroundStrokeWidth = 0.0f;
}
NSString *cornerRadius = [[styles objectForKey:@"border-radius"] lowercaseString];
if (cornerRadius)
if (cornerRadius && [cornerRadius isKindOfClass:[NSString class]])
{
_backgroundCornerRadius = [cornerRadius floatValue];
}
Expand All @@ -1332,7 +1352,7 @@ - (void)applyStyleDictionary:(NSDictionary *)styles
}

NSString *textIndentStr = [styles objectForKey:@"text-indent"];
if (textIndentStr && [textIndentStr isCSSLengthValue])
if (textIndentStr && [textIndentStr isKindOfClass:[NSString class]] && [textIndentStr isCSSLengthValue])
{
_pTextIndent = [textIndentStr pixelSizeOfCSSMeasureRelativeToCurrentTextSize:_currentTextSize textScale:_textScale];
}
Expand Down Expand Up @@ -1434,7 +1454,7 @@ - (void)applyStyleDictionary:(NSDictionary *)styles
}

NSString *coretextFontString = [styles objectForKey:@"-coretext-fontname"];
if (coretextFontString)
if (coretextFontString && [coretextFontString isKindOfClass:[NSString class]])
{
_fontDescriptor.fontName = [styles objectForKey:@"-coretext-fontname"];
}
Expand Down Expand Up @@ -1502,7 +1522,8 @@ + (NSSet *)attributesToIgnoreForCustomAttributesAttribute

- (NSString *)attributeForKey:(NSString *)key
{
return [_attributes objectForKey:key];
NSString *attribute = [_attributes objectForKey:key];
return [attribute isKindOfClass:[NSString class]] ? attribute : nil;
}

- (void)inheritAttributesFromElement:(DTHTMLElement *)element
Expand Down

0 comments on commit 83dfa41

Please sign in to comment.