-
-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #747 from jaydgruber/issue-684-failing-tests
- Loading branch information
Showing
5 changed files
with
113 additions
and
2 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
addon-test-support/@ember/test-helpers/dom/-guard-for-maxlength.ts
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,39 @@ | ||
import { FormControl } from './-is-form-control'; | ||
|
||
// ref: https://html.spec.whatwg.org/multipage/input.html#concept-input-apply | ||
const constrainedInputTypes = ['text', 'search', 'url', 'tel', 'email', 'password']; | ||
|
||
/** | ||
@private | ||
@param {Element} element - the element to check | ||
@returns {boolean} `true` when the element should constrain input by the maxlength attribute, `false` otherwise | ||
*/ | ||
function isMaxLengthConstrained( | ||
element: Element | ||
): element is HTMLInputElement | HTMLTextAreaElement { | ||
return ( | ||
!!Number(element.getAttribute('maxLength')) && | ||
(element instanceof HTMLInputElement || | ||
(element instanceof HTMLTextAreaElement && constrainedInputTypes.indexOf(element.type) > -1)) | ||
); | ||
} | ||
|
||
/** | ||
* @private | ||
* @param {Element} element - the element to check | ||
* @param {string} text - the text being added to element | ||
* @param {string} testHelper - the test helper context the guard is called from (for Error message) | ||
* @throws if `element` has `maxlength` & `value` exceeds `maxlength` | ||
*/ | ||
export default function guardForMaxlength( | ||
element: FormControl, | ||
text: string, | ||
testHelper: string | ||
): void { | ||
const maxlength = element.getAttribute('maxlength'); | ||
if (isMaxLengthConstrained(element) && maxlength && text && text.length > Number(maxlength)) { | ||
throw new Error( | ||
`Can not \`${testHelper}\` with text: '${text}' that exceeds maxlength: '${maxlength}'.` | ||
); | ||
} | ||
} |
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