web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
5.5 KiB

  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.checkHtmlElement = checkHtmlElement;
  7. exports.parseCSS = parseCSS;
  8. exports.deprecate = deprecate;
  9. exports.getMessage = getMessage;
  10. exports.matches = matches;
  11. exports.normalize = normalize;
  12. exports.getTag = getTag;
  13. exports.getSingleElementValue = getSingleElementValue;
  14. exports.compareArraysAsSet = compareArraysAsSet;
  15. exports.toSentence = toSentence;
  16. exports.HtmlElementTypeError = void 0;
  17. var _redent = _interopRequireDefault(require("redent"));
  18. var _css = require("css");
  19. var _isEqual = _interopRequireDefault(require("lodash/isEqual"));
  20. class HtmlElementTypeError extends Error {
  21. constructor(received, matcherFn, context) {
  22. super();
  23. /* istanbul ignore next */
  24. if (Error.captureStackTrace) {
  25. Error.captureStackTrace(this, matcherFn);
  26. }
  27. let withType = '';
  28. try {
  29. withType = context.utils.printWithType('Received', received, context.utils.printReceived);
  30. } catch (e) {// Can throw for Document:
  31. // https://github.com/jsdom/jsdom/issues/2304
  32. }
  33. this.message = [context.utils.matcherHint(`${context.isNot ? '.not' : ''}.${matcherFn.name}`, 'received', ''), '', // eslint-disable-next-line babel/new-cap
  34. `${context.utils.RECEIVED_COLOR('received')} value must be an HTMLElement or an SVGElement.`, withType].join('\n');
  35. }
  36. }
  37. exports.HtmlElementTypeError = HtmlElementTypeError;
  38. function checkHasWindow(htmlElement, ...args) {
  39. if (!htmlElement || !htmlElement.ownerDocument || !htmlElement.ownerDocument.defaultView) {
  40. throw new HtmlElementTypeError(htmlElement, ...args);
  41. }
  42. }
  43. function checkHtmlElement(htmlElement, ...args) {
  44. checkHasWindow(htmlElement, ...args);
  45. const window = htmlElement.ownerDocument.defaultView;
  46. if (!(htmlElement instanceof window.HTMLElement) && !(htmlElement instanceof window.SVGElement)) {
  47. throw new HtmlElementTypeError(htmlElement, ...args);
  48. }
  49. }
  50. class InvalidCSSError extends Error {
  51. constructor(received, matcherFn, context) {
  52. super();
  53. /* istanbul ignore next */
  54. if (Error.captureStackTrace) {
  55. Error.captureStackTrace(this, matcherFn);
  56. }
  57. this.message = [received.message, '', // eslint-disable-next-line babel/new-cap
  58. context.utils.RECEIVED_COLOR(`Failing css:`), // eslint-disable-next-line babel/new-cap
  59. context.utils.RECEIVED_COLOR(`${received.css}`)].join('\n');
  60. }
  61. }
  62. function parseCSS(css, ...args) {
  63. const ast = (0, _css.parse)(`selector { ${css} }`, {
  64. silent: true
  65. }).stylesheet;
  66. if (ast.parsingErrors && ast.parsingErrors.length > 0) {
  67. const {
  68. reason,
  69. line
  70. } = ast.parsingErrors[0];
  71. throw new InvalidCSSError({
  72. css,
  73. message: `Syntax error parsing expected css: ${reason} on line: ${line}`
  74. }, ...args);
  75. }
  76. const parsedRules = ast.rules[0].declarations.filter(d => d.type === 'declaration').reduce((obj, {
  77. property,
  78. value
  79. }) => Object.assign(obj, {
  80. [property]: value
  81. }), {});
  82. return parsedRules;
  83. }
  84. function display(context, value) {
  85. return typeof value === 'string' ? value : context.utils.stringify(value);
  86. }
  87. function getMessage(context, matcher, expectedLabel, expectedValue, receivedLabel, receivedValue) {
  88. return [`${matcher}\n`, // eslint-disable-next-line babel/new-cap
  89. `${expectedLabel}:\n${context.utils.EXPECTED_COLOR((0, _redent.default)(display(context, expectedValue), 2))}`, // eslint-disable-next-line babel/new-cap
  90. `${receivedLabel}:\n${context.utils.RECEIVED_COLOR((0, _redent.default)(display(context, receivedValue), 2))}`].join('\n');
  91. }
  92. function matches(textToMatch, matcher) {
  93. if (matcher instanceof RegExp) {
  94. return matcher.test(textToMatch);
  95. } else {
  96. return textToMatch.includes(String(matcher));
  97. }
  98. }
  99. function deprecate(name, replacementText) {
  100. // Notify user that they are using deprecated functionality.
  101. // eslint-disable-next-line no-console
  102. console.warn(`Warning: ${name} has been deprecated and will be removed in future updates.`, replacementText);
  103. }
  104. function normalize(text) {
  105. return text.replace(/\s+/g, ' ').trim();
  106. }
  107. function getTag(element) {
  108. return element.tagName && element.tagName.toLowerCase();
  109. }
  110. function getSelectValue({
  111. multiple,
  112. options
  113. }) {
  114. const selectedOptions = [...options].filter(option => option.selected);
  115. if (multiple) {
  116. return [...selectedOptions].map(opt => opt.value);
  117. }
  118. /* istanbul ignore if */
  119. if (selectedOptions.length === 0) {
  120. return undefined; // Couldn't make this happen, but just in case
  121. }
  122. return selectedOptions[0].value;
  123. }
  124. function getInputValue(inputElement) {
  125. switch (inputElement.type) {
  126. case 'number':
  127. return inputElement.value === '' ? null : Number(inputElement.value);
  128. case 'checkbox':
  129. return inputElement.checked;
  130. default:
  131. return inputElement.value;
  132. }
  133. }
  134. function getSingleElementValue(element) {
  135. /* istanbul ignore if */
  136. if (!element) {
  137. return undefined;
  138. }
  139. switch (element.tagName.toLowerCase()) {
  140. case 'input':
  141. return getInputValue(element);
  142. case 'select':
  143. return getSelectValue(element);
  144. default:
  145. return element.value;
  146. }
  147. }
  148. function compareArraysAsSet(a, b) {
  149. if (Array.isArray(a) && Array.isArray(b)) {
  150. return (0, _isEqual.default)(new Set(a), new Set(b));
  151. }
  152. return undefined;
  153. }
  154. function toSentence(array, {
  155. wordConnector = ', ',
  156. lastWordConnector = ' and '
  157. } = {}) {
  158. return [array.slice(0, -1).join(wordConnector), array[array.length - 1]].join(array.length > 1 ? lastWordConnector : '');
  159. }