Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for specificity for simple selectors
- Loading branch information
1 parent
522016b
commit b69f6c9
Showing
4 changed files
with
218 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package cascadia | ||
|
||
// Specificity is the CSS specificity as defined in | ||
// https://www.w3.org/TR/selectors/#specificity-rules | ||
// with the convention Specificity = [A,B,C]. | ||
type Specificity [3]int | ||
|
||
// returns `true` if s < other (strictly), false otherwise | ||
func (s Specificity) Less(other Specificity) bool { | ||
for i := range s { | ||
if s[i] < other[i] { | ||
return true | ||
} | ||
if s[i] > other[i] { | ||
return false | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (s Specificity) Add(other Specificity) Specificity { | ||
for i, sp := range other { | ||
s[i] += sp | ||
} | ||
return s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package cascadia | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"golang.org/x/net/html" | ||
) | ||
|
||
type testSpec struct { | ||
// html, css selector | ||
HTML, selector string | ||
// correct specificity | ||
spec Specificity | ||
} | ||
|
||
var testsSpecificity = []testSpec{ | ||
{ | ||
HTML: `<html><body><div><div><a href="http://www.foo.com"></a></div></div></body></html>`, | ||
selector: ":not(em, strong#foo)", | ||
spec: Specificity{1, 0, 1}, | ||
}, | ||
{ | ||
HTML: `<html><body><div><div><a href="http://www.foo.com"></a></div></div></body></html>`, | ||
selector: "*", | ||
spec: Specificity{0, 0, 0}, | ||
}, | ||
{ | ||
HTML: `<html><body><div><div><ul></ul></div></div></body></html>`, | ||
selector: "ul", | ||
spec: Specificity{0, 0, 1}, | ||
}, | ||
{ | ||
HTML: `<html><body><div><ul><li></li></ul></div></body></html>`, | ||
selector: "ul li", | ||
spec: Specificity{0, 0, 2}, | ||
}, | ||
{ | ||
HTML: `<html><body><div><ul><ol></ol><li></li></ul></div></body></html>`, | ||
selector: "ul ol+li", | ||
spec: Specificity{0, 0, 3}, | ||
}, | ||
{ | ||
HTML: `<html><body><div><ul><h1></h1><li rel="up"></li></ul></div></body></html>`, | ||
selector: "H1 + *[REL=up] ", | ||
spec: Specificity{0, 1, 1}, | ||
}, | ||
{ | ||
HTML: `<html><body><ul><ol><li class="red"></li></ol></ul></body></html>`, | ||
selector: "UL OL LI.red", | ||
spec: Specificity{0, 1, 3}, | ||
}, | ||
{ | ||
HTML: `<html><body><ul><ol><li class="red level"></li></ol></ul></body></html>`, | ||
selector: "LI.red.level", | ||
spec: Specificity{0, 2, 1}, | ||
}, | ||
{ | ||
HTML: `<html><body><ul><ol><li id="x34y"></li></ol></ul></body></html>`, | ||
selector: "#x34y", | ||
spec: Specificity{1, 0, 0}, | ||
}, | ||
{ | ||
HTML: `<html><body><ul><ol><li id="s12"></li></ol></ul></body></html>`, | ||
selector: "#s12:not(FOO)", | ||
spec: Specificity{1, 0, 1}, | ||
}, | ||
{ | ||
HTML: `<html><body><ul><ol><li id="s12"></li></ol></ul></body></html>`, | ||
selector: "#s12:not(FOO)", | ||
spec: Specificity{1, 0, 1}, | ||
}, | ||
{ | ||
HTML: `<html><body><ul><ol><li id="s12"></li></ol></ul></body></html>`, | ||
selector: "#s12:empty", | ||
spec: Specificity{1, 1, 0}, | ||
}, | ||
{ | ||
HTML: `<html><body><ul><ol><li id="s12"></li></ol></ul></body></html>`, | ||
selector: "#s12:only-child", | ||
spec: Specificity{1, 1, 0}, | ||
}, | ||
} | ||
|
||
func TestSpecificity(t *testing.T) { | ||
for _, test := range testsSpecificity { | ||
s, err := Parse(test.selector) | ||
if err != nil { | ||
t.Fatalf("error compiling %q: %s", test.selector, err) | ||
} | ||
|
||
doc, err := html.Parse(strings.NewReader(test.HTML)) | ||
if err != nil { | ||
t.Fatalf("error parsing %q: %s", test.HTML, err) | ||
} | ||
body := doc.FirstChild.LastChild | ||
testNode := body.FirstChild.FirstChild.LastChild | ||
if !s.Match(testNode) { | ||
t.Errorf("%s didn't match (html tree : \n %s) \n", test.selector, nodeString(doc)) | ||
continue | ||
} | ||
gotSpec := s.Specificity() | ||
if gotSpec != test.spec { | ||
t.Errorf("wrong specificity : expected %v, got %v", test.spec, gotSpec) | ||
} | ||
} | ||
} |