class Sablon::HTMLConverter::List

Manages the child nodes of a list type tag

Public Class Methods

new(env, node, properties) click to toggle source
Calls superclass method Sablon::HTMLConverter::Collection::new
# File lib/sablon/html/ast.rb, line 207
def initialize(env, node, properties)
  # intialize values
  @list_tag = node.name
  #
  @definition = nil
  if node.ancestors(".//#{@list_tag}").length.zero?
    # Only register a definition upon the first list tag encountered
    @definition = env.document.add_list_definition(properties['pStyle'])
  end

  # update attributes of all child nodes
  transfer_node_attributes(node.children, node.attributes)

  # Move any list tags that are a child of a list item up one level
  process_child_nodes(node)

  # convert children from HTML to AST nodes
  super(ASTBuilder.html_to_ast(env, node.children, properties))
end

Public Instance Methods

inspect() click to toggle source
# File lib/sablon/html/ast.rb, line 227
def inspect
  "<List: #{super}>"
end

Private Instance Methods

merge_attributes(child, parent_attributes) click to toggle source

merges parent and child attributes together, preappending the parent's values to allow the child node to override it if the value is already defined on the child node.

# File lib/sablon/html/ast.rb, line 251
def merge_attributes(child, parent_attributes)
  parent_attributes.each do |name, par_attr|
    child_attr = child[name] ? child[name].split(';') : []
    child[name] = par_attr.value.split(';').concat(child_attr).join('; ')
  end
end
process_child_nodes(node) click to toggle source

moves any list tags that are a child of a list item tag up one level so they become a sibling instead of a child

# File lib/sablon/html/ast.rb, line 260
def process_child_nodes(node)
  node.xpath("./li/#{@list_tag}").each do |list|
    # transfer attributes from parent now because the list tag will
    # no longer be a child and won't inheirit them as usual
    transfer_node_attributes(list.children, list.parent.attributes)
    list.parent.add_next_sibling(list)
  end
end
transfer_node_attributes(nodes, attributes) click to toggle source

handles passing all attributes on the parent down to children

# File lib/sablon/html/ast.rb, line 234
def transfer_node_attributes(nodes, attributes)
  nodes.each do |child|
    # update all attributes
    merge_attributes(child, attributes)

    # set attributes specific to list items
    if @definition
      child['pStyle'] = @definition.style
      child['numId'] = @definition.numid
    end
    child['ilvl'] = child.ancestors(".//#{@list_tag}").length - 1
  end
end