to-be-empty-dom-element.js 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.toBeEmptyDOMElement = toBeEmptyDOMElement;
  6. var _utils = require("./utils");
  7. function toBeEmptyDOMElement(element) {
  8. (0, _utils.checkHtmlElement)(element, toBeEmptyDOMElement, this);
  9. return {
  10. pass: isEmptyElement(element),
  11. message: () => {
  12. return [this.utils.matcherHint(`${this.isNot ? '.not' : ''}.toBeEmptyDOMElement`, 'element', ''), '', 'Received:', ` ${this.utils.printReceived(element.innerHTML)}`].join('\n');
  13. }
  14. };
  15. }
  16. /**
  17. * Identifies if an element doesn't contain child nodes (excluding comments)
  18. * ℹ Node.COMMENT_NODE can't be used because of the following issue
  19. * https://github.com/jsdom/jsdom/issues/2220
  20. *
  21. * @param {*} element an HtmlElement or SVGElement
  22. * @return {*} true if the element only contains comments or none
  23. */
  24. function isEmptyElement(element) {
  25. const nonCommentChildNodes = [...element.childNodes].filter(node => node.nodeType !== 8);
  26. return nonCommentChildNodes.length === 0;
  27. }