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.

Document.js 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _constants = require("../constants");
  7. var _errors = require("../errors");
  8. var _BlankLine = _interopRequireDefault(require("./BlankLine"));
  9. var _Collection = require("./Collection");
  10. var _Comment = _interopRequireDefault(require("./Comment"));
  11. var _Directive = _interopRequireDefault(require("./Directive"));
  12. var _Node = _interopRequireDefault(require("./Node"));
  13. var _Range = _interopRequireDefault(require("./Range"));
  14. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15. class Document extends _Node.default {
  16. static startCommentOrEndBlankLine(src, start) {
  17. const offset = _Node.default.endOfWhiteSpace(src, start);
  18. const ch = src[offset];
  19. return ch === '#' || ch === '\n' ? offset : start;
  20. }
  21. constructor() {
  22. super(_constants.Type.DOCUMENT);
  23. this.directives = null;
  24. this.contents = null;
  25. this.directivesEndMarker = null;
  26. this.documentEndMarker = null;
  27. }
  28. parseDirectives(start) {
  29. const {
  30. src
  31. } = this.context;
  32. this.directives = [];
  33. let atLineStart = true;
  34. let hasDirectives = false;
  35. let offset = start;
  36. while (!_Node.default.atDocumentBoundary(src, offset, _constants.Char.DIRECTIVES_END)) {
  37. offset = Document.startCommentOrEndBlankLine(src, offset);
  38. switch (src[offset]) {
  39. case '\n':
  40. if (atLineStart) {
  41. const blankLine = new _BlankLine.default();
  42. offset = blankLine.parse({
  43. src
  44. }, offset);
  45. if (offset < src.length) {
  46. this.directives.push(blankLine);
  47. }
  48. } else {
  49. offset += 1;
  50. atLineStart = true;
  51. }
  52. break;
  53. case '#':
  54. {
  55. const comment = new _Comment.default();
  56. offset = comment.parse({
  57. src
  58. }, offset);
  59. this.directives.push(comment);
  60. atLineStart = false;
  61. }
  62. break;
  63. case '%':
  64. {
  65. const directive = new _Directive.default();
  66. offset = directive.parse({
  67. parent: this,
  68. src
  69. }, offset);
  70. this.directives.push(directive);
  71. hasDirectives = true;
  72. atLineStart = false;
  73. }
  74. break;
  75. default:
  76. if (hasDirectives) {
  77. this.error = new _errors.YAMLSemanticError(this, 'Missing directives-end indicator line');
  78. } else if (this.directives.length > 0) {
  79. this.contents = this.directives;
  80. this.directives = [];
  81. }
  82. return offset;
  83. }
  84. }
  85. if (src[offset]) {
  86. this.directivesEndMarker = new _Range.default(offset, offset + 3);
  87. return offset + 3;
  88. }
  89. if (hasDirectives) {
  90. this.error = new _errors.YAMLSemanticError(this, 'Missing directives-end indicator line');
  91. } else if (this.directives.length > 0) {
  92. this.contents = this.directives;
  93. this.directives = [];
  94. }
  95. return offset;
  96. }
  97. parseContents(start) {
  98. const {
  99. parseNode,
  100. src
  101. } = this.context;
  102. if (!this.contents) this.contents = [];
  103. let lineStart = start;
  104. while (src[lineStart - 1] === '-') lineStart -= 1;
  105. let offset = _Node.default.endOfWhiteSpace(src, start);
  106. let atLineStart = lineStart === start;
  107. this.valueRange = new _Range.default(offset);
  108. while (!_Node.default.atDocumentBoundary(src, offset, _constants.Char.DOCUMENT_END)) {
  109. switch (src[offset]) {
  110. case '\n':
  111. if (atLineStart) {
  112. const blankLine = new _BlankLine.default();
  113. offset = blankLine.parse({
  114. src
  115. }, offset);
  116. if (offset < src.length) {
  117. this.contents.push(blankLine);
  118. }
  119. } else {
  120. offset += 1;
  121. atLineStart = true;
  122. }
  123. lineStart = offset;
  124. break;
  125. case '#':
  126. {
  127. const comment = new _Comment.default();
  128. offset = comment.parse({
  129. src
  130. }, offset);
  131. this.contents.push(comment);
  132. atLineStart = false;
  133. }
  134. break;
  135. default:
  136. {
  137. const iEnd = _Node.default.endOfIndent(src, offset);
  138. const context = {
  139. atLineStart,
  140. indent: -1,
  141. inFlow: false,
  142. inCollection: false,
  143. lineStart,
  144. parent: this
  145. };
  146. const node = parseNode(context, iEnd);
  147. if (!node) return this.valueRange.end = iEnd; // at next document start
  148. this.contents.push(node);
  149. offset = node.range.end;
  150. atLineStart = false;
  151. const ec = (0, _Collection.grabCollectionEndComments)(node);
  152. if (ec) Array.prototype.push.apply(this.contents, ec);
  153. }
  154. }
  155. offset = Document.startCommentOrEndBlankLine(src, offset);
  156. }
  157. this.valueRange.end = offset;
  158. if (src[offset]) {
  159. this.documentEndMarker = new _Range.default(offset, offset + 3);
  160. offset += 3;
  161. if (src[offset]) {
  162. offset = _Node.default.endOfWhiteSpace(src, offset);
  163. if (src[offset] === '#') {
  164. const comment = new _Comment.default();
  165. offset = comment.parse({
  166. src
  167. }, offset);
  168. this.contents.push(comment);
  169. }
  170. switch (src[offset]) {
  171. case '\n':
  172. offset += 1;
  173. break;
  174. case undefined:
  175. break;
  176. default:
  177. this.error = new _errors.YAMLSyntaxError(this, 'Document end marker line cannot have a non-comment suffix');
  178. }
  179. }
  180. }
  181. return offset;
  182. }
  183. /**
  184. * @param {ParseContext} context
  185. * @param {number} start - Index of first character
  186. * @returns {number} - Index of the character after this
  187. */
  188. parse(context, start) {
  189. context.root = this;
  190. this.context = context;
  191. const {
  192. src
  193. } = context;
  194. let offset = src.charCodeAt(start) === 0xfeff ? start + 1 : start; // skip BOM
  195. offset = this.parseDirectives(offset);
  196. offset = this.parseContents(offset);
  197. return offset;
  198. }
  199. setOrigRanges(cr, offset) {
  200. offset = super.setOrigRanges(cr, offset);
  201. this.directives.forEach(node => {
  202. offset = node.setOrigRanges(cr, offset);
  203. });
  204. if (this.directivesEndMarker) offset = this.directivesEndMarker.setOrigRange(cr, offset);
  205. this.contents.forEach(node => {
  206. offset = node.setOrigRanges(cr, offset);
  207. });
  208. if (this.documentEndMarker) offset = this.documentEndMarker.setOrigRange(cr, offset);
  209. return offset;
  210. }
  211. toString() {
  212. const {
  213. contents,
  214. directives,
  215. value
  216. } = this;
  217. if (value != null) return value;
  218. let str = directives.join('');
  219. if (contents.length > 0) {
  220. if (directives.length > 0 || contents[0].type === _constants.Type.COMMENT) str += '---\n';
  221. str += contents.join('');
  222. }
  223. if (str[str.length - 1] !== '\n') str += '\n';
  224. return str;
  225. }
  226. }
  227. exports.default = Document;