From ada97317192f99d3f37187e660cd5c6445ebcd05 Mon Sep 17 00:00:00 2001 From: Wouter Scherphof Date: Tue, 2 Apr 2013 12:07:49 +0200 Subject: [PATCH 1/3] Added lunitx dependency and failing test for issue #17 --- htmlparser-0.2-1.rockspec | 27 +++++++++++++++++++++++++++ tst/init.lua | 30 ++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 htmlparser-0.2-1.rockspec create mode 100644 tst/init.lua diff --git a/htmlparser-0.2-1.rockspec b/htmlparser-0.2-1.rockspec new file mode 100644 index 0000000..91cc9d9 --- /dev/null +++ b/htmlparser-0.2-1.rockspec @@ -0,0 +1,27 @@ +package = "htmlparser" +version = "0.1-1" +source = { + url = "git://github.com/wscherphof/lua-htmlparser.git", + branch = "v0.1" +} +description = { + summary = "Parse HTML text into a tree of elements with selectors", + detailed = [[ + Call parse() to build up a tree of element nodes. Each node in the tree, including the root node that is returned by parse(), supports a basic set of jQuery-like selectors. Or you could walk the tree by hand. + ]], + homepage = "http://wscherphof.github.com/lua-htmlparser/", + license = "MIT" +} +dependencies = { + "lua >= 5.2", + "set >= 0.1", + "lunitx >= 0.6" +} +build = { + type = "builtin", + modules = { + htmlparser = "src/htmlparser.lua", + ["htmlparser.ElementNode"] = "src/htmlparser/ElementNode.lua", + ["htmlparser.voidelements"] = "src/htmlparser/voidelements.lua" + } +} \ No newline at end of file diff --git a/tst/init.lua b/tst/init.lua new file mode 100644 index 0000000..fe62536 --- /dev/null +++ b/tst/init.lua @@ -0,0 +1,30 @@ +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_children() + local tree, sel + tree = htmlparser.parse([[ + 1 + 1.1 + 1.2 + 1.2.1 + + + 2 + 2.1 + 2.2 + 2.2.1 + + + ]]) + sel = tree("parent child") + assert_equal(6, sel:len(), 'parent child') + sel = tree("parent > child") + assert_equal(4, sel:len(), 'parent > child') +end \ No newline at end of file From fc12dc87e0330936fa80189349afb780432679f0 Mon Sep 17 00:00:00 2001 From: Wouter Scherphof Date: Tue, 2 Apr 2013 12:10:41 +0200 Subject: [PATCH 2/3] Made test succeed --- src/htmlparser/ElementNode.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/htmlparser/ElementNode.lua b/src/htmlparser/ElementNode.lua index 4d8fc3c..d6c99e8 100644 --- a/src/htmlparser/ElementNode.lua +++ b/src/htmlparser/ElementNode.lua @@ -141,9 +141,10 @@ local function select(self, s) resultset = Set:new() for subject in pairs(subjects) do local star = subject.deepernodes - if childrenonly then star = Set:new(subject.nodes) childrenonly = false end + if childrenonly then star = Set:new(subject.nodes) end resultset = resultset + star end + childrenonly = false if part == "*" then goto nextpart end local excludes, filter = Set:new() for t, w in string.gmatch(part, From f97cde994e98c290b5086d1976ee27696a65b753 Mon Sep 17 00:00:00 2001 From: Wouter Scherphof Date: Tue, 2 Apr 2013 12:13:23 +0200 Subject: [PATCH 3/3] Separated tests for descendants and children. Fixes #17 --- tst/init.lua | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/tst/init.lua b/tst/init.lua index fe62536..fc80b37 100644 --- a/tst/init.lua +++ b/tst/init.lua @@ -6,9 +6,9 @@ local lunitx = require("lunitx") module("html", lunitx.testcase, package.seeall) local htmlparser = require("htmlparser") +local tree, sel -function test_children() - local tree, sel +function test_descendants() tree = htmlparser.parse([[ 1 1.1 @@ -25,6 +25,23 @@ function test_children() ]]) sel = tree("parent child") assert_equal(6, sel:len(), 'parent child') +end + +function test_children() + tree = htmlparser.parse([[ + 1 + 1.1 + 1.2 + 1.2.1 + + + 2 + 2.1 + 2.2 + 2.2.1 + + + ]]) sel = tree("parent > child") assert_equal(4, sel:len(), 'parent > child') end \ No newline at end of file