Skip to content

Fix: downlit_html_*() keeps all pre classes#160

Open
etiennebacher wants to merge 7 commits into
r-lib:mainfrom
etiennebacher:fix_pre_class
Open

Fix: downlit_html_*() keeps all pre classes#160
etiennebacher wants to merge 7 commits into
r-lib:mainfrom
etiennebacher:fix_pre_class

Conversation

@etiennebacher

@etiennebacher etiennebacher commented Sep 20, 2022

Copy link
Copy Markdown

Close #149, close #156 (and v2 of #155).

downlit_html() now keeps all pre classes, which allows for customization of code blocks in pkgdown using chunk options class.source and attr.source.

Current behavior (.my-class and .another-class are dropped):

library(downlit)

html <- xml2::read_xml("
<div>
  <div class='sourceCode'>
    <pre class='sourceCode r my-class'>
      <code class='sourceCode r'>
        <span>Hello</span>
      </code>
    </pre>
  </div>
  <div class='sourceCode'>
    <pre class='sourceCode r another-class'>
      <code class='sourceCode r'>
        <span>Hello</span>
      </code>
    </pre>
  </div>
</div>")
downlit_html_node(html)
html
#> {xml_document}
#> <div>
#> [1] <div class="sourceCode">\n  <pre class="downlit sourceCode r">\n<code cla ...
#> [2] <div class="sourceCode">\n  <pre class="downlit sourceCode r">\n<code cla ...

New behavior:

library(downlit)

html <- xml2::read_xml("
<div>
  <div class='sourceCode'>
    <pre class='sourceCode r my-class'>
      <code class='sourceCode r'>
        <span>Hello</span>
      </code>
    </pre>
  </div>
  <div class='sourceCode'>
    <pre class='sourceCode r another-class'>
      <code class='sourceCode r'>
        <span>Hello</span>
      </code>
    </pre>
  </div>
</div>")
downlit_html_node(html)
html
#> {xml_document}
#> <div>
#> [1] <div class="sourceCode">\n  <pre class="downlit sourceCode r my-class">\n ...
#> [2] <div class="sourceCode">\n  <pre class="downlit sourceCode r another-clas ...

@etiennebacher

Copy link
Copy Markdown
Author

Test failures are unrelated to this PR.

Comment thread R/downlit-html.R

text <- xml2::xml_text(nodes)
replacement <- map_chr(text, fun, ...)
replacement <- unlist(lapply(seq_along(nodes), function(x) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could simplify this code by adding pre_class as an explicit argument to tweak_children() — then you don't need to go through gymnastics to figure out whether or not it's set.

@etiennebacher etiennebacher Sep 27, 2022

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand. If I add an explicit argument pre_class in tweak_children(), then I will have to check whether this argument is NULL (or FALSE) before applying strsplit(), so I don't think this would result in a simpler code. But maybe I misunderstood you?

@sgvignali

Copy link
Copy Markdown

Do you have any updates?

@etiennebacher

etiennebacher commented May 5, 2023

Copy link
Copy Markdown
Author

@sgvignali I don't think there's anything more I can do on my side, so I'm just waiting for someone from r-lib to review this

@froggleston

froggleston commented Apr 7, 2026

Copy link
Copy Markdown

Apologies for necroing this old PR and related issue, but I have been trying to fix a failing WCAG check and found this. Current accessibility standards state that scrollable elements need a tabindex to allow keyboard focus and scrolling. Given that downlit removes any user-added pre classes and attributes (e.g. tabindex="0"), the WCAG check will fail:

https://dequeuniversity.com/rules/axe-devtools/4.6/scrollable-region-focusable

Output from the AC Lite 1.0.7 Accessibility Checker extension for Chrome
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Autolinking Quarto html remove the "copy-code" button downlit does not keep code source option set with knitr

4 participants