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

Revise the rendering #168

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
79 changes: 79 additions & 0 deletions Sources/HTMLKit/Framework/Rendering/Encoder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import Foundation

/// A type responsible for encoding characters like `<`, `>`, `&` etc. to their safe equivalents
internal struct Encoder {

/// A enumeration of potential encoding mechanism
internal enum Mechanism {

/// Encodes characters in a attribute
case attribute

/// Encodes characters in a entity
case entity

/// The characters to replace based on the mechanism
var characters: Set<Unicode.Scalar> {

switch self {
case .attribute:
return ["&", "'", "\""]

case .entity:
return ["<", ">"]
}
}
}

/// A mapping table of characters with their corresponding replacements
private var characterTable: [Unicode.Scalar: String] {
return [
"&": "&amp;",
"<": "&lt;",
">": "&gt;",
"\"": "&quot;",
"'": "&apos;",
]
}

/// Encodes a string into a safe equivalent
///
/// - Parameters:
/// - string: The string to encode
/// - mechansim: The mechnism to use
///
/// - Returns: The encoded string
internal func encode(_ string: String, as mechansim: Mechanism) -> String {
return replace(set: mechansim.characters, on: string)
}

/// Replaces occurrences of characters in a string with their corresponding replacements
///
/// - Parameters:
/// - set: The set of character to check against
/// - string: The string where replacements will be made
///
/// - Returns: The replaced string
private func replace(set: Set<Unicode.Scalar>, on string: String) -> String {

var result = ""

for scalar in string.unicodeScalars {

if set.contains(scalar) {

if let replacement = characterTable[scalar] {
result.append(contentsOf: replacement)

} else {
result.append(contentsOf: String(scalar))
}

} else {
result.append(contentsOf: String(scalar))
}
}

return result
}
}
9 changes: 8 additions & 1 deletion Sources/HTMLKit/Framework/Rendering/Features.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
/// An option set of features.
/// An option set of different features
///
/// The feature set provides the flexibility to enable experimental features if desired.
public struct Features: Swift.OptionSet {

public var rawValue: Int

/// A flag that indicates whether the renderer should use markdown
public static let markdown = Features(rawValue: 1 << 0)

/// A flag that indicates whether the renderer should encode input
public static let escaping = Features(rawValue: 1 << 1)

/// Initializes the feature set
public init(rawValue: Int) {
self.rawValue = rawValue
}
Expand Down
Loading
Loading