Updated: Breaking changes for boolean attributes in HAML 6

Posted . Visible to the public. Auto-destruct in 43 days

Updated most aspects of this card.

Changes

  • -Haml 6 introduced [breaking changes](https://github.com/haml/haml/blob/main/CHANGELOG.md#600) regarding the definition of ["boolean attributes"](https://github.com/haml/haml/issues/1091). Only `aria-`, `data-` and a list of [select attributes](https://github.com/haml/haml/blob/main/CHANGELOG.md#600) (e.g. `hidden`, `disabled`) are considered "boolean". ["Boolean attributes" express meaning by their _presence_](https://haml.info/docs/yardoc/file.REFERENCE.html#boolean-attributes).
  • +Haml 6 introduced [breaking changes](https://github.com/haml/haml/blob/main/CHANGELOG.md#600) regarding the definition of ["boolean attributes"](https://github.com/haml/haml/issues/1091). Only `aria-`, `data-` and a list of [select attributes](https://github.com/haml/haml/blob/main/CHANGELOG.md#600) (e.g. `hidden`, `disabled`) are considered "boolean". ["Boolean attributes" express meaning by their _presence_](https://haml.info/docs/yardoc/file.REFERENCE.html#boolean-attributes).
  • **What actually changes is the handling non-boolean attributes** (see examples below). Haml will continue to render or drop boolean attributes depending on their value – but it will always render non-boolean attributes, even when given a value of `false`.
  • ### Interpretation of "boolean" attributes does not change
  • - No Haml value => no HTML value
  • - Boolean Haml value => attribute is rendered depending on that value
  • - All other values => rendered verbatim
  • | Haml | HTML generated by Haml 5 & 6 |
  • |----------------------------|-----------------------|
  • | %button(disabled) | `<button disabled></button>` |
  • | %button(disabled=true) | `<button disabled></button>` |
  • | %button(disabled=false) | `<button></button>` |
  • | %button(disabled='true') | `<button disabled="true"></button>` |
  • | %button(disabled='false') | `<button disabled="false"></button>` |
  • | %button(disabled='') | `<button disabled=''></button>` |
  • | %button(data-test) | `<button data-test></button>` |
  • | %button(data-test=true) | `<button data-test></button>` |
  • | %button(data-test=false) | `<button></button>` |
  • | %button(data-test='true') | `<button data-test="true"></button>` |
  • | %button(data-test='false') | `<button data-test="false"></button>` |
  • | %button(data-test='') | `<button data-test=''></button>` |
  • ### Other attributes will be handled differently by Haml 6
  • New: custom attributes will always render an HTML value. They are not toggled by their value. To render a custom attribute without a value, you can either
  • - Assign an empty string (`custom-attribute=''`)
  • - Use a `data-` attribute (`data-custom-attribute`) – recommended for attributes that actually convey data
  • - Or add the custom attribute to the list of boolean attributes (see below)
  • | Haml | HTML generated by Haml 5 | HTML generated by Haml 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>` | same |
  • | %button(up-test='false') | `<button up-test="false"></button>` | same |
  • | %button(up-test='') | `<button up-test=''></button>` | same |
  • ## Extending the list of attributes considered "boolean"
  • You can add to the list of "boolean attributes". However, please do sparingly and prefer the alternatives suggested above.
  • ```ruby
  • # config/initializers/haml.rb
  • Haml::BOOLEAN_ATTRIBUTES.push(/^app-/)
  • Haml::BOOLEAN_ATTRIBUTES.push(*%w[one another])
  • ```
  • ## Boolean [Unpoly](https://unpoly.com) attributes
  • Be very selective about patching `up-` attributes so you can still use expressions like `[up-follow=false]` or `[up-history=false]`.
  • Starting with [Unpoly 3.8](https://github.com/unpoly/unpoly/discussions/646), most Unpoly attributes can be enabled with a value `"true"` and be disabled with a value `"false"`. This pairs well with the default (unpatched) Haml 6 behavior.
  • A different approach is to create attributes from a ruby hash:
  • ```haml
  • %tr{ { 'up-expand': ('' if superuser?) }.compact }
  • ```
  • This will provide you with `<tr up-expand="">` for a superuser and `<tr>` otherwise.
Dominik Schöler
License
Source code in this card is licensed under the MIT License.
Posted by Dominik Schöler to makandra dev (2024-12-04 07:14)