-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Framework: Extract isEditableElement utility
- Loading branch information
1 parent
de8de1b
commit 5c24018
Showing
3 changed files
with
50 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
/** | ||
* Utility function to check whether the domElement provided is editable or not | ||
* An editable element means we can type in it to edit its content | ||
* This includes inputs and contenteditables | ||
* | ||
* @param {DomElement} domElement DOM Element | ||
* @return {Boolean} Whether the DOM Element is editable or not | ||
*/ | ||
export function isEditableElement( domElement ) { | ||
return [ 'textarea', 'input', 'select' ].indexOf( domElement.tagName.toLowerCase() ) !== -1 | ||
|| !! domElement.isContentEditable; | ||
} |
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,35 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { expect } from 'chai'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { isEditableElement } from '../dom'; | ||
|
||
describe.only( 'isEditableElement', () => { | ||
it( 'should return false for non editable nodes', () => { | ||
const div = document.createElement( 'div' ); | ||
|
||
expect( isEditableElement( div ) ).to.be.false(); | ||
} ); | ||
|
||
it( 'should return true for inputs', () => { | ||
const input = document.createElement( 'input' ); | ||
|
||
expect( isEditableElement( input ) ).to.be.true(); | ||
} ); | ||
|
||
it( 'should return true for textareas', () => { | ||
const textarea = document.createElement( 'textarea' ); | ||
|
||
expect( isEditableElement( textarea ) ).to.be.true(); | ||
} ); | ||
|
||
it( 'should return true for selects', () => { | ||
const select = document.createElement( 'select' ); | ||
|
||
expect( isEditableElement( select ) ).to.be.true(); | ||
} ); | ||
} ); |