Skip to content

Commit 3aca9c0

Browse files
hadleySebKrantz
authored andcommitted
Improve HTML5 compliance (r-lib#2523)
Fixes r-lib#2369
1 parent 6145d43 commit 3aca9c0

20 files changed

+136
-111
lines changed

NEWS.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# pkgdown (development version)
22

3+
* Improve HTML5 compliance (#2369):
4+
* No longer support IE9 or earlier
5+
* Strip trailing whitespace
6+
* Label `<nav>`s and improve navbar html.
37
* `build_home_index()` now renders math if you use it in your home page (#2263).
48
* `build_home()` now correctly escapes special HTML characters in the bibtex citation (#2022).
59
* BS5 templates no longer include empty link to logo when none exists (#2536).

R/navbar.R

+18-18
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,16 @@ bs4_navbar_links_tags <- function(links, depth = 0L, side = "left") {
274274
return(
275275
htmltools::tags$li(
276276
class = menu_class,
277-
htmltools::tags$a(
278-
href = "#", class = "nav-link dropdown-toggle",
279-
`data-bs-toggle` = "dropdown", role = "button",
280-
`aria-expanded` = "false", `aria-haspopup` = "true",
277+
htmltools::tags$button(
278+
href = "#",
279+
class = "nav-link dropdown-toggle",
280+
`data-bs-toggle` = "dropdown",
281+
type = "button",
282+
`aria-expanded` = "false",
283+
`aria-haspopup` = "true",
281284
link_text,
282285
id = paste0("dropdown-", make_slug(link_text)),
283-
"aria-label" = x$`aria-label` %||% NULL
286+
"aria-label" = x$`aria-label` %||% NULL
284287
),
285288
htmltools::tags$div(
286289
class = dropdown_class,
@@ -336,24 +339,21 @@ bs4_navbar_links_tags <- function(links, depth = 0L, side = "left") {
336339

337340
tags <- purrr::map2(links, seq_along(links), tackle_link, is_submenu = is_submenu, depth = depth)
338341
htmltools::tagList(tags)
339-
340342
}
341343

342344
bs4_navbar_link_text <- function(x, ...) {
343-
344345
if (!is.null(x$icon)) {
345-
# find the iconset
346-
split <- strsplit(x$icon, "-")
347-
if (length(split[[1]]) > 1) {
348-
iconset <- split[[1]][[1]]
349-
}
350-
else {
351-
iconset <- ""
352-
}
353-
htmltools::tagList(htmltools::tags$span(class = paste(iconset, x$icon)), " ", x$text, ...)
354-
}
355-
else
346+
# Find the icon set
347+
classes <- strsplit(x$icon, " ")[[1]]
348+
icon_classes <- classes[grepl("-", classes)]
349+
iconset <- purrr::map_chr(strsplit(icon_classes, "-"), 1)
350+
class <- paste0(unique(c(iconset, classes)), collapse = " ")
351+
352+
text <- paste0(if (!is.null(x$text)) " ", x$text)
353+
htmltools::tagList(htmltools::tags$span(class = class), text, ...)
354+
} else {
356355
htmltools::tagList(x$text, ...)
356+
}
357357
}
358358

359359
# Testing helpers ---------------------------------------------------------

R/render.R

+6-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ render_page_html <- function(pkg, name, data = list(), depth = 0L) {
6363
template <- find_template("layout", name, pkg = pkg)
6464
rendered <- render_template(template, components)
6565

66+
# Strip trailing whitespace
67+
rendered <- gsub("\\s+\n", "\n", rendered, perl = TRUE)
68+
6669
xml2::read_html(rendered, encoding = "UTF-8")
6770
}
6871

@@ -98,7 +101,9 @@ data_template <- function(pkg = ".", depth = 0L) {
98101
version = tr_("Version"),
99102
examples = tr_("Examples"),
100103
citation = tr_("Citation"),
101-
author_details = tr_("Additional details")
104+
author_details = tr_("Additional details"),
105+
toc = tr_("Table of contents"),
106+
site_nav = tr_("Site navigation")
102107
)
103108

104109
# Components that mostly end up in the <head>

inst/BS5/templates/content-article-index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ <h3>{{{title}}}</h3>
2121
</main>
2222

2323
<aside class="col-md-3">
24-
<nav id="toc">
24+
<nav id="toc" aria-label="{{#translate}}{{toc}}{{/translate}}">
2525
<h2>{{#translate}}{{on_this_page}}{{/translate}}</h2>
2626
</nav>
2727
</aside>

inst/BS5/templates/content-article.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ <h4 data-toc-skip class="date">$date$</h4>
4848
</main>
4949
{{#toc}}
5050
<aside class="col-md-3">
51-
<nav id="toc">
51+
<nav id="toc" aria-label="{{#translate}}{{toc}}{{/translate}}">
5252
<h2>{{#translate}}{{on_this_page}}{{/translate}}</h2>
5353
</nav>
5454
</aside>

inst/BS5/templates/content-authors.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ <h1>{{{pagetitle}}}</h1>
2020
</main>
2121

2222
<aside class="col-md-3">
23-
<nav id="toc">
23+
<nav id="toc" aria-label="{{#translate}}{{toc}}{{/translate}}">
2424
<h2>{{#translate}}{{on_this_page}}{{/translate}}</h2>
2525
</nav>
2626
</aside>

inst/BS5/templates/content-citation-authors.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ <h2>{{#translate}}{{author_details}}{{/translate}}</h2>
3838
</main>
3939

4040
<aside class="col-md-3">
41-
<nav id="toc">
41+
<nav id="toc" aria-label="{{#translate}}{{toc}}{{/translate}}">
4242
<h2>{{#translate}}{{on_this_page}}{{/translate}}</h2>
4343
</nav>
4444
</aside>

inst/BS5/templates/content-news-index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ <h1>{{{pagetitle}}}</h1>
1212
</main>
1313

1414
<aside class="col-md-3">
15-
<nav id="toc">
15+
<nav id="toc" aria-label="{{#translate}}{{toc}}{{/translate}}">
1616
<h2>{{#translate}}{{on_this_page}}{{/translate}}</h2>
1717
</nav>
1818
</aside>

inst/BS5/templates/content-news.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ <h1>{{{pagetitle}}}</h1>
1212
</main>
1313

1414
<aside class="col-md-3">
15-
<nav id="toc">
15+
<nav id="toc" aria-label="{{#translate}}{{toc}}{{/translate}}">
1616
<h2>{{#translate}}{{on_this_page}}{{/translate}}</h2>
1717
</nav>
1818
</aside>

inst/BS5/templates/content-reference-index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<h1>{{{pagetitle}}}</h1>
66
</div>
77

8-
{{#rows}}<div id="{{slug}}" class="section level2">
8+
{{#rows}}<div {{#slug}}id="{{.}}"{{/slug}} class="section level2">
99
{{#title}}<h2>{{{.}}}</h2>{{/title}}
1010
{{#subtitle}}<h3>{{{.}}}</h3>{{/subtitle}}
1111
{{#desc}}<div class="section-desc">{{{desc}}}</div>{{/desc}}
@@ -21,7 +21,7 @@ <h1>{{{pagetitle}}}</h1>
2121
</main>
2222

2323
<aside class="col-md-3">
24-
<nav id="toc">
24+
<nav id="toc" aria-label="{{#translate}}{{toc}}{{/translate}}">
2525
<h2>{{#translate}}{{on_this_page}}{{/translate}}</h2>
2626
</nav>
2727
</aside>

inst/BS5/templates/content-reference-topic.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ <h2>{{#translate}}{{examples}}{{/translate}}</h2>
4141
{{/examples}}
4242
</main>
4343
<aside class="col-md-3">
44-
<nav id="toc">
44+
<nav id="toc" aria-label="{{#translate}}{{toc}}{{/translate}}">
4545
<h2>{{#translate}}{{on_this_page}}{{/translate}}</h2>
4646
</nav>
4747
</aside>

inst/BS5/templates/content-title-body.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ <h1>{{pagetitle}}</h1>
1111
</main>
1212

1313
<aside class="col-md-3">
14-
<nav id="toc">
14+
<nav id="toc" aria-label="{{#translate}}{{toc}}{{/translate}}">
1515
<h2>{{#translate}}{{on_this_page}}{{/translate}}</h2>
1616
</nav>
1717
</aside>

inst/BS5/templates/content-tutorial-index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ <h1>{{{pagetitle}}}</h1>
1313
</main>
1414

1515
<aside class="col-md-3">
16-
<nav id="toc">
16+
<nav id="toc" aria-label="{{#translate}}{{toc}}{{/translate}}">
1717
<h2>{{#translate}}{{on_this_page}}{{/translate}}</h2>
1818
</nav>
1919
</aside>

inst/BS5/templates/content-tutorial.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ <h1>{{pagetitle}}</h1>
55
</div>
66
</main>
77
<aside class="col-md-3">
8-
<nav id="toc">
8+
<nav id="toc" aria-label="{{#translate}}{{toc}}{{/translate}}">
99
<h2>{{#translate}}{{on_this_page}}{{/translate}}</h2>
1010
</nav>
1111
</aside>

inst/BS5/templates/head.html

-5
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,6 @@
8484
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script>
8585
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script>
8686

87-
<!--[if lt IE 9]>
88-
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
89-
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
90-
<![endif]-->
91-
9287
{{#yaml}}{{#ganalytics}}
9388
<!-- Global site tag (gtag.js) - Google Analytics -->
9489
<script async src="https://www.googletagmanager.com/gtag/js?id={{.}}"></script>

inst/BS5/templates/navbar.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{{#navbar}}
2-
<nav class="navbar fixed-top navbar-{{{type}}} navbar-expand-lg bg-{{{bg}}}" data-bs-theme="{{{type}}}">
2+
<nav class="navbar fixed-top navbar-{{{type}}} navbar-expand-lg bg-{{{bg}}}" data-bs-theme="{{{type}}}" aria-label="{{#translate}}{{site_nav}}{{/translate}}">
33
<div class="container">
44
{{#includes}}{{{before_title}}}{{/includes}}
55
<a class="navbar-brand me-2" href="{{#site}}{{root}}{{/site}}index.html">{{#site}}{{title}}{{/site}}</a>

0 commit comments

Comments
 (0)