Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"data-" prefix is added to custom attributes, how to prevent this?

I have upgraded Pandoc to v2, and some of my regression tests fail now.

It's the ones where I add custom attributes to elements like so:

# Test {role="heading" aria-level="7"}

In earlier versions of Pandoc, this resulted in

<h1 role="heading" aria-level="7">Test</h1>

While in Pandoc 2, it's

<h1 data-role="heading" data-aria-level="7">Test</h1>

How can I change it back? Both role and aria- attributes are 100% valid HTML, so no data- prefix is needed.

like image 839
Joshua Muheim Avatar asked Jan 24 '18 19:01

Joshua Muheim


People also ask

What are custom data attributes?

Custom attributes are attributes that are not part of the standard HTML5 attributes but are explicitly created. They allow us to add our own information to HTML tags. These are not specific and can be used with all the HTML elements.

How do you set data attribute using JavaScript?

In order to create a new data attribute in JavaScript we just need to add a new property to the dataset object with a value. This will update the dataset object and our HTML which means our HTML will look like this.

Can you use custom HTML attributes?

You can add custom HTML attributes to pages and page elements, which are rendered on the HTML tag of the page element. For example, this is useful when working with third-party frameworks that render page elements differently based on certain attributes.

What are data attributes used for?

data-* attributes allow us to store extra information on standard, semantic HTML elements without other hacks such as non-standard attributes, or extra properties on DOM.


1 Answers

It seems to be a difference with Pandoc in the conversion from Markdown to HTML5 vs HTML4.

Using their test editor the HTML4 conversion works perfectly fine.

Pandoc HTML4 Conversion Example

However, when we switch to HTML5 the data- portion of the attribute gets injected.

Pandoc HTML5 Conversion Example

This makes me think that this is either intentionally done, for differences between HTML4 and HTML5, or that it is potentially a Pandoc bug, which you should probably raise an issue with them in their github issue tracker: https://github.com/jgm/pandoc/issues

From the Pandoc docs - html defaults to html5 as follows:

  • html or html5 ([HTML], i.e. [HTML5]/XHTML [polyglot markup])
  • html4 ([XHTML] 1.0 Transitional)

Update:

It looks like this IS because of HTML5 spec - the "role" and "aria" attributes themselves are no longer in the attribute list. Reference: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes

So this NOT a bug with Pandoc - this is expected behavior. My suggestion, use the html4 conversion is your answer.

Thanks to @Caramiriel in the comments for additional reference in the Pandoc code.

like image 148
tremor Avatar answered Nov 10 '22 16:11

tremor