to-be-disabled.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.toBeDisabled = toBeDisabled;
  6. exports.toBeEnabled = toBeEnabled;
  7. var _utils = require("./utils");
  8. // form elements that support 'disabled'
  9. const FORM_TAGS = ['fieldset', 'input', 'select', 'optgroup', 'option', 'button', 'textarea'];
  10. /*
  11. * According to specification:
  12. * If <fieldset> is disabled, the form controls that are its descendants,
  13. * except descendants of its first optional <legend> element, are disabled
  14. *
  15. * https://html.spec.whatwg.org/multipage/form-elements.html#concept-fieldset-disabled
  16. *
  17. * This method tests whether element is first legend child of fieldset parent
  18. */
  19. function isFirstLegendChildOfFieldset(element, parent) {
  20. return (0, _utils.getTag)(element) === 'legend' && (0, _utils.getTag)(parent) === 'fieldset' && element.isSameNode(Array.from(parent.children).find(child => (0, _utils.getTag)(child) === 'legend'));
  21. }
  22. function isElementDisabledByParent(element, parent) {
  23. return isElementDisabled(parent) && !isFirstLegendChildOfFieldset(element, parent);
  24. }
  25. function isCustomElement(tag) {
  26. return tag.includes('-');
  27. }
  28. /*
  29. * Only certain form elements and custom elements can actually be disabled:
  30. * https://html.spec.whatwg.org/multipage/semantics-other.html#disabled-elements
  31. */
  32. function canElementBeDisabled(element) {
  33. const tag = (0, _utils.getTag)(element);
  34. return FORM_TAGS.includes(tag) || isCustomElement(tag);
  35. }
  36. function isElementDisabled(element) {
  37. return canElementBeDisabled(element) && element.hasAttribute('disabled');
  38. }
  39. function isAncestorDisabled(element) {
  40. const parent = element.parentElement;
  41. return Boolean(parent) && (isElementDisabledByParent(element, parent) || isAncestorDisabled(parent));
  42. }
  43. function isElementOrAncestorDisabled(element) {
  44. return canElementBeDisabled(element) && (isElementDisabled(element) || isAncestorDisabled(element));
  45. }
  46. function toBeDisabled(element) {
  47. (0, _utils.checkHtmlElement)(element, toBeDisabled, this);
  48. const isDisabled = isElementOrAncestorDisabled(element);
  49. return {
  50. pass: isDisabled,
  51. message: () => {
  52. const is = isDisabled ? 'is' : 'is not';
  53. return [this.utils.matcherHint(`${this.isNot ? '.not' : ''}.toBeDisabled`, 'element', ''), '', `Received element ${is} disabled:`, ` ${this.utils.printReceived(element.cloneNode(false))}`].join('\n');
  54. }
  55. };
  56. }
  57. function toBeEnabled(element) {
  58. (0, _utils.checkHtmlElement)(element, toBeEnabled, this);
  59. const isEnabled = !isElementOrAncestorDisabled(element);
  60. return {
  61. pass: isEnabled,
  62. message: () => {
  63. const is = isEnabled ? 'is' : 'is not';
  64. return [this.utils.matcherHint(`${this.isNot ? '.not' : ''}.toBeEnabled`, 'element', ''), '', `Received element ${is} enabled:`, ` ${this.utils.printReceived(element.cloneNode(false))}`].join('\n');
  65. }
  66. };
  67. }