SVGKParserStyles.m 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // SVGStyleParser.m
  3. // SVGPad
  4. //
  5. // Created by Kevin Stich on 2/2/12.
  6. // Copyright (c) 2012 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "SVGKParserStyles.h"
  9. #import "CSSStyleSheet.h"
  10. #import "StyleSheetList+Mutable.h"
  11. @interface SVGKParserStyles ()
  12. @property (nonatomic) NSArray *supportedNamespaces;
  13. @property (nonatomic) NSArray *supportedTags;
  14. @end
  15. @implementation SVGKParserStyles
  16. -(NSArray *)supportedNamespaces
  17. {
  18. if( _supportedNamespaces == nil )
  19. _supportedNamespaces = @[@"http://www.w3.org/2000/svg"];
  20. return _supportedNamespaces;
  21. }
  22. -(NSArray *)supportedTags
  23. {
  24. if( _supportedTags == nil )
  25. _supportedTags = @[@"style"];
  26. return _supportedTags;
  27. }
  28. -(Node *)handleStartElement:(NSString *)name document:(SVGKSource *)document namePrefix:(NSString *)prefix namespaceURI:(NSString *)XMLNSURI attributes:(NSMutableDictionary *)attributes parseResult:(SVGKParseResult *)parseResult parentNode:(Node *)parentNode
  29. {
  30. if( [[self supportedNamespaces] containsObject:XMLNSURI] )
  31. {
  32. /**
  33. NB: this section of code is copy/pasted from SVGKParserDOM -- we don't want anything special, we want an ordinary DOM node,
  34. ...but we need this standalone parser-extension because a <style> tag needs some custom *post-processing*
  35. */
  36. NSString* qualifiedName = (prefix == nil) ? name : [NSString stringWithFormat:@"%@:%@", prefix, name];
  37. /** NB: must supply a NON-qualified name if we have no specific prefix here ! */
  38. // FIXME: we always return an empty Element here; for DOM spec, should we be detecting things like "comment" nodes? I dont know how libxml handles those and sends them to us. I've never seen one in action...
  39. Element *blankElement = [[Element alloc] initWithQualifiedName:qualifiedName inNameSpaceURI:XMLNSURI attributes:attributes];
  40. return blankElement;
  41. }
  42. else
  43. return nil;
  44. }
  45. -(void)handleEndElement:(Node *)newNode document:(SVGKSource *)document parseResult:(SVGKParseResult *)parseResult
  46. {
  47. /** This is where the magic happens ... */
  48. NSString* c = [newNode.textContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  49. if( c.length > 0 )
  50. {
  51. CSSStyleSheet* parsedStylesheet = [[CSSStyleSheet alloc] initWithString:c];
  52. [parseResult.parsedDocument.rootElement.styleSheets.internalArray addObject:parsedStylesheet];
  53. }
  54. }
  55. @end