Skip to content

Commit

Permalink
Merge pull request #452 from exyte/bug/parser-styles-fix
Browse files Browse the repository at this point in the history
Fixes for styles attributes
  • Loading branch information
ystrot authored Aug 23, 2018
2 parents c0ecdb5 + e9b7708 commit c88693a
Showing 1 changed file with 37 additions and 16 deletions.
53 changes: 37 additions & 16 deletions Source/svg/SVGParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -234,20 +234,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
}
}
}
Expand Down Expand Up @@ -547,10 +560,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)
}
}
}
}
}
Expand Down

0 comments on commit c88693a

Please sign in to comment.