Skip to content

Commit

Permalink
Added more if features on a html tag
Browse files Browse the repository at this point in the history
  • Loading branch information
MatsMoll committed Mar 27, 2019
1 parent 8869373 commit 7e1776b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Sources/HTMLKit/HTMLDocument.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@


/// A simple view that adds a doctype tag
struct HTMLDocument: StaticView {

/// The HTML document to render
let content: CompiledTemplate

func build() -> CompiledTemplate {
Expand Down
2 changes: 2 additions & 0 deletions Sources/HTMLKit/HTMLKitProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ public final class HTMLKitProvider: Provider {

public init() {}

// View `Provider` protocol
public func register(_ services: inout Services) throws {
services.register { (container) in
return try container.make(HTMLRenderer.self)
}
}

// View `Provider` protocol
public func didBoot(_ container: Container) throws -> EventLoopFuture<Void> {
return .done(on: container)
}
Expand Down
38 changes: 37 additions & 1 deletion Sources/HTMLKit/Protocols/DynamicAttributable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ extension DynamicAttributable {

/// Adds an dynamic attribute to a node. This may or may not be added to the renderd view based on the context given
///
/// div.class("foo").if(\.string == "E.g", add .checked)
///
/// - Parameters:
/// - condition: The condition used to evaluate when to add the attributes
/// - attributes: The attributes to add
Expand All @@ -32,7 +34,7 @@ extension DynamicAttributable {

/// Adds an dynamic attribute to a node. This may or may not be added to the renderd view based on the context given
///
/// dynamic(div).class("foo").if(\.isChecked, add .checked)
/// div.class("foo").if(\.isChecked, add .checked)
///
/// - Parameters:
/// - condition: The condition used to evaluate when to add the attributes
Expand All @@ -46,4 +48,38 @@ extension DynamicAttributable {
}
return copy
}

/// Adds an dynamic attribute to a node. This may or may not be added to the renderd view based on the context given
///
/// div.class("foo").if(isNil: \.optional, add .checked)
///
/// - Parameters:
/// - condition: The condition used to evaluate when to add the attributes
/// - attributes: The attributes to add
/// - Returns: A copy of the node with a dynamic attribute
public func `if`<Value>(isNil path: KeyPath<Root.Context, Value?>, add attributes: HTML.AttributeNode...) -> Self {
var copy = self
let condition = IsNullCondition<Root.Context, Value>(path: path)
for attribute in attributes { // Need to create a new `IF.Condition` since it is a referance type and will cause a weird bug otherwise
copy = copy.addDynamic(attribute, with: TemplateIF<Root>.Condition(condition: condition))
}
return copy
}

/// Adds an dynamic attribute to a node. This may or may not be added to the renderd view based on the context given
///
/// div.class("foo").if(isNotNil: \.optional, add .checked)
///
/// - Parameters:
/// - condition: The condition used to evaluate when to add the attributes
/// - attributes: The attributes to add
/// - Returns: A copy of the node with a dynamic attribute
public func `if`<Value>(isNotNil path: KeyPath<Root.Context, Value?>, add attributes: HTML.AttributeNode...) -> Self {
var copy = self
let condition = NotNullCondition<Root.Context, Value>(path: path)
for attribute in attributes { // Need to create a new `IF.Condition` since it is a referance type and will cause a weird bug otherwise
copy = copy.addDynamic(attribute, with: TemplateIF<Root>.Condition(condition: condition))
}
return copy
}
}
12 changes: 6 additions & 6 deletions Tests/HTMLKitTests/HTMLKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ final class HTMLKitTests: XCTestCase {
let thirdIfRender = try renderer.renderRaw(IFView.self, with: .init(name: "Per", age: 21, nullable: "Some", bool: false))
let varialbeRender = try renderer.renderRaw(VariableView.self, with: .init(string: "<script>\"'&</script>"))
let multipleEmbedRender = try renderer.renderRaw(MultipleContextualEmbed.self, with: .init(title: "Welcome", string: "String"))
let nonDynamic = try renderer.renderRaw(DynamicAttribute.self, with: .init(isChecked: false, isActive: false))
let oneDynamic = try renderer.renderRaw(DynamicAttribute.self, with: .init(isChecked: false, isActive: true))
let twoDynamic = try renderer.renderRaw(DynamicAttribute.self, with: .init(isChecked: true, isActive: true))
let nonDynamic = try renderer.renderRaw(DynamicAttribute.self, with: .init(isChecked: false, isActive: false, isOptional: nil))
let oneDynamic = try renderer.renderRaw(DynamicAttribute.self, with: .init(isChecked: false, isActive: true, isOptional: true))
let twoDynamic = try renderer.renderRaw(DynamicAttribute.self, with: .init(isChecked: true, isActive: true, isOptional: false))

let simpleRender = try renderer.renderRaw(SimpleView.self)
let chainedRender = try renderer.renderRaw(ChainedEqualAttributes.self)
Expand All @@ -50,9 +50,9 @@ final class HTMLKitTests: XCTestCase {
XCTAssertEqual(simpleRender, "<div><p>Text</p></div>")
XCTAssertEqual(chainedRender, "<div class='foo bar' id='id'></div>")
XCTAssertEqual(chaindDataRender, "<img class='foo bar' id='id'>")
XCTAssertEqual(nonDynamic, "<div class='foo'></div>")
XCTAssertEqual(oneDynamic, "<div class='foo' active></div>")
XCTAssertEqual(twoDynamic, "<div class='foo checked' active></div>")
XCTAssertEqual(nonDynamic, "<div class='foo' selected></div>")
XCTAssertEqual(oneDynamic, "<div class='foo not-nil' active></div>")
XCTAssertEqual(twoDynamic, "<div class='foo checked not-nil' active></div>")
// XCTAssertEqual(inputRender, "<div class='form-group'><label for='test'>test</label><input class='form-control' type='email' required name='test' id='test' placeholder=''/></div>")
}

Expand Down
25 changes: 18 additions & 7 deletions Tests/HTMLKitTests/HTMLTestDocuments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ struct StaticEmbedView: ContextualTemplate {
variable(\.string)
),

runtimeIf(\.int != nil,
small.child(
variable(\.int)
runtimeIf(
\.int != nil,

small.child(
variable(\.int)
)
)
)
Expand Down Expand Up @@ -147,8 +149,12 @@ struct IFView: ContextualTemplate {
func build() -> CompiledTemplate {
return
div.child(
runtimeIf(\.name == "Mats",
p.child("My name is: ", variable(\.name), "!")
runtimeIf(
\.name == "Mats",

p.child(
"My name is: ", variable(\.name), "!"
)
),

runtimeIf(\.age < 20,
Expand All @@ -165,8 +171,10 @@ struct IFView: ContextualTemplate {
p.child("Simple bool")
),

runtimeIf(\.nullable == "Some" && \.name == "Per",
div.child("And")
runtimeIf(
\.nullable == "Some" && \.name == "Per",

div.child("And")
)

)
Expand Down Expand Up @@ -307,12 +315,15 @@ struct DynamicAttribute: ContextualTemplate {
struct Context {
let isChecked: Bool
let isActive: Bool
let isOptional: Bool?
}

func build() -> CompiledTemplate {
return div.class("foo")
.if(\.isChecked, add: .class("checked"))
.if(\.isActive, add: .init(attribute: "active", value: nil))
.if(isNil: \.isOptional, add: .selected)
.if(isNotNil: \.isOptional, add: .class("not-nil"))
}
}

Expand Down

0 comments on commit 7e1776b

Please sign in to comment.