Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Element.ShadowRoot returns error when ShadowRoots is empty #788

Merged
merged 4 commits into from Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions element.go
Expand Up @@ -402,6 +402,9 @@ func (el *Element) ShadowRoot() (*Element, error) {
}

// though now it's an array, w3c changed the spec of it to be a single.
if len(node.ShadowRoots) == 0 {
return nil, &ErrNoShadowRoot{el}
}
id := node.ShadowRoots[0].BackendNodeID

shadowNode, err := proto.DOMResolveNode{BackendNodeID: id}.Call(el)
Expand Down
5 changes: 5 additions & 0 deletions element_test.go
Expand Up @@ -284,6 +284,11 @@ func TestShadowDOM(t *testing.T) {
g.mc.stubErr(1, proto.DOMResolveNode{})
el.MustShadowRoot()
})

elNoShadow := p.MustElement("script")
_, err := elNoShadow.ShadowRoot()
g.True((&rod.ErrNoShadowRoot{}).Is(err))
g.Has(err.Error(), "element has no shadow root:")
}

func TestInputTime(t *testing.T) {
Expand Down
13 changes: 13 additions & 0 deletions error.go
Expand Up @@ -181,3 +181,16 @@ type ErrPageNotFound struct {
func (e *ErrPageNotFound) Error() string {
return "cannot find page"
}

// ErrNoShadowRoot error
type ErrNoShadowRoot struct {
*Element
}

// Error ...
func (e *ErrNoShadowRoot) Error() string {
return fmt.Sprintf("element has no shadow root: %s", e.String())
}

// Is interface
func (e *ErrNoShadowRoot) Is(err error) bool { _, ok := err.(*ErrNoShadowRoot); return ok }