Parent

Class/Module Index [+]

Quicksearch

HTML5::InBodyPhase

Public Class Methods

new(parser, tree) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 49
def initialize(parser, tree)
  super(parser, tree)

  # for special handling of whitespace in <pre>
  @processSpaceCharactersDropNewline = false
  if $-w
    $-w = false
    alias processSpaceCharactersNonPre processSpaceCharacters
    $-w = true
  else
    alias processSpaceCharactersNonPre processSpaceCharacters
  end
end

Public Instance Methods

endTagBlock(name) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 365
def endTagBlock(name)
  #Put us back in the right whitespace handling mode
  @processSpaceCharactersDropNewline = false if name == 'pre'

  @tree.generateImpliedEndTags if in_scope?(name)

  unless @tree.open_elements.last.name == name
    parse_error("end-tag-too-early", {"name" => name})
  end

  if in_scope?(name)
    remove_open_elements_until(name)
  end
end
endTagBody(name) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 343
def endTagBody(name)
  # XXX Need to take open <p> tags into account here. We shouldn't imply
  # </p> but we should not throw a parse error either. Specification is
  # likely to be updated.
  unless @tree.open_elements[1].name == 'body'
    # inner_html case
    parse_error
    return
  end
  unless @tree.open_elements.last.name == 'body'
    parse_error("expected-one-end-tag-but-got-another",
            {"expectedName" => "body",
             "gotName" => @tree.open_elements.last.name})
  end
  @parser.phase = @parser.phases[:afterBody]
end
endTagBr(name) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 554
def endTagBr(name)
  parse_error("unexpected-end-tag-treated-as",
        {"originalName" => "br", "newName" => "br element"})
  @tree.reconstructActiveFormattingElements
  @tree.insert_element(name, {})
  @tree.open_elements.pop()
end
endTagButtonMarqueeObject(name) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 535
def endTagButtonMarqueeObject(name)
  @tree.generateImpliedEndTags if in_scope?(name)

  unless @tree.open_elements.last.name == name
    parse_error("end-tag-too-early", {"name" => name})
  end

  if in_scope?(name)
    remove_open_elements_until(name)

    @tree.clearActiveFormattingElements
  end
end
endTagCdataTextAreaXmp(name) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 567
def endTagCdataTextAreaXmp(name)
  if @tree.open_elements.last.name == name
    @tree.open_elements.pop
  else
    parse_error("unexpected-end-tag", {"name" => name})
  end
end
endTagForm(name) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 380
def endTagForm(name)
  if in_scope?(name)
    @tree.generateImpliedEndTags
  end
  if @tree.open_elements.last.name != name
    parse_error("end-tag-too-early-ignored", {"name" => "form"})
  else
    @tree.open_elements.pop
  end
  @tree.formPointer = nil
end
endTagFormatting(name) click to toggle source

The much-feared adoption agency algorithm

# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 424
def endTagFormatting(name)
  # http://www.whatwg.org/specs/web-apps/current-work/#adoptionAgency
  # XXX Better parse_error messages appreciated.
  while true
    # Step 1 paragraph 1
    afeElement = @tree.elementInActiveFormattingElements(name)
    if !afeElement or (@tree.open_elements.include?(afeElement) && !in_scope?(afeElement.name))
      parse_error("adoption-agency-1.1", {"name" => name})
      return
    # Step 1 paragraph 2
    elsif not @tree.open_elements.include?(afeElement)
      parse_error("adoption-agency-1.2", {"name" => name})
      @tree.activeFormattingElements.delete(afeElement)
      return
    end

    # Step 1 paragraph 3
    if afeElement != @tree.open_elements.last
      parse_error("adoption-agency-1.3", {"name" => name})
    end

    # Step 2
    # Start of the adoption agency algorithm proper
    afeIndex = @tree.open_elements.index(afeElement)
    furthestBlock = nil
    @tree.open_elements[afeIndex..-1].each do |element|
      if (SPECIAL_ELEMENTS + SCOPING_ELEMENTS).include?(element.name)
        furthestBlock = element
        break
      end
    end

    # Step 3
    if furthestBlock.nil?
      element = remove_open_elements_until {|element| element == afeElement }
      @tree.activeFormattingElements.delete(element)
      return
    end
    commonAncestor = @tree.open_elements[afeIndex - 1]

    # Step 5
    furthestBlock.parent.removeChild(furthestBlock) if furthestBlock.parent

    # Step 6
    # The bookmark is supposed to help us identify where to reinsert
    # nodes in step 12. We have to ensure that we reinsert nodes after
    # the node before the active formatting element. Note the bookmark
    # can move in step 7.4
    bookmark = @tree.activeFormattingElements.index(afeElement)

    # Step 7
    lastNode = node = furthestBlock
    while true
      # AT replace this with a function and recursion?
      # Node is element before node in open elements
      node = @tree.open_elements[@tree.open_elements.index(node) - 1]
      until @tree.activeFormattingElements.include?(node)
        tmpNode = node
        node = @tree.open_elements[@tree.open_elements.index(node) - 1]
        @tree.open_elements.delete(tmpNode)
      end
      # Step 7.3
      break if node == afeElement
      # Step 7.4
      if lastNode == furthestBlock
        # XXX should this be index(node) or index(node)+1
        # Anne: I think +1 is ok. Given x = [2,3,4,5]
        # x.index(3) gives 1 and then x[1 +1] gives 4...
        bookmark = @tree.activeFormattingElements.index(node) + 1
      end
      # Step 7.5
      cite = node.parent
      if node.hasContent
        clone = node.cloneNode
        # Replace node with clone
        @tree.activeFormattingElements[@tree.activeFormattingElements.index(node)] = clone
        @tree.open_elements[@tree.open_elements.index(node)] = clone
        node = clone
      end
      # Step 7.6
      # Remove lastNode from its parents, if any
      lastNode.parent.removeChild(lastNode) if lastNode.parent
      node.appendChild(lastNode)
      # Step 7.7
      lastNode = node
      # End of inner loop
    end

    # Step 8
    lastNode.parent.removeChild(lastNode) if lastNode.parent
    commonAncestor.appendChild(lastNode)

    # Step 9
    clone = afeElement.cloneNode

    # Step 10
    furthestBlock.reparentChildren(clone)

    # Step 11
    furthestBlock.appendChild(clone)

    # Step 12
    @tree.activeFormattingElements.delete(afeElement)
    @tree.activeFormattingElements.insert([bookmark,@tree.activeFormattingElements.length].min, clone)

    # Step 13
    @tree.open_elements.delete(afeElement)
    @tree.open_elements.insert(@tree.open_elements.index(furthestBlock) + 1, clone)
  end
end
endTagHeading(name) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 403
def endTagHeading(name)
  HEADING_ELEMENTS.each do |element|
    if in_scope?(element)
      @tree.generateImpliedEndTags
      break
    end
  end

  unless @tree.open_elements.last.name == name
    parse_error("end-tag-too-early", {"name" => name})
  end

  HEADING_ELEMENTS.each do |element|
    if in_scope?(element)
      remove_open_elements_until {|element| HEADING_ELEMENTS.include?(element.name)}
      break
    end
  end
end
endTagHtml(name) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 360
def endTagHtml(name)
  endTagBody(name)
  @parser.phase.processEndTag(name) unless @parser.inner_html
end
endTagListItem(name) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 392
def endTagListItem(name)
  # AT Could merge this with the Block case
  @tree.generateImpliedEndTags(name) if in_scope?(name)

  unless @tree.open_elements.last.name == name
    parse_error("end-tag-too-early", {"name" => name})
  end

  remove_open_elements_until(name) if in_scope?(name)
end
endTagMisplaced(name) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 549
def endTagMisplaced(name)
  # This handles elements with end tags in other insertion modes.
  parse_error("unexpected-end-tag", {"name" => name})
end
endTagNew(name) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 575
def endTagNew(name)
  # New HTML5 elements, "event-source", "section", "nav",
  # "article", "aside", "header", "footer", "datagrid", "command"
  # STDERR.puts "Warning: Undefined behaviour for end tag #{name}"
  endTagOther(name)
  #raise NotImplementedError
end
endTagNone(name) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 562
def endTagNone(name)
  # This handles elements with no end tag.
  parse_error("no-end-tag", {"name" => name})
end
endTagOther(name) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 583
def endTagOther(name)
  # XXX This logic should be moved into the treebuilder
  @tree.open_elements.reverse.each do |node|
    if node.name == name
      @tree.generateImpliedEndTags

      unless @tree.open_elements.last.name == name
        parse_error("unexpected-end-tag", {"name" => name})
      end

      remove_open_elements_until {|element| element == node }

      break
    else
      if (SPECIAL_ELEMENTS + SCOPING_ELEMENTS).include?(node.name)
        parse_error("unexpected-end-tag", {"name" => name})
        break
      end
    end
  end
end
endTagP(name) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 332
def endTagP(name)
  @tree.generateImpliedEndTags('p') if in_scope?('p')
  parse_error("unexpected-end-tag", {"name" => "p"}) unless @tree.open_elements.last.name == 'p'
  if in_scope?('p')
    @tree.open_elements.pop while in_scope?('p')
  else
    startTagCloseP('p', {})
    endTagP('p')
  end
end
processCharacters(data) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 90
def processCharacters(data)
  # XXX The specification says to do this for every character at the
  # moment, but apparently that doesn't match the real world so we don't
  # do it for space characters.
  @tree.reconstructActiveFormattingElements
  @tree.insertText(data)
end
processSpaceCharacters(data) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 85
def processSpaceCharacters(data)
  @tree.reconstructActiveFormattingElements()
  @tree.insertText(data)
end
processSpaceCharactersDropNewline(data) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 63
def processSpaceCharactersDropNewline(data)
  # #Sometimes (start of <pre> blocks) we want to drop leading newlines

  if $-w
    $-w = false
    alias processSpaceCharacters processSpaceCharactersNonPre
    $-w = true
  else
    alias processSpaceCharacters processSpaceCharactersNonPre
  end

  if (data.length > 0 and data[0] == \n\ && 
    ]pre textarea].include?(@tree.open_elements.last.name) && !@tree.open_elements.last.hasContent)
    data = data[1..-1]
  end

  if data.length > 0
    @tree.reconstructActiveFormattingElements
    @tree.insertText(data)
  end
end
startTagA(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 187
def startTagA(name, attributes)
  if afeAElement = @tree.elementInActiveFormattingElements('a')
    parse_error("unexpected-start-tag-implies-end-tag", {"startName" => "a", "endName" => "a"})
    endTagFormatting('a')
    @tree.open_elements.delete(afeAElement) if @tree.open_elements.include?(afeAElement)
    @tree.activeFormattingElements.delete(afeAElement) if @tree.activeFormattingElements.include?(afeAElement)
  end
  @tree.reconstructActiveFormattingElements
  addFormattingElement(name, attributes)
end
startTagBody(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 107
def startTagBody(name, attributes)
  parse_error("unexpected-start-tag", {"name" => "body"})

  if @tree.open_elements.length == 1 || @tree.open_elements[1].name != 'body'
    assert @parser.inner_html
  else
    attributes.each do |attr, value|
      unless @tree.open_elements[1].attributes.has_key?(attr)
        @tree.open_elements[1].attributes[attr] = value
      end
    end
  end
end
startTagButton(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 214
def startTagButton(name, attributes)
  if in_scope?('button')
    parse_error("unexpected-start-tag-implies-end-tag", {"startName" => "button", "endName" => "button"})
    processEndTag('button')
    @parser.phase.processStartTag(name, attributes)
  else
    @tree.reconstructActiveFormattingElements
    @tree.insert_element(name, attributes)
    @tree.activeFormattingElements.push(Marker)
  end
end
startTagCdata(name, attributes) click to toggle source

iframe, noembed noframes, noscript(if scripting enabled)

# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 299
def startTagCdata(name, attributes)
  @tree.insert_element(name, attributes)
  @parser.tokenizer.content_model_flag = :CDATA
end
startTagCloseP(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 121
def startTagCloseP(name, attributes)
  endTagP('p') if in_scope?('p')
  @tree.insert_element(name, attributes)
  @processSpaceCharactersDropNewline = true if name == 'pre'
end
startTagForm(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 127
def startTagForm(name, attributes)
  if @tree.formPointer
    parse_error("unexpected-start-tag", {"name" => name})
  else
    endTagP('p') if in_scope?('p')
    @tree.insert_element(name, attributes)
    @tree.formPointer = @tree.open_elements.last
  end
end
startTagFormatting(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 198
def startTagFormatting(name, attributes)
  @tree.reconstructActiveFormattingElements
  addFormattingElement(name, attributes)
end
startTagHeading(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 169
def startTagHeading(name, attributes)
  endTagP('p') if in_scope?('p')

  # Uncomment the following for IE7 behavior:
  # HEADING_ELEMENTS.each do |element|
  #   if in_scope?(element)
  #     parse_error("unexpected-start-tag", {"name" => name})
  # 
  #     remove_open_elements_until do |element|
  #       HEADING_ELEMENTS.include?(element.name)
  #     end
  #
  #     break
  #   end
  # end
  @tree.insert_element(name, attributes)
end
startTagHr(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 250
def startTagHr(name, attributes)
  endTagP('p') if in_scope?('p')
  @tree.insert_element(name, attributes)
  @tree.open_elements.pop
end
startTagImage(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 256
def startTagImage(name, attributes)
  # No really...
  parse_error("unexpected-start-tag-treated-as", {"originalName" => "image", "newName" => "img"})
  processStartTag('img', attributes)
end
startTagInput(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 262
def startTagInput(name, attributes)
  @tree.reconstructActiveFormattingElements
  @tree.insert_element(name, attributes)
  if @tree.formPointer
    # XXX Not exactly sure what to do here
    # @tree.open_elements[-1].form = @tree.formPointer
  end
  @tree.open_elements.pop
end
startTagIsindex(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 272
def startTagIsindex(name, attributes)
  parse_error("deprecated-tag", {"name" => "isindex"})
  return if @tree.formPointer
  processStartTag('form', {})
  processStartTag('hr', {})
  processStartTag('p', {})
  processStartTag('label', {})
  # XXX Localization ...
  processCharacters('This is a searchable index. Insert your search keywords here: ')
  attributes['name'] = 'isindex'
  attrs = attributes.to_a
  processStartTag('input', attributes)
  processEndTag('label')
  processEndTag('p')
  processStartTag('hr', {})
  processEndTag('form')
end
startTagListItem(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 137
def startTagListItem(name, attributes)
  endTagP('p') if in_scope?('p')
  stopNames = {'li' => ['li'], 'dd' => ['dd', 'dt'], 'dt' => ['dd', 'dt']}
  stopName = stopNames[name]

  @tree.open_elements.reverse.each_with_index do |node, i|
    if stopName.include?(node.name)
      poppedNodes = (0..i).collect { @tree.open_elements.pop }
      if i >= 1
        parse_error(
            i == 1 ? "missing-end-tag" : "missing-end-tags",
            {"name" => poppedNodes[0..-1].collect{|n| n.name}.join(", ")})

      end
      break
    end

    # Phrasing elements are all non special, non scoping, non
    # formatting elements
    break if ((SPECIAL_ELEMENTS + SCOPING_ELEMENTS).include?(node.name) && !]address div].include?(node.name))
  end

  # Always insert an <li> element.
  @tree.insert_element(name, attributes)
end
startTagMarqueeObject(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 226
def startTagMarqueeObject(name, attributes)
  @tree.reconstructActiveFormattingElements
  @tree.insert_element(name, attributes)
  @tree.activeFormattingElements.push(Marker)
end
startTagMisplaced(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 310
def startTagMisplaced(name, attributes)
  # Elements that should be children of other elements that have a
  # different insertion mode; here they are ignored
  # "caption", "col", "colgroup", "frame", "frameset", "head",
  # "option", "optgroup", "tbody", "td", "tfoot", "th", "thead",
  # "tr", "noscript"
  parse_error("unexpected-start-tag-ignored", {"name" => name})
end
startTagNew(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 319
def startTagNew(name, attributes)
  # New HTML5 elements, "event-source", "section", "nav",
  # "article", "aside", "header", "footer", "datagrid", "command"
  # $stderr.puts("Warning: Undefined behaviour for start tag #{name}")
  startTagOther(name, attributes)
  #raise NotImplementedError
end
startTagNobr(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 203
def startTagNobr(name, attributes)
  @tree.reconstructActiveFormattingElements
  if in_scope?('nobr')
    parse_error("unexpected-start-tag-implies-end-tag", {"startName" => "nobr", "endName" => "nobr"})
    processEndTag('nobr')
    # XXX Need tests that trigger the following
    @tree.reconstructActiveFormattingElements
  end
  addFormattingElement(name, attributes)
end
startTagOther(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 327
def startTagOther(name, attributes)
  @tree.reconstructActiveFormattingElements
  @tree.insert_element(name, attributes)
end
startTagPlaintext(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 163
def startTagPlaintext(name, attributes)
  endTagP('p') if in_scope?('p')
  @tree.insert_element(name, attributes)
  @parser.tokenizer.content_model_flag = :PLAINTEXT
end
startTagProcessInHead(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 98
def startTagProcessInHead(name, attributes)
  @parser.phases[:inHead].processStartTag(name, attributes)
end
startTagSelect(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 304
def startTagSelect(name, attributes)
  @tree.reconstructActiveFormattingElements
  @tree.insert_element(name, attributes)
  @parser.phase = @parser.phases[:inSelect]
end
startTagTable(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 238
def startTagTable(name, attributes)
  processEndTag('p') if in_scope?('p')
  @tree.insert_element(name, attributes)
  @parser.phase = @parser.phases[:inTable]
end
startTagTextarea(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 290
def startTagTextarea(name, attributes)
  # XXX Form element pointer checking here as well...
  @tree.insert_element(name, attributes)
  @parser.tokenizer.content_model_flag = :RCDATA
  @processSpaceCharactersDropNewline = true
  alias processSpaceCharacters processSpaceCharactersDropNewline
end
startTagTitle(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 102
def startTagTitle(name, attributes)
  parse_error("unexpected-start-tag-out-of-my-head", {"name" => name})
  @parser.phases[:inHead].processStartTag(name, attributes)
end
startTagVoidFormatting(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 244
def startTagVoidFormatting(name, attributes)
  @tree.reconstructActiveFormattingElements
  @tree.insert_element(name, attributes)
  @tree.open_elements.pop
end
startTagXmp(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 232
def startTagXmp(name, attributes)
  @tree.reconstructActiveFormattingElements
  @tree.insert_element(name, attributes)
  @parser.tokenizer.content_model_flag = :CDATA
end

Protected Instance Methods

addFormattingElement(name, attributes) click to toggle source
# File lib/feed_tools/vendor/html5/lib/html5/html5parser/in_body_phase.rb, line 607
def addFormattingElement(name, attributes)
  @tree.insert_element(name, attributes)
  @tree.activeFormattingElements.push(@tree.open_elements.last)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.