reorganize code in what seems like a sane structure

This commit is contained in:
Martin Angers
2012-08-30 17:35:41 -04:00
parent 85ad689f6a
commit de74d0bf97
17 changed files with 78 additions and 92 deletions
-35
View File
@@ -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)
}
}
}
+2 -2
View File
@@ -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
View File
View File
-28
View File
@@ -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
}
View File
View File
+8
View File
@@ -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 {
View File
View File
View File
-27
View File
@@ -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}
}
+22
View File
@@ -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.
+30
View File
@@ -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)
}
}
}
+14
View File
@@ -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}
}
View File
+2
View File
@@ -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