@@ -134,18 +134,26 @@ macro HTMLElements(
134134
135135// MARK: HTML
136136public protocol HTMLElement : CustomStringConvertible {
137+ /// Whether or not this element is a void element.
137138 var isVoid : Bool { get }
139+ /// Whether or not this element should include a forward slash in the tag name.
138140 var trailingSlash : Bool { get }
139- var tag : String { get }
141+ /// Whether or not to HTML escape the `<` & `>` characters directly adjacent of the opening and closing tag names when rendering.
142+ var escaped : Bool { get set }
143+ /// This element's tag name.
144+ var tag : String { get }
145+ /// The global attributes of this element.
140146 var attributes : [ HTMLElementAttribute ] { get }
147+ /// The inner HTML content of this element.
141148 var innerHTML : [ CustomStringConvertible ] { get }
142149}
143150
144151/// A custom HTML element.
145152public struct custom : HTMLElement {
146153 public var isVoid : Bool
147154 public var trailingSlash : Bool
148- public var tag : String
155+ public var escaped : Bool = false
156+ public let tag : String
149157 public var attributes : [ HTMLElementAttribute ]
150158 public var innerHTML : [ CustomStringConvertible ]
151159
@@ -171,7 +179,20 @@ public struct custom : HTMLElement {
171179 }
172180
173181 public var description : String {
174- return " < " + tag + ( isVoid && trailingSlash ? " / " : " " ) + " > " + ( isVoid ? " " : " </ " + tag + " > " )
182+ let attributes_string : String = self . attributes. compactMap ( {
183+ guard let v: String = $0. htmlValue else { return nil }
184+ let delimiter : String = $0. htmlValueDelimiter
185+ return $0. key + ( $0. htmlValueIsVoidable && v. isEmpty ? " " : " = \( delimiter) \( v) \( delimiter) " )
186+ } ) . joined ( separator: " " )
187+ let l : String , g : String
188+ if escaped {
189+ l = " < "
190+ g = " > "
191+ } else {
192+ l = " < "
193+ g = " > "
194+ }
195+ return l + tag + ( isVoid && trailingSlash ? " / " : " " ) + g + ( attributes_string. isEmpty ? " " : " " + attributes_string) + ( isVoid ? " " : l + " / " + tag + g)
175196 }
176197}
177198
0 commit comments