Skip to content
Merged
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
100 changes: 99 additions & 1 deletion Sources/HTMLKit/Abstraction/Attributes/BasicAttributes.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Foundation
import OrderedCollections

/// The alias combines the global attributes of the basic attributes.
@_documentation(visibility: internal)
public typealias GlobalAttributes = AccessKeyAttribute & AutocapitalizeAttribute & AutofocusAttribute & ClassAttribute & EditAttribute & DirectionAttribute & DragAttribute & EnterKeyHintAttribute & HiddenAttribute & InputModeAttribute & IsAttribute & ItemIdAttribute & ItemPropertyAttribute & ItemReferenceAttribute & ItemScopeAttribute & ItemTypeAttribute & IdentifierAttribute & LanguageAttribute & NonceAttribute & RoleAttribute & SpellCheckAttribute & StyleAttribute & TabulatorAttribute & TitleAttribute & TranslateAttribute & InertAttribute & PopoverAttribute
public typealias GlobalAttributes = AccessKeyAttribute & AutocapitalizeAttribute & AutofocusAttribute & ClassAttribute & EditAttribute & DirectionAttribute & DragAttribute & EnterKeyHintAttribute & HiddenAttribute & InputModeAttribute & IsAttribute & ItemAttribute & ItemIdAttribute & ItemPropertyAttribute & ItemReferenceAttribute & ItemScopeAttribute & ItemTypeAttribute & IdentifierAttribute & LanguageAttribute & NonceAttribute & RoleAttribute & SpellCheckAttribute & StyleAttribute & TabulatorAttribute & TitleAttribute & TranslateAttribute & InertAttribute & PopoverAttribute

/// A type that provides the `accessKey` modifier.
@_documentation(visibility: internal)
Expand Down Expand Up @@ -1516,6 +1517,103 @@ extension IsAttribute where Self: EmptyNode {
}
}

/// A type that provides the `item` modifier.
@_documentation(visibility: internal)
public protocol ItemAttribute: Attribute {

/// Create an new item.
///
/// ```swift
/// DefinitionList {
/// }
/// .item(id: "urn:...", as: "https://...", for: ["foo", "bar"])
/// ```
///
/// - Parameters:
/// - id: The identifier used to name the item.
/// - schema: The url of the schema the item should conform to.
/// - elements: The identifiers of elements to associated the item with.
///
/// - Returns: The element
func item(id: String?, as schema: URL?, for elements: [String]?) -> Self

/// Create an new item.
///
/// ```swift
/// DefinitionList {
/// }
/// .item(id: "urn:...", as: "https://...", for: "foo", "bar")
/// ```
///
/// - Parameters:
/// - id: The identifier used to name the item.
/// - schema: The url of the schema the item should conform to.
/// - elements: The identifiers of elements to associated the item with.
///
/// - Returns: The element
func item(id: String?, as schema: URL?, for elements: String...) -> Self
}

extension ItemAttribute where Self: ContentNode {

internal func mutate(itemid value: String?) -> Self {

if let value = value {
return self.mutate(key: "itemid", value: value)
}

return self
}

internal func mutate(itemtype value: String?) -> Self {

if let value = value {
return self.mutate(key: "itemtype", value: value)
}

return self
}

internal func mutate(itemref value: String?) -> Self {

if let value = value {
return self.mutate(key: "itemref", value: value)
}

return self
}
}

extension ItemAttribute where Self: EmptyNode {

internal func mutate(itemid value: String?) -> Self {

if let value = value {
return self.mutate(key: "itemid", value: value)
}

return self
}

internal func mutate(itemtype value: String?) -> Self {

if let value = value {
return self.mutate(key: "itemtype", value: value)
}

return self
}

internal func mutate(itemref value: String?) -> Self {

if let value = value {
return self.mutate(key: "itemref", value: value)
}

return self
}
}

/// A type that provides the `itemId` modifier.
@_documentation(visibility: internal)
public protocol ItemIdAttribute: Attribute {
Expand Down
13 changes: 13 additions & 0 deletions Sources/HTMLKit/Abstraction/Elements/BasicElements.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Foundation
import OrderedCollections

/// An element that represents a comment output.
Expand Down Expand Up @@ -164,7 +165,16 @@ extension Html: GlobalAttributes, GlobalEventAttributes {
public func `is`(_ value: String) -> Html {
return mutate(is: value)
}

public func item(id: String? = nil, as schema: URL? = nil, for elements: [String]? = nil) -> Html {
return self.mutate(itemscope: "itemscope").mutate(itemid: id).mutate(itemtype: schema?.absoluteString).mutate(itemref: elements?.joined(separator: " "))
}

public func item(id: String? = nil, as schema: URL? = nil, for elements: String...) -> Html {
return self.mutate(itemscope: "itemscope").mutate(itemid: id).mutate(itemtype: schema?.absoluteString).mutate(itemref: elements.joined(separator: " "))
}

@available(*, deprecated, message: "Use the item(id:as:for:) modifier instead.")
public func itemId(_ value: String) -> Html {
return mutate(itemid: value)
}
Expand All @@ -173,14 +183,17 @@ extension Html: GlobalAttributes, GlobalEventAttributes {
return mutate(itemprop: value)
}

@available(*, deprecated, message: "Use the item(id:as:for:) modifier instead.")
public func itemReference(_ value: String) -> Html {
return mutate(itemref: value)
}

@available(*, deprecated, message: "Use the item(id:as:for:) modifier instead.")
public func itemScope(_ value: String) -> Html {
return mutate(itemscope: value)
}

@available(*, deprecated, message: "Use the item(id:as:for:) modifier instead.")
public func itemType(_ value: String) -> Html {
return mutate(itemtype: value)
}
Expand Down
Loading