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 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. "use strict";
  2. var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
  3. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  4. Object.defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports.default = void 0;
  8. var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
  9. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  10. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  11. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
  12. var _addComment = _interopRequireDefault(require("./addComment"));
  13. var _Anchors = _interopRequireDefault(require("./Anchors"));
  14. var _constants = require("./constants");
  15. var _errors = require("./errors");
  16. var _listTagNames = _interopRequireDefault(require("./listTagNames"));
  17. var _schema = _interopRequireDefault(require("./schema"));
  18. var _Alias = _interopRequireDefault(require("./schema/Alias"));
  19. var _Collection = _interopRequireWildcard(require("./schema/Collection"));
  20. var _Node = _interopRequireDefault(require("./schema/Node"));
  21. var _Scalar = _interopRequireDefault(require("./schema/Scalar"));
  22. var _toJSON2 = _interopRequireDefault(require("./toJSON"));
  23. var isCollectionItem = function isCollectionItem(node) {
  24. return node && [_constants.Type.MAP_KEY, _constants.Type.MAP_VALUE, _constants.Type.SEQ_ITEM].includes(node.type);
  25. };
  26. var Document =
  27. /*#__PURE__*/
  28. function () {
  29. function Document(options) {
  30. (0, _classCallCheck2.default)(this, Document);
  31. this.anchors = new _Anchors.default(options.anchorPrefix);
  32. this.commentBefore = null;
  33. this.comment = null;
  34. this.contents = null;
  35. this.directivesEndMarker = null;
  36. this.errors = [];
  37. this.options = options;
  38. this.schema = null;
  39. this.tagPrefixes = [];
  40. this.version = null;
  41. this.warnings = [];
  42. }
  43. (0, _createClass2.default)(Document, [{
  44. key: "assertCollectionContents",
  45. value: function assertCollectionContents() {
  46. if (this.contents instanceof _Collection.default) return true;
  47. throw new Error('Expected a YAML collection as document contents');
  48. }
  49. }, {
  50. key: "add",
  51. value: function add(value) {
  52. this.assertCollectionContents();
  53. return this.contents.add(value);
  54. }
  55. }, {
  56. key: "addIn",
  57. value: function addIn(path, value) {
  58. this.assertCollectionContents();
  59. this.contents.addIn(path, value);
  60. }
  61. }, {
  62. key: "delete",
  63. value: function _delete(key) {
  64. this.assertCollectionContents();
  65. return this.contents.delete(key);
  66. }
  67. }, {
  68. key: "deleteIn",
  69. value: function deleteIn(path) {
  70. if ((0, _Collection.isEmptyPath)(path)) {
  71. if (this.contents == null) return false;
  72. this.contents = null;
  73. return true;
  74. }
  75. this.assertCollectionContents();
  76. return this.contents.deleteIn(path);
  77. }
  78. }, {
  79. key: "getDefaults",
  80. value: function getDefaults() {
  81. return Document.defaults[this.version] || Document.defaults[this.options.version] || {};
  82. }
  83. }, {
  84. key: "get",
  85. value: function get(key, keepScalar) {
  86. return this.contents instanceof _Collection.default ? this.contents.get(key, keepScalar) : undefined;
  87. }
  88. }, {
  89. key: "getIn",
  90. value: function getIn(path, keepScalar) {
  91. if ((0, _Collection.isEmptyPath)(path)) return !keepScalar && this.contents instanceof _Scalar.default ? this.contents.value : this.contents;
  92. return this.contents instanceof _Collection.default ? this.contents.getIn(path, keepScalar) : undefined;
  93. }
  94. }, {
  95. key: "has",
  96. value: function has(key) {
  97. return this.contents instanceof _Collection.default ? this.contents.has(key) : false;
  98. }
  99. }, {
  100. key: "hasIn",
  101. value: function hasIn(path) {
  102. if ((0, _Collection.isEmptyPath)(path)) return this.contents !== undefined;
  103. return this.contents instanceof _Collection.default ? this.contents.hasIn(path) : false;
  104. }
  105. }, {
  106. key: "set",
  107. value: function set(key, value) {
  108. this.assertCollectionContents();
  109. this.contents.set(key, value);
  110. }
  111. }, {
  112. key: "setIn",
  113. value: function setIn(path, value) {
  114. if ((0, _Collection.isEmptyPath)(path)) this.contents = value;else {
  115. this.assertCollectionContents();
  116. this.contents.setIn(path, value);
  117. }
  118. }
  119. }, {
  120. key: "setSchema",
  121. value: function setSchema(id, customTags) {
  122. if (!id && !customTags && this.schema) return;
  123. if (typeof id === 'number') id = id.toFixed(1);
  124. if (id === '1.0' || id === '1.1' || id === '1.2') {
  125. if (this.version) this.version = id;else this.options.version = id;
  126. delete this.options.schema;
  127. } else if (id && typeof id === 'string') {
  128. this.options.schema = id;
  129. }
  130. if (Array.isArray(customTags)) this.options.customTags = customTags;
  131. var opt = Object.assign({}, this.getDefaults(), this.options);
  132. this.schema = new _schema.default(opt);
  133. }
  134. }, {
  135. key: "parse",
  136. value: function parse(node, prevDoc) {
  137. if (this.options.keepCstNodes) this.cstNode = node;
  138. if (this.options.keepNodeTypes) this.type = 'DOCUMENT';
  139. var _node$directives = node.directives,
  140. directives = _node$directives === void 0 ? [] : _node$directives,
  141. _node$contents = node.contents,
  142. contents = _node$contents === void 0 ? [] : _node$contents,
  143. directivesEndMarker = node.directivesEndMarker,
  144. error = node.error,
  145. valueRange = node.valueRange;
  146. if (error) {
  147. if (!error.source) error.source = this;
  148. this.errors.push(error);
  149. }
  150. this.parseDirectives(directives, prevDoc);
  151. if (directivesEndMarker) this.directivesEndMarker = true;
  152. this.range = valueRange ? [valueRange.start, valueRange.end] : null;
  153. this.setSchema();
  154. this.anchors._cstAliases = [];
  155. this.parseContents(contents);
  156. this.anchors.resolveNodes();
  157. if (this.options.prettyErrors) {
  158. var _iteratorNormalCompletion = true;
  159. var _didIteratorError = false;
  160. var _iteratorError = undefined;
  161. try {
  162. for (var _iterator = this.errors[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  163. var _error = _step.value;
  164. if (_error instanceof _errors.YAMLError) _error.makePretty();
  165. }
  166. } catch (err) {
  167. _didIteratorError = true;
  168. _iteratorError = err;
  169. } finally {
  170. try {
  171. if (!_iteratorNormalCompletion && _iterator.return != null) {
  172. _iterator.return();
  173. }
  174. } finally {
  175. if (_didIteratorError) {
  176. throw _iteratorError;
  177. }
  178. }
  179. }
  180. var _iteratorNormalCompletion2 = true;
  181. var _didIteratorError2 = false;
  182. var _iteratorError2 = undefined;
  183. try {
  184. for (var _iterator2 = this.warnings[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
  185. var warn = _step2.value;
  186. if (warn instanceof _errors.YAMLError) warn.makePretty();
  187. }
  188. } catch (err) {
  189. _didIteratorError2 = true;
  190. _iteratorError2 = err;
  191. } finally {
  192. try {
  193. if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
  194. _iterator2.return();
  195. }
  196. } finally {
  197. if (_didIteratorError2) {
  198. throw _iteratorError2;
  199. }
  200. }
  201. }
  202. }
  203. return this;
  204. }
  205. }, {
  206. key: "parseDirectives",
  207. value: function parseDirectives(directives, prevDoc) {
  208. var _this = this;
  209. var directiveComments = [];
  210. var hasDirectives = false;
  211. directives.forEach(function (directive) {
  212. var comment = directive.comment,
  213. name = directive.name;
  214. switch (name) {
  215. case 'TAG':
  216. _this.resolveTagDirective(directive);
  217. hasDirectives = true;
  218. break;
  219. case 'YAML':
  220. case 'YAML:1.0':
  221. _this.resolveYamlDirective(directive);
  222. hasDirectives = true;
  223. break;
  224. default:
  225. if (name) {
  226. var msg = "YAML only supports %TAG and %YAML directives, and not %".concat(name);
  227. _this.warnings.push(new _errors.YAMLWarning(directive, msg));
  228. }
  229. }
  230. if (comment) directiveComments.push(comment);
  231. });
  232. if (prevDoc && !hasDirectives && '1.1' === (this.version || prevDoc.version || this.options.version)) {
  233. var copyTagPrefix = function copyTagPrefix(_ref) {
  234. var handle = _ref.handle,
  235. prefix = _ref.prefix;
  236. return {
  237. handle: handle,
  238. prefix: prefix
  239. };
  240. };
  241. this.tagPrefixes = prevDoc.tagPrefixes.map(copyTagPrefix);
  242. this.version = prevDoc.version;
  243. }
  244. this.commentBefore = directiveComments.join('\n') || null;
  245. }
  246. }, {
  247. key: "parseContents",
  248. value: function parseContents(contents) {
  249. var _this2 = this;
  250. var comments = {
  251. before: [],
  252. after: []
  253. };
  254. var contentNodes = [];
  255. var spaceBefore = false;
  256. contents.forEach(function (node) {
  257. if (node.valueRange) {
  258. if (contentNodes.length === 1) {
  259. var msg = 'Document is not valid YAML (bad indentation?)';
  260. _this2.errors.push(new _errors.YAMLSyntaxError(node, msg));
  261. }
  262. var res = _this2.resolveNode(node);
  263. if (spaceBefore) {
  264. res.spaceBefore = true;
  265. spaceBefore = false;
  266. }
  267. contentNodes.push(res);
  268. } else if (node.comment !== null) {
  269. var cc = contentNodes.length === 0 ? comments.before : comments.after;
  270. cc.push(node.comment);
  271. } else if (node.type === _constants.Type.BLANK_LINE) {
  272. spaceBefore = true;
  273. if (contentNodes.length === 0 && comments.before.length > 0 && !_this2.commentBefore) {
  274. // space-separated comments at start are parsed as document comments
  275. _this2.commentBefore = comments.before.join('\n');
  276. comments.before = [];
  277. }
  278. }
  279. });
  280. switch (contentNodes.length) {
  281. case 0:
  282. this.contents = null;
  283. comments.after = comments.before;
  284. break;
  285. case 1:
  286. this.contents = contentNodes[0];
  287. if (this.contents) {
  288. var cb = comments.before.join('\n') || null;
  289. if (cb) {
  290. var cbNode = this.contents instanceof _Collection.default && this.contents.items[0] ? this.contents.items[0] : this.contents;
  291. cbNode.commentBefore = cbNode.commentBefore ? "".concat(cb, "\n").concat(cbNode.commentBefore) : cb;
  292. }
  293. } else {
  294. comments.after = comments.before.concat(comments.after);
  295. }
  296. break;
  297. default:
  298. this.contents = contentNodes;
  299. if (this.contents[0]) {
  300. this.contents[0].commentBefore = comments.before.join('\n') || null;
  301. } else {
  302. comments.after = comments.before.concat(comments.after);
  303. }
  304. }
  305. this.comment = comments.after.join('\n') || null;
  306. }
  307. }, {
  308. key: "resolveTagDirective",
  309. value: function resolveTagDirective(directive) {
  310. var _directive$parameters = (0, _slicedToArray2.default)(directive.parameters, 2),
  311. handle = _directive$parameters[0],
  312. prefix = _directive$parameters[1];
  313. if (handle && prefix) {
  314. if (this.tagPrefixes.every(function (p) {
  315. return p.handle !== handle;
  316. })) {
  317. this.tagPrefixes.push({
  318. handle: handle,
  319. prefix: prefix
  320. });
  321. } else {
  322. var msg = 'The %TAG directive must only be given at most once per handle in the same document.';
  323. this.errors.push(new _errors.YAMLSemanticError(directive, msg));
  324. }
  325. } else {
  326. var _msg = 'Insufficient parameters given for %TAG directive';
  327. this.errors.push(new _errors.YAMLSemanticError(directive, _msg));
  328. }
  329. }
  330. }, {
  331. key: "resolveYamlDirective",
  332. value: function resolveYamlDirective(directive) {
  333. var _directive$parameters2 = (0, _slicedToArray2.default)(directive.parameters, 1),
  334. version = _directive$parameters2[0];
  335. if (directive.name === 'YAML:1.0') version = '1.0';
  336. if (this.version) {
  337. var msg = 'The %YAML directive must only be given at most once per document.';
  338. this.errors.push(new _errors.YAMLSemanticError(directive, msg));
  339. }
  340. if (!version) {
  341. var _msg2 = 'Insufficient parameters given for %YAML directive';
  342. this.errors.push(new _errors.YAMLSemanticError(directive, _msg2));
  343. } else {
  344. if (!Document.defaults[version]) {
  345. var v0 = this.version || this.options.version;
  346. var _msg3 = "Document will be parsed as YAML ".concat(v0, " rather than YAML ").concat(version);
  347. this.warnings.push(new _errors.YAMLWarning(directive, _msg3));
  348. }
  349. this.version = version;
  350. }
  351. }
  352. }, {
  353. key: "resolveTagName",
  354. value: function resolveTagName(node) {
  355. var tag = node.tag,
  356. type = node.type;
  357. var nonSpecific = false;
  358. if (tag) {
  359. var handle = tag.handle,
  360. suffix = tag.suffix,
  361. verbatim = tag.verbatim;
  362. if (verbatim) {
  363. if (verbatim !== '!' && verbatim !== '!!') return verbatim;
  364. var msg = "Verbatim tags aren't resolved, so ".concat(verbatim, " is invalid.");
  365. this.errors.push(new _errors.YAMLSemanticError(node, msg));
  366. } else if (handle === '!' && !suffix) {
  367. nonSpecific = true;
  368. } else {
  369. var prefix = this.tagPrefixes.find(function (p) {
  370. return p.handle === handle;
  371. });
  372. if (!prefix) {
  373. var dtp = this.getDefaults().tagPrefixes;
  374. if (dtp) prefix = dtp.find(function (p) {
  375. return p.handle === handle;
  376. });
  377. }
  378. if (prefix) {
  379. if (suffix) {
  380. if (handle === '!' && (this.version || this.options.version) === '1.0') {
  381. if (suffix[0] === '^') return suffix;
  382. if (/[:/]/.test(suffix)) {
  383. // word/foo -> tag:word.yaml.org,2002:foo
  384. var vocab = suffix.match(/^([a-z0-9-]+)\/(.*)/i);
  385. return vocab ? "tag:".concat(vocab[1], ".yaml.org,2002:").concat(vocab[2]) : "tag:".concat(suffix);
  386. }
  387. }
  388. return prefix.prefix + decodeURIComponent(suffix);
  389. }
  390. this.errors.push(new _errors.YAMLSemanticError(node, "The ".concat(handle, " tag has no suffix.")));
  391. } else {
  392. var _msg4 = "The ".concat(handle, " tag handle is non-default and was not declared.");
  393. this.errors.push(new _errors.YAMLSemanticError(node, _msg4));
  394. }
  395. }
  396. }
  397. switch (type) {
  398. case _constants.Type.BLOCK_FOLDED:
  399. case _constants.Type.BLOCK_LITERAL:
  400. case _constants.Type.QUOTE_DOUBLE:
  401. case _constants.Type.QUOTE_SINGLE:
  402. return _schema.default.defaultTags.STR;
  403. case _constants.Type.FLOW_MAP:
  404. case _constants.Type.MAP:
  405. return _schema.default.defaultTags.MAP;
  406. case _constants.Type.FLOW_SEQ:
  407. case _constants.Type.SEQ:
  408. return _schema.default.defaultTags.SEQ;
  409. case _constants.Type.PLAIN:
  410. return nonSpecific ? _schema.default.defaultTags.STR : null;
  411. default:
  412. return null;
  413. }
  414. }
  415. }, {
  416. key: "resolveNode",
  417. value: function resolveNode(node) {
  418. if (!node) return null;
  419. var anchors = this.anchors,
  420. errors = this.errors,
  421. schema = this.schema;
  422. var hasAnchor = false;
  423. var hasTag = false;
  424. var comments = {
  425. before: [],
  426. after: []
  427. };
  428. var props = isCollectionItem(node.context.parent) ? node.context.parent.props.concat(node.props) : node.props;
  429. var _iteratorNormalCompletion3 = true;
  430. var _didIteratorError3 = false;
  431. var _iteratorError3 = undefined;
  432. try {
  433. for (var _iterator3 = props[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
  434. var _step3$value = _step3.value,
  435. start = _step3$value.start,
  436. end = _step3$value.end;
  437. switch (node.context.src[start]) {
  438. case _constants.Char.COMMENT:
  439. {
  440. if (!node.commentHasRequiredWhitespace(start)) {
  441. var _msg7 = 'Comments must be separated from other tokens by white space characters';
  442. errors.push(new _errors.YAMLSemanticError(node, _msg7));
  443. }
  444. var c = node.context.src.slice(start + 1, end);
  445. var header = node.header,
  446. valueRange = node.valueRange;
  447. if (valueRange && (start > valueRange.start || header && start > header.start)) {
  448. comments.after.push(c);
  449. } else {
  450. comments.before.push(c);
  451. }
  452. }
  453. break;
  454. case _constants.Char.ANCHOR:
  455. if (hasAnchor) {
  456. var _msg8 = 'A node can have at most one anchor';
  457. errors.push(new _errors.YAMLSemanticError(node, _msg8));
  458. }
  459. hasAnchor = true;
  460. break;
  461. case _constants.Char.TAG:
  462. if (hasTag) {
  463. var _msg9 = 'A node can have at most one tag';
  464. errors.push(new _errors.YAMLSemanticError(node, _msg9));
  465. }
  466. hasTag = true;
  467. break;
  468. }
  469. }
  470. } catch (err) {
  471. _didIteratorError3 = true;
  472. _iteratorError3 = err;
  473. } finally {
  474. try {
  475. if (!_iteratorNormalCompletion3 && _iterator3.return != null) {
  476. _iterator3.return();
  477. }
  478. } finally {
  479. if (_didIteratorError3) {
  480. throw _iteratorError3;
  481. }
  482. }
  483. }
  484. if (hasAnchor) {
  485. var name = node.anchor;
  486. var prev = anchors.getNode(name); // At this point, aliases for any preceding node with the same anchor
  487. // name have already been resolved, so it may safely be renamed.
  488. if (prev) anchors.map[anchors.newName(name)] = prev; // During parsing, we need to store the CST node in anchors.map as
  489. // anchors need to be available during resolution to allow for
  490. // circular references.
  491. anchors.map[name] = node;
  492. }
  493. var res;
  494. if (node.type === _constants.Type.ALIAS) {
  495. if (hasAnchor || hasTag) {
  496. var msg = 'An alias node must not specify any properties';
  497. errors.push(new _errors.YAMLSemanticError(node, msg));
  498. }
  499. var _name = node.rawValue;
  500. var src = anchors.getNode(_name);
  501. if (!src) {
  502. var _msg5 = "Aliased anchor not found: ".concat(_name);
  503. errors.push(new _errors.YAMLReferenceError(node, _msg5));
  504. return null;
  505. } // Lazy resolution for circular references
  506. res = new _Alias.default(src);
  507. anchors._cstAliases.push(res);
  508. } else {
  509. var tagName = this.resolveTagName(node);
  510. if (tagName) {
  511. res = schema.resolveNodeWithFallback(this, node, tagName);
  512. } else {
  513. if (node.type !== _constants.Type.PLAIN) {
  514. var _msg6 = "Failed to resolve ".concat(node.type, " node here");
  515. errors.push(new _errors.YAMLSyntaxError(node, _msg6));
  516. return null;
  517. }
  518. try {
  519. res = schema.resolveScalar(node.strValue || '');
  520. } catch (error) {
  521. if (!error.source) error.source = node;
  522. errors.push(error);
  523. return null;
  524. }
  525. }
  526. }
  527. if (res) {
  528. res.range = [node.range.start, node.range.end];
  529. if (this.options.keepCstNodes) res.cstNode = node;
  530. if (this.options.keepNodeTypes) res.type = node.type;
  531. var cb = comments.before.join('\n');
  532. if (cb) {
  533. res.commentBefore = res.commentBefore ? "".concat(res.commentBefore, "\n").concat(cb) : cb;
  534. }
  535. var ca = comments.after.join('\n');
  536. if (ca) res.comment = res.comment ? "".concat(res.comment, "\n").concat(ca) : ca;
  537. }
  538. return node.resolved = res;
  539. }
  540. }, {
  541. key: "listNonDefaultTags",
  542. value: function listNonDefaultTags() {
  543. return (0, _listTagNames.default)(this.contents).filter(function (t) {
  544. return t.indexOf(_schema.default.defaultPrefix) !== 0;
  545. });
  546. }
  547. }, {
  548. key: "setTagPrefix",
  549. value: function setTagPrefix(handle, prefix) {
  550. if (handle[0] !== '!' || handle[handle.length - 1] !== '!') throw new Error('Handle must start and end with !');
  551. if (prefix) {
  552. var prev = this.tagPrefixes.find(function (p) {
  553. return p.handle === handle;
  554. });
  555. if (prev) prev.prefix = prefix;else this.tagPrefixes.push({
  556. handle: handle,
  557. prefix: prefix
  558. });
  559. } else {
  560. this.tagPrefixes = this.tagPrefixes.filter(function (p) {
  561. return p.handle !== handle;
  562. });
  563. }
  564. }
  565. }, {
  566. key: "stringifyTag",
  567. value: function stringifyTag(tag) {
  568. if ((this.version || this.options.version) === '1.0') {
  569. var priv = tag.match(/^tag:private\.yaml\.org,2002:([^:/]+)$/);
  570. if (priv) return '!' + priv[1];
  571. var vocab = tag.match(/^tag:([a-zA-Z0-9-]+)\.yaml\.org,2002:(.*)/);
  572. return vocab ? "!".concat(vocab[1], "/").concat(vocab[2]) : "!".concat(tag.replace(/^tag:/, ''));
  573. } else {
  574. var p = this.tagPrefixes.find(function (p) {
  575. return tag.indexOf(p.prefix) === 0;
  576. });
  577. if (!p) {
  578. var dtp = this.getDefaults().tagPrefixes;
  579. p = dtp && dtp.find(function (p) {
  580. return tag.indexOf(p.prefix) === 0;
  581. });
  582. }
  583. if (!p) return tag[0] === '!' ? tag : "!<".concat(tag, ">");
  584. var suffix = tag.substr(p.prefix.length).replace(/[!,[\]{}]/g, function (ch) {
  585. return {
  586. '!': '%21',
  587. ',': '%2C',
  588. '[': '%5B',
  589. ']': '%5D',
  590. '{': '%7B',
  591. '}': '%7D'
  592. }[ch];
  593. });
  594. return p.handle + suffix;
  595. }
  596. }
  597. }, {
  598. key: "toJSON",
  599. value: function toJSON(arg) {
  600. var _this3 = this;
  601. var _this$options = this.options,
  602. keepBlobsInJSON = _this$options.keepBlobsInJSON,
  603. mapAsMap = _this$options.mapAsMap,
  604. maxAliasCount = _this$options.maxAliasCount;
  605. var keep = keepBlobsInJSON && (typeof arg !== 'string' || !(this.contents instanceof _Scalar.default));
  606. var ctx = {
  607. doc: this,
  608. keep: keep,
  609. mapAsMap: keep && !!mapAsMap,
  610. maxAliasCount: maxAliasCount
  611. };
  612. var anchorNames = Object.keys(this.anchors.map);
  613. if (anchorNames.length > 0) ctx.anchors = anchorNames.map(function (name) {
  614. return {
  615. alias: [],
  616. aliasCount: 0,
  617. count: 1,
  618. node: _this3.anchors.map[name]
  619. };
  620. });
  621. return (0, _toJSON2.default)(this.contents, arg, ctx);
  622. }
  623. }, {
  624. key: "toString",
  625. value: function toString() {
  626. if (this.errors.length > 0) throw new Error('Document with errors cannot be stringified');
  627. this.setSchema();
  628. var lines = [];
  629. var hasDirectives = false;
  630. if (this.version) {
  631. var vd = '%YAML 1.2';
  632. if (this.schema.name === 'yaml-1.1') {
  633. if (this.version === '1.0') vd = '%YAML:1.0';else if (this.version === '1.1') vd = '%YAML 1.1';
  634. }
  635. lines.push(vd);
  636. hasDirectives = true;
  637. }
  638. var tagNames = this.listNonDefaultTags();
  639. this.tagPrefixes.forEach(function (_ref2) {
  640. var handle = _ref2.handle,
  641. prefix = _ref2.prefix;
  642. if (tagNames.some(function (t) {
  643. return t.indexOf(prefix) === 0;
  644. })) {
  645. lines.push("%TAG ".concat(handle, " ").concat(prefix));
  646. hasDirectives = true;
  647. }
  648. });
  649. if (hasDirectives || this.directivesEndMarker) lines.push('---');
  650. if (this.commentBefore) {
  651. if (hasDirectives || !this.directivesEndMarker) lines.unshift('');
  652. lines.unshift(this.commentBefore.replace(/^/gm, '#'));
  653. }
  654. var ctx = {
  655. anchors: {},
  656. doc: this,
  657. indent: ''
  658. };
  659. var chompKeep = false;
  660. var contentComment = null;
  661. if (this.contents) {
  662. if (this.contents instanceof _Node.default) {
  663. if (this.contents.spaceBefore && (hasDirectives || this.directivesEndMarker)) lines.push('');
  664. if (this.contents.commentBefore) lines.push(this.contents.commentBefore.replace(/^/gm, '#')); // top-level block scalars need to be indented if followed by a comment
  665. ctx.forceBlockIndent = !!this.comment;
  666. contentComment = this.contents.comment;
  667. }
  668. var onChompKeep = contentComment ? null : function () {
  669. return chompKeep = true;
  670. };
  671. var body = this.schema.stringify(this.contents, ctx, function () {
  672. return contentComment = null;
  673. }, onChompKeep);
  674. lines.push((0, _addComment.default)(body, '', contentComment));
  675. } else if (this.contents !== undefined) {
  676. lines.push(this.schema.stringify(this.contents, ctx));
  677. }
  678. if (this.comment) {
  679. if ((!chompKeep || contentComment) && lines[lines.length - 1] !== '') lines.push('');
  680. lines.push(this.comment.replace(/^/gm, '#'));
  681. }
  682. return lines.join('\n') + '\n';
  683. }
  684. }]);
  685. return Document;
  686. }();
  687. exports.default = Document;
  688. (0, _defineProperty2.default)(Document, "defaults", {
  689. '1.0': {
  690. schema: 'yaml-1.1',
  691. merge: true,
  692. tagPrefixes: [{
  693. handle: '!',
  694. prefix: _schema.default.defaultPrefix
  695. }, {
  696. handle: '!!',
  697. prefix: 'tag:private.yaml.org,2002:'
  698. }]
  699. },
  700. '1.1': {
  701. schema: 'yaml-1.1',
  702. merge: true,
  703. tagPrefixes: [{
  704. handle: '!',
  705. prefix: '!'
  706. }, {
  707. handle: '!!',
  708. prefix: _schema.default.defaultPrefix
  709. }]
  710. },
  711. '1.2': {
  712. schema: 'core',
  713. merge: false,
  714. tagPrefixes: [{
  715. handle: '!',
  716. prefix: '!'
  717. }, {
  718. handle: '!!',
  719. prefix: _schema.default.defaultPrefix
  720. }]
  721. }
  722. });