Read more

Breaking changes for boolean attributes in HAML 6

Emanuel
May 16, 2023Software engineer at makandra GmbH

Haml 6 has some breaking changes Show archive.org snapshot regarding boolean attributes Show archive.org snapshot .

Only the following attributes and aria/data attributes are considered boolean attributes: allowfullscreen, async, autobuffer, autofocus, autoplay, checked, controls, default, defer, disabled, download, formnovalidate, hidden, inert, ismap, itemscope, loop, multiple, muted, novalidate, open, pubdate, readonly, required, reversed, scoped,
seamless, selected, sortable, truespeed, typemustmatch

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

This requires you to change custom attributes in case you want them to be an attribute without a value e.g. a(up-test) has to be changed to a(up-test='') in HAML 6.

Examples

Haml (no breaking changes) Version 5 Version 6
%button(disabled) <button disabled=""></button> <button disabled=""></button>
%button(disabled=true) <button disabled=""></button> <button disabled=""></button>
%button(disabled=false) <button></button> <button></button>
%button(disabled='true') <button disabled="true"></button> <button disabled="true"></button>
%button(disabled='false') <button disabled="false"></button> <button disabled="false"></button>
%button(disabled='') <button disabled=""></button> <button disabled=""></button>
Haml (no breaking changes) Version 5 Version 6
%button(data-test) <button data-test=""></button> <button data-test=""></button>
%button(data-test=true) <button data-test=""></button> <button data-test=""></button>
%button(data-test=false) <button></button> <button></button>
%button(data-test='true') <button data-test="true"></button> <button data-test="true"></button>
%button(data-test='false') <button data-test="false"></button> <button data-test="false"></button>
%button(data-test='') <button data-test=""></button> <button data-test=""></button>
Haml (with breaking changes) Version 5 Version 6
%button(up-test) <button up-test=""></button> <button up-test="true"></button>
%button(up-test=true) <button up-test=""></button> <button up-test="true"></button>
%button(up-test=false) <button></button> <button up-test="false"></button>
%button(up-test='true') <button up-test="true"></button> <button up-test="true"></button>
%button(up-test='') <button up-test=""></button> <button up-test=""></button>

Monkey Patch

In case you want to reconfigure the attributes that a recognized as boolean attributes you can use a monkey patch in an initializer. For example, the following would consider all attributes prefixed with app- to be boolean attributes:

### Haml 6.3 and newer
Haml.class_eval do
  remove_const(:BOOLEAN_ATTRIBUTES).tap do |boolean_attributes|
    const_set(:BOOLEAN_ATTRIBUTES, [*boolean_attributes, /^app-/].freeze)
  end
end

### Haml 6.0 - 6.2.x
Haml::AttributeBuilder.class_eval do
  remove_const(:BOOLEAN_ATTRIBUTES).tap do |boolean_attributes|
    const_set(:BOOLEAN_ATTRIBUTES, [*boolean_attributes, /^app-/].freeze)
  end
end

Note: You might want to consider using data attributes for all your custom attributes e.g. scrollable => data-scrollable. It might still be legit to allow framework prefixes in case they do not support data attributes e.g. ng- (Angular) or up- (Unpoly).
But be very selective about patching up- attributes so you can still use expressions like [up-follow=false] or [up-history=false].

A different approach is to create attributes from a ruby hash:

%tr{ { 'up-expand': ('' if superuser?) }.compact }

This will provide you with <tr up-expand=""> for a superuser and <tr> otherwise.

Emanuel
May 16, 2023Software engineer at makandra GmbH
Posted by Emanuel to makandra dev (2023-05-16 16:14)