require "helper" require 'nkf' module Nokogiri module HTML class TestNode < Nokogiri::TestCase def setup super @html = Nokogiri::HTML(<<-eohtml)
first
eohtml end def test_to_a assert_equal [['class', 'bar'], ['href', 'foo']],@html.at('a').to_a.sort end def test_attr node = @html.at('div.baz') assert_equal node['class'], node.attr('class') end def test_get_attribute element = @html.at('div') assert_equal 'baz', element.get_attribute('class') assert_equal 'baz', element['class'] element['href'] = "javascript:alert(\"AGGA-KA-BOO!\")" assert_match(/%22AGGA-KA-BOO!%22/, element.to_html) end def test_css_path_round_trip doc = Nokogiri::HTML(File.read(HTML_FILE)) %w{ #header small div[2] div.post body }.each do |css_sel| ele = doc.at css_sel assert_equal ele, doc.at(ele.css_path), ele.css_path end end def test_path_round_trip doc = Nokogiri::HTML(File.read(HTML_FILE)) %w{ #header small div[2] div.post body }.each do |css_sel| ele = doc.at css_sel assert_equal ele, doc.at(ele.path), ele.path end end def test_append_with_document assert_raises(ArgumentError) do @html.root << Nokogiri::HTML::Document.new end end ### # Make sure a document that doesn't declare a meta encoding returns # nil. def test_meta_encoding assert_nil @html.meta_encoding end def test_description assert desc = @html.at('a.bar').description assert_equal 'a', desc.name end def test_ancestors_with_selector assert node = @html.at('a.bar').child assert list = node.ancestors('.baz') assert_equal 1, list.length assert_equal 'div', list.first.name end def test_matches_inside_fragment fragment = DocumentFragment.new @html fragment << XML::Node.new('a', @html) a = fragment.children.last assert a.matches?('a'), 'a should match' end def test_css_matches? assert node = @html.at('a.bar') assert node.matches?('a.bar') end def test_xpath_matches? assert node = @html.at('//a') assert node.matches?('//a') end def test_unlink_then_swap node = @html.at('a') node.unlink another_node = @html.at('div') assert another_node, 'should have a node' # This used to segv assert_nothing_raised do node.add_previous_sibling another_node end end def test_swap @html.at('div').swap('bar') a_tag = @html.css('a').first assert_equal 'body', a_tag.parent.name assert_equal 0, @html.css('div').length end def test_swap_with_regex_characters @html.at('div').swap('ba)r') a_tag = @html.css('a').first assert_equal 'ba)r', a_tag.text end def test_attribute_decodes_entities node = @html.at('div') node['href'] = 'foo&bar' assert_equal 'foo&bar', node['href'] node['href'] += '&baz' assert_equal 'foo&bar&baz', node['href'] end def test_parse_config_option node = @html.at('div') options = nil node.parse("
") do |config| options = config end assert_equal Nokogiri::XML::ParseOptions::DEFAULT_HTML, options.to_i end def test_fragment_handler_does_not_regurge_on_invalid_attributes iframe = %Q{} assert_nothing_raised { @html.at('div').fragment(iframe) } end def test_fragment fragment = @html.fragment(<<-eohtml) hello

bar

world eohtml assert_match(/^hello/, fragment.inner_html.strip) assert_equal 3, fragment.children.length assert p_tag = fragment.css('p').first assert_equal 'div', p_tag.parent.name assert_equal 'foo', p_tag.parent['class'] end def test_fragment_serialization fragment = Nokogiri::HTML.fragment("
foo
") assert_equal "
foo
", fragment.serialize.chomp assert_equal "
foo
", fragment.to_xml.chomp assert_equal "
foo
", fragment.inner_html assert_equal "
foo
", fragment.to_html assert_equal "
foo
", fragment.to_s end def test_to_html_does_not_contain_entities return unless defined?(NKF) # NKF is not implemented on Rubinius as of 2009-11-23 html = NKF.nkf("-e --msdos", <<-EOH)

test paragraph foo bar

EOH nokogiri = Nokogiri::HTML.parse(html) if RUBY_PLATFORM =~ /java/ # NKF linebreak modes are not supported as of jruby 1.2 # see http://jira.codehaus.org/browse/JRUBY-3602 for status assert_equal "

testparagraph\nfoobar

", nokogiri.at("p").to_html.gsub(/ /, '') else assert_equal "

testparagraph\r\nfoobar

", nokogiri.at("p").to_html.gsub(/ /, '') end end end end end