mirror of
https://github.com/PuerkitoBio/goquery.git
synced 2024-04-21 12:31:36 +00:00
simplify string search expressions, remove unused code
This commit is contained in:
+3
-3
@@ -112,7 +112,7 @@ func (s *Selection) AddClass(class ...string) *Selection {
|
||||
for _, n := range s.Nodes {
|
||||
curClasses, attr := getClassesAndAttr(n, true)
|
||||
for _, newClass := range tcls {
|
||||
if strings.Index(curClasses, " "+newClass+" ") == -1 {
|
||||
if !strings.Contains(curClasses, " "+newClass+" ") {
|
||||
curClasses += newClass + " "
|
||||
}
|
||||
}
|
||||
@@ -129,7 +129,7 @@ func (s *Selection) HasClass(class string) bool {
|
||||
class = " " + class + " "
|
||||
for _, n := range s.Nodes {
|
||||
classes, _ := getClassesAndAttr(n, false)
|
||||
if strings.Index(classes, class) > -1 {
|
||||
if strings.Contains(classes, class) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -179,7 +179,7 @@ func (s *Selection) ToggleClass(class ...string) *Selection {
|
||||
for _, n := range s.Nodes {
|
||||
classes, attr := getClassesAndAttr(n, true)
|
||||
for _, tcl := range tcls {
|
||||
if strings.Index(classes, " "+tcl+" ") != -1 {
|
||||
if strings.Contains(classes, " "+tcl+" ") {
|
||||
classes = strings.Replace(classes, " "+tcl+" ", " ", -1)
|
||||
} else {
|
||||
classes += tcl + " "
|
||||
|
||||
+6
-22
@@ -23,45 +23,42 @@ func Doc() *Document {
|
||||
}
|
||||
return doc
|
||||
}
|
||||
func DocClone() *Document {
|
||||
return CloneDocument(Doc())
|
||||
}
|
||||
|
||||
func Doc2() *Document {
|
||||
if doc2 == nil {
|
||||
doc2 = loadDoc("page2.html")
|
||||
}
|
||||
return doc2
|
||||
}
|
||||
|
||||
func Doc2Clone() *Document {
|
||||
return CloneDocument(Doc2())
|
||||
}
|
||||
|
||||
func Doc3() *Document {
|
||||
if doc3 == nil {
|
||||
doc3 = loadDoc("page3.html")
|
||||
}
|
||||
return doc3
|
||||
}
|
||||
|
||||
func Doc3Clone() *Document {
|
||||
return CloneDocument(Doc3())
|
||||
}
|
||||
|
||||
func DocB() *Document {
|
||||
if docB == nil {
|
||||
docB = loadDoc("gotesting.html")
|
||||
}
|
||||
return docB
|
||||
}
|
||||
func DocBClone() *Document {
|
||||
return CloneDocument(DocB())
|
||||
}
|
||||
|
||||
func DocW() *Document {
|
||||
if docW == nil {
|
||||
docW = loadDoc("gowiki.html")
|
||||
}
|
||||
return docW
|
||||
}
|
||||
func DocWClone() *Document {
|
||||
return CloneDocument(DocW())
|
||||
}
|
||||
|
||||
func assertLength(t *testing.T, nodes []*html.Node, length int) {
|
||||
if len(nodes) != length {
|
||||
@@ -108,19 +105,6 @@ func printSel(t *testing.T, sel *Selection) {
|
||||
}
|
||||
}
|
||||
|
||||
func shortPrintSel(t *testing.T, sel *Selection) {
|
||||
sel.Each(func(i int, s *Selection) {
|
||||
h, _ := OuterHtml(s)
|
||||
if ix := strings.Index(h, "\n"); ix >= 0 {
|
||||
h = h[:ix]
|
||||
}
|
||||
if len(h) > 100 {
|
||||
h = h[:100]
|
||||
}
|
||||
fmt.Println(i, ">>> ", h)
|
||||
})
|
||||
}
|
||||
|
||||
func loadDoc(page string) *Document {
|
||||
var f *os.File
|
||||
var e error
|
||||
|
||||
@@ -62,13 +62,6 @@ func OuterHtml(s *Selection) (string, error) {
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
||||
func getChildren(n *html.Node) (result []*html.Node) {
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
result = append(result, c)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Loop through all container nodes to search for the target node.
|
||||
func sliceContains(container []*html.Node, contained *html.Node) bool {
|
||||
for _, n := range container {
|
||||
|
||||
Reference in New Issue
Block a user