From 2db07845a95774b5d83e80acf3d5d5a6724198ee Mon Sep 17 00:00:00 2001 From: Martin Angers Date: Thu, 30 Aug 2012 14:36:36 -0400 Subject: [PATCH] rework some internals, similar to jQuery --- children.go | 4 ++-- doc.go | 38 ++++++++++++++++++++++++++++++++------ each.go | 19 ------------------- filter.go | 33 +++++++++++++++++++++++---------- find.go | 4 ++-- iterators.go | 10 ++++++++++ selection.go | 4 ++-- utilities.go | 15 +++++++++++++++ 8 files changed, 86 insertions(+), 41 deletions(-) delete mode 100644 each.go diff --git a/children.go b/children.go index 17798d0..64e1cad 100644 --- a/children.go +++ b/children.go @@ -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 { diff --git a/doc.go b/doc.go index 284aa84..22fbc9e 100644 --- a/doc.go +++ b/doc.go @@ -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 diff --git a/each.go b/each.go deleted file mode 100644 index 47b037d..0000000 --- a/each.go +++ /dev/null @@ -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 -} diff --git a/filter.go b/filter.go index 7fe5108..48a658b 100644 --- a/filter.go +++ b/filter.go @@ -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 { diff --git a/find.go b/find.go index 4dca32b..eb241e0 100644 --- a/find.go +++ b/find.go @@ -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 diff --git a/iterators.go b/iterators.go index 1a40ca8..47b037d 100644 --- a/iterators.go +++ b/iterators.go @@ -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 +} diff --git a/selection.go b/selection.go index bb17658..b750f64 100644 --- a/selection.go +++ b/selection.go @@ -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} } diff --git a/utilities.go b/utilities.go index 82dec43..3334aba 100644 --- a/utilities.go +++ b/utilities.go @@ -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 +}