Dashboard sipadu mbip
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.

PlainValue.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _Node = _interopRequireDefault(require("./Node"));
  7. var _Range = _interopRequireDefault(require("./Range"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. class PlainValue extends _Node.default {
  10. static endOfLine(src, start, inFlow) {
  11. let ch = src[start];
  12. let offset = start;
  13. while (ch && ch !== '\n') {
  14. if (inFlow && (ch === '[' || ch === ']' || ch === '{' || ch === '}' || ch === ',')) break;
  15. const next = src[offset + 1];
  16. if (ch === ':' && (!next || next === '\n' || next === '\t' || next === ' ' || inFlow && next === ',')) break;
  17. if ((ch === ' ' || ch === '\t') && next === '#') break;
  18. offset += 1;
  19. ch = next;
  20. }
  21. return offset;
  22. }
  23. get strValue() {
  24. if (!this.valueRange || !this.context) return null;
  25. let {
  26. start,
  27. end
  28. } = this.valueRange;
  29. const {
  30. src
  31. } = this.context;
  32. let ch = src[end - 1];
  33. while (start < end && (ch === '\n' || ch === '\t' || ch === ' ')) ch = src[--end - 1];
  34. ch = src[start];
  35. while (start < end && (ch === '\n' || ch === '\t' || ch === ' ')) ch = src[++start];
  36. let str = '';
  37. for (let i = start; i < end; ++i) {
  38. const ch = src[i];
  39. if (ch === '\n') {
  40. const {
  41. fold,
  42. offset
  43. } = _Node.default.foldNewline(src, i, -1);
  44. str += fold;
  45. i = offset;
  46. } else if (ch === ' ' || ch === '\t') {
  47. // trim trailing whitespace
  48. const wsStart = i;
  49. let next = src[i + 1];
  50. while (i < end && (next === ' ' || next === '\t')) {
  51. i += 1;
  52. next = src[i + 1];
  53. }
  54. if (next !== '\n') str += i > wsStart ? src.slice(wsStart, i + 1) : ch;
  55. } else {
  56. str += ch;
  57. }
  58. }
  59. return str;
  60. }
  61. parseBlockValue(start) {
  62. const {
  63. indent,
  64. inFlow,
  65. src
  66. } = this.context;
  67. let offset = start;
  68. let valueEnd = start;
  69. for (let ch = src[offset]; ch === '\n'; ch = src[offset]) {
  70. if (_Node.default.atDocumentBoundary(src, offset + 1)) break;
  71. const end = _Node.default.endOfBlockIndent(src, indent, offset + 1);
  72. if (end === null || src[end] === '#') break;
  73. if (src[end] === '\n') {
  74. offset = end;
  75. } else {
  76. valueEnd = PlainValue.endOfLine(src, end, inFlow);
  77. offset = valueEnd;
  78. }
  79. }
  80. if (this.valueRange.isEmpty()) this.valueRange.start = start;
  81. this.valueRange.end = valueEnd;
  82. return valueEnd;
  83. }
  84. /**
  85. * Parses a plain value from the source
  86. *
  87. * Accepted forms are:
  88. * ```
  89. * #comment
  90. *
  91. * first line
  92. *
  93. * first line #comment
  94. *
  95. * first line
  96. * block
  97. * lines
  98. *
  99. * #comment
  100. * block
  101. * lines
  102. * ```
  103. * where block lines are empty or have an indent level greater than `indent`.
  104. *
  105. * @param {ParseContext} context
  106. * @param {number} start - Index of first character
  107. * @returns {number} - Index of the character after this scalar, may be `\n`
  108. */
  109. parse(context, start) {
  110. this.context = context;
  111. const {
  112. inFlow,
  113. src
  114. } = context;
  115. let offset = start;
  116. const ch = src[offset];
  117. if (ch && ch !== '#' && ch !== '\n') {
  118. offset = PlainValue.endOfLine(src, start, inFlow);
  119. }
  120. this.valueRange = new _Range.default(start, offset);
  121. offset = _Node.default.endOfWhiteSpace(src, offset);
  122. offset = this.parseComment(offset);
  123. if (!this.hasComment || this.valueRange.isEmpty()) {
  124. offset = this.parseBlockValue(offset);
  125. }
  126. return offset;
  127. }
  128. }
  129. exports.default = PlainValue;