Skip to content

Commit

Permalink
Merge pull request #41 from pixel-foundry/master
Browse files Browse the repository at this point in the history
Add viewport-fit to viewport meta tag
  • Loading branch information
JohnSundell authored Mar 31, 2023
2 parents 42ab862 + ae6a174 commit d8fa91a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Sources/Plot/API/HTMLComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,15 @@ public extension Node where Context == HTML.HeadContext {
/// - parameter widthMode: How the viewport's width should scale according
/// to the device the page is being rendered on. See `HTMLViewportWidthMode`.
/// - parameter initialScale: The initial scale that the page should use.
/// - parameter fit: How the viewport should be laid out on screen in relation
/// to the screen’s safe area insets. See `HTMLViewportFitMode`.
static func viewport(_ widthMode: HTMLViewportWidthMode,
initialScale: Double = 1) -> Node {
let content = "width=\(widthMode.string), initial-scale=\(initialScale)"
initialScale: Double = 1,
fit: HTMLViewportFitMode? = nil) -> Node {
var content = "width=\(widthMode.string), initial-scale=\(initialScale)"
if let fit = fit {
content += ", viewport-fit=\(fit.rawValue)"
}
return .meta(.name("viewport"), .content(content))
}

Expand Down
19 changes: 19 additions & 0 deletions Sources/Plot/API/HTMLViewportFitMode.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Plot
* Copyright (c) John Sundell 2019
* MIT license, see LICENSE file for details
*/

import Foundation

/// Enum defining the fit parameters of the viewport meta tag.
public enum HTMLViewportFitMode: String {
/// The default viewport fit behavior.
case auto
/// The initial layout viewport and the visual viewport are set
/// to fit within the safe area insets of the screen of the device.
case contain
/// The initial layout viewport and the visual viewport are set
/// to the outer rectangle covering the screen, ignoring safe area insets.
case cover
}
7 changes: 7 additions & 0 deletions Tests/PlotTests/HTMLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ final class HTMLTests: XCTestCase {
<head><meta name="viewport" content="width=500, initial-scale=1.0"/></head>
""")
}

func testViewportFit() {
let html = HTML(.head(.viewport(.accordingToDevice, fit: .cover)))
assertEqualHTMLContent(html, """
<head><meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover"/></head>
""")
}

func testFavicon() {
let html = HTML(.head(.favicon("icon.png")))
Expand Down

0 comments on commit d8fa91a

Please sign in to comment.