From de74d0bf9740157a2ce3983fc662135e9bf0c4db Mon Sep 17 00:00:00 2001 From: Martin Angers Date: Thu, 30 Aug 2012 17:35:41 -0400 Subject: [PATCH] reorganize code in what seems like a sane structure --- children_test.go | 35 ------------------------------- doc.go | 4 ++-- add.go => expand.go | 0 add_test.go => expand_test.go | 0 find.go | 28 ------------------------- iterators.go => iteration.go | 0 each_test.go => iteration_test.go | 0 attr.go => property.go | 8 +++++++ attr_test.go => property_test.go | 0 hasclass.go => query.go | 0 hasclass_test.go => query_test.go | 0 selection.go | 27 ------------------------ children.go => traversal.go | 22 +++++++++++++++++++ find_test.go => traversal_test.go | 30 ++++++++++++++++++++++++++ document.go => type.go | 14 +++++++++++++ document_test.go => type_test.go | 0 utilities.go | 2 ++ 17 files changed, 78 insertions(+), 92 deletions(-) delete mode 100644 children_test.go rename add.go => expand.go (100%) rename add_test.go => expand_test.go (100%) delete mode 100644 find.go rename iterators.go => iteration.go (100%) rename each_test.go => iteration_test.go (100%) rename attr.go => property.go (84%) rename attr_test.go => property_test.go (100%) rename hasclass.go => query.go (100%) rename hasclass_test.go => query_test.go (100%) delete mode 100644 selection.go rename children.go => traversal.go (64%) rename find_test.go => traversal_test.go (56%) rename document.go => type.go (65%) rename document_test.go => type_test.go (100%) diff --git a/children_test.go b/children_test.go deleted file mode 100644 index f597b63..0000000 --- a/children_test.go +++ /dev/null @@ -1,35 +0,0 @@ -package goquery - -import ( - "testing" -) - -func TestChildren(t *testing.T) { - sel := doc.Find(".pvk-content").Children() - if len(sel.Nodes) != 13 { - t.Errorf("Expected 13 child nodes, got %v.", len(sel.Nodes)) - for _, n := range sel.Nodes { - t.Logf("%+v", n) - } - } -} - -func TestChildrenFiltered(t *testing.T) { - sel := doc.Find(".pvk-content").ChildrenFiltered(".hero-unit") - if len(sel.Nodes) != 1 { - t.Errorf("Expected 1 child nodes, got %v.", len(sel.Nodes)) - for _, n := range sel.Nodes { - t.Logf("%+v", n) - } - } -} - -func TestChildrenFilteredNone(t *testing.T) { - sel := doc.Find(".pvk-content").ChildrenFiltered("a.btn") - if len(sel.Nodes) != 0 { - t.Errorf("Expected 0 child node, got %v.", len(sel.Nodes)) - for _, n := range sel.Nodes { - t.Logf("%+v", n) - } - } -} diff --git a/doc.go b/doc.go index 32ce86a..ba1eaa9 100644 --- a/doc.go +++ b/doc.go @@ -37,7 +37,7 @@ necessary since multiple return values cannot be used to allow a chainable inter */ package goquery -// array.go : Positional Manipulation: First(), Last(), Eq(), Get(), Index(), Slice() +// DONE array.go : Positional Manipulation: First(), Last(), Eq(), Get(), Index(), Slice() // filter.go : Filtering: Filter(), Not(), Has(), End() // expand.go : "Expanding": Add(), AndSelf() // query.go : Reflect (query) node: Is(), Contains(), HasClass() @@ -68,7 +68,7 @@ package goquery // x Has() - Filtering // x HasClass() - Attributes // - Html() ? - Attributes -// - Index() - DOM Manipulation +// x Index() - DOM Manipulation // - Is() - Filtering // x Last() - Filtering // x Length() / Size() - jQUery property diff --git a/add.go b/expand.go similarity index 100% rename from add.go rename to expand.go diff --git a/add_test.go b/expand_test.go similarity index 100% rename from add_test.go rename to expand_test.go diff --git a/find.go b/find.go deleted file mode 100644 index 84bf0a8..0000000 --- a/find.go +++ /dev/null @@ -1,28 +0,0 @@ -package goquery - -import ( - "code.google.com/p/cascadia" - "exp/html" -) - -// Returns a new Selection object -func (this *Document) Find(selector string) *Selection { - return &Selection{findWithContext(selector, this.Root), this, nil} -} - -// Returns a new Selection object -func (this *Selection) Find(selector string) *Selection { - return &Selection{findWithContext(selector, this.Nodes...), this.document, nil} -} - -// Private internal implementation of the various Find() methods -func findWithContext(selector string, nodes ...*html.Node) []*html.Node { - var matches []*html.Node - - sel := cascadia.MustCompile(selector) - // Match the selector on each node - for _, n := range nodes { - matches = append(matches, sel.MatchAll(n)...) - } - return matches -} diff --git a/iterators.go b/iteration.go similarity index 100% rename from iterators.go rename to iteration.go diff --git a/each_test.go b/iteration_test.go similarity index 100% rename from each_test.go rename to iteration_test.go diff --git a/attr.go b/property.go similarity index 84% rename from attr.go rename to property.go index 8f76fcb..fddd45a 100644 --- a/attr.go +++ b/property.go @@ -13,6 +13,14 @@ func (this *Selection) Attr(attrName string) (val string, exists bool) { return getAttributeValue(attrName, this.Nodes[0]) } +func (this *Selection) Size() int { + return this.Length() +} + +func (this *Selection) Length() int { + return len(this.Nodes) +} + // Private function to get the specified attribute's value from a node. func getAttributeValue(attrName string, n *html.Node) (val string, exists bool) { if n == nil { diff --git a/attr_test.go b/property_test.go similarity index 100% rename from attr_test.go rename to property_test.go diff --git a/hasclass.go b/query.go similarity index 100% rename from hasclass.go rename to query.go diff --git a/hasclass_test.go b/query_test.go similarity index 100% rename from hasclass_test.go rename to query_test.go diff --git a/selection.go b/selection.go deleted file mode 100644 index b750f64..0000000 --- a/selection.go +++ /dev/null @@ -1,27 +0,0 @@ -package goquery - -import ( - "exp/html" -) - -type Selection struct { - Nodes []*html.Node - document *Document - prevSel *Selection -} - -func (this *Selection) Size() int { - return this.Length() -} - -func (this *Selection) Length() int { - return len(this.Nodes) -} - -func newEmptySelection(doc *Document) *Selection { - return &Selection{nil, doc, nil} -} - -func newSingleSelection(node *html.Node, doc *Document) *Selection { - return &Selection{[]*html.Node{node}, doc, nil} -} diff --git a/children.go b/traversal.go similarity index 64% rename from children.go rename to traversal.go index dc4cc22..e4d740a 100644 --- a/children.go +++ b/traversal.go @@ -6,6 +6,28 @@ import ( "strings" ) +// Returns a new Selection object +func (this *Document) Find(selector string) *Selection { + return &Selection{findWithContext(selector, this.Root), this, nil} +} + +// Returns a new Selection object +func (this *Selection) Find(selector string) *Selection { + return &Selection{findWithContext(selector, this.Nodes...), this.document, nil} +} + +// Private internal implementation of the various Find() methods +func findWithContext(selector string, nodes ...*html.Node) []*html.Node { + var matches []*html.Node + + sel := cascadia.MustCompile(selector) + // Match the selector on each node + for _, n := range nodes { + matches = append(matches, sel.MatchAll(n)...) + } + return matches +} + // TODO : Filtered using Node and other Selection object // Returns a new Selection object. diff --git a/find_test.go b/traversal_test.go similarity index 56% rename from find_test.go rename to traversal_test.go index 7342d0b..a2defa2 100644 --- a/find_test.go +++ b/traversal_test.go @@ -43,3 +43,33 @@ func TestChainedFind(t *testing.T) { t.Errorf("Expected 4 matching nodes, found %v.", len(sel.Nodes)) } } + +func TestChildren(t *testing.T) { + sel := doc.Find(".pvk-content").Children() + if len(sel.Nodes) != 13 { + t.Errorf("Expected 13 child nodes, got %v.", len(sel.Nodes)) + for _, n := range sel.Nodes { + t.Logf("%+v", n) + } + } +} + +func TestChildrenFiltered(t *testing.T) { + sel := doc.Find(".pvk-content").ChildrenFiltered(".hero-unit") + if len(sel.Nodes) != 1 { + t.Errorf("Expected 1 child nodes, got %v.", len(sel.Nodes)) + for _, n := range sel.Nodes { + t.Logf("%+v", n) + } + } +} + +func TestChildrenFilteredNone(t *testing.T) { + sel := doc.Find(".pvk-content").ChildrenFiltered("a.btn") + if len(sel.Nodes) != 0 { + t.Errorf("Expected 0 child node, got %v.", len(sel.Nodes)) + for _, n := range sel.Nodes { + t.Logf("%+v", n) + } + } +} diff --git a/document.go b/type.go similarity index 65% rename from document.go rename to type.go index d7e64a0..e073c23 100644 --- a/document.go +++ b/type.go @@ -35,3 +35,17 @@ func NewDocument(url string) (d *Document, e error) { d = &Document{root, res.Request.URL} return } + +type Selection struct { + Nodes []*html.Node + document *Document + prevSel *Selection +} + +func newEmptySelection(doc *Document) *Selection { + return &Selection{nil, doc, nil} +} + +func newSingleSelection(node *html.Node, doc *Document) *Selection { + return &Selection{[]*html.Node{node}, doc, nil} +} diff --git a/document_test.go b/type_test.go similarity index 100% rename from document_test.go rename to type_test.go diff --git a/utilities.go b/utilities.go index 398e53e..61490fc 100644 --- a/utilities.go +++ b/utilities.go @@ -85,6 +85,8 @@ func grep(sel *Selection, predicate func(i int, s *Selection) bool) (result []*h return } +// Creates a new Selection object based on the specified nodes, and keeps the source +// Selection object on the stack (linked list). func pushStack(fromSel *Selection, nodes []*html.Node) (result *Selection) { result = &Selection{nodes, fromSel.document, fromSel} return