|
| 1 | +// |
| 2 | +// Parser.XML.StyleSheet.swift |
| 3 | +// SwiftDraw |
| 4 | +// |
| 5 | +// Created by Simon Whitty on 18/8/22. |
| 6 | +// Copyright 2022 Simon Whitty |
| 7 | +// |
| 8 | +// Distributed under the permissive zlib license |
| 9 | +// Get the latest version from here: |
| 10 | +// |
| 11 | +// https://github.com/swhitty/SwiftDraw |
| 12 | +// |
| 13 | +// This software is provided 'as-is', without any express or implied |
| 14 | +// warranty. In no event will the authors be held liable for any damages |
| 15 | +// arising from the use of this software. |
| 16 | +// |
| 17 | +// Permission is granted to anyone to use this software for any purpose, |
| 18 | +// including commercial applications, and to alter it and redistribute it |
| 19 | +// freely, subject to the following restrictions: |
| 20 | +// |
| 21 | +// 1. The origin of this software must not be misrepresented; you must not |
| 22 | +// claim that you wrote the original software. If you use this software |
| 23 | +// in a product, an acknowledgment in the product documentation would be |
| 24 | +// appreciated but is not required. |
| 25 | +// |
| 26 | +// 2. Altered source versions must be plainly marked as such, and must not be |
| 27 | +// misrepresented as being the original software. |
| 28 | +// |
| 29 | +// 3. This notice may not be removed or altered from any source distribution. |
| 30 | +// |
| 31 | + |
| 32 | + |
| 33 | +import Foundation |
| 34 | + |
| 35 | +extension XMLParser { |
| 36 | + |
| 37 | + func findStyleElements(within element: XML.Element) -> [XML.Element] { |
| 38 | + return element.children.reduce(into: [XML.Element]()) { |
| 39 | + if $1.name == "style" { |
| 40 | + $0.append($1) |
| 41 | + } else { |
| 42 | + $0.append(contentsOf: findStyleElements(within: $1)) |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + func parseStyleSheetElements(within element: XML.Element) -> [DOM.StyleSheet] { |
| 48 | + var sheets = [DOM.StyleSheet]() |
| 49 | + |
| 50 | + for e in findStyleElements(within: element) { |
| 51 | + do { |
| 52 | + try sheets.append(parseStyleSheetElement(e.innerText)) |
| 53 | + } catch { |
| 54 | + Self.logParsingError(for: error, filename: filename, parsing: e) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return sheets |
| 59 | + } |
| 60 | + |
| 61 | + func parseStyleSheetElement(_ text: String?) throws -> DOM.StyleSheet { |
| 62 | + var scanner = XMLParser.Scanner(text: text ?? "") |
| 63 | + var sheet = DOM.StyleSheet() |
| 64 | + |
| 65 | + var last: (DOM.StyleSheet.Selector, String)? |
| 66 | + repeat { |
| 67 | + last = try scanner.scanNextSelector() |
| 68 | + if let last = last { |
| 69 | + sheet.entries[last.0] = DOM.GraphicsElement() |
| 70 | + } |
| 71 | + } while last != nil |
| 72 | + |
| 73 | + return sheet |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +extension XMLParser.Scanner { |
| 78 | + |
| 79 | + mutating func scanNextSelector() throws -> (DOM.StyleSheet.Selector, String)? { |
| 80 | + try scanPastComments() |
| 81 | + if let c = try scanNextClass() { |
| 82 | + return (.class(c), try scanNextBody()) |
| 83 | + } else if let id = try scanNextID() { |
| 84 | + return (.id(id), try scanNextBody()) |
| 85 | + } else if let e = try scanNextElement() { |
| 86 | + return (.element(e), try scanNextBody()) |
| 87 | + } |
| 88 | + return nil |
| 89 | + } |
| 90 | + |
| 91 | + mutating func scanPastComments() throws { |
| 92 | + while try scanPastNextComment() { |
| 93 | + () |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + mutating func scanPastNextComment() throws -> Bool { |
| 98 | + guard scanStringIfPossible("/*") else { return false } |
| 99 | + _ = try scanString(upTo: "*/") |
| 100 | + _ = try scanString("*/") |
| 101 | + return true |
| 102 | + } |
| 103 | + |
| 104 | + mutating func scanNextClass() throws -> String? { |
| 105 | + guard scanStringIfPossible(".") else { return nil } |
| 106 | + return try scanSelectorName() |
| 107 | + } |
| 108 | + |
| 109 | + mutating func scanNextID() throws -> String? { |
| 110 | + guard scanStringIfPossible("#") else { return nil } |
| 111 | + return try scanSelectorName() |
| 112 | + } |
| 113 | + |
| 114 | + mutating func scanNextElement() throws -> String? { |
| 115 | + do { |
| 116 | + return try scanSelectorName() |
| 117 | + } catch { |
| 118 | + guard isEOF else { |
| 119 | + throw error |
| 120 | + } |
| 121 | + return nil |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + mutating func scanSelectorName() throws -> String? { |
| 126 | + try scanString(upTo: "{").trimmingCharacters(in: .whitespacesAndNewlines) |
| 127 | + } |
| 128 | + |
| 129 | + mutating func scanNextBody() throws -> String { |
| 130 | + _ = try scanString("{") |
| 131 | + let body = try scanString(upTo: "}") |
| 132 | + _ = try scanString("}") |
| 133 | + return body |
| 134 | + } |
| 135 | +} |
0 commit comments