Class: Haml

Inherits: Node
Defined in: src/nodes/haml.coffee

Overview

HAML node that contains Haml a haml tag that can have attributes and a text or code assignment. There are shortcuts for id and class generation and some special logic for merging attributes into existing ids and classes.

Haml HTML attributes are very limited and allows only simple string (with interpolation) or variable assignment to an attribute.

Ruby HTML attributes are more powerful and allows in addition to the HTML attributes function calls:

Examples:

Haml tag

%footer                               =>  <footer></footer>

Haml id

#content                              =>  <div id='content'></div>
%span#status{ :id => @user.status }   =>  <span id='status_#{ @user.status }'></span>

Haml classes

.hidden                               => <div class='hidden'></div>
%span.large.hidden                    => <span class='large hidden'></span>
.large{ :class => @user.role }        => <div class='large #{ @user.role }'></div>

Haml HTML attributes

%p(class='hidden')                     => <p class='hidden'><p>
#account(class=@status)                => <div id='account' class='#{ status }'></div>
.logout(title="Logout #{ user.name }") => <div class='logout' title='Logout #{ user.name }'></div>

Haml Ruby attributes

%p{ :class => App.user.get('role') }   => <p class='#{ App.user.get('role') }'></p>

Constant Summary

Constants inherited from Node

CLEAR_WHITESPACE_LEFT, CLEAR_WHITESPACE_RIGHT

Instance Method Summary

Methods inherited from Node

#constructor, #addChild, #getOpener, #getCloser, #isPreserved, #markText, #markRunningCode, #markInsertingCode, #evaluate, #render

Instance Method Details

- (void) evaluate()

Evaluate the node content and store the opener tag and the closer tag if applicable.

- (Object) parseExpression(exp)

Parses the expression and detect the tag, attributes and any assignment. In addition class and id cleanup is performed according the the Haml spec:

  • Classes are merged together
  • When multiple ids are provided, the last one is taken, except they are defined in shortcut notation and attribute notation. In this case, they will be combined, separated by underscore.

Examples:

Id merging

#user{ :id => @user.id }    =>  <div id='user_#{ @user.id }'></div>

Parameters:

  • exp (String) the HAML expression

Returns:

  • (Object) the parsed tag and options tokens

- (Object) parseTag(exp)

Parse a tag line. This recognizes DocType tags !!! and HAML tags like #id.class text.

It also parses the code assignment =, }= and )= or inline text and the whitespace removal markers < and >.

Parameters:

  • exp (String) the HAML expression

Returns:

  • (Object) the parsed tag tokens

- (Array) parseAttributes(exp)

Parse attributes either in Ruby style %tagundefined or HTML style %tag(attr='value). Both styles can be mixed: %tag(attr='value)undefined.

This takes also care of proper attribute interpolation, unwrapping quoted keys and value, e.g. 'a' => 'hello' becomes a => hello.

Parameters:

  • exp (String) the HAML expression

Returns:

  • (Array) the parsed attribute tokens

- (Array<String, Array>) getDataAttributes(exp)

Extracts the data attributes.

Examples:

data attribute

`:data => { :test => '123' }`

Parameters:

  • exp (String) the expression to check

Returns:

  • (Array) the expressions and data attributes

- (String) buildHtmlTagPrefix(tokens)

Build the HTML tag prefix by concatenating all the tag information together. The result is an unfinished html tag that must be further processed:

The Haml spec sorts the class names, even when they contain interpolated classes. This is supported by sorting classes at template render time.

Examples:

Prefix tag

<a id='id' class='class' attr='value'

Template render time sorting

<p class='#{ [@user.name(), 'show'].sort().join(' ') }'>

Parameters:

  • tokens (Object) all parsed tag tokens

Returns:

  • (String) the tag prefix

- (String) quoteAttributeValue(value)

Quote the attribute value, depending on its content.

Parameters:

  • value (String) the without start and end quote

Returns:

  • (String) the quoted value

- (String) buildDocType(doctype)

Build the DocType string depending on the !!! token and the currently used HTML format.

Parameters:

  • doctype (String) the HAML doctype

Returns:

  • (String) the HTML doctype

- (Boolean) isNotSelfClosing(tag)

Test if the given tag is a non-self enclosing tag, by matching against a fixed tag list or parse for the self closing slash / at the end.

Parameters:

  • tag (String) the tag name without brackets

Returns:

  • (Boolean) true when a non self closing tag