Skip to content

Commit 44b4722

Browse files
authored
Make the sandbox modifier accept an enum (#167)
* Make the sandbox modifier accept an enum * Assign the sandbox attribute to the inlineframe element
1 parent d3dcaa6 commit 44b4722

File tree

4 files changed

+101
-6
lines changed

4 files changed

+101
-6
lines changed

Sources/HTMLKit/Abstraction/Attributes/BasicAttributes.swift

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,16 +2191,41 @@ extension RowSpanAttribute where Self: EmptyNode {
21912191
}
21922192
}
21932193

2194-
/// The protocol provides the element with the sandbox handler.
2194+
/// A type that provides the `sandbox` modifier.
2195+
///
2196+
/// > Note: Currently, this protocol is only applicable to `InlineFrame`.
21952197
@_documentation(visibility: internal)
21962198
public protocol SandboxAttribute: Attribute {
21972199

2198-
/// The function represents the html-attribute 'sandbox'.
2200+
/// Define the permissions for the element.
21992201
///
2200-
/// ```html
2201-
/// <tag sandbox />
2202+
/// ```swift
2203+
/// InlineFrame {
2204+
/// }
2205+
/// .source("https://...")
2206+
/// .sandbox()
22022207
/// ```
22032208
func sandbox() -> Self
2209+
2210+
/// Define the permissions for the element.
2211+
///
2212+
/// ```swift
2213+
/// InlineFrame {
2214+
/// }
2215+
/// .source("https://...")
2216+
/// .sandbox(.allowDownloads)
2217+
/// ```
2218+
func sandbox(_ value: Values.Permission) -> Self
2219+
2220+
/// Define the permissions for the element.
2221+
///
2222+
/// ```swift
2223+
/// InlineFrame {
2224+
/// }
2225+
/// .source("https://...")
2226+
/// .sandbox([.allowDownloads, .allowPopups])
2227+
/// ```
2228+
func sandbox(_ values: OrderedSet<Values.Permission>) -> Self
22042229
}
22052230

22062231
extension SandboxAttribute where Self: ContentNode {

Sources/HTMLKit/Abstraction/Elements/BodyElements.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15956,7 +15956,7 @@ public struct InlineFrame: ContentNode, HtmlElement, BodyElement, FormElement, F
1595615956
}
1595715957
}
1595815958

15959-
extension InlineFrame: GlobalAttributes, GlobalEventAttributes, GlobalAriaAttributes, SourceAttribute, NameAttribute, WidthAttribute, HeightAttribute, ReferrerPolicyAttribute & LoadingAttribute {
15959+
extension InlineFrame: GlobalAttributes, GlobalEventAttributes, GlobalAriaAttributes, SourceAttribute, NameAttribute, WidthAttribute, HeightAttribute, ReferrerPolicyAttribute & LoadingAttribute & SandboxAttribute {
1596015960

1596115961
public func accessKey(_ value: Character) -> InlineFrame {
1596215962
return mutate(accesskey: value)
@@ -16085,6 +16085,18 @@ extension InlineFrame: GlobalAttributes, GlobalEventAttributes, GlobalAriaAttrib
1608516085
return self
1608616086
}
1608716087

16088+
public func sandbox() -> InlineFrame {
16089+
return mutate(sandbox: "sandbox")
16090+
}
16091+
16092+
public func sandbox(_ value: Values.Permission) -> InlineFrame {
16093+
return mutate(sandbox: value.rawValue)
16094+
}
16095+
16096+
public func sandbox(_ values: OrderedCollections.OrderedSet<Values.Permission>) -> InlineFrame {
16097+
return mutate(sandbox: values.map { $0.rawValue }.joined(separator: " "))
16098+
}
16099+
1608816100
public func source(_ value: String) -> InlineFrame {
1608916101
return mutate(source: value)
1609016102
}

Sources/HTMLKit/Abstraction/Tokens/ValueTokens.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,4 +1188,50 @@ public enum Values {
11881188
/// Expects a url for instant messaging
11891189
case messagingProtocol = "impp"
11901190
}
1191+
1192+
/// A enumeration of extra permission
1193+
///
1194+
/// ```swift
1195+
/// InlineFrame {
1196+
/// }
1197+
/// .sandbox([.allowDownloads, .allowForms])
1198+
/// ```
1199+
public enum Permission: String {
1200+
1201+
/// Permits downloads
1202+
case allowDownloads = "allow-downloads"
1203+
1204+
/// Permits form submissions within the content
1205+
case allowForms = "allow-forms"
1206+
1207+
/// Permits to open modals
1208+
case allowModals = "allow-modals"
1209+
1210+
/// Permits to lock the screen orientation
1211+
case allowOrientationLock = "allow-orientation-lock"
1212+
1213+
/// Permits the use of the pointer lock API
1214+
case allowPointerLock = "allow-pointer-lock"
1215+
1216+
/// Permits to open popups
1217+
case allowPopups = "allow-popups"
1218+
1219+
/// Permits popups to open new windows without inheriting the sandboxing
1220+
case allowPopupsToEscapeSandbox = "allow-popups-to-escape-sandbox"
1221+
1222+
/// Permits to start a presentation session
1223+
case allowPresentation = "allow-presentation"
1224+
1225+
/// Permits the content to be treated as being from the same origin
1226+
case allowSameOrigin = "allow-same-origin"
1227+
1228+
/// Permits script execution
1229+
case allowScripts = "allow-scripts"
1230+
1231+
/// Permits the content to navigate its top-level browsing context
1232+
case allowTopNavigation = "allow-top-navigation"
1233+
1234+
/// Permits the content to navigate its top-level browsing context, but only if initiated by user
1235+
case allowTopNavigationByUserActivation = "allow-top-navigation-by-user-activation"
1236+
}
11911237
}

Tests/HTMLKitTests/AttributesTests.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,14 @@ final class AttributesTests: XCTestCase {
439439
return self.mutate(sandbox: "sandbox")
440440
}
441441

442+
func sandbox(_ value: Values.Permission) -> Tag {
443+
return self.mutate(sandbox: value.rawValue)
444+
}
445+
446+
func sandbox(_ values: OrderedSet<Values.Permission>) -> Tag {
447+
return self.mutate(sandbox: values.map { $0.rawValue }.joined(separator: " "))
448+
}
449+
442450
func scope(_ value: Values.Scope) -> Tag {
443451
return self.mutate(scope: value.rawValue)
444452
}
@@ -1834,11 +1842,15 @@ final class AttributesTests: XCTestCase {
18341842

18351843
let view = TestView {
18361844
Tag {}.sandbox()
1845+
Tag {}.sandbox(.allowDownloads)
1846+
Tag {}.sandbox([.allowDownloads, .allowForms])
18371847
}
18381848

18391849
XCTAssertEqual(try renderer.render(view: view),
18401850
"""
1841-
<tag sandbox="sandbox"></tag>
1851+
<tag sandbox="sandbox"></tag>\
1852+
<tag sandbox="allow-downloads"></tag>\
1853+
<tag sandbox="allow-downloads allow-forms"></tag>
18421854
"""
18431855
)
18441856
}

0 commit comments

Comments
 (0)