From dab32f8a2809be7f511fdf1a2ba79f3519bfb924 Mon Sep 17 00:00:00 2001 From: Anton Marunko Date: Mon, 16 Jul 2018 19:49:09 +0700 Subject: [PATCH 1/4] Fixes for styles attributes --- Source/svg/SVGParser.swift | 41 +++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/Source/svg/SVGParser.swift b/Source/svg/SVGParser.swift index 0e86019e..8aad13b9 100644 --- a/Source/svg/SVGParser.swift +++ b/Source/svg/SVGParser.swift @@ -190,20 +190,33 @@ open class SVGParser { fileprivate func parseStyle(_ styleNode: XMLIndexer) { if let rawStyle = styleNode.element?.text { - var styleAttributes: [String: String] = [:] - let parts = rawStyle.trimmingCharacters(in: .whitespacesAndNewlines).split(separator: "{") - if parts.count == 2 { - let className = String(parts[0].dropFirst()) - if !className.isEmpty { - let style = String(parts[1].dropLast()) - let styleParts = style.replacingOccurrences(of: " ", with: "").components(separatedBy: ";") - styleParts.forEach { styleAttribute in - let currentStyle = styleAttribute.components(separatedBy: ":") - if currentStyle.count == 2 { - styleAttributes.updateValue(currentStyle[1], forKey: currentStyle[0]) + + let parts = rawStyle.components(separatedBy: .whitespacesAndNewlines).joined().split(separator: "{") + + var separatedParts = [String.SubSequence]() + + parts.forEach { substring in + separatedParts.append(contentsOf: substring.split(separator: "}")) + } + + if separatedParts.count % 2 == 0 { + + let classNames = stride(from: 0, to: separatedParts.count, by: 2).map { String(separatedParts[$0].dropFirst()) } + let styles = stride(from: 1, to: separatedParts.count, by: 2).map { separatedParts[$0] } + + for (index, className) in classNames.enumerated() { + var styleAttributes: [String: String] = [:] + if !className.isEmpty { + let style = String(styles[index].dropLast()) + let styleParts = style.components(separatedBy: ";") + styleParts.forEach { styleAttribute in + let currentStyle = styleAttribute.components(separatedBy: ":") + if currentStyle.count == 2 { + styleAttributes.updateValue(currentStyle[1], forKey: currentStyle[0]) + } } + styleTable[className] = styleAttributes } - styleTable[className] = styleAttributes } } } @@ -1004,6 +1017,10 @@ open class SVGParser { return UserSpaceLocus(locus: path!, userSpace: userSpace) } + fileprivate func parseCSS() { + + } + fileprivate func parseMask(_ mask: XMLIndexer) -> UserSpaceNode? { var userSpace = true if let units = mask.element?.allAttributes["maskContentUnits"]?.text, units == "objectBoundingBox" { From ce6ef2906c96e2b788311b10098da64c77963927 Mon Sep 17 00:00:00 2001 From: Anton Marunko Date: Tue, 17 Jul 2018 12:08:13 +0700 Subject: [PATCH 2/4] Fix for more than one class attributes --- Source/svg/SVGParser.swift | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Source/svg/SVGParser.swift b/Source/svg/SVGParser.swift index 8aad13b9..c711e722 100644 --- a/Source/svg/SVGParser.swift +++ b/Source/svg/SVGParser.swift @@ -516,10 +516,18 @@ open class SVGParser { fileprivate func getStyleAttributes(_ groupAttributes: [String: String], element: SWXMLHash.XMLElement) -> [String: String] { var styleAttributes: [String: String] = groupAttributes - if let className = element.allAttributes["class"]?.text, let styleAttributesFromTable = styleTable[className] { - for (att, val) in styleAttributesFromTable { - if styleAttributes.index(forKey: att) == nil { - styleAttributes.updateValue(val, forKey: att) + if let classNamesString = element.allAttributes["class"]?.text { + let classNames = classNamesString.split(separator: " ") + + classNames.forEach { className in + let classString = String(className) + + if let styleAttributesFromTable = styleTable[classString] { + for (att, val) in styleAttributesFromTable { + if styleAttributes.index(forKey: att) == nil { + styleAttributes.updateValue(val, forKey: att) + } + } } } } From 335336d4b92528917fa701bcf2461789d8deede2 Mon Sep 17 00:00:00 2001 From: Anton Marunko Date: Tue, 17 Jul 2018 12:12:08 +0700 Subject: [PATCH 3/4] Formatting issue --- Source/svg/SVGParser.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/svg/SVGParser.swift b/Source/svg/SVGParser.swift index c711e722..95e2b330 100644 --- a/Source/svg/SVGParser.swift +++ b/Source/svg/SVGParser.swift @@ -518,10 +518,10 @@ open class SVGParser { if let classNamesString = element.allAttributes["class"]?.text { let classNames = classNamesString.split(separator: " ") - + classNames.forEach { className in let classString = String(className) - + if let styleAttributesFromTable = styleTable[classString] { for (att, val) in styleAttributesFromTable { if styleAttributes.index(forKey: att) == nil { From 3ddb5d19ad6cafe75932096a43835bcf116776ad Mon Sep 17 00:00:00 2001 From: Anton Marunko Date: Tue, 17 Jul 2018 12:20:38 +0700 Subject: [PATCH 4/4] Removing boilerplate code --- Source/svg/SVGParser.swift | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Source/svg/SVGParser.swift b/Source/svg/SVGParser.swift index 95e2b330..d7bdf617 100644 --- a/Source/svg/SVGParser.swift +++ b/Source/svg/SVGParser.swift @@ -1025,10 +1025,6 @@ open class SVGParser { return UserSpaceLocus(locus: path!, userSpace: userSpace) } - fileprivate func parseCSS() { - - } - fileprivate func parseMask(_ mask: XMLIndexer) -> UserSpaceNode? { var userSpace = true if let units = mask.element?.allAttributes["maskContentUnits"]?.text, units == "objectBoundingBox" {