phug-php/bemto

View on GitHub
src/templates/bemto_tag.pug

Summary

Maintainability
Test Coverage
//- bemto
//- Copyright(c) 2012 Roman Komarov <kizu@kizu.ru>
//- MIT Licensed

include bemto_custom_tag.pug

//- Tag mixin, used for tweaking what tag we are throwing and do we need to wrap anything here

mixin bemto_tag($tag, $tagMetadata)
  - $settings = $get_bemto_settings()
  - $tagMetadata = (array) ($tagMetadata ?: [])

  //- rewriting tag name on different contexts
  - $newTag = $tag ?: $settings['default_tag']
  - $contextIndex = count($bemto_chain_contexts)
  - $listContext = ($contextIndex && $bemto_chain_contexts[$contextIndex - 1] === 'list')

  //- Checks for contexts if no tag given
  if !$tag
    if $contextIndex && $bemto_chain_contexts[$contextIndex - 1] === 'inline'
      - $newTag = 'span'
    else if $listContext
      - $newTag = 'li'
    else if $contextIndex && $bemto_chain_contexts[$contextIndex - 1] === 'optionlist'
      - $newTag = 'option'

  //- Attributes context checks
  //- Only when no actual tag is given
  if (!$tag) || ($tag === 'span') || ($tag === 'div')
    if isset($attributes['href']) && $attributes['href']
      - $newTag = 'a'
    if isset($attributes['for']) && $attributes['for']
      - $newTag = 'label'
    if isset($attributes['type']) && $attributes['type']
      //- TODO: Add more checks for different type value patterns
      - $newTag = $block ? 'button' : 'input'
    else if isset($attributes['src']) && $attributes['src']
      - $newTag = 'img'
  - $liWrap = ($listContext && $newTag !== 'li')
  //- Contextual wrappers
  if $liWrap
    | <li>
  else if $contextIndex && ($bemto_chain_contexts[$contextIndex - 1] !== 'list') && ($bemto_chain_contexts[$contextIndex - 1] !== 'pseudo-list') && ($newTag === 'li')
    | <ul>
    - $bemto_chain_contexts[count($bemto_chain_contexts)] = 'pseudo-list'
  else if $contextIndex && ($bemto_chain_contexts[$contextIndex - 1] === 'pseudo-list') && ($newTag !== 'li')
    | </ul>
    - $bemto_chain_contexts = array_splice($bemto_chain_contexts, 0, -1)


  //- Setting context
  - $content_type = isset($tagMetadata['content_type']) && $tagMetadata['content_type'] ? $tagMetadata['content_type'] : $get_bemto_tag_content_type($newTag)
  - $bemto_chain_contexts[] = $content_type

  if $newTag === 'img'
    //- If there is no title we don't need it to show even if there is some alt
    if isset($attributes['alt']) && $attributes['alt'] && !(isset($attributes['title']) && $attributes['title'])
      - $attributes['title'] = ''
    //- If we have title, we must have it in alt if it's not set
    if isset($attributes['title']) && $attributes['title'] && !(isset($attributes['alt']) && $attributes['alt'])
      - $attributes['alt'] = $attributes['title']
    if !(isset($attributes['alt']) && $attributes['alt'])
      - $attributes['alt'] = ''
    if isset($attributes['alt']) && $attributes['alt'] === ''
      - $attributes['role'] = 'presentation'
    if !(isset($attributes['src']) && $attributes['src'])
      if isset($settings['nosrc_substitute']) && $settings['nosrc_substitute'] === true
        - $attributes['src'] = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
      else if isset($settings['nosrc_substitute']) && $settings['nosrc_substitute']
        - $attributes['src'] = $settings['nosrc_substitute']

  if $newTag === 'input'
    if !(isset($attributes['type']) && $attributes['type'])
      - $attributes['type'] = 'text'

  if $newTag === 'main'
    if !(isset($attributes['role']) && $attributes['role'])
      - $attributes['role'] = 'main'

  if $newTag === 'html'
    doctype html

  +bemto_custom_tag($newTag, $tagMetadata)&attributes($attributes)
    if $block
      block

  //- Closing all the wrapper tails
  if $liWrap
    | </li>