Ratio.cjs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use strict';
  2. const types = require('../../tokenizer/types.cjs');
  3. const SOLIDUS = 0x002F; // U+002F SOLIDUS (/)
  4. // Media Queries Level 3 defines terms of <ratio> as a positive (not zero or negative)
  5. // integers (see https://drafts.csswg.org/mediaqueries-3/#values)
  6. // However, Media Queries Level 4 removes any definition of values
  7. // (see https://drafts.csswg.org/mediaqueries-4/#values) and refers to
  8. // CSS Values and Units for detail. In CSS Values and Units Level 4 a <ratio>
  9. // definition was added (see https://drafts.csswg.org/css-values-4/#ratios) which
  10. // defines ratio as "<number [0,∞]> [ / <number [0,∞]> ]?" and based on it
  11. // any constrains on terms were removed. Parser also doesn't test numbers
  12. // in any way to make possible for linting and fixing them by the tools using CSSTree.
  13. // An additional syntax examination may be applied by a lexer.
  14. function consumeTerm() {
  15. this.skipSC();
  16. switch (this.tokenType) {
  17. case types.Number:
  18. return this.Number();
  19. case types.Function:
  20. return this.Function(this.readSequence, this.scope.Value);
  21. default:
  22. this.error('Number of function is expected');
  23. }
  24. }
  25. const name = 'Ratio';
  26. const structure = {
  27. left: ['Number', 'Function'],
  28. right: ['Number', 'Function', null]
  29. };
  30. // <number [0,∞]> [ / <number [0,∞]> ]?
  31. function parse() {
  32. const start = this.tokenStart;
  33. const left = consumeTerm.call(this);
  34. let right = null;
  35. this.skipSC();
  36. if (this.isDelim(SOLIDUS)) {
  37. this.eatDelim(SOLIDUS);
  38. right = consumeTerm.call(this);
  39. }
  40. return {
  41. type: 'Ratio',
  42. loc: this.getLocation(start, this.tokenStart),
  43. left,
  44. right
  45. };
  46. }
  47. function generate(node) {
  48. this.node(node.left);
  49. this.token(types.Delim, '/');
  50. if (node.right) {
  51. this.node(node.right);
  52. } else {
  53. this.node(types.Number, 1);
  54. }
  55. }
  56. exports.generate = generate;
  57. exports.name = name;
  58. exports.parse = parse;
  59. exports.structure = structure;