package cascadia import ( "strings" "testing" "golang.org/x/net/html" ) type selectorTest struct { HTML, selector string results []string } func nodeString(n *html.Node) string { switch n.Type { case html.TextNode: return n.Data case html.ElementNode: return html.Token{ Type: html.StartTagToken, Data: n.Data, Attr: n.Attr, }.String() } return "" } var selectorTests = []selectorTest{ { `
This address...`, "address", []string{ "", }, }, { ``, "*", []string{ "", "", "", "", }, }, { ``, "#foo", []string{ `
`, }, }, { `
`, "li#t1", []string{ `
`, "p.t1", []string{ `
`, }, }, { `
`, ".t1.fail", []string{}, }, { `
`, "p.t1.t2", []string{ `
`, }, }, { `
`, "p[title]", []string{ `
`, }, }, { `
`, `address[title="foo"]`, []string{ ``, }, }, { ``, `[ title ~= foo ]`, []string{ `
`, }, }, { `
`, `[title~="hello world"]`, []string{}, }, { `
`, `[lang|="en"]`, []string{ `
`, `
`, }, }, { `
`, `[title^="foo"]`, []string{ `
`, }, }, { `
`, `[title$="bar"]`, []string{ `
`, }, }, { `
`, `[title*="bar"]`, []string{ `
`, }, }, { `
`, ".t1:not(.t2)", []string{}, }, { `
some text and a span and another
`, `span:first-child`, []string{ ``, }, }, { `a span and some text`, `span:last-child`, []string{ ``, }, }, { ``, `p:nth-of-type(2)`, []string{ `
`, }, }, { `
`, `p:nth-last-of-type(2)`, []string{ ``, }, }, { `
`, `p:last-of-type`, []string{ ``, }, }, { `
`, `p:first-of-type`, []string{ ``, }, }, { `
`, }, }, { `
`, }, }, { `
Hello
`,
`:empty`,
[]string{
``,
` `,
``,
},
},
{
` `,
`div p`,
[]string{
` `,
` `,
},
},
{
` `,
`div table p`,
[]string{
` `,
},
},
{
` `,
` `,
},
},
{
` `,
`p ~ p`,
[]string{
` `,
` `,
},
},
{
` `,
`p + p`,
[]string{
` `,
},
},
{
` `,
`li, p`,
[]string{
" ",
},
},
{
` `,
`p +/*This is a comment*/ p`,
[]string{
` `,
},
},
{
` Text block that wraps inner text and continues `,
},
},
{
` Text block that wraps inner text and continues Text block that wraps inner text and continues Text block that wraps inner text and continues `,
},
},
{
` text content contents 1 contents 2 contents 1 contents 2 `,
},
},
{
` contents 1 contents 2 `,
},
},
{
` 0123456789 abcdef 0123ABCD `,
` `,
},
},
{
` 0123456789 abcdef 0123ABCD `,
},
},
{
` 0123456789 abcdef 0123ABCD `,
` `,
},
},
{
` 0123456789 abcdef 0123ABCD `,
` `,
},
},
{
` 0123456789 abcdef 0123ABCD `,
` `,
` `,
},
},
{
` 0123456789 abcdef 0123ABCD `,
},
},
{
` 0123456789 abcdef 0123ABCD `,
` `,
},
},
{
` 0123456789 `,
``,
},
},
{
``,
`[href#=(fina)]:not([href#=(\/\/[^\/]+untrusted)])`,
[]string{
``,
``,
},
},
{
``,
`[href#=(^https:\/\/[^\/]*\/?news)]`,
[]string{
``,
},
},
{
``,
`:input`,
[]string{
``,
``,
`