CSSStyleSheet.m 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #import "CSSStyleSheet.h"
  2. #import "CSSRuleList+Mutable.h"
  3. #import "CSSStyleRule.h"
  4. @implementation CSSStyleSheet
  5. @synthesize ownerRule;
  6. @synthesize cssRules;
  7. /**
  8. Used to insert a new rule into the style sheet. The new rule now becomes part of the cascade.
  9. Parameters
  10. rule of type DOMString
  11. The parsable text representing the rule. For rule sets this contains both the selector and the style declaration. For at-rules, this specifies both the at-identifier and the rule content.
  12. index of type unsigned long
  13. The index within the style sheet's rule list of the rule before which to insert the specified rule. If the specified index is equal to the length of the style sheet's rule collection, the rule will be added to the end of the style sheet.
  14. Return Value
  15. unsigned long The index within the style sheet's rule collection of the newly inserted rule.
  16. */
  17. -(long)insertRule:(NSString *)rule index:(unsigned long)index
  18. {
  19. if( index == self.cssRules.length )
  20. index = self.cssRules.length + 1; // forces it to insert "before the one that doesn't exist" (stupid API design!)
  21. NSCharacterSet *whitespaceSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
  22. rule = [rule stringByTrimmingCharactersInSet:whitespaceSet];
  23. // SVGKitLogVerbose(@"A substringie %@", idStyleString);
  24. NSArray* stringSplitContainer = [rule componentsSeparatedByString:@"{"];
  25. if( [stringSplitContainer count] >= 2 ) //not necessary unless using shitty svgs
  26. {
  27. CSSStyleRule* newRule = [[CSSStyleRule alloc] initWithSelectorText:[stringSplitContainer objectAtIndex:0] styleText:[stringSplitContainer objectAtIndex:1]];
  28. [self.cssRules.internalArray insertObject:newRule atIndex:index-1]; // CSS says you insert "BEFORE" the index, which is the opposite of most C-based programming languages
  29. return index-1;
  30. }
  31. else
  32. NSAssert(FALSE, @"No idea what to do here");
  33. return -1; // failed, assert fired!
  34. }
  35. -(void)deleteRule:(unsigned long)index
  36. {
  37. [self.cssRules.internalArray removeObjectAtIndex:index];
  38. }
  39. #pragma mark - methods needed for ObjectiveC implementation
  40. - (id)initWithString:(NSString*) styleSheetBody
  41. {
  42. self = [super init];
  43. if (self)
  44. {
  45. self.cssRules = [[CSSRuleList alloc]init];
  46. @autoreleasepool { //creating lots of autoreleased strings, not helpful for older devices
  47. /**
  48. We have to manually handle the "ignore anything that is between / * and * / because those are comments"
  49. NB: you NEED the NSRegularExpressionDotMatchesLineSeparators argument - which Apple DOES NOT HONOUR in NSString - hence have to use NSRegularExpression
  50. */
  51. NSError* error;
  52. NSRegularExpression* regexp = [NSRegularExpression regularExpressionWithPattern:@"/\\*.*?\\*/" options: NSRegularExpressionDotMatchesLineSeparators error:&error];
  53. styleSheetBody = [regexp stringByReplacingMatchesInString:styleSheetBody options:0 range:NSMakeRange(0,styleSheetBody.length) withTemplate:@""];
  54. NSArray *classNameAndStyleStrings = [styleSheetBody componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"}"]];
  55. for( NSString *idStyleString in classNameAndStyleStrings )
  56. {
  57. if( [idStyleString length] > 1 ) //not necessary unless using shitty svgs
  58. {
  59. [self insertRule:idStyleString index:self.cssRules.length];
  60. }
  61. }
  62. }
  63. }
  64. return self;
  65. }
  66. @end