From bd2b8a425367c37478673b9a546acc63cd8a6c26 Mon Sep 17 00:00:00 2001 From: Mathieu Barnachon Date: Sat, 29 Jun 2019 08:49:30 +0200 Subject: [PATCH] Add a ; at the end of each style, like recommended here: https://www.w3schools.com/html/html_styles.asp The previous version was not adding the ; when more than 2 styles were used. --- Sources/Vaux/Attributes.swift | 6 ++--- Tests/VauxTests/VauxTests.swift | 48 ++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/Sources/Vaux/Attributes.swift b/Sources/Vaux/Attributes.swift index 91fb626..fd63532 100644 --- a/Sources/Vaux/Attributes.swift +++ b/Sources/Vaux/Attributes.swift @@ -101,13 +101,11 @@ extension HTML { /// - attributes: An array of structs typed `StyleAttribute` that contain a key and value for inline styling. public func style(_ attributes: [StyleAttribute]) -> HTML { var inlineStyle = String() - for (index, attribute) in attributes.enumerated() { + for attribute in attributes { inlineStyle.write(attribute.key) inlineStyle.write(":") inlineStyle.write(attribute.value) - if index == 0, attributes.count > 1, index != attributes.count - 1 { - inlineStyle.write(";") - } + inlineStyle.write(";") } return attr("style", inlineStyle) } diff --git a/Tests/VauxTests/VauxTests.swift b/Tests/VauxTests/VauxTests.swift index 4bbc93b..666ebd3 100644 --- a/Tests/VauxTests/VauxTests.swift +++ b/Tests/VauxTests/VauxTests.swift @@ -259,7 +259,7 @@ final class VauxTests: XCTestCase {

When I was - + a young boy, my father took me into the city. @@ -790,7 +790,7 @@ final class VauxTests: XCTestCase { let correctHTML = """ - +

This is a heading of weight 1

@@ -1311,8 +1311,8 @@ final class VauxTests: XCTestCase { - - + +
@@ -3162,6 +3162,45 @@ final class VauxTests: XCTestCase { } } + func testStyles() { + func pageWithHeading() -> HTML { + html { + body { + heading(.h1) { + "This is a heading" + }.style([StyleAttribute(key: "color", value: "blue"), + StyleAttribute(key: "font-family", value: "verdana")]) + paragraph { + "This is a paragraph." + }.style([StyleAttribute(key: "font-family", value: "courier"), + StyleAttribute(key: "font-size", value: "160%"), + StyleAttribute(key: "color", value: "blue")]) + }.style([StyleAttribute(key: "background-color", value: "powderblue")]) + } + } + let correctHTML = """ + + + +

+ This is a heading +

+

+ This is a paragraph. +

+ + + """.replacingOccurrences(of: "\n", with: "") + let vaux = Vaux() + vaux.outputLocation = .file(filepath: Filepath(name: "testing", path: "/tmp/")) + do { + let rendered = try VauxTests.renderForTesting(with: vaux, html: pageWithHeading()) + XCTAssertEqual(rendered, correctHTML) + } catch let error { + XCTFail(error.localizedDescription) + } + } + public static func renderForTesting(with vaux: Vaux, html: HTML) throws -> String { do { try vaux.render(html) @@ -3260,5 +3299,6 @@ final class VauxTests: XCTestCase { ("testUnderline", testUnderline), ("testVariable", testVariable), ("testWordBreak", testWordBreak), + ("testStyles", testStyles), ] }