| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- "use strict";
-
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = void 0;
-
- var _Node = _interopRequireDefault(require("./Node"));
-
- var _Range = _interopRequireDefault(require("./Range"));
-
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
-
- class PlainValue extends _Node.default {
- static endOfLine(src, start, inFlow) {
- let ch = src[start];
- let offset = start;
-
- while (ch && ch !== '\n') {
- if (inFlow && (ch === '[' || ch === ']' || ch === '{' || ch === '}' || ch === ',')) break;
- const next = src[offset + 1];
- if (ch === ':' && (!next || next === '\n' || next === '\t' || next === ' ' || inFlow && next === ',')) break;
- if ((ch === ' ' || ch === '\t') && next === '#') break;
- offset += 1;
- ch = next;
- }
-
- return offset;
- }
-
- get strValue() {
- if (!this.valueRange || !this.context) return null;
- let {
- start,
- end
- } = this.valueRange;
- const {
- src
- } = this.context;
- let ch = src[end - 1];
-
- while (start < end && (ch === '\n' || ch === '\t' || ch === ' ')) ch = src[--end - 1];
-
- ch = src[start];
-
- while (start < end && (ch === '\n' || ch === '\t' || ch === ' ')) ch = src[++start];
-
- let str = '';
-
- for (let i = start; i < end; ++i) {
- const ch = src[i];
-
- if (ch === '\n') {
- const {
- fold,
- offset
- } = _Node.default.foldNewline(src, i, -1);
-
- str += fold;
- i = offset;
- } else if (ch === ' ' || ch === '\t') {
- // trim trailing whitespace
- const wsStart = i;
- let next = src[i + 1];
-
- while (i < end && (next === ' ' || next === '\t')) {
- i += 1;
- next = src[i + 1];
- }
-
- if (next !== '\n') str += i > wsStart ? src.slice(wsStart, i + 1) : ch;
- } else {
- str += ch;
- }
- }
-
- return str;
- }
-
- parseBlockValue(start) {
- const {
- indent,
- inFlow,
- src
- } = this.context;
- let offset = start;
- let valueEnd = start;
-
- for (let ch = src[offset]; ch === '\n'; ch = src[offset]) {
- if (_Node.default.atDocumentBoundary(src, offset + 1)) break;
-
- const end = _Node.default.endOfBlockIndent(src, indent, offset + 1);
-
- if (end === null || src[end] === '#') break;
-
- if (src[end] === '\n') {
- offset = end;
- } else {
- valueEnd = PlainValue.endOfLine(src, end, inFlow);
- offset = valueEnd;
- }
- }
-
- if (this.valueRange.isEmpty()) this.valueRange.start = start;
- this.valueRange.end = valueEnd;
- return valueEnd;
- }
- /**
- * Parses a plain value from the source
- *
- * Accepted forms are:
- * ```
- * #comment
- *
- * first line
- *
- * first line #comment
- *
- * first line
- * block
- * lines
- *
- * #comment
- * block
- * lines
- * ```
- * where block lines are empty or have an indent level greater than `indent`.
- *
- * @param {ParseContext} context
- * @param {number} start - Index of first character
- * @returns {number} - Index of the character after this scalar, may be `\n`
- */
-
-
- parse(context, start) {
- this.context = context;
- const {
- inFlow,
- src
- } = context;
- let offset = start;
- const ch = src[offset];
-
- if (ch && ch !== '#' && ch !== '\n') {
- offset = PlainValue.endOfLine(src, start, inFlow);
- }
-
- this.valueRange = new _Range.default(start, offset);
- offset = _Node.default.endOfWhiteSpace(src, offset);
- offset = this.parseComment(offset);
-
- if (!this.hasComment || this.valueRange.isEmpty()) {
- offset = this.parseBlockValue(offset);
- }
-
- return offset;
- }
-
- }
-
- exports.default = PlainValue;
|