From 8bef3ef15b0474cd9509bbed731066dd28b72cf6 Mon Sep 17 00:00:00 2001 From: mdrokz <33598169+mdrokz@users.noreply.github.com> Date: Sat, 14 Aug 2021 14:34:43 +0530 Subject: [PATCH 1/4] added support for -n+A for :nth-child --- selector.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/selector.go b/selector.go index 6249f77..728981b 100644 --- a/selector.go +++ b/selector.go @@ -524,8 +524,18 @@ func parseNthPseudo(cmd string) (PseudoClass, error) { return n.Type == html.ElementNode && countNth(n)%cycle == offset }, nil } + + r = regexp.MustCompile(`^-n[ ]?\+[ ]?([0-9])`) + subMatch = r.FindAllStringSubmatch(number, -1) + if len(subMatch) == 1 && len(subMatch[0]) == 2 { + offset, _ := strconv.Atoi(subMatch[0][1]) + return func(n *html.Node) bool { + return n.Type == html.ElementNode && countNth(n) <= offset + }, nil + } + // check against 'n+2' pattern - r = regexp.MustCompile(`n[ ]?\+[ ]?([0-9])`) + r = regexp.MustCompile(`^n[ ]?\+[ ]?([0-9])`) subMatch = r.FindAllStringSubmatch(number, -1) if len(subMatch) == 1 && len(subMatch[0]) == 2 { offset, _ := strconv.Atoi(subMatch[0][1]) From 0a205591741b0e972163860a16a9d09201cc0fce Mon Sep 17 00:00:00 2001 From: mdrokz <33598169+mdrokz@users.noreply.github.com> Date: Sun, 15 Aug 2021 07:45:44 +0530 Subject: [PATCH 2/4] comments --- selector.go | 1 + 1 file changed, 1 insertion(+) diff --git a/selector.go b/selector.go index 728981b..c8f0b98 100644 --- a/selector.go +++ b/selector.go @@ -525,6 +525,7 @@ func parseNthPseudo(cmd string) (PseudoClass, error) { }, nil } + // check against '-n+2' pattern r = regexp.MustCompile(`^-n[ ]?\+[ ]?([0-9])`) subMatch = r.FindAllStringSubmatch(number, -1) if len(subMatch) == 1 && len(subMatch[0]) == 2 { From 7ffc0578dea1211a762e0b20c315c51acc250ee1 Mon Sep 17 00:00:00 2001 From: mdrokz <33598169+mdrokz@users.noreply.github.com> Date: Sun, 15 Aug 2021 08:39:59 +0530 Subject: [PATCH 3/4] modified regex to allow numbers over 9 :nth-child --- selector.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/selector.go b/selector.go index c8f0b98..cc6e059 100644 --- a/selector.go +++ b/selector.go @@ -510,12 +510,13 @@ func parseNthPseudo(cmd string) (PseudoClass, error) { oddOrEven = 0 } if oddOrEven > -1 { + fmt.Println("bruh it stopped") return func(n *html.Node) bool { return n.Type == html.ElementNode && countNth(n)%2 == oddOrEven }, nil } // Check against '3n+4' pattern - r := regexp.MustCompile(`([0-9]+)n[ ]?\+[ ]?([0-9])`) + r := regexp.MustCompile(`([0-9]+)n[ ]?\+[ ]?([1-9][0-9]{0,2}|1000)$`) subMatch := r.FindAllStringSubmatch(number, -1) if len(subMatch) == 1 && len(subMatch[0]) == 3 { cycle, _ := strconv.Atoi(subMatch[0][1]) @@ -526,7 +527,7 @@ func parseNthPseudo(cmd string) (PseudoClass, error) { } // check against '-n+2' pattern - r = regexp.MustCompile(`^-n[ ]?\+[ ]?([0-9])`) + r = regexp.MustCompile(`^-n[ ]?\+[ ]?([1-9][0-9]{0,2}|1000)$`) subMatch = r.FindAllStringSubmatch(number, -1) if len(subMatch) == 1 && len(subMatch[0]) == 2 { offset, _ := strconv.Atoi(subMatch[0][1]) @@ -536,7 +537,7 @@ func parseNthPseudo(cmd string) (PseudoClass, error) { } // check against 'n+2' pattern - r = regexp.MustCompile(`^n[ ]?\+[ ]?([0-9])`) + r = regexp.MustCompile(`^n[ ]?\+[ ]?([1-9][0-9]{0,2}|1000)$`) subMatch = r.FindAllStringSubmatch(number, -1) if len(subMatch) == 1 && len(subMatch[0]) == 2 { offset, _ := strconv.Atoi(subMatch[0][1]) From f8070675912f82581bdc915edf8aad9fc4a3a518 Mon Sep 17 00:00:00 2001 From: mdrokz <33598169+mdrokz@users.noreply.github.com> Date: Sun, 15 Aug 2021 14:23:26 +0530 Subject: [PATCH 4/4] Support wildcard element selector --- selector.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/selector.go b/selector.go index cc6e059..da802a7 100644 --- a/selector.go +++ b/selector.go @@ -85,7 +85,7 @@ func (s CSSSelector) Match(node *html.Node) bool { if node.Type != html.ElementNode { return false } - if s.Tag != "" { + if s.Tag != "" && s.Tag != "*" { if s.Tag != node.DataAtom.String() { return false }