Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 1062 lines (890 sloc) 26.958 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
//
// DTHTMLElement.m
// CoreTextExtensions
//
// Created by Oliver Drobnik on 4/14/11.
// Copyright 2011 Drobnik.com. All rights reserved.
//

#import "DTCoreText.h"
#import "DTHTMLElement.h"

@interface DTHTMLElement ()

@property (nonatomic, strong) NSMutableDictionary *fontCache;
@property (nonatomic, strong) NSMutableArray *children;
@property (nonatomic, strong) NSString *linkGUID;

- (DTCSSListStyle *)calculatedListStyle;

@end


@implementation DTHTMLElement
{
DTHTMLElement *parent;

DTCoreTextFontDescriptor *fontDescriptor;
DTCoreTextParagraphStyle *paragraphStyle;
DTTextAttachment *_textAttachment;
DTTextAttachmentVerticalAlignment _textAttachmentAlignment;
NSURL *_link;
NSString *_anchorName;

DTColor *_textColor;
DTColor *backgroundColor;

CTUnderlineStyle underlineStyle;

NSString *tagName;

NSString *beforeContent;
NSString *text;

NSString *_linkGUID;

BOOL tagContentInvisible;
BOOL strikeOut;
NSInteger superscriptStyle;

NSInteger headerLevel;

NSArray *shadows;

NSMutableDictionary *_fontCache;

NSMutableDictionary *_additionalAttributes;

DTHTMLElementDisplayStyle _displayStyle;
DTHTMLElementFloatStyle floatStyle;

BOOL isColorInherited;

BOOL preserveNewlines;

DTHTMLElementFontVariant fontVariant;

CGFloat textScale;
CGSize size;

NSMutableArray *_children;
NSDictionary *_attributes; // contains all attributes from parsing

NSDictionary *_styles;
}

- (id)init
{
self = [super init];
if (self)
{
}

return self;
}

- (NSDictionary *)attributesDictionary
{
NSMutableDictionary *tmpDict = [NSMutableDictionary dictionary];

BOOL shouldAddFont = YES;

// copy additional attributes
if (_additionalAttributes)
{
[tmpDict setDictionary:_additionalAttributes];
}

// add text attachment
if (_textAttachment)
{
#if TARGET_OS_IPHONE
// need run delegate for sizing (only supported on iOS)
CTRunDelegateRef embeddedObjectRunDelegate = createEmbeddedObjectRunDelegate(_textAttachment);
[tmpDict setObject:CFBridgingRelease(embeddedObjectRunDelegate) forKey:(id)kCTRunDelegateAttributeName];
#endif

// add attachment
[tmpDict setObject:_textAttachment forKey:NSAttachmentAttributeName];

// remember original paragraphSpacing
[tmpDict setObject:[NSNumber numberWithFloat:self.paragraphStyle.paragraphSpacing] forKey:DTAttachmentParagraphSpacingAttribute];
#ifndef DT_ADD_FONT_ON_ATTACHMENTS
// omit adding a font unless we need it also on attachments, e.g. for editing
shouldAddFont = NO;
#endif
}

// otherwise we have a font
if (shouldAddFont)
{
// try font cache first
NSNumber *key = [NSNumber numberWithUnsignedInteger:[fontDescriptor hash]];
CTFontRef font = (__bridge CTFontRef)[self.fontCache objectForKey:key];

if (!font)
{
font = [fontDescriptor newMatchingFont];

if (font)
{
[self.fontCache setObject:CFBridgingRelease(font) forKey:key];
}
}

if (font)
{
// __bridge since its already retained elsewhere
[tmpDict setObject:(__bridge id)(font) forKey:(id)kCTFontAttributeName];

// use this font to adjust the values needed for the run delegate during layout time
[_textAttachment adjustVerticalAlignmentForFont:font];
}
}

// add hyperlink
if (_link)
{
[tmpDict setObject:_link forKey:DTLinkAttribute];

// add a GUID to group multiple glyph runs belonging to same link
[tmpDict setObject:_linkGUID forKey:DTGUIDAttribute];
}

// add anchor
if (_anchorName)
{
[tmpDict setObject:_anchorName forKey:DTAnchorAttribute];
}

// add strikout if applicable
if (strikeOut)
{
[tmpDict setObject:[NSNumber numberWithBool:YES] forKey:DTStrikeOutAttribute];
}

// set underline style
if (underlineStyle)
{
[tmpDict setObject:[NSNumber numberWithInteger:underlineStyle] forKey:(id)kCTUnderlineStyleAttributeName];

// we could set an underline color as well if we wanted, but not supported by HTML
// [attributes setObject:(id)[DTImage redColor].CGColor forKey:(id)kCTUnderlineColorAttributeName];
}

if (_textColor)
{
[tmpDict setObject:(id)[_textColor CGColor] forKey:(id)kCTForegroundColorAttributeName];
}

if (backgroundColor)
{
[tmpDict setObject:(id)[backgroundColor CGColor] forKey:DTBackgroundColorAttribute];
}

if (superscriptStyle)
{
[tmpDict setObject:(id)[NSNumber numberWithInteger:superscriptStyle] forKey:(id)kCTSuperscriptAttributeName];
}

// add paragraph style
if (paragraphStyle)
{
CTParagraphStyleRef newParagraphStyle = [self.paragraphStyle createCTParagraphStyle];
[tmpDict setObject:CFBridgingRelease(newParagraphStyle) forKey:(id)kCTParagraphStyleAttributeName];
//CFRelease(newParagraphStyle);
}

// add shadow array if applicable
if (shadows)
{
[tmpDict setObject:shadows forKey:DTShadowsAttribute];
}

// add tag for PRE so that we can omit changing this font if we override fonts
if (preserveNewlines)
{
[tmpDict setObject:[NSNumber numberWithBool:YES] forKey:DTPreserveNewlinesAttribute];
}

if (headerLevel)
{
[tmpDict setObject:[NSNumber numberWithInteger:headerLevel] forKey:DTHeaderLevelAttribute];
}

if (paragraphStyle.textLists)
{
[tmpDict setObject:paragraphStyle.textLists forKey:DTTextListsAttribute];
}

if (paragraphStyle.textBlocks)
{
[tmpDict setObject:paragraphStyle.textBlocks forKey:DTTextBlocksAttribute];
}
return tmpDict;
}

- (NSAttributedString *)attributedString
{
NSDictionary *attributes = [self attributesDictionary];

if (_textAttachment)
{
// ignore text, use unicode object placeholder
NSMutableAttributedString *tmpString = [[NSMutableAttributedString alloc] initWithString:UNICODE_OBJECT_PLACEHOLDER attributes:attributes];

return tmpString;
}
else
{
if (self.fontVariant == DTHTMLElementFontVariantNormal)
{
return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}
else
{
if ([self.fontDescriptor supportsNativeSmallCaps])
{
DTCoreTextFontDescriptor *smallDesc = [self.fontDescriptor copy];
smallDesc.smallCapsFeature = YES;

CTFontRef smallerFont = [smallDesc newMatchingFont];

NSMutableDictionary *smallAttributes = [attributes mutableCopy];
[smallAttributes setObject:CFBridgingRelease(smallerFont) forKey:(id)kCTFontAttributeName];

return [[NSAttributedString alloc] initWithString:text attributes:smallAttributes];
}

return [NSAttributedString synthesizedSmallCapsAttributedStringWithText:text attributes:attributes];
}
}
}

- (void)applyStyleDictionary:(NSDictionary *)styles
{
if (![styles count])
{
return;
}

// keep that for later lookup
_styles = styles;

// register pseudo-selector contents
self.beforeContent = [[_styles objectForKey:@"before:content"] stringByDecodingCSSContentAttribute];

NSString *fontSize = [styles objectForKey:@"font-size"];
if (fontSize)
{
// absolute sizes based on 12.0 CoreText default size, Safari has 16.0

if ([fontSize isEqualToString:@"smaller"])
{
fontDescriptor.pointSize /= 1.2f;
}
else if ([fontSize isEqualToString:@"larger"])
{
fontDescriptor.pointSize *= 1.2f;
}
else if ([fontSize isEqualToString:@"xx-small"])
{
fontDescriptor.pointSize = 9.0f/1.3333f * textScale;
}
else if ([fontSize isEqualToString:@"x-small"])
{
fontDescriptor.pointSize = 10.0f/1.3333f * textScale;
}
else if ([fontSize isEqualToString:@"small"])
{
fontDescriptor.pointSize = 13.0f/1.3333f * textScale;
}
else if ([fontSize isEqualToString:@"medium"])
{
fontDescriptor.pointSize = 16.0f/1.3333f * textScale;
}
else if ([fontSize isEqualToString:@"large"])
{
fontDescriptor.pointSize = 22.0f/1.3333f * textScale;
}
else if ([fontSize isEqualToString:@"x-large"])
{
fontDescriptor.pointSize = 24.0f/1.3333f * textScale;
}
else if ([fontSize isEqualToString:@"xx-large"])
{
fontDescriptor.pointSize = 37.0f/1.3333f * textScale;
}
else if ([fontSize isEqualToString:@"inherit"])
{
fontDescriptor.pointSize = parent.fontDescriptor.pointSize;
}
else
{
fontDescriptor.pointSize = [fontSize pixelSizeOfCSSMeasureRelativeToCurrentTextSize:fontDescriptor.pointSize]; // already multiplied with textScale
}
}

NSString *color = [styles objectForKey:@"color"];
if (color)
{
self.textColor = [DTColor colorWithHTMLName:color];
}

NSString *bgColor = [styles objectForKey:@"background-color"];
if (bgColor)
{
self.backgroundColor = [DTColor colorWithHTMLName:bgColor];
}

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

if (floatString)
{
if ([floatString isEqualToString:@"left"])
{
floatStyle = DTHTMLElementFloatStyleLeft;
}
else if ([floatString isEqualToString:@"right"])
{
floatStyle = DTHTMLElementFloatStyleRight;
}
else if ([floatString isEqualToString:@"none"])
{
floatStyle = DTHTMLElementFloatStyleNone;
}
}

NSString *fontFamily = [[styles objectForKey:@"font-family"] stringByTrimmingCharactersInSet:[NSCharacterSet quoteCharacterSet]];

if (fontFamily)
{
NSString *lowercaseFontFamily = [fontFamily lowercaseString];

if ([lowercaseFontFamily rangeOfString:@"geneva"].length)
{
fontDescriptor.fontFamily = @"Helvetica";
}
else if ([lowercaseFontFamily rangeOfString:@"cursive"].length)
{
fontDescriptor.stylisticClass = kCTFontScriptsClass;
fontDescriptor.fontFamily = nil;
}
else if ([lowercaseFontFamily rangeOfString:@"sans-serif"].length)
{
// too many matches (24)
// fontDescriptor.stylisticClass = kCTFontSansSerifClass;
fontDescriptor.fontFamily = @"Helvetica";
}
else if ([lowercaseFontFamily rangeOfString:@"serif"].length)
{
// kCTFontTransitionalSerifsClass = Baskerville
// kCTFontClarendonSerifsClass = American Typewriter
// kCTFontSlabSerifsClass = Courier New
//
// strangely none of the classes yields Times
fontDescriptor.fontFamily = @"Times New Roman";
}
else if ([lowercaseFontFamily rangeOfString:@"fantasy"].length)
{
fontDescriptor.fontFamily = @"Papyrus"; // only available on iPad
}
else if ([lowercaseFontFamily rangeOfString:@"monospace"].length)
{
fontDescriptor.monospaceTrait = YES;
fontDescriptor.fontFamily = @"Courier";
}
else if ([lowercaseFontFamily rangeOfString:@"times"].length)
{
fontDescriptor.fontFamily = @"Times New Roman";
}
else
{
// probably custom font registered in info.plist
fontDescriptor.fontFamily = fontFamily;
}
}

NSString *fontStyle = [[styles objectForKey:@"font-style"] lowercaseString];
if (fontStyle)
{
if ([fontStyle isEqualToString:@"normal"])
{
fontDescriptor.italicTrait = NO;
}
else if ([fontStyle isEqualToString:@"italic"] || [fontStyle isEqualToString:@"oblique"])
{
fontDescriptor.italicTrait = YES;
}
else if ([fontStyle isEqualToString:@"inherit"])
{
// nothing to do
}
}

NSString *fontWeight = [[styles objectForKey:@"font-weight"] lowercaseString];
if (fontWeight)
{
if ([fontWeight isEqualToString:@"normal"])
{
fontDescriptor.boldTrait = NO;
}
else if ([fontWeight isEqualToString:@"bold"])
{
fontDescriptor.boldTrait = YES;
}
else if ([fontWeight isEqualToString:@"bolder"])
{
fontDescriptor.boldTrait = YES;
}
else if ([fontWeight isEqualToString:@"lighter"])
{
fontDescriptor.boldTrait = NO;
}
else
{
// can be 100 - 900

NSInteger value = [fontWeight intValue];

if (value<=600)
{
fontDescriptor.boldTrait = NO;
}
else
{
fontDescriptor.boldTrait = YES;
}
}
}


NSString *decoration = [[styles objectForKey:@"text-decoration"] lowercaseString];
if (decoration)
{
if ([decoration isEqualToString:@"underline"])
{
self.underlineStyle = kCTUnderlineStyleSingle;
}
else if ([decoration isEqualToString:@"line-through"])
{
self.strikeOut = YES;
}
else if ([decoration isEqualToString:@"none"])
{
// remove all
self.underlineStyle = kCTUnderlineStyleNone;
self.strikeOut = NO;
}
else if ([decoration isEqualToString:@"overline"])
{
//TODO: add support for overline decoration
}
else if ([decoration isEqualToString:@"blink"])
{
//TODO: add support for blink decoration
}
else if ([decoration isEqualToString:@"inherit"])
{
// nothing to do
}
}

NSString *alignment = [[styles objectForKey:@"text-align"] lowercaseString];
if (alignment)
{
if ([alignment isEqualToString:@"left"])
{
self.paragraphStyle.alignment = kCTLeftTextAlignment;
}
else if ([alignment isEqualToString:@"right"])
{
self.paragraphStyle.alignment = kCTRightTextAlignment;
}
else if ([alignment isEqualToString:@"center"])
{
self.paragraphStyle.alignment = kCTCenterTextAlignment;
}
else if ([alignment isEqualToString:@"justify"])
{
self.paragraphStyle.alignment = kCTJustifiedTextAlignment;
}
else if ([alignment isEqualToString:@"inherit"])
{
// nothing to do
}
}

NSString *verticalAlignment = [[styles objectForKey:@"vertical-align"] lowercaseString];
if (verticalAlignment)
{
if ([verticalAlignment isEqualToString:@"sub"])
{
self.superscriptStyle = -1;
}
else if ([verticalAlignment isEqualToString:@"super"])
{
self.superscriptStyle = +1;
}
else if ([verticalAlignment isEqualToString:@"baseline"])
{
self.superscriptStyle = 0;
}
else if ([verticalAlignment isEqualToString:@"inherit"])
{
// nothing to do
}
}

NSString *shadow = [styles objectForKey:@"text-shadow"];
if (shadow)
{
self.shadows = [shadow arrayOfCSSShadowsWithCurrentTextSize:fontDescriptor.pointSize currentColor:_textColor];
}

NSString *lineHeight = [[styles objectForKey:@"line-height"] lowercaseString];
if (lineHeight)
{
if ([lineHeight isEqualToString:@"normal"])
{
self.paragraphStyle.lineHeightMultiple = 0.0; // default
self.paragraphStyle.minimumLineHeight = 0.0; // default
self.paragraphStyle.maximumLineHeight = 0.0; // default
}
else if ([lineHeight isEqualToString:@"inherit"])
{
// no op, we already inherited it
}
else if ([lineHeight isNumeric])
{
self.paragraphStyle.lineHeightMultiple = [lineHeight floatValue];
}
else // interpret as length
{
self.paragraphStyle.minimumLineHeight = [lineHeight pixelSizeOfCSSMeasureRelativeToCurrentTextSize:fontDescriptor.pointSize];
self.paragraphStyle.maximumLineHeight = self.paragraphStyle.minimumLineHeight;
}
}

NSString *marginBottom = [styles objectForKey:@"margin-bottom"];
if (marginBottom)
{
self.paragraphStyle.paragraphSpacing = [marginBottom pixelSizeOfCSSMeasureRelativeToCurrentTextSize:fontDescriptor.pointSize];
}
else
{
NSString *webkitMarginAfter = [styles objectForKey:@"-webkit-margin-after"];
if (webkitMarginAfter)
{
self.paragraphStyle.paragraphSpacing = [webkitMarginAfter pixelSizeOfCSSMeasureRelativeToCurrentTextSize:fontDescriptor.pointSize];
}
}
NSString *fontVariantStr = [[styles objectForKey:@"font-variant"] lowercaseString];
if (fontVariantStr)
{
if ([fontVariantStr isEqualToString:@"small-caps"])
{
fontVariant = DTHTMLElementFontVariantSmallCaps;
}
else if ([fontVariantStr isEqualToString:@"inherit"])
{
fontVariant = DTHTMLElementFontVariantInherit;
}
else
{
fontVariant = DTHTMLElementFontVariantNormal;
}
}

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

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

NSString *whitespaceString = [styles objectForKey:@"white-space"];
if ([whitespaceString hasPrefix:@"pre"])
{
preserveNewlines = YES;
}
else
{
preserveNewlines = NO;
}

NSString *displayString = [styles objectForKey:@"display"];
if (displayString)
{
if ([displayString isEqualToString:@"none"])
{
_displayStyle = DTHTMLElementDisplayStyleNone;
}
else if ([displayString isEqualToString:@"block"])
{
_displayStyle = DTHTMLElementDisplayStyleBlock;
}
else if ([displayString isEqualToString:@"inline"])
{
_displayStyle = DTHTMLElementDisplayStyleInline;
}
else if ([displayString isEqualToString:@"list-item"])
{
_displayStyle = DTHTMLElementDisplayStyleListItem;
}
else if ([displayString isEqualToString:@"table"])
{
_displayStyle = DTHTMLElementDisplayStyleTable;
}
else if ([verticalAlignment isEqualToString:@"inherit"])
{
// nothing to do
}
}

// only works for objects!
NSString *verticalAlignString = [styles objectForKey:@"vertical-align"];
if (verticalAlignString)
{
if ([verticalAlignString isEqualToString:@"text-top"])
{
_textAttachmentAlignment = DTTextAttachmentVerticalAlignmentTop;
}
else if ([verticalAlignString isEqualToString:@"middle"])
{
_textAttachmentAlignment = DTTextAttachmentVerticalAlignmentCenter;
}
else if ([verticalAlignString isEqualToString:@"text-bottom"])
{
_textAttachmentAlignment = DTTextAttachmentVerticalAlignmentBottom;
}
else if ([verticalAlignString isEqualToString:@"baseline"])
{
_textAttachmentAlignment = DTTextAttachmentVerticalAlignmentBaseline;
}
}


DTEdgeInsets padding = {0,0,0,0};

// webkit default value
NSString *webkitPaddingStart = [styles objectForKey:@"-webkit-padding-start"];

if (webkitPaddingStart)
{
self.paragraphStyle.listIndent = [webkitPaddingStart pixelSizeOfCSSMeasureRelativeToCurrentTextSize:self.fontDescriptor.pointSize];
}

BOOL needsTextBlock = (backgroundColor!=nil);

NSString *paddingString = [styles objectForKey:@"padding"];

if (paddingString)
{
// maybe it's using the short style
NSArray *parts = [paddingString componentsSeparatedByString:@" "];

if ([parts count] == 4)
{
padding.top = [[parts objectAtIndex:0] pixelSizeOfCSSMeasureRelativeToCurrentTextSize:self.fontDescriptor.pointSize];
padding.right = [[parts objectAtIndex:1] pixelSizeOfCSSMeasureRelativeToCurrentTextSize:self.fontDescriptor.pointSize];
padding.bottom = [[parts objectAtIndex:2] pixelSizeOfCSSMeasureRelativeToCurrentTextSize:self.fontDescriptor.pointSize];
padding.left = [[parts objectAtIndex:3] pixelSizeOfCSSMeasureRelativeToCurrentTextSize:self.fontDescriptor.pointSize];
}
else if ([parts count] == 3)
{
padding.top = [[parts objectAtIndex:0] pixelSizeOfCSSMeasureRelativeToCurrentTextSize:self.fontDescriptor.pointSize];
padding.right = [[parts objectAtIndex:1] pixelSizeOfCSSMeasureRelativeToCurrentTextSize:self.fontDescriptor.pointSize];
padding.bottom = [[parts objectAtIndex:2] pixelSizeOfCSSMeasureRelativeToCurrentTextSize:self.fontDescriptor.pointSize];
padding.left = padding.right;
}
else if ([parts count] == 2)
{
padding.top = [[parts objectAtIndex:0] pixelSizeOfCSSMeasureRelativeToCurrentTextSize:self.fontDescriptor.pointSize];
padding.right = [[parts objectAtIndex:1] pixelSizeOfCSSMeasureRelativeToCurrentTextSize:self.fontDescriptor.pointSize];
padding.bottom = padding.top;
padding.left = padding.right;
}
else
{
CGFloat paddingAmount = [paddingString pixelSizeOfCSSMeasureRelativeToCurrentTextSize:self.fontDescriptor.pointSize];
padding = DTEdgeInsetsMake(paddingAmount, paddingAmount, paddingAmount, paddingAmount);
}

// left padding overrides webkit list indent
self.paragraphStyle.listIndent = padding.left;

needsTextBlock = YES;
}
else
{
paddingString = [styles objectForKey:@"padding-left"];

if (paddingString)
{
padding.left = [paddingString pixelSizeOfCSSMeasureRelativeToCurrentTextSize:self.fontDescriptor.pointSize];
needsTextBlock = YES;

// left padding overrides webkit list indent
self.paragraphStyle.listIndent = padding.left;
}

paddingString = [styles objectForKey:@"padding-top"];

if (paddingString)
{
padding.top = [paddingString pixelSizeOfCSSMeasureRelativeToCurrentTextSize:self.fontDescriptor.pointSize];
needsTextBlock = YES;
}

paddingString = [styles objectForKey:@"padding-right"];

if (paddingString)
{
padding.right = [paddingString pixelSizeOfCSSMeasureRelativeToCurrentTextSize:self.fontDescriptor.pointSize];
needsTextBlock = YES;
}

paddingString = [styles objectForKey:@"padding-bottom"];

if (paddingString)
{
padding.bottom = [paddingString pixelSizeOfCSSMeasureRelativeToCurrentTextSize:self.fontDescriptor.pointSize];
needsTextBlock = YES;
}
}

if (_displayStyle == DTHTMLElementDisplayStyleBlock)
{
if (needsTextBlock)
{
// need a block
DTTextBlock *newBlock = [[DTTextBlock alloc] init];

newBlock.padding = padding;

// transfer background color to block
newBlock.backgroundColor = backgroundColor;
backgroundColor = nil;

NSArray *newBlocks = [self.paragraphStyle.textBlocks mutableCopy];

if (!newBlocks)
{
// need an array, this is the first block
newBlocks = [NSArray arrayWithObject:newBlock];
}

self.paragraphStyle.textBlocks = newBlocks;
}
}
}

- (NSDictionary *)styles
{
return _styles;
}

- (void)parseStyleString:(NSString *)styleString
{
NSDictionary *styles = [styleString dictionaryOfCSSStyles];
[self applyStyleDictionary:styles];
}

- (void)addAdditionalAttribute:(id)attribute forKey:(id)key
{
if (!_additionalAttributes)
{
_additionalAttributes = [[NSMutableDictionary alloc] init];
}

[_additionalAttributes setObject:attribute forKey:key];
}

- (void)addChild:(DTHTMLElement *)child
{
child.parent = self;
[self.children addObject:child];
}

- (void)removeChild:(DTHTMLElement *)child
{
child.parent = nil;
[self.children removeObject:child];
}

- (DTHTMLElement *)parentWithTagName:(NSString *)name
{
if ([self.parent.tagName isEqualToString:name])
{
return self.parent;
}

return [self.parent parentWithTagName:name];
}

- (BOOL)isContainedInBlockElement
{
if (!parent || !parent.tagName) // default tag has no tag name
{
return NO;
}

if (self.parent.displayStyle == DTHTMLElementDisplayStyleInline)
{
return [self.parent isContainedInBlockElement];
}

return YES;
}

- (NSString *)attributeForKey:(NSString *)key
{
return [_attributes objectForKey:key];
}

#pragma mark Calulcating Properties

- (id)valueForKeyPathWithInheritance:(NSString *)keyPath
{


id value = [self valueForKeyPath:keyPath];

// if property is not set we also go to parent
if (!value && parent)
{
return [parent valueForKeyPathWithInheritance:keyPath];
}

// enum properties have 0 for inherit
if ([value isKindOfClass:[NSNumber class]])
{
NSNumber *number = value;

if (([number integerValue]==0) && parent)
{
return [parent valueForKeyPathWithInheritance:keyPath];
}
}

// string properties have 'inherit' for inheriting
if ([value isKindOfClass:[NSString class]])
{
NSString *string = value;

if ([string isEqualToString:@"inherit"] && parent)
{
return [parent valueForKeyPathWithInheritance:keyPath];
}
}

// obviously not inherited
return value;
}


- (DTCSSListStyle *)calculatedListStyle
{
DTCSSListStyle *style = [[DTCSSListStyle alloc] init];

id calcType = [self valueForKeyPathWithInheritance:@"listStyle.type"];
id calcPos = [self valueForKeyPathWithInheritance:@"listStyle.position"];
id calcImage = [self valueForKeyPathWithInheritance:@"listStyle.imageName"];

style.type = (DTCSSListStyleType)[calcType integerValue];
style.position = (DTCSSListStylePosition)[calcPos integerValue];
style.imageName = calcImage;

return style;
}

#pragma mark Copying

- (id)copyWithZone:(NSZone *)zone
{
DTHTMLElement *newObject = [[DTHTMLElement allocWithZone:zone] init];

newObject.fontDescriptor = self.fontDescriptor; // copy
newObject.paragraphStyle = self.paragraphStyle; // copy

newObject.fontVariant = self.fontVariant;

newObject.underlineStyle = self.underlineStyle;
newObject.tagContentInvisible = self.tagContentInvisible;
newObject.textColor = self.textColor;
newObject.isColorInherited = YES;

newObject.strikeOut = self.strikeOut;
newObject.superscriptStyle = self.superscriptStyle;
newObject.shadows = self.shadows;

newObject.link = self.link; // copy
newObject.anchorName = self.anchorName; // copy
newObject.linkGUID = _linkGUID; // transfer the GUID

newObject.preserveNewlines = self.preserveNewlines;

newObject.fontCache = self.fontCache; // reference

return newObject;
}

#pragma mark Properties

- (NSMutableDictionary *)fontCache
{
if (!_fontCache)
{
_fontCache = [[NSMutableDictionary alloc] init];
}

return _fontCache;
}

- (void)setTextColor:(DTColor *)textColor
{
if (_textColor != textColor)
{

_textColor = textColor;
isColorInherited = NO;
}
}

- (DTHTMLElementFontVariant)fontVariant
{
if (fontVariant == DTHTMLElementFontVariantInherit)
{
if (parent)
{
return parent.fontVariant;
}

return DTHTMLElementFontVariantNormal;
}

return fontVariant;
}

- (NSString *)path
{
if (parent)
{
return [[parent path] stringByAppendingFormat:@"/%@", self.tagName];
}

if (tagName)
{
return tagName;
}

return @"root";
}

- (NSMutableArray *)children
{
if (!_children)
{
_children = [[NSMutableArray alloc] init];
}

return _children;
}

- (void)setAttributes:(NSDictionary *)attributes
{
if (_attributes != attributes)
{
_attributes = attributes;

// decode size contained in attributes, might be overridden later by CSS size
size = CGSizeMake([[self attributeForKey:@"width"] floatValue], [[self attributeForKey:@"height"] floatValue]);
}
}

- (void)setTextAttachment:(DTTextAttachment *)textAttachment
{
textAttachment.verticalAlignment = _textAttachmentAlignment;
_textAttachment = textAttachment;

// transfer link GUID
_textAttachment.hyperLinkGUID = _linkGUID;
}

- (void)setLink:(NSURL *)link
{
_linkGUID = [NSString guid];
_link = [link copy];

if (_textAttachment)
{
_textAttachment.hyperLinkGUID = _linkGUID;
}
}

@synthesize parent;
@synthesize fontDescriptor;
@synthesize paragraphStyle;
@synthesize textColor = _textColor;
@synthesize backgroundColor;
@synthesize tagName;
@synthesize beforeContent;
@synthesize text;
@synthesize link = _link;
@synthesize anchorName = _anchorName;
@synthesize underlineStyle;
@synthesize textAttachment = _textAttachment;
@synthesize tagContentInvisible;
@synthesize strikeOut;
@synthesize superscriptStyle;
@synthesize headerLevel;
@synthesize shadows;
@synthesize floatStyle;
@synthesize isColorInherited;
@synthesize preserveNewlines;
@synthesize displayStyle = _displayStyle;
@synthesize fontVariant;
@synthesize textScale;
@synthesize size;

@synthesize fontCache = _fontCache;
@synthesize children = _children;
@synthesize attributes = _attributes;
@synthesize linkGUID = _linkGUID;

@end
Something went wrong with that request. Please try again.