Class: JsDuck::HtmlStack

Inherits:
Object
  • Object
show all
Defined in:
lib/jsduck/html_stack.rb

Overview

Tracks opening and closing of HTML tags, with the purpose of closing down the unfinished tags.

Instance Method Summary collapse

Constructor Details

#initialize(ignore_html = {}, doc_context = {}) ⇒ HtmlStack

Initializes the stack with two optional parameters:

Parameters:

  • ignore_html (defaults to: {})

    A hash of additional HTML tags that don’t need closing.

  • doc_context (defaults to: {})

    Filename and linenr of the current doc-comment.



13
14
15
16
17
# File 'lib/jsduck/html_stack.rb', line 13

def initialize(ignore_html={}, doc_context={})
  @ignore_html = ignore_html
  @doc_context = doc_context
  @open_tags = []
end

Instance Method Details

#close(s) ⇒ Object

Scans a closing tag in HTML using the passed in StringScanner.



25
26
27
28
29
30
31
# File 'lib/jsduck/html_stack.rb', line 25

def close(s)
  s.scan(/<\//)
  tag = s.scan(/\w+/)
  s.scan(/>/)

  pop_tags(tag).map {|t| "</#{t}>" }.join
end

#open(s) ⇒ Object

Scans an opening tag in HTML using the passed in StringScanner.



20
21
22
# File 'lib/jsduck/html_stack.rb', line 20

def open(s)
  s.scan(/</) + push_tag(s.scan(/\w+/)) + s.scan_until(/>|\Z/)
end

#open?(tag) ⇒ Boolean

True when the tag is currently open.

Returns:

  • (Boolean)


40
41
42
# File 'lib/jsduck/html_stack.rb', line 40

def open?(tag)
  @open_tags.include?(tag)
end

#push_tag(tag) ⇒ Object

Registers opening of a tag. Returns the tag.



34
35
36
37
# File 'lib/jsduck/html_stack.rb', line 34

def push_tag(tag)
  @open_tags.push(tag) unless void?(tag)
  tag
end