-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Block toolbar: restrict visible child calculation to known blocks #66702
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
078d25a
Refactor getVisibleElementBounds to only check visible children for s…
ramonjd e23980a
Apply suggestions to code comments from code review
ramonjd ae38d46
Add extra tests
ramonjd 2154b0e
Rebase
ramonjd 545014a
Merge remote-tracking branch 'origin/update/dom-get-element-bounds-re…
ramonjd ae72973
Added a test for viewport clipping and adjusted test descriptions
ramonjd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,224 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getElementBounds, WITH_OVERFLOW_ELEMENT_BLOCKS } from '../dom'; | ||
describe( 'dom', () => { | ||
describe( 'getElementBounds', () => { | ||
ramonjd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
it( 'should return a DOMRectReadOnly object if the viewport is not available', () => { | ||
const element = { | ||
ownerDocument: { | ||
defaultView: null, | ||
}, | ||
}; | ||
expect( getElementBounds( element ) ).toEqual( | ||
new window.DOMRectReadOnly() | ||
); | ||
} ); | ||
it( 'should return a DOMRectReadOnly object if the viewport is available', () => { | ||
const element = { | ||
ownerDocument: { | ||
defaultView: { | ||
getComputedStyle: () => ( { | ||
display: 'block', | ||
visibility: 'visible', | ||
opacity: '1', | ||
} ), | ||
}, | ||
}, | ||
getBoundingClientRect: () => ( { | ||
left: 0, | ||
top: 0, | ||
right: 100, | ||
bottom: 100, | ||
width: 100, | ||
height: 100, | ||
} ), | ||
getAttribute: ( x ) => x, | ||
}; | ||
expect( getElementBounds( element ) ).toEqual( | ||
new window.DOMRectReadOnly( 0, 0, 100, 100 ) | ||
); | ||
} ); | ||
it( 'should clip left and right values when an element is larger than the viewport width', () => { | ||
const element = window.document.createElement( 'div' ); | ||
element.getBoundingClientRect = jest.fn().mockReturnValue( { | ||
left: -10, | ||
top: 0, | ||
right: window.innerWidth + 10, | ||
bottom: 100, | ||
width: window.innerWidth, | ||
height: 100, | ||
} ); | ||
expect( getElementBounds( element ).toJSON() ).toEqual( { | ||
left: 0, // Reset to min left bound. | ||
top: 0, | ||
right: window.innerWidth, // Reset to max right bound. | ||
bottom: 100, | ||
width: window.innerWidth, | ||
height: 100, | ||
x: 0, | ||
y: 0, | ||
} ); | ||
} ); | ||
it( 'should return the parent DOMRectReadOnly object if the parent block type is not supported', () => { | ||
const element = window.document.createElement( 'div' ); | ||
element.getBoundingClientRect = jest.fn().mockReturnValue( { | ||
left: 0, | ||
top: 0, | ||
right: 100, | ||
bottom: 100, | ||
width: 100, | ||
height: 100, | ||
} ); | ||
element.setAttribute( 'data-type', 'test' ); | ||
const childElement = window.document.createElement( 'div' ); | ||
childElement.getBoundingClientRect = jest.fn().mockReturnValue( { | ||
left: 0, | ||
top: 0, | ||
right: 333, | ||
bottom: 333, | ||
width: 333, | ||
height: 333, | ||
x: 0, | ||
y: 0, | ||
} ); | ||
element.appendChild( childElement ); | ||
|
||
expect( getElementBounds( element ).toJSON() ).toEqual( { | ||
left: 0, | ||
top: 0, | ||
right: 100, | ||
bottom: 100, | ||
width: 100, | ||
height: 100, | ||
x: 0, | ||
y: 0, | ||
} ); | ||
} ); | ||
describe( 'With known block type', () => { | ||
it( 'should return the child DOMRectReadOnly object if it is visible and a known block type', () => { | ||
const element = window.document.createElement( 'div' ); | ||
element.getBoundingClientRect = jest.fn().mockReturnValue( { | ||
left: 0, | ||
top: 0, | ||
right: 100, | ||
bottom: 100, | ||
width: 100, | ||
height: 100, | ||
} ); | ||
element.setAttribute( | ||
'data-type', | ||
WITH_OVERFLOW_ELEMENT_BLOCKS[ 0 ] | ||
); | ||
const childElement = window.document.createElement( 'div' ); | ||
childElement.getBoundingClientRect = jest | ||
.fn() | ||
.mockReturnValue( { | ||
left: 0, | ||
top: 0, | ||
right: 333, | ||
bottom: 333, | ||
width: 333, | ||
height: 333, | ||
x: 0, | ||
y: 0, | ||
} ); | ||
element.appendChild( childElement ); | ||
|
||
expect( getElementBounds( element ).toJSON() ).toEqual( { | ||
left: 0, | ||
top: 0, | ||
right: 333, | ||
bottom: 333, | ||
width: 333, | ||
height: 333, | ||
x: 0, | ||
y: 0, | ||
} ); | ||
} ); | ||
it( 'should return the parent DOMRectReadOnly if the child is scrollable', () => { | ||
const element = window.document.createElement( 'div' ); | ||
element.setAttribute( | ||
'data-type', | ||
WITH_OVERFLOW_ELEMENT_BLOCKS[ 0 ] | ||
); | ||
element.style.overflowX = 'auto'; | ||
element.style.overflowY = 'auto'; | ||
element.getBoundingClientRect = jest.fn().mockReturnValue( { | ||
left: 0, | ||
top: 0, | ||
right: 100, | ||
bottom: 100, | ||
width: 100, | ||
height: 100, | ||
} ); | ||
const childElement = window.document.createElement( 'div' ); | ||
childElement.getBoundingClientRect = jest | ||
.fn() | ||
.mockReturnValue( { | ||
left: 0, | ||
top: 0, | ||
right: 333, | ||
bottom: 333, | ||
width: 333, | ||
height: 333, | ||
x: 0, | ||
y: 0, | ||
} ); | ||
element.appendChild( childElement ); | ||
|
||
expect( getElementBounds( element ).toJSON() ).toEqual( { | ||
left: 0, | ||
top: 0, | ||
right: 100, | ||
bottom: 100, | ||
width: 100, | ||
height: 100, | ||
x: 0, | ||
y: 0, | ||
} ); | ||
} ); | ||
it( 'should return the parent DOMRectReadOnly object if the child element is not visible', () => { | ||
const element = window.document.createElement( 'div' ); | ||
element.getBoundingClientRect = jest.fn().mockReturnValue( { | ||
left: 0, | ||
top: 0, | ||
right: 100, | ||
bottom: 100, | ||
width: 100, | ||
height: 100, | ||
} ); | ||
element.setAttribute( | ||
'data-type', | ||
WITH_OVERFLOW_ELEMENT_BLOCKS[ 0 ] | ||
); | ||
const childElement = window.document.createElement( 'div' ); | ||
childElement.getBoundingClientRect = jest | ||
.fn() | ||
.mockReturnValue( { | ||
left: 0, | ||
top: 0, | ||
right: 333, | ||
bottom: 333, | ||
width: 333, | ||
height: 333, | ||
x: 0, | ||
y: 0, | ||
} ); | ||
childElement.style.display = 'none'; | ||
element.appendChild( childElement ); | ||
|
||
expect( getElementBounds( element ).toJSON() ).toEqual( { | ||
left: 0, | ||
top: 0, | ||
right: 100, | ||
bottom: 100, | ||
width: 100, | ||
height: 100, | ||
x: 0, | ||
y: 0, | ||
} ); | ||
} ); | ||
} ); | ||
} ); | ||
} ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if you considered this elsewhere, but I wonder whether in the future we could make this list extensible as an editor setting? That way it can be modified in PHP for custom blocks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for cherry picking this one!
@t-hamano had an idea to add it to the block api
I think it's worth investigating if it the issue arises again.