faml icon indicating copy to clipboard operation
faml copied to clipboard

Skip falsey attribute rendering only if it's boolean attribute

Open eagletmt opened this issue 9 years ago • 2 comments

This is an optimization idea of Hamlit.

eagletmt avatar Jun 14 '15 08:06 eagletmt

It seems there's no standard on how to render true/false/nil of non-boolean attributes.

  • Haml, Slim, Faml (before this PR)
    • %span{disabled: true, hidden: false, readonly: nil, foo: true, bar: false, baz: nil} renders <span disabled foo></span> .
    • All falsey attributes are not rendered.
    • It doesn't consider boolean attributes or not.
  • Rails
    • tag("span", disabled: true, hidden: false, readonly: nil, foo: true, bar: false, baz: nil) renders <span disabled="disabled" foo="true" bar="false" /> .
    • Falsey attributes are not rendered if it is a boolean attribute.
    • nil attribute is not rendered if it isn't boolean attribute.
  • Faml (after this PR), Hamlt (maybe)
    • %span{disabled: true, hidden: false, readonly: nil, foo: true, bar: false, baz: nil} renders <span bar='false' baz='' disabled foo='true'></span> .
    • Falsey attributes are not rendered if it is a boolean attribute.
    • All attributes are rendered if it isn't a boolean attribute.

eagletmt avatar Jun 14 '15 09:06 eagletmt

This optimization only contributes to the performacne of "dynamic attributes" by removing runtime conditional branching.

Input

- url = 'https://wanko.cc'
%a{href: url}

Before

_buf = []; extend ::Faml::Helpers; url = 'https://wanko.cc'; 
; _buf << ("<a".freeze); _faml_html1 = (url); case (_faml_html1); when true; _buf << (" href".freeze); when false, nil; else; _buf << (" href='".freeze); _buf << (::Temple::Utils.escape_html((_faml_html1))); _buf << ("'".freeze); end; _buf << ("></a>\n".freeze); 
; _buf = _buf.join

After

_buf = []; extend ::Faml::Helpers; url = 'https://wanko.cc'; 
; _buf << ("<a href='".freeze); _buf << (::Temple::Utils.escape_html((url))); _buf << ("'></a>\n".freeze); 
; _buf = _buf.join

eagletmt avatar Jun 14 '15 10:06 eagletmt