Skip to content

Commit

Permalink
Add CSSParser to the project and fix lint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
ystrot committed Oct 29, 2018
1 parent a0b0fee commit edda796
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
4 changes: 4 additions & 0 deletions Macaw.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@
57F1087C1F53CA7E00DC365B /* MDisplayLink_iOS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57F1087B1F53CA7E00DC365B /* MDisplayLink_iOS.swift */; };
57FCD2771D76EA4600CC0FB6 /* Macaw.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 57FCD26C1D76EA4600CC0FB6 /* Macaw.framework */; };
57FCD27C1D76EA4600CC0FB6 /* MacawTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57FCD27B1D76EA4600CC0FB6 /* MacawTests.swift */; };
5815D57F2186DC7900BD08F9 /* CSSParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5815D57E2186DC7900BD08F9 /* CSSParser.swift */; };
5835969B20A9CA150090400C /* CGMappings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5835969A20A9CA140090400C /* CGMappings.swift */; };
5835969C20A9CA150090400C /* CGMappings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5835969A20A9CA140090400C /* CGMappings.swift */; };
585288F420AD96A2003E51D1 /* ContentLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 585288F320AD96A2003E51D1 /* ContentLayout.swift */; };
Expand Down Expand Up @@ -664,6 +665,7 @@
57FCD2761D76EA4600CC0FB6 /* MacawTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MacawTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
57FCD27B1D76EA4600CC0FB6 /* MacawTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacawTests.swift; sourceTree = "<group>"; };
57FCD27D1D76EA4600CC0FB6 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
5815D57E2186DC7900BD08F9 /* CSSParser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CSSParser.swift; sourceTree = "<group>"; };
5835969A20A9CA140090400C /* CGMappings.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CGMappings.swift; sourceTree = "<group>"; };
585288F320AD96A2003E51D1 /* ContentLayout.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ContentLayout.swift; sourceTree = "<group>"; };
5852891520B29D67003E51D1 /* TransformedLocus.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TransformedLocus.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1254,6 +1256,7 @@
57E5E1451E3B393900D1CB28 /* svg */ = {
isa = PBXGroup;
children = (
5815D57E2186DC7900BD08F9 /* CSSParser.swift */,
5BAE201E208E1211006BF277 /* SVGCanvas.swift */,
57E5E1461E3B393900D1CB28 /* SVGConstants.swift */,
5B1A8C7520A15F7300E5FFAE /* SVGNodeLayout.swift */,
Expand Down Expand Up @@ -2205,6 +2208,7 @@
57F1087C1F53CA7E00DC365B /* MDisplayLink_iOS.swift in Sources */,
57E5E1A61E3B393900D1CB28 /* RenderContext.swift in Sources */,
30FF4969215CED8100FF653C /* MCAShapeLayerLineJoin_iOS.swift in Sources */,
5815D57F2186DC7900BD08F9 /* CSSParser.swift in Sources */,
57E5E19C1E3B393900D1CB28 /* Size.swift in Sources */,
57E5E1991E3B393900D1CB28 /* Polyline.swift in Sources */,
5B6E193120AC58F900454E7E /* LineJoin.swift in Sources */,
Expand Down
52 changes: 25 additions & 27 deletions Source/svg/CSSParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,36 @@ class CSSParser {

func parse(content: String) {
let parts = content.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 headers = stride(from: 0, to: separatedParts.count, by: 2).map { String(separatedParts[$0]) }
let bodies = stride(from: 1, to: separatedParts.count, by: 2).map { separatedParts[$0] }

for (index, header) in headers.enumerated() {
for headerPart in header.split(separator: ",") {
if headerPart.count > 1 {
let selector = parseSelector(text: String(headerPart))
var currentStyles = getStyles(selector: selector)
if (currentStyles == nil) {
currentStyles = [String:String]()
}
let style = String(bodies[index])
let styleParts = style.components(separatedBy: ";")
styleParts.forEach { styleAttribute in
if !styleAttribute.isEmpty {
let currentStyle = styleAttribute.components(separatedBy: ":")
if currentStyle.count == 2 {
currentStyles![currentStyle[0]] = currentStyle[1]
}
for headerPart in header.split(separator: ",") where headerPart.count > 1 {
let selector = parseSelector(text: String(headerPart))
var currentStyles = getStyles(selector: selector)
if currentStyles == nil {
currentStyles = [String: String]()
}
let style = String(bodies[index])
let styleParts = style.components(separatedBy: ";")
styleParts.forEach { styleAttribute in
if !styleAttribute.isEmpty {
let currentStyle = styleAttribute.components(separatedBy: ":")
if currentStyle.count == 2 {
currentStyles![currentStyle[0]] = currentStyle[1]
}
}
setStyles(selector: selector, styles: currentStyles!)
}
setStyles(selector: selector, styles: currentStyles!)
}
}
}
Expand Down Expand Up @@ -84,10 +82,10 @@ class CSSParser {

if let classNamesString = element.allAttributes["class"]?.text {
let classNames = classNamesString.split(separator: " ")

classNames.forEach { className in
let classString = String(className)

if let styleAttributesFromTable = stylesByClass[classString] {
for (att, val) in styleAttributesFromTable {
if styleAttributes.index(forKey: att) == nil {
Expand All @@ -97,7 +95,7 @@ class CSSParser {
}
}
}

if let idString = element.allAttributes["id"]?.text {
if let styleAttributesFromTable = stylesById[idString] {
for (att, val) in styleAttributesFromTable {
Expand All @@ -107,11 +105,11 @@ class CSSParser {
}
}
}

return styleAttributes
}

fileprivate func getStyles(selector: Selector) -> [String:String]? {
fileprivate func getStyles(selector: Selector) -> [String: String]? {
switch selector {
case .byId(let id):
return stylesById[id]
Expand All @@ -122,7 +120,7 @@ class CSSParser {
}
}

fileprivate func setStyles(selector: Selector, styles: [String:String]) {
fileprivate func setStyles(selector: Selector, styles: [String: String]) {
switch selector {
case .byId(let id):
stylesById[id] = styles
Expand Down
7 changes: 4 additions & 3 deletions Source/svg/SVGParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ open class SVGParser {
fileprivate var defEffects = [String: Effect]()

fileprivate var styles = CSSParser()

fileprivate enum PathCommandType {
case moveTo
case lineTo
Expand All @@ -114,9 +114,10 @@ open class SVGParser {
}

fileprivate func parse() throws -> Group {
let parsedXml = SWXMLHash.config { config in
let config = SWXMLHash.config { config in
config.shouldProcessNamespaces = true
}.parse(xmlString)
}
let parsedXml = config.parse(xmlString)

var layout: NodeLayout?
for child in parsedXml.children {
Expand Down

0 comments on commit edda796

Please sign in to comment.