Skip to content

Commit

Permalink
Move escapeContent function.
Browse files Browse the repository at this point in the history
  • Loading branch information
dasautoooo committed May 18, 2021
1 parent 5fa8f11 commit 3c8b431
Showing 1 changed file with 34 additions and 32 deletions.
66 changes: 34 additions & 32 deletions Sources/Parma/ParmaCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,40 +62,9 @@ class ParmaCore: NSObject {
/// The context for element composing.
let context = ComposingContext()

/// Sanitize input to prevent < and > from causing problems
private static func escapeContent(_ rawContent: String) -> String {

enum EscapedCharacters: String, CaseIterable {

case leftAngleBracket = "<",
rightAngleBracket = ">"

func replacement() -> String {
switch self {
case .leftAngleBracket:
return "&lt;"
case .rightAngleBracket:
return "&gt;"
}
}

static func escapeString(_ string: String) -> String {
var escapedValue = string

self.allCases.forEach {
escapedValue = escapedValue.replacingOccurrences(of: $0.rawValue, with: $0.replacement())
}

return escapedValue
}
}

return EscapedCharacters.escapeString(rawContent)
}

// MARK: - Initialization
convenience init(_ markdown: String) throws {
let down = Down(markdownString: Self.escapeContent(markdown))
let down = Down(markdownString: escapeContent(markdown))
let xml = try down.toXML()
self.init(xmlData: Data(xml.utf8))
}
Expand Down Expand Up @@ -133,6 +102,39 @@ class ParmaCore: NSObject {
}
}

// MARK: - Private function

/// Sanitize input to prevent `<` and `>` from causing problems
private func escapeContent(_ rawContent: String) -> String {

enum EscapedCharacters: String, CaseIterable {

case leftAngleBracket = "<",
rightAngleBracket = ">"

func replacement() -> String {
switch self {
case .leftAngleBracket:
return "&lt;"
case .rightAngleBracket:
return "&gt;"
}
}

static func escapeString(_ string: String) -> String {
var escapedValue = string

self.allCases.forEach {
escapedValue = escapedValue.replacingOccurrences(of: $0.rawValue, with: $0.replacement())
}

return escapedValue
}
}

return EscapedCharacters.escapeString(rawContent)
}

// MARK: - XML parsing logic
extension ParmaCore: XMLParserDelegate {
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
Expand Down

0 comments on commit 3c8b431

Please sign in to comment.