class TestHTreeTemplate

Public Instance Methods

ok(e, s) click to toggle source
# File vendor/qwik/lib/qwik/htree-template.rb, line 127
def ok(e, s)
  ok_eq(e, s.format_xml)
end
test_all() click to toggle source
# File vendor/qwik/lib/qwik/htree-template.rb, line 131
def test_all
  g = HTree::Generator.new

  # test_symbol_to_str
  HTree::Elem.instance_eval {
    public :symbol_to_str
  }
  ok_eq({'t'=>'s'}, g.a.symbol_to_str({:t => 's'}))

  # test_apply
  org = HTree("<p><div id='a'/><div id='b'/></p>")
  data = {}
  data[:a] = 'a'
  data[:b] = g.b
  h = org.apply(data)
  ok("<p><div id=\"a\">a</div><div id=\"b\"><b/></div></p>", h)

  data = {}
  data[:a] = nil
  data[:b] = HTree::Attr.new(:action => 'd.html')
  h = org.apply(data)
  ok("<p><div action=\"d.html\" id=\"b\"></div></p>", h)

  data = {}
  data[:a] = [['a', g.hr]] # OK.
  data[:b] = nil
  h = org.apply(data)
  ok("<p><div id=\"a\">a<hr/></div></p>", h)

  # test_each_tag
  org = HTree("<a><b><c/><d/><c/></b></a>")
  ok("<a><b><c/><d/><c/></b></a>", org)
  xml = org.each_tag('c'){|e| nil }
  ok("<a><b><d/></b></a>", xml)
  xml = org.each_tag('c'){|e| e }
  ok("<a><b><c/><d/><c/></b></a>", xml)
  xml = org.each_tag('c'){|e| HTree::Elem.new('cc') }
  ok("<a><b><cc/><d/><cc/></b></a>", xml)
  xml = org.each_tag('c'){|e| g.dd }
  ok("<a><b><dd/><d/><dd/></b></a>", xml)
  xml = org.each_tag('c'){|e| e.clone_with('test1') }
  ok("<a><b><c>test1</c><d/><c>test1</c></b></a>", xml)

  # test_set_attr
  e = g.a(:href => 't.html'){'t'}
  ok("<a href=\"t.html\">t</a>", e)
  e = e.clone_with('href' => 's.html')
  ok("<a href=\"s.html\">t</a>", e)
  e = e.clone_with('s')
  ok("<a href=\"s.html\">ts</a>", e)

  # test_textarea
  org = HTree("<textarea></textarea>")
  htree = org.replace('textarea'){|e|
    e.clone_with('text')
  }
  ok("<textarea>text</textarea>", htree)
  org = HTree("<textarea id='contents'></textarea>")
  data = {}
  data[:contents] = 'text'
  htree = org.apply(data)
  ok("<textarea id=\"contents\">text</textarea>", htree)

  # test_html_tags
  h = HTree("<a>")
  ok_eq(['a', "{http://www.w3.org/1999/xhtml}a"], h.html_tags('a'))
  ok_eq(['a', "{http://www.w3.org/1999/xhtml}a", 'b', "{http://www.w3.org/1999/xhtml}b"], h.html_tags('a', 'b'))

  # test_clone
  h = HTree("<a href='1.html'>t</a>")
  e = h.children[0]
  xml = e.clone_with('test1')
  ok("<a href=\"1.html\">ttest1</a>", xml)
  xml = e.clone_with('href'=>'n.html')
  ok("<a href=\"n.html\">t</a>", xml)
end
test_replace() click to toggle source
# File vendor/qwik/lib/qwik/htree-template.rb, line 208
def test_replace
  g = HTree::Generator.new

  h = HTree("<a><b/><c/></a>")

  h2 = h.replace {|e| e.name == 'c' ? 'text' : e } # insert a text
  ok("<a><b/>text</a>", h2)

  h2 = h.replace('nosuch') {|e| nil } # no effect
  ok("<a><b/><c/></a>", h2)

  h2 = h.replace('b') {|e| e } # no effect
  ok("<a><b/><c/></a>", h2)

  h2 = h.replace('b') {|e| nil } # delete it
  ok("<a><c/></a>", h2)

  h2 = h.replace('b') {|e| 'text' } # insert a text
  ok("<a>text<c/></a>", h2)

  h2 = h.replace('b') {|e| g.d } # insert a element
  ok("<a><d/><c/></a>", h2)

  h2 = h.replace('b') {|e| g.d{'text'} } # insert a element with text
  ok("<a><d>text</d><c/></a>", h2)

  h = HTree("<p><span id='a'/><span id='b'/></p>")

  h2 = h.replace('span'){|e| e.get_attr('id') } # insert the id as text
  ok("<p>ab</p>", h2)

  h2 = h.replace('span'){|e| e.get_attr('id') == 'b' ? e : nil }
  ok('<p><span id='b'/></p>', h2)

  h = HTree("<h2/><h3/><h4/><h5/><h6/>")

  h2 = h.replace('h3', 'h4') {|e| g.make(e.name){ e.name } } # insert a text
  ok("<h2/><h3>h3</h3><h4>h4</h4><h5/><h6/>", h2)

  h2 = h.replace {|e| e.name == 'h5' ? 'text' : e } # insert a text
  ok("<h2/><h3/><h4/>text<h6/>", h2)

  h2 = h.replace('h4') {|e| [g.h3{'h'}, g.hr] } #
  ok("<h2/><h3/><h3>h</h3><hr/><h5/><h6/>", h2)

  h = HTree("<span></span>")
  h2 = h.each_tag('span'){|e| [g.h3{'h'}, g.hr] }
  ok("<h3>h</h3><hr/>", h2)

  h2 = h.each_tag('span'){|e|
    [g.h3{'h3'}, g.ul{g.li{g.a('href'=>'1.html'){'1'}}}]
  }
  ok("<h3>h3</h3><ul><li><a href=\"1.html\">1</a></li></ul>", h2)
end