Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for styles attributes #452

Merged
merged 6 commits into from
Aug 23, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 37 additions & 16 deletions Source/svg/SVGParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down Expand Up @@ -503,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)
}
}
}
}
}
Expand Down