require("luarocks.loader") -- Omit next line in actual module clients; it's only to support development of the module itself package.path = "../src/?.lua;" .. package.path local lunitx = require("lunitx") module("html", lunitx.testcase, package.seeall) local htmlparser = require("htmlparser") function test_void() local tree = htmlparser.parse([[









]]) assert_equal(5, #tree.nodes, "top level") for _,n in ipairs(tree.nodes) do if n.name == "p" then assert_equal(4, #n.nodes, "deeper level") else assert_equal("br", n.name, "name") assert_equal("", n:getcontent(), "content") end end end function test_attr() local tree = htmlparser.parse([[ ]]) assert_equal(1, #tree.nodes, "top level") local n = tree.nodes[1] assert(tree("[a1]")[n], "a1") assert(tree("[a2]")[n], "a2") assert(tree("[a3]")[n], "a3") assert(tree("[a4]")[n], "a4") assert(tree("[a5]")[n], "a5") assert(tree("[a6]")[n], "a6") assert(tree("[a7]")[n], "a7") assert(tree("[a8]")[n], "a8") assert(tree("[a9]")[n], "a9") assert(tree("[a10]")[n], "a10") end function test_attr_equal() local tree = htmlparser.parse([[ ]]) assert_equal(1, #tree.nodes, "top level") local n = tree.nodes[1] assert(tree("[a1='']")[n], "a1=''") assert(tree("[a2='']")[n], "a2=''") assert(tree("[a3='']")[n], "a3=''") assert(tree("[a4='']")[n], "a4=''") assert(tree("[a5='a\"5\"']")[n], "a5='a\"5\"'") assert(tree("[a6=\"a'6'\"]")[n], "a6=\"a'6'\"") -- not these characters -- (because these have a special meaning as id, class, or attribute selector, hierarchy separator, or filter command) -- they can occur in the HTML, but not in a selector string -- assert(tree("[a7='#.[] :()']")[n], "a7='#.[] :()'") assert(tree("[a8='|*+-=?$^%&/']")[n], "a8='|*+-=?$^%&/'") assert(tree("[a9='a9']")[n], "a9='a9'") assert(tree("[a10='']")[n], "a10=''") assert(tree("[a10=]")[n], "a10=") end function test_attr_notequal() local tree = htmlparser.parse([[ ]]) assert_equal(4, #tree.nodes, "top level") assert_equal(3, tree("[a1!='a1']"):len(), "a1!='a1'") assert_equal(4, tree("[a1!='b1']"):len(), "a1!='b1'") assert_equal(3, tree("[a1!='']"):len(), "a1!=''") assert_equal(3, tree("[a1!=]"):len(), "a1!=") end function test_attr_prefix_start_end() local tree = htmlparser.parse([[ ]]) assert_equal(5, #tree.nodes, "top level") assert_equal(3, tree("[a1|='en']"):len(), "a1|='en'") assert_equal(4, tree("[a1^='en']"):len(), "a1^='en'") assert_equal(2, tree("[a1$='en']"):len(), "a1$='en'") end function test_attr_word() local tree = htmlparser.parse([[ ]]) assert_equal(4, #tree.nodes, "top level") assert_equal(1, tree("[a1~='two']"):len(), "a1~='two'") assert_equal(2, tree("[a1~='three']"):len(), "a1~='three'") assert_equal(1, tree("[a1~='four']"):len(), "a1~='four'") end function test_attr_contains() local tree = htmlparser.parse([[ ]]) assert_equal(6, #tree.nodes, "top level") assert_equal(2, tree("[a1*='one']"):len(), "a1*='one'") assert_equal(2, tree("[a1*='t']"):len(), "a1*='t'") assert_equal(1, tree("[a1*='f']"):len(), "a1*='f'") assert_equal(5, tree("[a1*='']"):len(), "a1*=''") assert_equal(5, tree("[a1*=]"):len(), "a1*=") end function test_descendants() local tree = htmlparser.parse([[ 1 1 2 3 4 2 5 6 7 8 not ]]) assert_equal(8, tree("parent child"):len(), 'parent child') end function test_children() local tree = htmlparser.parse([[ 1 1 2 not not 2 3 4 not not not ]]) assert_equal(4, tree("parent > child"):len(), 'parent > child') end