rework some internals, similar to jQuery

This commit is contained in:
Martin Angers
2012-08-30 14:36:36 -04:00
parent c3f897ba31
commit 2db07845a9
8 changed files with 86 additions and 41 deletions
+2 -2
View File
@@ -20,12 +20,12 @@ func (this *Selection) Children() *Selection {
// Returns a new Selection object.
func (this *Document) ChildrenFiltered(selector string) *Selection {
return &Selection{childrenWithContext(selector, this.Root), this}
return &Selection{childrenWithContext(selector, this.Root), this, nil}
}
// Returns a new Selection object.
func (this *Selection) ChildrenFiltered(selector string) *Selection {
return &Selection{childrenWithContext(selector, this.Nodes...), this.document}
return &Selection{childrenWithContext(selector, this.Nodes...), this.document, nil}
}
func childrenWithContext(selector string, nodes ...*html.Node) []*html.Node {
+32 -6
View File
@@ -1,10 +1,25 @@
// Copyright (c) 2012, Martin Angers & Contributors
// All rights reserved.
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
// * Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation and/or
// other materials provided with the distribution.
// * Neither the name of the author nor the names of its contributors may be used to
// endorse or promote products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
// WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/*
Package goquery implements features similar to jQuery, including the chainable syntax,
@@ -15,12 +30,23 @@ as "exp/html". See this tutorial on how to install it accordingly: http://code.g
It uses Cascadia as CSS selector (similar to Sizzle for jQuery). This dependency is automatically installed
when using "go get ..." to install GoQuery.
To provide chainable interface, error management is strict, and goquery panics if an invalid Cascadia selector
is used (basically the same behavior as jQuery/Sizzle/document.querySelectorAll, an error is thrown). This is
necessary since multiple return values cannot be used to allow a chainable interface.
*/
package goquery
// Positional Filtering: First(), Last(), Eq()
// Positional Filtering: First(), Last(), Eq(), Get(), Index()
// "Expanding": Add(), AndSelf()
// Reflect (query) node: Is(), Contains(), HasClass()
// Inspect node: Contents(), Html(), Text(), Attr()
// Selection "properties": Length(), Size()
// TODO : Benchmarks
// TODO : Check each method, if it applies to any node or only Element nodes (Cascadia's selectors already make sure of that)
// TODO : Add the following methods:
// x Add() - Misc. Traversing
// - AndSelf() - Misc. Traversing
-19
View File
@@ -1,19 +0,0 @@
package goquery
// Returns this (same Selection object)
func (this *Selection) Each(f func(int, *Selection)) *Selection {
for i, n := range this.Nodes {
f(i, newSingleSelection(n, this.document))
}
return this
}
// Map() passes each element in the current matched set through a function,
// producing a slice of string holding the returned values.
func (this *Selection) Map(f func(int, *Selection) string) (result []string) {
for i, n := range this.Nodes {
result = append(result, f(i, newSingleSelection(n, this.document)))
}
return result
}
+23 -10
View File
@@ -12,19 +12,15 @@ func (this *Selection) Filter(selector string) *Selection {
return newEmptySelection(this.document)
}
return &Selection{sel.Filter(this.Nodes), this.document}
return &Selection{sel.Filter(this.Nodes), this.document, nil}
}
func (this *Selection) FilterFunction(f func(int, *Selection) bool) *Selection {
var matches []*html.Node
return &Selection{winnowFunction(this, f, true), this.document, nil}
}
// Check for a match for each current selection
for i, n := range this.Nodes {
if f(i, newSingleSelection(n, this.document)) {
matches = append(matches, n)
}
}
return &Selection{matches, this.document}
func (this *Selection) NotFunction(f func(int, *Selection) bool) *Selection {
return &Selection{winnowFunction(this, f, false), this.document, nil}
}
func (this *Selection) FilterNode(node *html.Node) *Selection {
@@ -54,7 +50,24 @@ func (this *Selection) FilterSelection(s *Selection) *Selection {
}
}
}
return &Selection{matches, this.document}
return &Selection{matches, this.document, nil}
}
func winnow(sel *Selection, selector string) []*html.Node {
cs, e := cascadia.Compile(selector)
if e != nil {
// Selector doesn't compile, which means empty selection
return nil
}
return cs.Filter(sel.Nodes)
}
// Identical functionality for FilterFunction() and NotFunction(), only keep changes.
func winnowFunction(sel *Selection, f func(int, *Selection) bool, keep bool) []*html.Node {
return grep(sel, func(i int, s *Selection) bool {
return f(i, s) == keep
})
}
func (this *Selection) Has(selector string) *Selection {
+2 -2
View File
@@ -7,12 +7,12 @@ import (
// Returns a new Selection object
func (this *Document) Find(selector string) *Selection {
return &Selection{findWithContext(selector, this.Root), this}
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}
return &Selection{findWithContext(selector, this.Nodes...), this.document, nil}
}
// Private internal implementation of the various Find() methods
+10
View File
@@ -7,3 +7,13 @@ func (this *Selection) Each(f func(int, *Selection)) *Selection {
}
return this
}
// Map() passes each element in the current matched set through a function,
// producing a slice of string holding the returned values.
func (this *Selection) Map(f func(int, *Selection) string) (result []string) {
for i, n := range this.Nodes {
result = append(result, f(i, newSingleSelection(n, this.document)))
}
return result
}
+2 -2
View File
@@ -19,9 +19,9 @@ func (this *Selection) Length() int {
}
func newEmptySelection(doc *Document) *Selection {
return &Selection{nil, doc}
return &Selection{nil, doc, nil}
}
func newSingleSelection(node *html.Node, doc *Document) *Selection {
return &Selection{[]*html.Node{node}, doc}
return &Selection{[]*html.Node{node}, doc, nil}
}
+15
View File
@@ -67,3 +67,18 @@ func appendWithoutDuplicates(target []*html.Node, nodes []*html.Node) []*html.No
return target
}
// Loop through a selection, returning only those nodes that pass the predicate function.
func grep(sel *Selection, predicate func(i int, s *Selection) bool) (result []*html.Node) {
for i, n := range sel.Nodes {
if predicate(i, newSingleSelection(n, sel.document)) {
result = append(result, n)
}
}
return
}
func pushStack(fromSel *Selection, nodes []*html.Node) (result *Selection) {
result = &Selection{nodes, fromSel.document, fromSel}
return
}