Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions src/utils/isElementVisible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,34 @@ function isStyleVisible<T extends Element>(element: T) {
)
}

function isAttributeVisible<T extends Element>(element: T) {
return (
!element.hasAttribute('hidden') &&
(element.nodeName === 'DETAILS' ? element.hasAttribute('open') : true)
)
function isAttributeVisible<T extends Element>(
element: T,
childElement?: Element
) {
if (element.hasAttribute('hidden')) return false

// A closed <details> renders only its <summary>: the <details> itself is
// visible, the <summary> is visible, but other descendants are not.
if (
element.nodeName === 'DETAILS' &&
!element.hasAttribute('open') &&
childElement &&
childElement.nodeName !== 'SUMMARY'
) {
return false
}

return true
}

export function isElementVisible<T extends Element>(element: T): boolean {
export function isElementVisible<T extends Element>(
element: T,
childElement?: Element
): boolean {
return (
element.nodeName !== '#comment' &&
isStyleVisible(element) &&
isAttributeVisible(element) &&
(!element.parentElement || isElementVisible(element.parentElement))
isAttributeVisible(element, childElement) &&
(!element.parentElement || isElementVisible(element.parentElement, element))
)
}
44 changes: 44 additions & 0 deletions tests/isVisible.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,50 @@ describe('isVisible', () => {
expect(wrapper.html()).not.toContain('Item: 2')
})

describe('details element', () => {
const Comp = defineComponent({
props: {
open: { type: Boolean, default: false }
},
template: `
<details :open="open">
<summary>Toggle</summary>
<p class="content">Content</p>
</details>
`
})

it('details, summary and content are visible when open', () => {
const wrapper = mount(Comp, { props: { open: true } })

expect(wrapper.get('details').isVisible()).toBe(true)
expect(wrapper.get('summary').isVisible()).toBe(true)
expect(wrapper.get('p.content').isVisible()).toBe(true)
})

it('details and summary stay visible when closed, content is hidden', () => {
const wrapper = mount(Comp, { props: { open: false } })

expect(wrapper.get('details').isVisible()).toBe(true)
expect(wrapper.get('summary').isVisible()).toBe(true)
expect(wrapper.get('p.content').isVisible()).toBe(false)
})

it('non-summary descendants of a closed details are hidden even when nested', () => {
const Nested = defineComponent({
template: `
<details>
<summary>Toggle</summary>
<div><span class="deep">Deep</span></div>
</details>
`
})
const wrapper = mount(Nested)

expect(wrapper.get('span.deep').isVisible()).toBe(false)
})
})

it('should take css into account', async () => {
const style = document.createElement('style')
style.type = 'text/css'
Expand Down