js-yaml.js 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880
  1. /*! js-yaml 4.1.1 https://github.com/nodeca/js-yaml @license MIT */
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  4. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  5. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.jsyaml = {}));
  6. })(this, (function (exports) { 'use strict';
  7. function isNothing(subject) {
  8. return (typeof subject === 'undefined') || (subject === null);
  9. }
  10. function isObject(subject) {
  11. return (typeof subject === 'object') && (subject !== null);
  12. }
  13. function toArray(sequence) {
  14. if (Array.isArray(sequence)) return sequence;
  15. else if (isNothing(sequence)) return [];
  16. return [ sequence ];
  17. }
  18. function extend(target, source) {
  19. var index, length, key, sourceKeys;
  20. if (source) {
  21. sourceKeys = Object.keys(source);
  22. for (index = 0, length = sourceKeys.length; index < length; index += 1) {
  23. key = sourceKeys[index];
  24. target[key] = source[key];
  25. }
  26. }
  27. return target;
  28. }
  29. function repeat(string, count) {
  30. var result = '', cycle;
  31. for (cycle = 0; cycle < count; cycle += 1) {
  32. result += string;
  33. }
  34. return result;
  35. }
  36. function isNegativeZero(number) {
  37. return (number === 0) && (Number.NEGATIVE_INFINITY === 1 / number);
  38. }
  39. var isNothing_1 = isNothing;
  40. var isObject_1 = isObject;
  41. var toArray_1 = toArray;
  42. var repeat_1 = repeat;
  43. var isNegativeZero_1 = isNegativeZero;
  44. var extend_1 = extend;
  45. var common = {
  46. isNothing: isNothing_1,
  47. isObject: isObject_1,
  48. toArray: toArray_1,
  49. repeat: repeat_1,
  50. isNegativeZero: isNegativeZero_1,
  51. extend: extend_1
  52. };
  53. // YAML error class. http://stackoverflow.com/questions/8458984
  54. function formatError(exception, compact) {
  55. var where = '', message = exception.reason || '(unknown reason)';
  56. if (!exception.mark) return message;
  57. if (exception.mark.name) {
  58. where += 'in "' + exception.mark.name + '" ';
  59. }
  60. where += '(' + (exception.mark.line + 1) + ':' + (exception.mark.column + 1) + ')';
  61. if (!compact && exception.mark.snippet) {
  62. where += '\n\n' + exception.mark.snippet;
  63. }
  64. return message + ' ' + where;
  65. }
  66. function YAMLException$1(reason, mark) {
  67. // Super constructor
  68. Error.call(this);
  69. this.name = 'YAMLException';
  70. this.reason = reason;
  71. this.mark = mark;
  72. this.message = formatError(this, false);
  73. // Include stack trace in error object
  74. if (Error.captureStackTrace) {
  75. // Chrome and NodeJS
  76. Error.captureStackTrace(this, this.constructor);
  77. } else {
  78. // FF, IE 10+ and Safari 6+. Fallback for others
  79. this.stack = (new Error()).stack || '';
  80. }
  81. }
  82. // Inherit from Error
  83. YAMLException$1.prototype = Object.create(Error.prototype);
  84. YAMLException$1.prototype.constructor = YAMLException$1;
  85. YAMLException$1.prototype.toString = function toString(compact) {
  86. return this.name + ': ' + formatError(this, compact);
  87. };
  88. var exception = YAMLException$1;
  89. // get snippet for a single line, respecting maxLength
  90. function getLine(buffer, lineStart, lineEnd, position, maxLineLength) {
  91. var head = '';
  92. var tail = '';
  93. var maxHalfLength = Math.floor(maxLineLength / 2) - 1;
  94. if (position - lineStart > maxHalfLength) {
  95. head = ' ... ';
  96. lineStart = position - maxHalfLength + head.length;
  97. }
  98. if (lineEnd - position > maxHalfLength) {
  99. tail = ' ...';
  100. lineEnd = position + maxHalfLength - tail.length;
  101. }
  102. return {
  103. str: head + buffer.slice(lineStart, lineEnd).replace(/\t/g, '→') + tail,
  104. pos: position - lineStart + head.length // relative position
  105. };
  106. }
  107. function padStart(string, max) {
  108. return common.repeat(' ', max - string.length) + string;
  109. }
  110. function makeSnippet(mark, options) {
  111. options = Object.create(options || null);
  112. if (!mark.buffer) return null;
  113. if (!options.maxLength) options.maxLength = 79;
  114. if (typeof options.indent !== 'number') options.indent = 1;
  115. if (typeof options.linesBefore !== 'number') options.linesBefore = 3;
  116. if (typeof options.linesAfter !== 'number') options.linesAfter = 2;
  117. var re = /\r?\n|\r|\0/g;
  118. var lineStarts = [ 0 ];
  119. var lineEnds = [];
  120. var match;
  121. var foundLineNo = -1;
  122. while ((match = re.exec(mark.buffer))) {
  123. lineEnds.push(match.index);
  124. lineStarts.push(match.index + match[0].length);
  125. if (mark.position <= match.index && foundLineNo < 0) {
  126. foundLineNo = lineStarts.length - 2;
  127. }
  128. }
  129. if (foundLineNo < 0) foundLineNo = lineStarts.length - 1;
  130. var result = '', i, line;
  131. var lineNoLength = Math.min(mark.line + options.linesAfter, lineEnds.length).toString().length;
  132. var maxLineLength = options.maxLength - (options.indent + lineNoLength + 3);
  133. for (i = 1; i <= options.linesBefore; i++) {
  134. if (foundLineNo - i < 0) break;
  135. line = getLine(
  136. mark.buffer,
  137. lineStarts[foundLineNo - i],
  138. lineEnds[foundLineNo - i],
  139. mark.position - (lineStarts[foundLineNo] - lineStarts[foundLineNo - i]),
  140. maxLineLength
  141. );
  142. result = common.repeat(' ', options.indent) + padStart((mark.line - i + 1).toString(), lineNoLength) +
  143. ' | ' + line.str + '\n' + result;
  144. }
  145. line = getLine(mark.buffer, lineStarts[foundLineNo], lineEnds[foundLineNo], mark.position, maxLineLength);
  146. result += common.repeat(' ', options.indent) + padStart((mark.line + 1).toString(), lineNoLength) +
  147. ' | ' + line.str + '\n';
  148. result += common.repeat('-', options.indent + lineNoLength + 3 + line.pos) + '^' + '\n';
  149. for (i = 1; i <= options.linesAfter; i++) {
  150. if (foundLineNo + i >= lineEnds.length) break;
  151. line = getLine(
  152. mark.buffer,
  153. lineStarts[foundLineNo + i],
  154. lineEnds[foundLineNo + i],
  155. mark.position - (lineStarts[foundLineNo] - lineStarts[foundLineNo + i]),
  156. maxLineLength
  157. );
  158. result += common.repeat(' ', options.indent) + padStart((mark.line + i + 1).toString(), lineNoLength) +
  159. ' | ' + line.str + '\n';
  160. }
  161. return result.replace(/\n$/, '');
  162. }
  163. var snippet = makeSnippet;
  164. var TYPE_CONSTRUCTOR_OPTIONS = [
  165. 'kind',
  166. 'multi',
  167. 'resolve',
  168. 'construct',
  169. 'instanceOf',
  170. 'predicate',
  171. 'represent',
  172. 'representName',
  173. 'defaultStyle',
  174. 'styleAliases'
  175. ];
  176. var YAML_NODE_KINDS = [
  177. 'scalar',
  178. 'sequence',
  179. 'mapping'
  180. ];
  181. function compileStyleAliases(map) {
  182. var result = {};
  183. if (map !== null) {
  184. Object.keys(map).forEach(function (style) {
  185. map[style].forEach(function (alias) {
  186. result[String(alias)] = style;
  187. });
  188. });
  189. }
  190. return result;
  191. }
  192. function Type$1(tag, options) {
  193. options = options || {};
  194. Object.keys(options).forEach(function (name) {
  195. if (TYPE_CONSTRUCTOR_OPTIONS.indexOf(name) === -1) {
  196. throw new exception('Unknown option "' + name + '" is met in definition of "' + tag + '" YAML type.');
  197. }
  198. });
  199. // TODO: Add tag format check.
  200. this.options = options; // keep original options in case user wants to extend this type later
  201. this.tag = tag;
  202. this.kind = options['kind'] || null;
  203. this.resolve = options['resolve'] || function () { return true; };
  204. this.construct = options['construct'] || function (data) { return data; };
  205. this.instanceOf = options['instanceOf'] || null;
  206. this.predicate = options['predicate'] || null;
  207. this.represent = options['represent'] || null;
  208. this.representName = options['representName'] || null;
  209. this.defaultStyle = options['defaultStyle'] || null;
  210. this.multi = options['multi'] || false;
  211. this.styleAliases = compileStyleAliases(options['styleAliases'] || null);
  212. if (YAML_NODE_KINDS.indexOf(this.kind) === -1) {
  213. throw new exception('Unknown kind "' + this.kind + '" is specified for "' + tag + '" YAML type.');
  214. }
  215. }
  216. var type = Type$1;
  217. /*eslint-disable max-len*/
  218. function compileList(schema, name) {
  219. var result = [];
  220. schema[name].forEach(function (currentType) {
  221. var newIndex = result.length;
  222. result.forEach(function (previousType, previousIndex) {
  223. if (previousType.tag === currentType.tag &&
  224. previousType.kind === currentType.kind &&
  225. previousType.multi === currentType.multi) {
  226. newIndex = previousIndex;
  227. }
  228. });
  229. result[newIndex] = currentType;
  230. });
  231. return result;
  232. }
  233. function compileMap(/* lists... */) {
  234. var result = {
  235. scalar: {},
  236. sequence: {},
  237. mapping: {},
  238. fallback: {},
  239. multi: {
  240. scalar: [],
  241. sequence: [],
  242. mapping: [],
  243. fallback: []
  244. }
  245. }, index, length;
  246. function collectType(type) {
  247. if (type.multi) {
  248. result.multi[type.kind].push(type);
  249. result.multi['fallback'].push(type);
  250. } else {
  251. result[type.kind][type.tag] = result['fallback'][type.tag] = type;
  252. }
  253. }
  254. for (index = 0, length = arguments.length; index < length; index += 1) {
  255. arguments[index].forEach(collectType);
  256. }
  257. return result;
  258. }
  259. function Schema$1(definition) {
  260. return this.extend(definition);
  261. }
  262. Schema$1.prototype.extend = function extend(definition) {
  263. var implicit = [];
  264. var explicit = [];
  265. if (definition instanceof type) {
  266. // Schema.extend(type)
  267. explicit.push(definition);
  268. } else if (Array.isArray(definition)) {
  269. // Schema.extend([ type1, type2, ... ])
  270. explicit = explicit.concat(definition);
  271. } else if (definition && (Array.isArray(definition.implicit) || Array.isArray(definition.explicit))) {
  272. // Schema.extend({ explicit: [ type1, type2, ... ], implicit: [ type1, type2, ... ] })
  273. if (definition.implicit) implicit = implicit.concat(definition.implicit);
  274. if (definition.explicit) explicit = explicit.concat(definition.explicit);
  275. } else {
  276. throw new exception('Schema.extend argument should be a Type, [ Type ], ' +
  277. 'or a schema definition ({ implicit: [...], explicit: [...] })');
  278. }
  279. implicit.forEach(function (type$1) {
  280. if (!(type$1 instanceof type)) {
  281. throw new exception('Specified list of YAML types (or a single Type object) contains a non-Type object.');
  282. }
  283. if (type$1.loadKind && type$1.loadKind !== 'scalar') {
  284. throw new exception('There is a non-scalar type in the implicit list of a schema. Implicit resolving of such types is not supported.');
  285. }
  286. if (type$1.multi) {
  287. throw new exception('There is a multi type in the implicit list of a schema. Multi tags can only be listed as explicit.');
  288. }
  289. });
  290. explicit.forEach(function (type$1) {
  291. if (!(type$1 instanceof type)) {
  292. throw new exception('Specified list of YAML types (or a single Type object) contains a non-Type object.');
  293. }
  294. });
  295. var result = Object.create(Schema$1.prototype);
  296. result.implicit = (this.implicit || []).concat(implicit);
  297. result.explicit = (this.explicit || []).concat(explicit);
  298. result.compiledImplicit = compileList(result, 'implicit');
  299. result.compiledExplicit = compileList(result, 'explicit');
  300. result.compiledTypeMap = compileMap(result.compiledImplicit, result.compiledExplicit);
  301. return result;
  302. };
  303. var schema = Schema$1;
  304. var str = new type('tag:yaml.org,2002:str', {
  305. kind: 'scalar',
  306. construct: function (data) { return data !== null ? data : ''; }
  307. });
  308. var seq = new type('tag:yaml.org,2002:seq', {
  309. kind: 'sequence',
  310. construct: function (data) { return data !== null ? data : []; }
  311. });
  312. var map = new type('tag:yaml.org,2002:map', {
  313. kind: 'mapping',
  314. construct: function (data) { return data !== null ? data : {}; }
  315. });
  316. var failsafe = new schema({
  317. explicit: [
  318. str,
  319. seq,
  320. map
  321. ]
  322. });
  323. function resolveYamlNull(data) {
  324. if (data === null) return true;
  325. var max = data.length;
  326. return (max === 1 && data === '~') ||
  327. (max === 4 && (data === 'null' || data === 'Null' || data === 'NULL'));
  328. }
  329. function constructYamlNull() {
  330. return null;
  331. }
  332. function isNull(object) {
  333. return object === null;
  334. }
  335. var _null = new type('tag:yaml.org,2002:null', {
  336. kind: 'scalar',
  337. resolve: resolveYamlNull,
  338. construct: constructYamlNull,
  339. predicate: isNull,
  340. represent: {
  341. canonical: function () { return '~'; },
  342. lowercase: function () { return 'null'; },
  343. uppercase: function () { return 'NULL'; },
  344. camelcase: function () { return 'Null'; },
  345. empty: function () { return ''; }
  346. },
  347. defaultStyle: 'lowercase'
  348. });
  349. function resolveYamlBoolean(data) {
  350. if (data === null) return false;
  351. var max = data.length;
  352. return (max === 4 && (data === 'true' || data === 'True' || data === 'TRUE')) ||
  353. (max === 5 && (data === 'false' || data === 'False' || data === 'FALSE'));
  354. }
  355. function constructYamlBoolean(data) {
  356. return data === 'true' ||
  357. data === 'True' ||
  358. data === 'TRUE';
  359. }
  360. function isBoolean(object) {
  361. return Object.prototype.toString.call(object) === '[object Boolean]';
  362. }
  363. var bool = new type('tag:yaml.org,2002:bool', {
  364. kind: 'scalar',
  365. resolve: resolveYamlBoolean,
  366. construct: constructYamlBoolean,
  367. predicate: isBoolean,
  368. represent: {
  369. lowercase: function (object) { return object ? 'true' : 'false'; },
  370. uppercase: function (object) { return object ? 'TRUE' : 'FALSE'; },
  371. camelcase: function (object) { return object ? 'True' : 'False'; }
  372. },
  373. defaultStyle: 'lowercase'
  374. });
  375. function isHexCode(c) {
  376. return ((0x30/* 0 */ <= c) && (c <= 0x39/* 9 */)) ||
  377. ((0x41/* A */ <= c) && (c <= 0x46/* F */)) ||
  378. ((0x61/* a */ <= c) && (c <= 0x66/* f */));
  379. }
  380. function isOctCode(c) {
  381. return ((0x30/* 0 */ <= c) && (c <= 0x37/* 7 */));
  382. }
  383. function isDecCode(c) {
  384. return ((0x30/* 0 */ <= c) && (c <= 0x39/* 9 */));
  385. }
  386. function resolveYamlInteger(data) {
  387. if (data === null) return false;
  388. var max = data.length,
  389. index = 0,
  390. hasDigits = false,
  391. ch;
  392. if (!max) return false;
  393. ch = data[index];
  394. // sign
  395. if (ch === '-' || ch === '+') {
  396. ch = data[++index];
  397. }
  398. if (ch === '0') {
  399. // 0
  400. if (index + 1 === max) return true;
  401. ch = data[++index];
  402. // base 2, base 8, base 16
  403. if (ch === 'b') {
  404. // base 2
  405. index++;
  406. for (; index < max; index++) {
  407. ch = data[index];
  408. if (ch === '_') continue;
  409. if (ch !== '0' && ch !== '1') return false;
  410. hasDigits = true;
  411. }
  412. return hasDigits && ch !== '_';
  413. }
  414. if (ch === 'x') {
  415. // base 16
  416. index++;
  417. for (; index < max; index++) {
  418. ch = data[index];
  419. if (ch === '_') continue;
  420. if (!isHexCode(data.charCodeAt(index))) return false;
  421. hasDigits = true;
  422. }
  423. return hasDigits && ch !== '_';
  424. }
  425. if (ch === 'o') {
  426. // base 8
  427. index++;
  428. for (; index < max; index++) {
  429. ch = data[index];
  430. if (ch === '_') continue;
  431. if (!isOctCode(data.charCodeAt(index))) return false;
  432. hasDigits = true;
  433. }
  434. return hasDigits && ch !== '_';
  435. }
  436. }
  437. // base 10 (except 0)
  438. // value should not start with `_`;
  439. if (ch === '_') return false;
  440. for (; index < max; index++) {
  441. ch = data[index];
  442. if (ch === '_') continue;
  443. if (!isDecCode(data.charCodeAt(index))) {
  444. return false;
  445. }
  446. hasDigits = true;
  447. }
  448. // Should have digits and should not end with `_`
  449. if (!hasDigits || ch === '_') return false;
  450. return true;
  451. }
  452. function constructYamlInteger(data) {
  453. var value = data, sign = 1, ch;
  454. if (value.indexOf('_') !== -1) {
  455. value = value.replace(/_/g, '');
  456. }
  457. ch = value[0];
  458. if (ch === '-' || ch === '+') {
  459. if (ch === '-') sign = -1;
  460. value = value.slice(1);
  461. ch = value[0];
  462. }
  463. if (value === '0') return 0;
  464. if (ch === '0') {
  465. if (value[1] === 'b') return sign * parseInt(value.slice(2), 2);
  466. if (value[1] === 'x') return sign * parseInt(value.slice(2), 16);
  467. if (value[1] === 'o') return sign * parseInt(value.slice(2), 8);
  468. }
  469. return sign * parseInt(value, 10);
  470. }
  471. function isInteger(object) {
  472. return (Object.prototype.toString.call(object)) === '[object Number]' &&
  473. (object % 1 === 0 && !common.isNegativeZero(object));
  474. }
  475. var int = new type('tag:yaml.org,2002:int', {
  476. kind: 'scalar',
  477. resolve: resolveYamlInteger,
  478. construct: constructYamlInteger,
  479. predicate: isInteger,
  480. represent: {
  481. binary: function (obj) { return obj >= 0 ? '0b' + obj.toString(2) : '-0b' + obj.toString(2).slice(1); },
  482. octal: function (obj) { return obj >= 0 ? '0o' + obj.toString(8) : '-0o' + obj.toString(8).slice(1); },
  483. decimal: function (obj) { return obj.toString(10); },
  484. /* eslint-disable max-len */
  485. hexadecimal: function (obj) { return obj >= 0 ? '0x' + obj.toString(16).toUpperCase() : '-0x' + obj.toString(16).toUpperCase().slice(1); }
  486. },
  487. defaultStyle: 'decimal',
  488. styleAliases: {
  489. binary: [ 2, 'bin' ],
  490. octal: [ 8, 'oct' ],
  491. decimal: [ 10, 'dec' ],
  492. hexadecimal: [ 16, 'hex' ]
  493. }
  494. });
  495. var YAML_FLOAT_PATTERN = new RegExp(
  496. // 2.5e4, 2.5 and integers
  497. '^(?:[-+]?(?:[0-9][0-9_]*)(?:\\.[0-9_]*)?(?:[eE][-+]?[0-9]+)?' +
  498. // .2e4, .2
  499. // special case, seems not from spec
  500. '|\\.[0-9_]+(?:[eE][-+]?[0-9]+)?' +
  501. // .inf
  502. '|[-+]?\\.(?:inf|Inf|INF)' +
  503. // .nan
  504. '|\\.(?:nan|NaN|NAN))$');
  505. function resolveYamlFloat(data) {
  506. if (data === null) return false;
  507. if (!YAML_FLOAT_PATTERN.test(data) ||
  508. // Quick hack to not allow integers end with `_`
  509. // Probably should update regexp & check speed
  510. data[data.length - 1] === '_') {
  511. return false;
  512. }
  513. return true;
  514. }
  515. function constructYamlFloat(data) {
  516. var value, sign;
  517. value = data.replace(/_/g, '').toLowerCase();
  518. sign = value[0] === '-' ? -1 : 1;
  519. if ('+-'.indexOf(value[0]) >= 0) {
  520. value = value.slice(1);
  521. }
  522. if (value === '.inf') {
  523. return (sign === 1) ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
  524. } else if (value === '.nan') {
  525. return NaN;
  526. }
  527. return sign * parseFloat(value, 10);
  528. }
  529. var SCIENTIFIC_WITHOUT_DOT = /^[-+]?[0-9]+e/;
  530. function representYamlFloat(object, style) {
  531. var res;
  532. if (isNaN(object)) {
  533. switch (style) {
  534. case 'lowercase': return '.nan';
  535. case 'uppercase': return '.NAN';
  536. case 'camelcase': return '.NaN';
  537. }
  538. } else if (Number.POSITIVE_INFINITY === object) {
  539. switch (style) {
  540. case 'lowercase': return '.inf';
  541. case 'uppercase': return '.INF';
  542. case 'camelcase': return '.Inf';
  543. }
  544. } else if (Number.NEGATIVE_INFINITY === object) {
  545. switch (style) {
  546. case 'lowercase': return '-.inf';
  547. case 'uppercase': return '-.INF';
  548. case 'camelcase': return '-.Inf';
  549. }
  550. } else if (common.isNegativeZero(object)) {
  551. return '-0.0';
  552. }
  553. res = object.toString(10);
  554. // JS stringifier can build scientific format without dots: 5e-100,
  555. // while YAML requres dot: 5.e-100. Fix it with simple hack
  556. return SCIENTIFIC_WITHOUT_DOT.test(res) ? res.replace('e', '.e') : res;
  557. }
  558. function isFloat(object) {
  559. return (Object.prototype.toString.call(object) === '[object Number]') &&
  560. (object % 1 !== 0 || common.isNegativeZero(object));
  561. }
  562. var float = new type('tag:yaml.org,2002:float', {
  563. kind: 'scalar',
  564. resolve: resolveYamlFloat,
  565. construct: constructYamlFloat,
  566. predicate: isFloat,
  567. represent: representYamlFloat,
  568. defaultStyle: 'lowercase'
  569. });
  570. var json = failsafe.extend({
  571. implicit: [
  572. _null,
  573. bool,
  574. int,
  575. float
  576. ]
  577. });
  578. var core = json;
  579. var YAML_DATE_REGEXP = new RegExp(
  580. '^([0-9][0-9][0-9][0-9])' + // [1] year
  581. '-([0-9][0-9])' + // [2] month
  582. '-([0-9][0-9])$'); // [3] day
  583. var YAML_TIMESTAMP_REGEXP = new RegExp(
  584. '^([0-9][0-9][0-9][0-9])' + // [1] year
  585. '-([0-9][0-9]?)' + // [2] month
  586. '-([0-9][0-9]?)' + // [3] day
  587. '(?:[Tt]|[ \\t]+)' + // ...
  588. '([0-9][0-9]?)' + // [4] hour
  589. ':([0-9][0-9])' + // [5] minute
  590. ':([0-9][0-9])' + // [6] second
  591. '(?:\\.([0-9]*))?' + // [7] fraction
  592. '(?:[ \\t]*(Z|([-+])([0-9][0-9]?)' + // [8] tz [9] tz_sign [10] tz_hour
  593. '(?::([0-9][0-9]))?))?$'); // [11] tz_minute
  594. function resolveYamlTimestamp(data) {
  595. if (data === null) return false;
  596. if (YAML_DATE_REGEXP.exec(data) !== null) return true;
  597. if (YAML_TIMESTAMP_REGEXP.exec(data) !== null) return true;
  598. return false;
  599. }
  600. function constructYamlTimestamp(data) {
  601. var match, year, month, day, hour, minute, second, fraction = 0,
  602. delta = null, tz_hour, tz_minute, date;
  603. match = YAML_DATE_REGEXP.exec(data);
  604. if (match === null) match = YAML_TIMESTAMP_REGEXP.exec(data);
  605. if (match === null) throw new Error('Date resolve error');
  606. // match: [1] year [2] month [3] day
  607. year = +(match[1]);
  608. month = +(match[2]) - 1; // JS month starts with 0
  609. day = +(match[3]);
  610. if (!match[4]) { // no hour
  611. return new Date(Date.UTC(year, month, day));
  612. }
  613. // match: [4] hour [5] minute [6] second [7] fraction
  614. hour = +(match[4]);
  615. minute = +(match[5]);
  616. second = +(match[6]);
  617. if (match[7]) {
  618. fraction = match[7].slice(0, 3);
  619. while (fraction.length < 3) { // milli-seconds
  620. fraction += '0';
  621. }
  622. fraction = +fraction;
  623. }
  624. // match: [8] tz [9] tz_sign [10] tz_hour [11] tz_minute
  625. if (match[9]) {
  626. tz_hour = +(match[10]);
  627. tz_minute = +(match[11] || 0);
  628. delta = (tz_hour * 60 + tz_minute) * 60000; // delta in mili-seconds
  629. if (match[9] === '-') delta = -delta;
  630. }
  631. date = new Date(Date.UTC(year, month, day, hour, minute, second, fraction));
  632. if (delta) date.setTime(date.getTime() - delta);
  633. return date;
  634. }
  635. function representYamlTimestamp(object /*, style*/) {
  636. return object.toISOString();
  637. }
  638. var timestamp = new type('tag:yaml.org,2002:timestamp', {
  639. kind: 'scalar',
  640. resolve: resolveYamlTimestamp,
  641. construct: constructYamlTimestamp,
  642. instanceOf: Date,
  643. represent: representYamlTimestamp
  644. });
  645. function resolveYamlMerge(data) {
  646. return data === '<<' || data === null;
  647. }
  648. var merge = new type('tag:yaml.org,2002:merge', {
  649. kind: 'scalar',
  650. resolve: resolveYamlMerge
  651. });
  652. /*eslint-disable no-bitwise*/
  653. // [ 64, 65, 66 ] -> [ padding, CR, LF ]
  654. var BASE64_MAP = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n\r';
  655. function resolveYamlBinary(data) {
  656. if (data === null) return false;
  657. var code, idx, bitlen = 0, max = data.length, map = BASE64_MAP;
  658. // Convert one by one.
  659. for (idx = 0; idx < max; idx++) {
  660. code = map.indexOf(data.charAt(idx));
  661. // Skip CR/LF
  662. if (code > 64) continue;
  663. // Fail on illegal characters
  664. if (code < 0) return false;
  665. bitlen += 6;
  666. }
  667. // If there are any bits left, source was corrupted
  668. return (bitlen % 8) === 0;
  669. }
  670. function constructYamlBinary(data) {
  671. var idx, tailbits,
  672. input = data.replace(/[\r\n=]/g, ''), // remove CR/LF & padding to simplify scan
  673. max = input.length,
  674. map = BASE64_MAP,
  675. bits = 0,
  676. result = [];
  677. // Collect by 6*4 bits (3 bytes)
  678. for (idx = 0; idx < max; idx++) {
  679. if ((idx % 4 === 0) && idx) {
  680. result.push((bits >> 16) & 0xFF);
  681. result.push((bits >> 8) & 0xFF);
  682. result.push(bits & 0xFF);
  683. }
  684. bits = (bits << 6) | map.indexOf(input.charAt(idx));
  685. }
  686. // Dump tail
  687. tailbits = (max % 4) * 6;
  688. if (tailbits === 0) {
  689. result.push((bits >> 16) & 0xFF);
  690. result.push((bits >> 8) & 0xFF);
  691. result.push(bits & 0xFF);
  692. } else if (tailbits === 18) {
  693. result.push((bits >> 10) & 0xFF);
  694. result.push((bits >> 2) & 0xFF);
  695. } else if (tailbits === 12) {
  696. result.push((bits >> 4) & 0xFF);
  697. }
  698. return new Uint8Array(result);
  699. }
  700. function representYamlBinary(object /*, style*/) {
  701. var result = '', bits = 0, idx, tail,
  702. max = object.length,
  703. map = BASE64_MAP;
  704. // Convert every three bytes to 4 ASCII characters.
  705. for (idx = 0; idx < max; idx++) {
  706. if ((idx % 3 === 0) && idx) {
  707. result += map[(bits >> 18) & 0x3F];
  708. result += map[(bits >> 12) & 0x3F];
  709. result += map[(bits >> 6) & 0x3F];
  710. result += map[bits & 0x3F];
  711. }
  712. bits = (bits << 8) + object[idx];
  713. }
  714. // Dump tail
  715. tail = max % 3;
  716. if (tail === 0) {
  717. result += map[(bits >> 18) & 0x3F];
  718. result += map[(bits >> 12) & 0x3F];
  719. result += map[(bits >> 6) & 0x3F];
  720. result += map[bits & 0x3F];
  721. } else if (tail === 2) {
  722. result += map[(bits >> 10) & 0x3F];
  723. result += map[(bits >> 4) & 0x3F];
  724. result += map[(bits << 2) & 0x3F];
  725. result += map[64];
  726. } else if (tail === 1) {
  727. result += map[(bits >> 2) & 0x3F];
  728. result += map[(bits << 4) & 0x3F];
  729. result += map[64];
  730. result += map[64];
  731. }
  732. return result;
  733. }
  734. function isBinary(obj) {
  735. return Object.prototype.toString.call(obj) === '[object Uint8Array]';
  736. }
  737. var binary = new type('tag:yaml.org,2002:binary', {
  738. kind: 'scalar',
  739. resolve: resolveYamlBinary,
  740. construct: constructYamlBinary,
  741. predicate: isBinary,
  742. represent: representYamlBinary
  743. });
  744. var _hasOwnProperty$3 = Object.prototype.hasOwnProperty;
  745. var _toString$2 = Object.prototype.toString;
  746. function resolveYamlOmap(data) {
  747. if (data === null) return true;
  748. var objectKeys = [], index, length, pair, pairKey, pairHasKey,
  749. object = data;
  750. for (index = 0, length = object.length; index < length; index += 1) {
  751. pair = object[index];
  752. pairHasKey = false;
  753. if (_toString$2.call(pair) !== '[object Object]') return false;
  754. for (pairKey in pair) {
  755. if (_hasOwnProperty$3.call(pair, pairKey)) {
  756. if (!pairHasKey) pairHasKey = true;
  757. else return false;
  758. }
  759. }
  760. if (!pairHasKey) return false;
  761. if (objectKeys.indexOf(pairKey) === -1) objectKeys.push(pairKey);
  762. else return false;
  763. }
  764. return true;
  765. }
  766. function constructYamlOmap(data) {
  767. return data !== null ? data : [];
  768. }
  769. var omap = new type('tag:yaml.org,2002:omap', {
  770. kind: 'sequence',
  771. resolve: resolveYamlOmap,
  772. construct: constructYamlOmap
  773. });
  774. var _toString$1 = Object.prototype.toString;
  775. function resolveYamlPairs(data) {
  776. if (data === null) return true;
  777. var index, length, pair, keys, result,
  778. object = data;
  779. result = new Array(object.length);
  780. for (index = 0, length = object.length; index < length; index += 1) {
  781. pair = object[index];
  782. if (_toString$1.call(pair) !== '[object Object]') return false;
  783. keys = Object.keys(pair);
  784. if (keys.length !== 1) return false;
  785. result[index] = [ keys[0], pair[keys[0]] ];
  786. }
  787. return true;
  788. }
  789. function constructYamlPairs(data) {
  790. if (data === null) return [];
  791. var index, length, pair, keys, result,
  792. object = data;
  793. result = new Array(object.length);
  794. for (index = 0, length = object.length; index < length; index += 1) {
  795. pair = object[index];
  796. keys = Object.keys(pair);
  797. result[index] = [ keys[0], pair[keys[0]] ];
  798. }
  799. return result;
  800. }
  801. var pairs = new type('tag:yaml.org,2002:pairs', {
  802. kind: 'sequence',
  803. resolve: resolveYamlPairs,
  804. construct: constructYamlPairs
  805. });
  806. var _hasOwnProperty$2 = Object.prototype.hasOwnProperty;
  807. function resolveYamlSet(data) {
  808. if (data === null) return true;
  809. var key, object = data;
  810. for (key in object) {
  811. if (_hasOwnProperty$2.call(object, key)) {
  812. if (object[key] !== null) return false;
  813. }
  814. }
  815. return true;
  816. }
  817. function constructYamlSet(data) {
  818. return data !== null ? data : {};
  819. }
  820. var set = new type('tag:yaml.org,2002:set', {
  821. kind: 'mapping',
  822. resolve: resolveYamlSet,
  823. construct: constructYamlSet
  824. });
  825. var _default = core.extend({
  826. implicit: [
  827. timestamp,
  828. merge
  829. ],
  830. explicit: [
  831. binary,
  832. omap,
  833. pairs,
  834. set
  835. ]
  836. });
  837. /*eslint-disable max-len,no-use-before-define*/
  838. var _hasOwnProperty$1 = Object.prototype.hasOwnProperty;
  839. var CONTEXT_FLOW_IN = 1;
  840. var CONTEXT_FLOW_OUT = 2;
  841. var CONTEXT_BLOCK_IN = 3;
  842. var CONTEXT_BLOCK_OUT = 4;
  843. var CHOMPING_CLIP = 1;
  844. var CHOMPING_STRIP = 2;
  845. var CHOMPING_KEEP = 3;
  846. var PATTERN_NON_PRINTABLE = /[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x84\x86-\x9F\uFFFE\uFFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/;
  847. var PATTERN_NON_ASCII_LINE_BREAKS = /[\x85\u2028\u2029]/;
  848. var PATTERN_FLOW_INDICATORS = /[,\[\]\{\}]/;
  849. var PATTERN_TAG_HANDLE = /^(?:!|!!|![a-z\-]+!)$/i;
  850. var PATTERN_TAG_URI = /^(?:!|[^,\[\]\{\}])(?:%[0-9a-f]{2}|[0-9a-z\-#;\/\?:@&=\+\$,_\.!~\*'\(\)\[\]])*$/i;
  851. function _class(obj) { return Object.prototype.toString.call(obj); }
  852. function is_EOL(c) {
  853. return (c === 0x0A/* LF */) || (c === 0x0D/* CR */);
  854. }
  855. function is_WHITE_SPACE(c) {
  856. return (c === 0x09/* Tab */) || (c === 0x20/* Space */);
  857. }
  858. function is_WS_OR_EOL(c) {
  859. return (c === 0x09/* Tab */) ||
  860. (c === 0x20/* Space */) ||
  861. (c === 0x0A/* LF */) ||
  862. (c === 0x0D/* CR */);
  863. }
  864. function is_FLOW_INDICATOR(c) {
  865. return c === 0x2C/* , */ ||
  866. c === 0x5B/* [ */ ||
  867. c === 0x5D/* ] */ ||
  868. c === 0x7B/* { */ ||
  869. c === 0x7D/* } */;
  870. }
  871. function fromHexCode(c) {
  872. var lc;
  873. if ((0x30/* 0 */ <= c) && (c <= 0x39/* 9 */)) {
  874. return c - 0x30;
  875. }
  876. /*eslint-disable no-bitwise*/
  877. lc = c | 0x20;
  878. if ((0x61/* a */ <= lc) && (lc <= 0x66/* f */)) {
  879. return lc - 0x61 + 10;
  880. }
  881. return -1;
  882. }
  883. function escapedHexLen(c) {
  884. if (c === 0x78/* x */) { return 2; }
  885. if (c === 0x75/* u */) { return 4; }
  886. if (c === 0x55/* U */) { return 8; }
  887. return 0;
  888. }
  889. function fromDecimalCode(c) {
  890. if ((0x30/* 0 */ <= c) && (c <= 0x39/* 9 */)) {
  891. return c - 0x30;
  892. }
  893. return -1;
  894. }
  895. function simpleEscapeSequence(c) {
  896. /* eslint-disable indent */
  897. return (c === 0x30/* 0 */) ? '\x00' :
  898. (c === 0x61/* a */) ? '\x07' :
  899. (c === 0x62/* b */) ? '\x08' :
  900. (c === 0x74/* t */) ? '\x09' :
  901. (c === 0x09/* Tab */) ? '\x09' :
  902. (c === 0x6E/* n */) ? '\x0A' :
  903. (c === 0x76/* v */) ? '\x0B' :
  904. (c === 0x66/* f */) ? '\x0C' :
  905. (c === 0x72/* r */) ? '\x0D' :
  906. (c === 0x65/* e */) ? '\x1B' :
  907. (c === 0x20/* Space */) ? ' ' :
  908. (c === 0x22/* " */) ? '\x22' :
  909. (c === 0x2F/* / */) ? '/' :
  910. (c === 0x5C/* \ */) ? '\x5C' :
  911. (c === 0x4E/* N */) ? '\x85' :
  912. (c === 0x5F/* _ */) ? '\xA0' :
  913. (c === 0x4C/* L */) ? '\u2028' :
  914. (c === 0x50/* P */) ? '\u2029' : '';
  915. }
  916. function charFromCodepoint(c) {
  917. if (c <= 0xFFFF) {
  918. return String.fromCharCode(c);
  919. }
  920. // Encode UTF-16 surrogate pair
  921. // https://en.wikipedia.org/wiki/UTF-16#Code_points_U.2B010000_to_U.2B10FFFF
  922. return String.fromCharCode(
  923. ((c - 0x010000) >> 10) + 0xD800,
  924. ((c - 0x010000) & 0x03FF) + 0xDC00
  925. );
  926. }
  927. // set a property of a literal object, while protecting against prototype pollution,
  928. // see https://github.com/nodeca/js-yaml/issues/164 for more details
  929. function setProperty(object, key, value) {
  930. // used for this specific key only because Object.defineProperty is slow
  931. if (key === '__proto__') {
  932. Object.defineProperty(object, key, {
  933. configurable: true,
  934. enumerable: true,
  935. writable: true,
  936. value: value
  937. });
  938. } else {
  939. object[key] = value;
  940. }
  941. }
  942. var simpleEscapeCheck = new Array(256); // integer, for fast access
  943. var simpleEscapeMap = new Array(256);
  944. for (var i = 0; i < 256; i++) {
  945. simpleEscapeCheck[i] = simpleEscapeSequence(i) ? 1 : 0;
  946. simpleEscapeMap[i] = simpleEscapeSequence(i);
  947. }
  948. function State$1(input, options) {
  949. this.input = input;
  950. this.filename = options['filename'] || null;
  951. this.schema = options['schema'] || _default;
  952. this.onWarning = options['onWarning'] || null;
  953. // (Hidden) Remove? makes the loader to expect YAML 1.1 documents
  954. // if such documents have no explicit %YAML directive
  955. this.legacy = options['legacy'] || false;
  956. this.json = options['json'] || false;
  957. this.listener = options['listener'] || null;
  958. this.implicitTypes = this.schema.compiledImplicit;
  959. this.typeMap = this.schema.compiledTypeMap;
  960. this.length = input.length;
  961. this.position = 0;
  962. this.line = 0;
  963. this.lineStart = 0;
  964. this.lineIndent = 0;
  965. // position of first leading tab in the current line,
  966. // used to make sure there are no tabs in the indentation
  967. this.firstTabInLine = -1;
  968. this.documents = [];
  969. /*
  970. this.version;
  971. this.checkLineBreaks;
  972. this.tagMap;
  973. this.anchorMap;
  974. this.tag;
  975. this.anchor;
  976. this.kind;
  977. this.result;*/
  978. }
  979. function generateError(state, message) {
  980. var mark = {
  981. name: state.filename,
  982. buffer: state.input.slice(0, -1), // omit trailing \0
  983. position: state.position,
  984. line: state.line,
  985. column: state.position - state.lineStart
  986. };
  987. mark.snippet = snippet(mark);
  988. return new exception(message, mark);
  989. }
  990. function throwError(state, message) {
  991. throw generateError(state, message);
  992. }
  993. function throwWarning(state, message) {
  994. if (state.onWarning) {
  995. state.onWarning.call(null, generateError(state, message));
  996. }
  997. }
  998. var directiveHandlers = {
  999. YAML: function handleYamlDirective(state, name, args) {
  1000. var match, major, minor;
  1001. if (state.version !== null) {
  1002. throwError(state, 'duplication of %YAML directive');
  1003. }
  1004. if (args.length !== 1) {
  1005. throwError(state, 'YAML directive accepts exactly one argument');
  1006. }
  1007. match = /^([0-9]+)\.([0-9]+)$/.exec(args[0]);
  1008. if (match === null) {
  1009. throwError(state, 'ill-formed argument of the YAML directive');
  1010. }
  1011. major = parseInt(match[1], 10);
  1012. minor = parseInt(match[2], 10);
  1013. if (major !== 1) {
  1014. throwError(state, 'unacceptable YAML version of the document');
  1015. }
  1016. state.version = args[0];
  1017. state.checkLineBreaks = (minor < 2);
  1018. if (minor !== 1 && minor !== 2) {
  1019. throwWarning(state, 'unsupported YAML version of the document');
  1020. }
  1021. },
  1022. TAG: function handleTagDirective(state, name, args) {
  1023. var handle, prefix;
  1024. if (args.length !== 2) {
  1025. throwError(state, 'TAG directive accepts exactly two arguments');
  1026. }
  1027. handle = args[0];
  1028. prefix = args[1];
  1029. if (!PATTERN_TAG_HANDLE.test(handle)) {
  1030. throwError(state, 'ill-formed tag handle (first argument) of the TAG directive');
  1031. }
  1032. if (_hasOwnProperty$1.call(state.tagMap, handle)) {
  1033. throwError(state, 'there is a previously declared suffix for "' + handle + '" tag handle');
  1034. }
  1035. if (!PATTERN_TAG_URI.test(prefix)) {
  1036. throwError(state, 'ill-formed tag prefix (second argument) of the TAG directive');
  1037. }
  1038. try {
  1039. prefix = decodeURIComponent(prefix);
  1040. } catch (err) {
  1041. throwError(state, 'tag prefix is malformed: ' + prefix);
  1042. }
  1043. state.tagMap[handle] = prefix;
  1044. }
  1045. };
  1046. function captureSegment(state, start, end, checkJson) {
  1047. var _position, _length, _character, _result;
  1048. if (start < end) {
  1049. _result = state.input.slice(start, end);
  1050. if (checkJson) {
  1051. for (_position = 0, _length = _result.length; _position < _length; _position += 1) {
  1052. _character = _result.charCodeAt(_position);
  1053. if (!(_character === 0x09 ||
  1054. (0x20 <= _character && _character <= 0x10FFFF))) {
  1055. throwError(state, 'expected valid JSON character');
  1056. }
  1057. }
  1058. } else if (PATTERN_NON_PRINTABLE.test(_result)) {
  1059. throwError(state, 'the stream contains non-printable characters');
  1060. }
  1061. state.result += _result;
  1062. }
  1063. }
  1064. function mergeMappings(state, destination, source, overridableKeys) {
  1065. var sourceKeys, key, index, quantity;
  1066. if (!common.isObject(source)) {
  1067. throwError(state, 'cannot merge mappings; the provided source object is unacceptable');
  1068. }
  1069. sourceKeys = Object.keys(source);
  1070. for (index = 0, quantity = sourceKeys.length; index < quantity; index += 1) {
  1071. key = sourceKeys[index];
  1072. if (!_hasOwnProperty$1.call(destination, key)) {
  1073. setProperty(destination, key, source[key]);
  1074. overridableKeys[key] = true;
  1075. }
  1076. }
  1077. }
  1078. function storeMappingPair(state, _result, overridableKeys, keyTag, keyNode, valueNode,
  1079. startLine, startLineStart, startPos) {
  1080. var index, quantity;
  1081. // The output is a plain object here, so keys can only be strings.
  1082. // We need to convert keyNode to a string, but doing so can hang the process
  1083. // (deeply nested arrays that explode exponentially using aliases).
  1084. if (Array.isArray(keyNode)) {
  1085. keyNode = Array.prototype.slice.call(keyNode);
  1086. for (index = 0, quantity = keyNode.length; index < quantity; index += 1) {
  1087. if (Array.isArray(keyNode[index])) {
  1088. throwError(state, 'nested arrays are not supported inside keys');
  1089. }
  1090. if (typeof keyNode === 'object' && _class(keyNode[index]) === '[object Object]') {
  1091. keyNode[index] = '[object Object]';
  1092. }
  1093. }
  1094. }
  1095. // Avoid code execution in load() via toString property
  1096. // (still use its own toString for arrays, timestamps,
  1097. // and whatever user schema extensions happen to have @@toStringTag)
  1098. if (typeof keyNode === 'object' && _class(keyNode) === '[object Object]') {
  1099. keyNode = '[object Object]';
  1100. }
  1101. keyNode = String(keyNode);
  1102. if (_result === null) {
  1103. _result = {};
  1104. }
  1105. if (keyTag === 'tag:yaml.org,2002:merge') {
  1106. if (Array.isArray(valueNode)) {
  1107. for (index = 0, quantity = valueNode.length; index < quantity; index += 1) {
  1108. mergeMappings(state, _result, valueNode[index], overridableKeys);
  1109. }
  1110. } else {
  1111. mergeMappings(state, _result, valueNode, overridableKeys);
  1112. }
  1113. } else {
  1114. if (!state.json &&
  1115. !_hasOwnProperty$1.call(overridableKeys, keyNode) &&
  1116. _hasOwnProperty$1.call(_result, keyNode)) {
  1117. state.line = startLine || state.line;
  1118. state.lineStart = startLineStart || state.lineStart;
  1119. state.position = startPos || state.position;
  1120. throwError(state, 'duplicated mapping key');
  1121. }
  1122. setProperty(_result, keyNode, valueNode);
  1123. delete overridableKeys[keyNode];
  1124. }
  1125. return _result;
  1126. }
  1127. function readLineBreak(state) {
  1128. var ch;
  1129. ch = state.input.charCodeAt(state.position);
  1130. if (ch === 0x0A/* LF */) {
  1131. state.position++;
  1132. } else if (ch === 0x0D/* CR */) {
  1133. state.position++;
  1134. if (state.input.charCodeAt(state.position) === 0x0A/* LF */) {
  1135. state.position++;
  1136. }
  1137. } else {
  1138. throwError(state, 'a line break is expected');
  1139. }
  1140. state.line += 1;
  1141. state.lineStart = state.position;
  1142. state.firstTabInLine = -1;
  1143. }
  1144. function skipSeparationSpace(state, allowComments, checkIndent) {
  1145. var lineBreaks = 0,
  1146. ch = state.input.charCodeAt(state.position);
  1147. while (ch !== 0) {
  1148. while (is_WHITE_SPACE(ch)) {
  1149. if (ch === 0x09/* Tab */ && state.firstTabInLine === -1) {
  1150. state.firstTabInLine = state.position;
  1151. }
  1152. ch = state.input.charCodeAt(++state.position);
  1153. }
  1154. if (allowComments && ch === 0x23/* # */) {
  1155. do {
  1156. ch = state.input.charCodeAt(++state.position);
  1157. } while (ch !== 0x0A/* LF */ && ch !== 0x0D/* CR */ && ch !== 0);
  1158. }
  1159. if (is_EOL(ch)) {
  1160. readLineBreak(state);
  1161. ch = state.input.charCodeAt(state.position);
  1162. lineBreaks++;
  1163. state.lineIndent = 0;
  1164. while (ch === 0x20/* Space */) {
  1165. state.lineIndent++;
  1166. ch = state.input.charCodeAt(++state.position);
  1167. }
  1168. } else {
  1169. break;
  1170. }
  1171. }
  1172. if (checkIndent !== -1 && lineBreaks !== 0 && state.lineIndent < checkIndent) {
  1173. throwWarning(state, 'deficient indentation');
  1174. }
  1175. return lineBreaks;
  1176. }
  1177. function testDocumentSeparator(state) {
  1178. var _position = state.position,
  1179. ch;
  1180. ch = state.input.charCodeAt(_position);
  1181. // Condition state.position === state.lineStart is tested
  1182. // in parent on each call, for efficiency. No needs to test here again.
  1183. if ((ch === 0x2D/* - */ || ch === 0x2E/* . */) &&
  1184. ch === state.input.charCodeAt(_position + 1) &&
  1185. ch === state.input.charCodeAt(_position + 2)) {
  1186. _position += 3;
  1187. ch = state.input.charCodeAt(_position);
  1188. if (ch === 0 || is_WS_OR_EOL(ch)) {
  1189. return true;
  1190. }
  1191. }
  1192. return false;
  1193. }
  1194. function writeFoldedLines(state, count) {
  1195. if (count === 1) {
  1196. state.result += ' ';
  1197. } else if (count > 1) {
  1198. state.result += common.repeat('\n', count - 1);
  1199. }
  1200. }
  1201. function readPlainScalar(state, nodeIndent, withinFlowCollection) {
  1202. var preceding,
  1203. following,
  1204. captureStart,
  1205. captureEnd,
  1206. hasPendingContent,
  1207. _line,
  1208. _lineStart,
  1209. _lineIndent,
  1210. _kind = state.kind,
  1211. _result = state.result,
  1212. ch;
  1213. ch = state.input.charCodeAt(state.position);
  1214. if (is_WS_OR_EOL(ch) ||
  1215. is_FLOW_INDICATOR(ch) ||
  1216. ch === 0x23/* # */ ||
  1217. ch === 0x26/* & */ ||
  1218. ch === 0x2A/* * */ ||
  1219. ch === 0x21/* ! */ ||
  1220. ch === 0x7C/* | */ ||
  1221. ch === 0x3E/* > */ ||
  1222. ch === 0x27/* ' */ ||
  1223. ch === 0x22/* " */ ||
  1224. ch === 0x25/* % */ ||
  1225. ch === 0x40/* @ */ ||
  1226. ch === 0x60/* ` */) {
  1227. return false;
  1228. }
  1229. if (ch === 0x3F/* ? */ || ch === 0x2D/* - */) {
  1230. following = state.input.charCodeAt(state.position + 1);
  1231. if (is_WS_OR_EOL(following) ||
  1232. withinFlowCollection && is_FLOW_INDICATOR(following)) {
  1233. return false;
  1234. }
  1235. }
  1236. state.kind = 'scalar';
  1237. state.result = '';
  1238. captureStart = captureEnd = state.position;
  1239. hasPendingContent = false;
  1240. while (ch !== 0) {
  1241. if (ch === 0x3A/* : */) {
  1242. following = state.input.charCodeAt(state.position + 1);
  1243. if (is_WS_OR_EOL(following) ||
  1244. withinFlowCollection && is_FLOW_INDICATOR(following)) {
  1245. break;
  1246. }
  1247. } else if (ch === 0x23/* # */) {
  1248. preceding = state.input.charCodeAt(state.position - 1);
  1249. if (is_WS_OR_EOL(preceding)) {
  1250. break;
  1251. }
  1252. } else if ((state.position === state.lineStart && testDocumentSeparator(state)) ||
  1253. withinFlowCollection && is_FLOW_INDICATOR(ch)) {
  1254. break;
  1255. } else if (is_EOL(ch)) {
  1256. _line = state.line;
  1257. _lineStart = state.lineStart;
  1258. _lineIndent = state.lineIndent;
  1259. skipSeparationSpace(state, false, -1);
  1260. if (state.lineIndent >= nodeIndent) {
  1261. hasPendingContent = true;
  1262. ch = state.input.charCodeAt(state.position);
  1263. continue;
  1264. } else {
  1265. state.position = captureEnd;
  1266. state.line = _line;
  1267. state.lineStart = _lineStart;
  1268. state.lineIndent = _lineIndent;
  1269. break;
  1270. }
  1271. }
  1272. if (hasPendingContent) {
  1273. captureSegment(state, captureStart, captureEnd, false);
  1274. writeFoldedLines(state, state.line - _line);
  1275. captureStart = captureEnd = state.position;
  1276. hasPendingContent = false;
  1277. }
  1278. if (!is_WHITE_SPACE(ch)) {
  1279. captureEnd = state.position + 1;
  1280. }
  1281. ch = state.input.charCodeAt(++state.position);
  1282. }
  1283. captureSegment(state, captureStart, captureEnd, false);
  1284. if (state.result) {
  1285. return true;
  1286. }
  1287. state.kind = _kind;
  1288. state.result = _result;
  1289. return false;
  1290. }
  1291. function readSingleQuotedScalar(state, nodeIndent) {
  1292. var ch,
  1293. captureStart, captureEnd;
  1294. ch = state.input.charCodeAt(state.position);
  1295. if (ch !== 0x27/* ' */) {
  1296. return false;
  1297. }
  1298. state.kind = 'scalar';
  1299. state.result = '';
  1300. state.position++;
  1301. captureStart = captureEnd = state.position;
  1302. while ((ch = state.input.charCodeAt(state.position)) !== 0) {
  1303. if (ch === 0x27/* ' */) {
  1304. captureSegment(state, captureStart, state.position, true);
  1305. ch = state.input.charCodeAt(++state.position);
  1306. if (ch === 0x27/* ' */) {
  1307. captureStart = state.position;
  1308. state.position++;
  1309. captureEnd = state.position;
  1310. } else {
  1311. return true;
  1312. }
  1313. } else if (is_EOL(ch)) {
  1314. captureSegment(state, captureStart, captureEnd, true);
  1315. writeFoldedLines(state, skipSeparationSpace(state, false, nodeIndent));
  1316. captureStart = captureEnd = state.position;
  1317. } else if (state.position === state.lineStart && testDocumentSeparator(state)) {
  1318. throwError(state, 'unexpected end of the document within a single quoted scalar');
  1319. } else {
  1320. state.position++;
  1321. captureEnd = state.position;
  1322. }
  1323. }
  1324. throwError(state, 'unexpected end of the stream within a single quoted scalar');
  1325. }
  1326. function readDoubleQuotedScalar(state, nodeIndent) {
  1327. var captureStart,
  1328. captureEnd,
  1329. hexLength,
  1330. hexResult,
  1331. tmp,
  1332. ch;
  1333. ch = state.input.charCodeAt(state.position);
  1334. if (ch !== 0x22/* " */) {
  1335. return false;
  1336. }
  1337. state.kind = 'scalar';
  1338. state.result = '';
  1339. state.position++;
  1340. captureStart = captureEnd = state.position;
  1341. while ((ch = state.input.charCodeAt(state.position)) !== 0) {
  1342. if (ch === 0x22/* " */) {
  1343. captureSegment(state, captureStart, state.position, true);
  1344. state.position++;
  1345. return true;
  1346. } else if (ch === 0x5C/* \ */) {
  1347. captureSegment(state, captureStart, state.position, true);
  1348. ch = state.input.charCodeAt(++state.position);
  1349. if (is_EOL(ch)) {
  1350. skipSeparationSpace(state, false, nodeIndent);
  1351. // TODO: rework to inline fn with no type cast?
  1352. } else if (ch < 256 && simpleEscapeCheck[ch]) {
  1353. state.result += simpleEscapeMap[ch];
  1354. state.position++;
  1355. } else if ((tmp = escapedHexLen(ch)) > 0) {
  1356. hexLength = tmp;
  1357. hexResult = 0;
  1358. for (; hexLength > 0; hexLength--) {
  1359. ch = state.input.charCodeAt(++state.position);
  1360. if ((tmp = fromHexCode(ch)) >= 0) {
  1361. hexResult = (hexResult << 4) + tmp;
  1362. } else {
  1363. throwError(state, 'expected hexadecimal character');
  1364. }
  1365. }
  1366. state.result += charFromCodepoint(hexResult);
  1367. state.position++;
  1368. } else {
  1369. throwError(state, 'unknown escape sequence');
  1370. }
  1371. captureStart = captureEnd = state.position;
  1372. } else if (is_EOL(ch)) {
  1373. captureSegment(state, captureStart, captureEnd, true);
  1374. writeFoldedLines(state, skipSeparationSpace(state, false, nodeIndent));
  1375. captureStart = captureEnd = state.position;
  1376. } else if (state.position === state.lineStart && testDocumentSeparator(state)) {
  1377. throwError(state, 'unexpected end of the document within a double quoted scalar');
  1378. } else {
  1379. state.position++;
  1380. captureEnd = state.position;
  1381. }
  1382. }
  1383. throwError(state, 'unexpected end of the stream within a double quoted scalar');
  1384. }
  1385. function readFlowCollection(state, nodeIndent) {
  1386. var readNext = true,
  1387. _line,
  1388. _lineStart,
  1389. _pos,
  1390. _tag = state.tag,
  1391. _result,
  1392. _anchor = state.anchor,
  1393. following,
  1394. terminator,
  1395. isPair,
  1396. isExplicitPair,
  1397. isMapping,
  1398. overridableKeys = Object.create(null),
  1399. keyNode,
  1400. keyTag,
  1401. valueNode,
  1402. ch;
  1403. ch = state.input.charCodeAt(state.position);
  1404. if (ch === 0x5B/* [ */) {
  1405. terminator = 0x5D;/* ] */
  1406. isMapping = false;
  1407. _result = [];
  1408. } else if (ch === 0x7B/* { */) {
  1409. terminator = 0x7D;/* } */
  1410. isMapping = true;
  1411. _result = {};
  1412. } else {
  1413. return false;
  1414. }
  1415. if (state.anchor !== null) {
  1416. state.anchorMap[state.anchor] = _result;
  1417. }
  1418. ch = state.input.charCodeAt(++state.position);
  1419. while (ch !== 0) {
  1420. skipSeparationSpace(state, true, nodeIndent);
  1421. ch = state.input.charCodeAt(state.position);
  1422. if (ch === terminator) {
  1423. state.position++;
  1424. state.tag = _tag;
  1425. state.anchor = _anchor;
  1426. state.kind = isMapping ? 'mapping' : 'sequence';
  1427. state.result = _result;
  1428. return true;
  1429. } else if (!readNext) {
  1430. throwError(state, 'missed comma between flow collection entries');
  1431. } else if (ch === 0x2C/* , */) {
  1432. // "flow collection entries can never be completely empty", as per YAML 1.2, section 7.4
  1433. throwError(state, "expected the node content, but found ','");
  1434. }
  1435. keyTag = keyNode = valueNode = null;
  1436. isPair = isExplicitPair = false;
  1437. if (ch === 0x3F/* ? */) {
  1438. following = state.input.charCodeAt(state.position + 1);
  1439. if (is_WS_OR_EOL(following)) {
  1440. isPair = isExplicitPair = true;
  1441. state.position++;
  1442. skipSeparationSpace(state, true, nodeIndent);
  1443. }
  1444. }
  1445. _line = state.line; // Save the current line.
  1446. _lineStart = state.lineStart;
  1447. _pos = state.position;
  1448. composeNode(state, nodeIndent, CONTEXT_FLOW_IN, false, true);
  1449. keyTag = state.tag;
  1450. keyNode = state.result;
  1451. skipSeparationSpace(state, true, nodeIndent);
  1452. ch = state.input.charCodeAt(state.position);
  1453. if ((isExplicitPair || state.line === _line) && ch === 0x3A/* : */) {
  1454. isPair = true;
  1455. ch = state.input.charCodeAt(++state.position);
  1456. skipSeparationSpace(state, true, nodeIndent);
  1457. composeNode(state, nodeIndent, CONTEXT_FLOW_IN, false, true);
  1458. valueNode = state.result;
  1459. }
  1460. if (isMapping) {
  1461. storeMappingPair(state, _result, overridableKeys, keyTag, keyNode, valueNode, _line, _lineStart, _pos);
  1462. } else if (isPair) {
  1463. _result.push(storeMappingPair(state, null, overridableKeys, keyTag, keyNode, valueNode, _line, _lineStart, _pos));
  1464. } else {
  1465. _result.push(keyNode);
  1466. }
  1467. skipSeparationSpace(state, true, nodeIndent);
  1468. ch = state.input.charCodeAt(state.position);
  1469. if (ch === 0x2C/* , */) {
  1470. readNext = true;
  1471. ch = state.input.charCodeAt(++state.position);
  1472. } else {
  1473. readNext = false;
  1474. }
  1475. }
  1476. throwError(state, 'unexpected end of the stream within a flow collection');
  1477. }
  1478. function readBlockScalar(state, nodeIndent) {
  1479. var captureStart,
  1480. folding,
  1481. chomping = CHOMPING_CLIP,
  1482. didReadContent = false,
  1483. detectedIndent = false,
  1484. textIndent = nodeIndent,
  1485. emptyLines = 0,
  1486. atMoreIndented = false,
  1487. tmp,
  1488. ch;
  1489. ch = state.input.charCodeAt(state.position);
  1490. if (ch === 0x7C/* | */) {
  1491. folding = false;
  1492. } else if (ch === 0x3E/* > */) {
  1493. folding = true;
  1494. } else {
  1495. return false;
  1496. }
  1497. state.kind = 'scalar';
  1498. state.result = '';
  1499. while (ch !== 0) {
  1500. ch = state.input.charCodeAt(++state.position);
  1501. if (ch === 0x2B/* + */ || ch === 0x2D/* - */) {
  1502. if (CHOMPING_CLIP === chomping) {
  1503. chomping = (ch === 0x2B/* + */) ? CHOMPING_KEEP : CHOMPING_STRIP;
  1504. } else {
  1505. throwError(state, 'repeat of a chomping mode identifier');
  1506. }
  1507. } else if ((tmp = fromDecimalCode(ch)) >= 0) {
  1508. if (tmp === 0) {
  1509. throwError(state, 'bad explicit indentation width of a block scalar; it cannot be less than one');
  1510. } else if (!detectedIndent) {
  1511. textIndent = nodeIndent + tmp - 1;
  1512. detectedIndent = true;
  1513. } else {
  1514. throwError(state, 'repeat of an indentation width identifier');
  1515. }
  1516. } else {
  1517. break;
  1518. }
  1519. }
  1520. if (is_WHITE_SPACE(ch)) {
  1521. do { ch = state.input.charCodeAt(++state.position); }
  1522. while (is_WHITE_SPACE(ch));
  1523. if (ch === 0x23/* # */) {
  1524. do { ch = state.input.charCodeAt(++state.position); }
  1525. while (!is_EOL(ch) && (ch !== 0));
  1526. }
  1527. }
  1528. while (ch !== 0) {
  1529. readLineBreak(state);
  1530. state.lineIndent = 0;
  1531. ch = state.input.charCodeAt(state.position);
  1532. while ((!detectedIndent || state.lineIndent < textIndent) &&
  1533. (ch === 0x20/* Space */)) {
  1534. state.lineIndent++;
  1535. ch = state.input.charCodeAt(++state.position);
  1536. }
  1537. if (!detectedIndent && state.lineIndent > textIndent) {
  1538. textIndent = state.lineIndent;
  1539. }
  1540. if (is_EOL(ch)) {
  1541. emptyLines++;
  1542. continue;
  1543. }
  1544. // End of the scalar.
  1545. if (state.lineIndent < textIndent) {
  1546. // Perform the chomping.
  1547. if (chomping === CHOMPING_KEEP) {
  1548. state.result += common.repeat('\n', didReadContent ? 1 + emptyLines : emptyLines);
  1549. } else if (chomping === CHOMPING_CLIP) {
  1550. if (didReadContent) { // i.e. only if the scalar is not empty.
  1551. state.result += '\n';
  1552. }
  1553. }
  1554. // Break this `while` cycle and go to the funciton's epilogue.
  1555. break;
  1556. }
  1557. // Folded style: use fancy rules to handle line breaks.
  1558. if (folding) {
  1559. // Lines starting with white space characters (more-indented lines) are not folded.
  1560. if (is_WHITE_SPACE(ch)) {
  1561. atMoreIndented = true;
  1562. // except for the first content line (cf. Example 8.1)
  1563. state.result += common.repeat('\n', didReadContent ? 1 + emptyLines : emptyLines);
  1564. // End of more-indented block.
  1565. } else if (atMoreIndented) {
  1566. atMoreIndented = false;
  1567. state.result += common.repeat('\n', emptyLines + 1);
  1568. // Just one line break - perceive as the same line.
  1569. } else if (emptyLines === 0) {
  1570. if (didReadContent) { // i.e. only if we have already read some scalar content.
  1571. state.result += ' ';
  1572. }
  1573. // Several line breaks - perceive as different lines.
  1574. } else {
  1575. state.result += common.repeat('\n', emptyLines);
  1576. }
  1577. // Literal style: just add exact number of line breaks between content lines.
  1578. } else {
  1579. // Keep all line breaks except the header line break.
  1580. state.result += common.repeat('\n', didReadContent ? 1 + emptyLines : emptyLines);
  1581. }
  1582. didReadContent = true;
  1583. detectedIndent = true;
  1584. emptyLines = 0;
  1585. captureStart = state.position;
  1586. while (!is_EOL(ch) && (ch !== 0)) {
  1587. ch = state.input.charCodeAt(++state.position);
  1588. }
  1589. captureSegment(state, captureStart, state.position, false);
  1590. }
  1591. return true;
  1592. }
  1593. function readBlockSequence(state, nodeIndent) {
  1594. var _line,
  1595. _tag = state.tag,
  1596. _anchor = state.anchor,
  1597. _result = [],
  1598. following,
  1599. detected = false,
  1600. ch;
  1601. // there is a leading tab before this token, so it can't be a block sequence/mapping;
  1602. // it can still be flow sequence/mapping or a scalar
  1603. if (state.firstTabInLine !== -1) return false;
  1604. if (state.anchor !== null) {
  1605. state.anchorMap[state.anchor] = _result;
  1606. }
  1607. ch = state.input.charCodeAt(state.position);
  1608. while (ch !== 0) {
  1609. if (state.firstTabInLine !== -1) {
  1610. state.position = state.firstTabInLine;
  1611. throwError(state, 'tab characters must not be used in indentation');
  1612. }
  1613. if (ch !== 0x2D/* - */) {
  1614. break;
  1615. }
  1616. following = state.input.charCodeAt(state.position + 1);
  1617. if (!is_WS_OR_EOL(following)) {
  1618. break;
  1619. }
  1620. detected = true;
  1621. state.position++;
  1622. if (skipSeparationSpace(state, true, -1)) {
  1623. if (state.lineIndent <= nodeIndent) {
  1624. _result.push(null);
  1625. ch = state.input.charCodeAt(state.position);
  1626. continue;
  1627. }
  1628. }
  1629. _line = state.line;
  1630. composeNode(state, nodeIndent, CONTEXT_BLOCK_IN, false, true);
  1631. _result.push(state.result);
  1632. skipSeparationSpace(state, true, -1);
  1633. ch = state.input.charCodeAt(state.position);
  1634. if ((state.line === _line || state.lineIndent > nodeIndent) && (ch !== 0)) {
  1635. throwError(state, 'bad indentation of a sequence entry');
  1636. } else if (state.lineIndent < nodeIndent) {
  1637. break;
  1638. }
  1639. }
  1640. if (detected) {
  1641. state.tag = _tag;
  1642. state.anchor = _anchor;
  1643. state.kind = 'sequence';
  1644. state.result = _result;
  1645. return true;
  1646. }
  1647. return false;
  1648. }
  1649. function readBlockMapping(state, nodeIndent, flowIndent) {
  1650. var following,
  1651. allowCompact,
  1652. _line,
  1653. _keyLine,
  1654. _keyLineStart,
  1655. _keyPos,
  1656. _tag = state.tag,
  1657. _anchor = state.anchor,
  1658. _result = {},
  1659. overridableKeys = Object.create(null),
  1660. keyTag = null,
  1661. keyNode = null,
  1662. valueNode = null,
  1663. atExplicitKey = false,
  1664. detected = false,
  1665. ch;
  1666. // there is a leading tab before this token, so it can't be a block sequence/mapping;
  1667. // it can still be flow sequence/mapping or a scalar
  1668. if (state.firstTabInLine !== -1) return false;
  1669. if (state.anchor !== null) {
  1670. state.anchorMap[state.anchor] = _result;
  1671. }
  1672. ch = state.input.charCodeAt(state.position);
  1673. while (ch !== 0) {
  1674. if (!atExplicitKey && state.firstTabInLine !== -1) {
  1675. state.position = state.firstTabInLine;
  1676. throwError(state, 'tab characters must not be used in indentation');
  1677. }
  1678. following = state.input.charCodeAt(state.position + 1);
  1679. _line = state.line; // Save the current line.
  1680. //
  1681. // Explicit notation case. There are two separate blocks:
  1682. // first for the key (denoted by "?") and second for the value (denoted by ":")
  1683. //
  1684. if ((ch === 0x3F/* ? */ || ch === 0x3A/* : */) && is_WS_OR_EOL(following)) {
  1685. if (ch === 0x3F/* ? */) {
  1686. if (atExplicitKey) {
  1687. storeMappingPair(state, _result, overridableKeys, keyTag, keyNode, null, _keyLine, _keyLineStart, _keyPos);
  1688. keyTag = keyNode = valueNode = null;
  1689. }
  1690. detected = true;
  1691. atExplicitKey = true;
  1692. allowCompact = true;
  1693. } else if (atExplicitKey) {
  1694. // i.e. 0x3A/* : */ === character after the explicit key.
  1695. atExplicitKey = false;
  1696. allowCompact = true;
  1697. } else {
  1698. throwError(state, 'incomplete explicit mapping pair; a key node is missed; or followed by a non-tabulated empty line');
  1699. }
  1700. state.position += 1;
  1701. ch = following;
  1702. //
  1703. // Implicit notation case. Flow-style node as the key first, then ":", and the value.
  1704. //
  1705. } else {
  1706. _keyLine = state.line;
  1707. _keyLineStart = state.lineStart;
  1708. _keyPos = state.position;
  1709. if (!composeNode(state, flowIndent, CONTEXT_FLOW_OUT, false, true)) {
  1710. // Neither implicit nor explicit notation.
  1711. // Reading is done. Go to the epilogue.
  1712. break;
  1713. }
  1714. if (state.line === _line) {
  1715. ch = state.input.charCodeAt(state.position);
  1716. while (is_WHITE_SPACE(ch)) {
  1717. ch = state.input.charCodeAt(++state.position);
  1718. }
  1719. if (ch === 0x3A/* : */) {
  1720. ch = state.input.charCodeAt(++state.position);
  1721. if (!is_WS_OR_EOL(ch)) {
  1722. throwError(state, 'a whitespace character is expected after the key-value separator within a block mapping');
  1723. }
  1724. if (atExplicitKey) {
  1725. storeMappingPair(state, _result, overridableKeys, keyTag, keyNode, null, _keyLine, _keyLineStart, _keyPos);
  1726. keyTag = keyNode = valueNode = null;
  1727. }
  1728. detected = true;
  1729. atExplicitKey = false;
  1730. allowCompact = false;
  1731. keyTag = state.tag;
  1732. keyNode = state.result;
  1733. } else if (detected) {
  1734. throwError(state, 'can not read an implicit mapping pair; a colon is missed');
  1735. } else {
  1736. state.tag = _tag;
  1737. state.anchor = _anchor;
  1738. return true; // Keep the result of `composeNode`.
  1739. }
  1740. } else if (detected) {
  1741. throwError(state, 'can not read a block mapping entry; a multiline key may not be an implicit key');
  1742. } else {
  1743. state.tag = _tag;
  1744. state.anchor = _anchor;
  1745. return true; // Keep the result of `composeNode`.
  1746. }
  1747. }
  1748. //
  1749. // Common reading code for both explicit and implicit notations.
  1750. //
  1751. if (state.line === _line || state.lineIndent > nodeIndent) {
  1752. if (atExplicitKey) {
  1753. _keyLine = state.line;
  1754. _keyLineStart = state.lineStart;
  1755. _keyPos = state.position;
  1756. }
  1757. if (composeNode(state, nodeIndent, CONTEXT_BLOCK_OUT, true, allowCompact)) {
  1758. if (atExplicitKey) {
  1759. keyNode = state.result;
  1760. } else {
  1761. valueNode = state.result;
  1762. }
  1763. }
  1764. if (!atExplicitKey) {
  1765. storeMappingPair(state, _result, overridableKeys, keyTag, keyNode, valueNode, _keyLine, _keyLineStart, _keyPos);
  1766. keyTag = keyNode = valueNode = null;
  1767. }
  1768. skipSeparationSpace(state, true, -1);
  1769. ch = state.input.charCodeAt(state.position);
  1770. }
  1771. if ((state.line === _line || state.lineIndent > nodeIndent) && (ch !== 0)) {
  1772. throwError(state, 'bad indentation of a mapping entry');
  1773. } else if (state.lineIndent < nodeIndent) {
  1774. break;
  1775. }
  1776. }
  1777. //
  1778. // Epilogue.
  1779. //
  1780. // Special case: last mapping's node contains only the key in explicit notation.
  1781. if (atExplicitKey) {
  1782. storeMappingPair(state, _result, overridableKeys, keyTag, keyNode, null, _keyLine, _keyLineStart, _keyPos);
  1783. }
  1784. // Expose the resulting mapping.
  1785. if (detected) {
  1786. state.tag = _tag;
  1787. state.anchor = _anchor;
  1788. state.kind = 'mapping';
  1789. state.result = _result;
  1790. }
  1791. return detected;
  1792. }
  1793. function readTagProperty(state) {
  1794. var _position,
  1795. isVerbatim = false,
  1796. isNamed = false,
  1797. tagHandle,
  1798. tagName,
  1799. ch;
  1800. ch = state.input.charCodeAt(state.position);
  1801. if (ch !== 0x21/* ! */) return false;
  1802. if (state.tag !== null) {
  1803. throwError(state, 'duplication of a tag property');
  1804. }
  1805. ch = state.input.charCodeAt(++state.position);
  1806. if (ch === 0x3C/* < */) {
  1807. isVerbatim = true;
  1808. ch = state.input.charCodeAt(++state.position);
  1809. } else if (ch === 0x21/* ! */) {
  1810. isNamed = true;
  1811. tagHandle = '!!';
  1812. ch = state.input.charCodeAt(++state.position);
  1813. } else {
  1814. tagHandle = '!';
  1815. }
  1816. _position = state.position;
  1817. if (isVerbatim) {
  1818. do { ch = state.input.charCodeAt(++state.position); }
  1819. while (ch !== 0 && ch !== 0x3E/* > */);
  1820. if (state.position < state.length) {
  1821. tagName = state.input.slice(_position, state.position);
  1822. ch = state.input.charCodeAt(++state.position);
  1823. } else {
  1824. throwError(state, 'unexpected end of the stream within a verbatim tag');
  1825. }
  1826. } else {
  1827. while (ch !== 0 && !is_WS_OR_EOL(ch)) {
  1828. if (ch === 0x21/* ! */) {
  1829. if (!isNamed) {
  1830. tagHandle = state.input.slice(_position - 1, state.position + 1);
  1831. if (!PATTERN_TAG_HANDLE.test(tagHandle)) {
  1832. throwError(state, 'named tag handle cannot contain such characters');
  1833. }
  1834. isNamed = true;
  1835. _position = state.position + 1;
  1836. } else {
  1837. throwError(state, 'tag suffix cannot contain exclamation marks');
  1838. }
  1839. }
  1840. ch = state.input.charCodeAt(++state.position);
  1841. }
  1842. tagName = state.input.slice(_position, state.position);
  1843. if (PATTERN_FLOW_INDICATORS.test(tagName)) {
  1844. throwError(state, 'tag suffix cannot contain flow indicator characters');
  1845. }
  1846. }
  1847. if (tagName && !PATTERN_TAG_URI.test(tagName)) {
  1848. throwError(state, 'tag name cannot contain such characters: ' + tagName);
  1849. }
  1850. try {
  1851. tagName = decodeURIComponent(tagName);
  1852. } catch (err) {
  1853. throwError(state, 'tag name is malformed: ' + tagName);
  1854. }
  1855. if (isVerbatim) {
  1856. state.tag = tagName;
  1857. } else if (_hasOwnProperty$1.call(state.tagMap, tagHandle)) {
  1858. state.tag = state.tagMap[tagHandle] + tagName;
  1859. } else if (tagHandle === '!') {
  1860. state.tag = '!' + tagName;
  1861. } else if (tagHandle === '!!') {
  1862. state.tag = 'tag:yaml.org,2002:' + tagName;
  1863. } else {
  1864. throwError(state, 'undeclared tag handle "' + tagHandle + '"');
  1865. }
  1866. return true;
  1867. }
  1868. function readAnchorProperty(state) {
  1869. var _position,
  1870. ch;
  1871. ch = state.input.charCodeAt(state.position);
  1872. if (ch !== 0x26/* & */) return false;
  1873. if (state.anchor !== null) {
  1874. throwError(state, 'duplication of an anchor property');
  1875. }
  1876. ch = state.input.charCodeAt(++state.position);
  1877. _position = state.position;
  1878. while (ch !== 0 && !is_WS_OR_EOL(ch) && !is_FLOW_INDICATOR(ch)) {
  1879. ch = state.input.charCodeAt(++state.position);
  1880. }
  1881. if (state.position === _position) {
  1882. throwError(state, 'name of an anchor node must contain at least one character');
  1883. }
  1884. state.anchor = state.input.slice(_position, state.position);
  1885. return true;
  1886. }
  1887. function readAlias(state) {
  1888. var _position, alias,
  1889. ch;
  1890. ch = state.input.charCodeAt(state.position);
  1891. if (ch !== 0x2A/* * */) return false;
  1892. ch = state.input.charCodeAt(++state.position);
  1893. _position = state.position;
  1894. while (ch !== 0 && !is_WS_OR_EOL(ch) && !is_FLOW_INDICATOR(ch)) {
  1895. ch = state.input.charCodeAt(++state.position);
  1896. }
  1897. if (state.position === _position) {
  1898. throwError(state, 'name of an alias node must contain at least one character');
  1899. }
  1900. alias = state.input.slice(_position, state.position);
  1901. if (!_hasOwnProperty$1.call(state.anchorMap, alias)) {
  1902. throwError(state, 'unidentified alias "' + alias + '"');
  1903. }
  1904. state.result = state.anchorMap[alias];
  1905. skipSeparationSpace(state, true, -1);
  1906. return true;
  1907. }
  1908. function composeNode(state, parentIndent, nodeContext, allowToSeek, allowCompact) {
  1909. var allowBlockStyles,
  1910. allowBlockScalars,
  1911. allowBlockCollections,
  1912. indentStatus = 1, // 1: this>parent, 0: this=parent, -1: this<parent
  1913. atNewLine = false,
  1914. hasContent = false,
  1915. typeIndex,
  1916. typeQuantity,
  1917. typeList,
  1918. type,
  1919. flowIndent,
  1920. blockIndent;
  1921. if (state.listener !== null) {
  1922. state.listener('open', state);
  1923. }
  1924. state.tag = null;
  1925. state.anchor = null;
  1926. state.kind = null;
  1927. state.result = null;
  1928. allowBlockStyles = allowBlockScalars = allowBlockCollections =
  1929. CONTEXT_BLOCK_OUT === nodeContext ||
  1930. CONTEXT_BLOCK_IN === nodeContext;
  1931. if (allowToSeek) {
  1932. if (skipSeparationSpace(state, true, -1)) {
  1933. atNewLine = true;
  1934. if (state.lineIndent > parentIndent) {
  1935. indentStatus = 1;
  1936. } else if (state.lineIndent === parentIndent) {
  1937. indentStatus = 0;
  1938. } else if (state.lineIndent < parentIndent) {
  1939. indentStatus = -1;
  1940. }
  1941. }
  1942. }
  1943. if (indentStatus === 1) {
  1944. while (readTagProperty(state) || readAnchorProperty(state)) {
  1945. if (skipSeparationSpace(state, true, -1)) {
  1946. atNewLine = true;
  1947. allowBlockCollections = allowBlockStyles;
  1948. if (state.lineIndent > parentIndent) {
  1949. indentStatus = 1;
  1950. } else if (state.lineIndent === parentIndent) {
  1951. indentStatus = 0;
  1952. } else if (state.lineIndent < parentIndent) {
  1953. indentStatus = -1;
  1954. }
  1955. } else {
  1956. allowBlockCollections = false;
  1957. }
  1958. }
  1959. }
  1960. if (allowBlockCollections) {
  1961. allowBlockCollections = atNewLine || allowCompact;
  1962. }
  1963. if (indentStatus === 1 || CONTEXT_BLOCK_OUT === nodeContext) {
  1964. if (CONTEXT_FLOW_IN === nodeContext || CONTEXT_FLOW_OUT === nodeContext) {
  1965. flowIndent = parentIndent;
  1966. } else {
  1967. flowIndent = parentIndent + 1;
  1968. }
  1969. blockIndent = state.position - state.lineStart;
  1970. if (indentStatus === 1) {
  1971. if (allowBlockCollections &&
  1972. (readBlockSequence(state, blockIndent) ||
  1973. readBlockMapping(state, blockIndent, flowIndent)) ||
  1974. readFlowCollection(state, flowIndent)) {
  1975. hasContent = true;
  1976. } else {
  1977. if ((allowBlockScalars && readBlockScalar(state, flowIndent)) ||
  1978. readSingleQuotedScalar(state, flowIndent) ||
  1979. readDoubleQuotedScalar(state, flowIndent)) {
  1980. hasContent = true;
  1981. } else if (readAlias(state)) {
  1982. hasContent = true;
  1983. if (state.tag !== null || state.anchor !== null) {
  1984. throwError(state, 'alias node should not have any properties');
  1985. }
  1986. } else if (readPlainScalar(state, flowIndent, CONTEXT_FLOW_IN === nodeContext)) {
  1987. hasContent = true;
  1988. if (state.tag === null) {
  1989. state.tag = '?';
  1990. }
  1991. }
  1992. if (state.anchor !== null) {
  1993. state.anchorMap[state.anchor] = state.result;
  1994. }
  1995. }
  1996. } else if (indentStatus === 0) {
  1997. // Special case: block sequences are allowed to have same indentation level as the parent.
  1998. // http://www.yaml.org/spec/1.2/spec.html#id2799784
  1999. hasContent = allowBlockCollections && readBlockSequence(state, blockIndent);
  2000. }
  2001. }
  2002. if (state.tag === null) {
  2003. if (state.anchor !== null) {
  2004. state.anchorMap[state.anchor] = state.result;
  2005. }
  2006. } else if (state.tag === '?') {
  2007. // Implicit resolving is not allowed for non-scalar types, and '?'
  2008. // non-specific tag is only automatically assigned to plain scalars.
  2009. //
  2010. // We only need to check kind conformity in case user explicitly assigns '?'
  2011. // tag, for example like this: "!<?> [0]"
  2012. //
  2013. if (state.result !== null && state.kind !== 'scalar') {
  2014. throwError(state, 'unacceptable node kind for !<?> tag; it should be "scalar", not "' + state.kind + '"');
  2015. }
  2016. for (typeIndex = 0, typeQuantity = state.implicitTypes.length; typeIndex < typeQuantity; typeIndex += 1) {
  2017. type = state.implicitTypes[typeIndex];
  2018. if (type.resolve(state.result)) { // `state.result` updated in resolver if matched
  2019. state.result = type.construct(state.result);
  2020. state.tag = type.tag;
  2021. if (state.anchor !== null) {
  2022. state.anchorMap[state.anchor] = state.result;
  2023. }
  2024. break;
  2025. }
  2026. }
  2027. } else if (state.tag !== '!') {
  2028. if (_hasOwnProperty$1.call(state.typeMap[state.kind || 'fallback'], state.tag)) {
  2029. type = state.typeMap[state.kind || 'fallback'][state.tag];
  2030. } else {
  2031. // looking for multi type
  2032. type = null;
  2033. typeList = state.typeMap.multi[state.kind || 'fallback'];
  2034. for (typeIndex = 0, typeQuantity = typeList.length; typeIndex < typeQuantity; typeIndex += 1) {
  2035. if (state.tag.slice(0, typeList[typeIndex].tag.length) === typeList[typeIndex].tag) {
  2036. type = typeList[typeIndex];
  2037. break;
  2038. }
  2039. }
  2040. }
  2041. if (!type) {
  2042. throwError(state, 'unknown tag !<' + state.tag + '>');
  2043. }
  2044. if (state.result !== null && type.kind !== state.kind) {
  2045. throwError(state, 'unacceptable node kind for !<' + state.tag + '> tag; it should be "' + type.kind + '", not "' + state.kind + '"');
  2046. }
  2047. if (!type.resolve(state.result, state.tag)) { // `state.result` updated in resolver if matched
  2048. throwError(state, 'cannot resolve a node with !<' + state.tag + '> explicit tag');
  2049. } else {
  2050. state.result = type.construct(state.result, state.tag);
  2051. if (state.anchor !== null) {
  2052. state.anchorMap[state.anchor] = state.result;
  2053. }
  2054. }
  2055. }
  2056. if (state.listener !== null) {
  2057. state.listener('close', state);
  2058. }
  2059. return state.tag !== null || state.anchor !== null || hasContent;
  2060. }
  2061. function readDocument(state) {
  2062. var documentStart = state.position,
  2063. _position,
  2064. directiveName,
  2065. directiveArgs,
  2066. hasDirectives = false,
  2067. ch;
  2068. state.version = null;
  2069. state.checkLineBreaks = state.legacy;
  2070. state.tagMap = Object.create(null);
  2071. state.anchorMap = Object.create(null);
  2072. while ((ch = state.input.charCodeAt(state.position)) !== 0) {
  2073. skipSeparationSpace(state, true, -1);
  2074. ch = state.input.charCodeAt(state.position);
  2075. if (state.lineIndent > 0 || ch !== 0x25/* % */) {
  2076. break;
  2077. }
  2078. hasDirectives = true;
  2079. ch = state.input.charCodeAt(++state.position);
  2080. _position = state.position;
  2081. while (ch !== 0 && !is_WS_OR_EOL(ch)) {
  2082. ch = state.input.charCodeAt(++state.position);
  2083. }
  2084. directiveName = state.input.slice(_position, state.position);
  2085. directiveArgs = [];
  2086. if (directiveName.length < 1) {
  2087. throwError(state, 'directive name must not be less than one character in length');
  2088. }
  2089. while (ch !== 0) {
  2090. while (is_WHITE_SPACE(ch)) {
  2091. ch = state.input.charCodeAt(++state.position);
  2092. }
  2093. if (ch === 0x23/* # */) {
  2094. do { ch = state.input.charCodeAt(++state.position); }
  2095. while (ch !== 0 && !is_EOL(ch));
  2096. break;
  2097. }
  2098. if (is_EOL(ch)) break;
  2099. _position = state.position;
  2100. while (ch !== 0 && !is_WS_OR_EOL(ch)) {
  2101. ch = state.input.charCodeAt(++state.position);
  2102. }
  2103. directiveArgs.push(state.input.slice(_position, state.position));
  2104. }
  2105. if (ch !== 0) readLineBreak(state);
  2106. if (_hasOwnProperty$1.call(directiveHandlers, directiveName)) {
  2107. directiveHandlers[directiveName](state, directiveName, directiveArgs);
  2108. } else {
  2109. throwWarning(state, 'unknown document directive "' + directiveName + '"');
  2110. }
  2111. }
  2112. skipSeparationSpace(state, true, -1);
  2113. if (state.lineIndent === 0 &&
  2114. state.input.charCodeAt(state.position) === 0x2D/* - */ &&
  2115. state.input.charCodeAt(state.position + 1) === 0x2D/* - */ &&
  2116. state.input.charCodeAt(state.position + 2) === 0x2D/* - */) {
  2117. state.position += 3;
  2118. skipSeparationSpace(state, true, -1);
  2119. } else if (hasDirectives) {
  2120. throwError(state, 'directives end mark is expected');
  2121. }
  2122. composeNode(state, state.lineIndent - 1, CONTEXT_BLOCK_OUT, false, true);
  2123. skipSeparationSpace(state, true, -1);
  2124. if (state.checkLineBreaks &&
  2125. PATTERN_NON_ASCII_LINE_BREAKS.test(state.input.slice(documentStart, state.position))) {
  2126. throwWarning(state, 'non-ASCII line breaks are interpreted as content');
  2127. }
  2128. state.documents.push(state.result);
  2129. if (state.position === state.lineStart && testDocumentSeparator(state)) {
  2130. if (state.input.charCodeAt(state.position) === 0x2E/* . */) {
  2131. state.position += 3;
  2132. skipSeparationSpace(state, true, -1);
  2133. }
  2134. return;
  2135. }
  2136. if (state.position < (state.length - 1)) {
  2137. throwError(state, 'end of the stream or a document separator is expected');
  2138. } else {
  2139. return;
  2140. }
  2141. }
  2142. function loadDocuments(input, options) {
  2143. input = String(input);
  2144. options = options || {};
  2145. if (input.length !== 0) {
  2146. // Add tailing `\n` if not exists
  2147. if (input.charCodeAt(input.length - 1) !== 0x0A/* LF */ &&
  2148. input.charCodeAt(input.length - 1) !== 0x0D/* CR */) {
  2149. input += '\n';
  2150. }
  2151. // Strip BOM
  2152. if (input.charCodeAt(0) === 0xFEFF) {
  2153. input = input.slice(1);
  2154. }
  2155. }
  2156. var state = new State$1(input, options);
  2157. var nullpos = input.indexOf('\0');
  2158. if (nullpos !== -1) {
  2159. state.position = nullpos;
  2160. throwError(state, 'null byte is not allowed in input');
  2161. }
  2162. // Use 0 as string terminator. That significantly simplifies bounds check.
  2163. state.input += '\0';
  2164. while (state.input.charCodeAt(state.position) === 0x20/* Space */) {
  2165. state.lineIndent += 1;
  2166. state.position += 1;
  2167. }
  2168. while (state.position < (state.length - 1)) {
  2169. readDocument(state);
  2170. }
  2171. return state.documents;
  2172. }
  2173. function loadAll$1(input, iterator, options) {
  2174. if (iterator !== null && typeof iterator === 'object' && typeof options === 'undefined') {
  2175. options = iterator;
  2176. iterator = null;
  2177. }
  2178. var documents = loadDocuments(input, options);
  2179. if (typeof iterator !== 'function') {
  2180. return documents;
  2181. }
  2182. for (var index = 0, length = documents.length; index < length; index += 1) {
  2183. iterator(documents[index]);
  2184. }
  2185. }
  2186. function load$1(input, options) {
  2187. var documents = loadDocuments(input, options);
  2188. if (documents.length === 0) {
  2189. /*eslint-disable no-undefined*/
  2190. return undefined;
  2191. } else if (documents.length === 1) {
  2192. return documents[0];
  2193. }
  2194. throw new exception('expected a single document in the stream, but found more');
  2195. }
  2196. var loadAll_1 = loadAll$1;
  2197. var load_1 = load$1;
  2198. var loader = {
  2199. loadAll: loadAll_1,
  2200. load: load_1
  2201. };
  2202. /*eslint-disable no-use-before-define*/
  2203. var _toString = Object.prototype.toString;
  2204. var _hasOwnProperty = Object.prototype.hasOwnProperty;
  2205. var CHAR_BOM = 0xFEFF;
  2206. var CHAR_TAB = 0x09; /* Tab */
  2207. var CHAR_LINE_FEED = 0x0A; /* LF */
  2208. var CHAR_CARRIAGE_RETURN = 0x0D; /* CR */
  2209. var CHAR_SPACE = 0x20; /* Space */
  2210. var CHAR_EXCLAMATION = 0x21; /* ! */
  2211. var CHAR_DOUBLE_QUOTE = 0x22; /* " */
  2212. var CHAR_SHARP = 0x23; /* # */
  2213. var CHAR_PERCENT = 0x25; /* % */
  2214. var CHAR_AMPERSAND = 0x26; /* & */
  2215. var CHAR_SINGLE_QUOTE = 0x27; /* ' */
  2216. var CHAR_ASTERISK = 0x2A; /* * */
  2217. var CHAR_COMMA = 0x2C; /* , */
  2218. var CHAR_MINUS = 0x2D; /* - */
  2219. var CHAR_COLON = 0x3A; /* : */
  2220. var CHAR_EQUALS = 0x3D; /* = */
  2221. var CHAR_GREATER_THAN = 0x3E; /* > */
  2222. var CHAR_QUESTION = 0x3F; /* ? */
  2223. var CHAR_COMMERCIAL_AT = 0x40; /* @ */
  2224. var CHAR_LEFT_SQUARE_BRACKET = 0x5B; /* [ */
  2225. var CHAR_RIGHT_SQUARE_BRACKET = 0x5D; /* ] */
  2226. var CHAR_GRAVE_ACCENT = 0x60; /* ` */
  2227. var CHAR_LEFT_CURLY_BRACKET = 0x7B; /* { */
  2228. var CHAR_VERTICAL_LINE = 0x7C; /* | */
  2229. var CHAR_RIGHT_CURLY_BRACKET = 0x7D; /* } */
  2230. var ESCAPE_SEQUENCES = {};
  2231. ESCAPE_SEQUENCES[0x00] = '\\0';
  2232. ESCAPE_SEQUENCES[0x07] = '\\a';
  2233. ESCAPE_SEQUENCES[0x08] = '\\b';
  2234. ESCAPE_SEQUENCES[0x09] = '\\t';
  2235. ESCAPE_SEQUENCES[0x0A] = '\\n';
  2236. ESCAPE_SEQUENCES[0x0B] = '\\v';
  2237. ESCAPE_SEQUENCES[0x0C] = '\\f';
  2238. ESCAPE_SEQUENCES[0x0D] = '\\r';
  2239. ESCAPE_SEQUENCES[0x1B] = '\\e';
  2240. ESCAPE_SEQUENCES[0x22] = '\\"';
  2241. ESCAPE_SEQUENCES[0x5C] = '\\\\';
  2242. ESCAPE_SEQUENCES[0x85] = '\\N';
  2243. ESCAPE_SEQUENCES[0xA0] = '\\_';
  2244. ESCAPE_SEQUENCES[0x2028] = '\\L';
  2245. ESCAPE_SEQUENCES[0x2029] = '\\P';
  2246. var DEPRECATED_BOOLEANS_SYNTAX = [
  2247. 'y', 'Y', 'yes', 'Yes', 'YES', 'on', 'On', 'ON',
  2248. 'n', 'N', 'no', 'No', 'NO', 'off', 'Off', 'OFF'
  2249. ];
  2250. var DEPRECATED_BASE60_SYNTAX = /^[-+]?[0-9_]+(?::[0-9_]+)+(?:\.[0-9_]*)?$/;
  2251. function compileStyleMap(schema, map) {
  2252. var result, keys, index, length, tag, style, type;
  2253. if (map === null) return {};
  2254. result = {};
  2255. keys = Object.keys(map);
  2256. for (index = 0, length = keys.length; index < length; index += 1) {
  2257. tag = keys[index];
  2258. style = String(map[tag]);
  2259. if (tag.slice(0, 2) === '!!') {
  2260. tag = 'tag:yaml.org,2002:' + tag.slice(2);
  2261. }
  2262. type = schema.compiledTypeMap['fallback'][tag];
  2263. if (type && _hasOwnProperty.call(type.styleAliases, style)) {
  2264. style = type.styleAliases[style];
  2265. }
  2266. result[tag] = style;
  2267. }
  2268. return result;
  2269. }
  2270. function encodeHex(character) {
  2271. var string, handle, length;
  2272. string = character.toString(16).toUpperCase();
  2273. if (character <= 0xFF) {
  2274. handle = 'x';
  2275. length = 2;
  2276. } else if (character <= 0xFFFF) {
  2277. handle = 'u';
  2278. length = 4;
  2279. } else if (character <= 0xFFFFFFFF) {
  2280. handle = 'U';
  2281. length = 8;
  2282. } else {
  2283. throw new exception('code point within a string may not be greater than 0xFFFFFFFF');
  2284. }
  2285. return '\\' + handle + common.repeat('0', length - string.length) + string;
  2286. }
  2287. var QUOTING_TYPE_SINGLE = 1,
  2288. QUOTING_TYPE_DOUBLE = 2;
  2289. function State(options) {
  2290. this.schema = options['schema'] || _default;
  2291. this.indent = Math.max(1, (options['indent'] || 2));
  2292. this.noArrayIndent = options['noArrayIndent'] || false;
  2293. this.skipInvalid = options['skipInvalid'] || false;
  2294. this.flowLevel = (common.isNothing(options['flowLevel']) ? -1 : options['flowLevel']);
  2295. this.styleMap = compileStyleMap(this.schema, options['styles'] || null);
  2296. this.sortKeys = options['sortKeys'] || false;
  2297. this.lineWidth = options['lineWidth'] || 80;
  2298. this.noRefs = options['noRefs'] || false;
  2299. this.noCompatMode = options['noCompatMode'] || false;
  2300. this.condenseFlow = options['condenseFlow'] || false;
  2301. this.quotingType = options['quotingType'] === '"' ? QUOTING_TYPE_DOUBLE : QUOTING_TYPE_SINGLE;
  2302. this.forceQuotes = options['forceQuotes'] || false;
  2303. this.replacer = typeof options['replacer'] === 'function' ? options['replacer'] : null;
  2304. this.implicitTypes = this.schema.compiledImplicit;
  2305. this.explicitTypes = this.schema.compiledExplicit;
  2306. this.tag = null;
  2307. this.result = '';
  2308. this.duplicates = [];
  2309. this.usedDuplicates = null;
  2310. }
  2311. // Indents every line in a string. Empty lines (\n only) are not indented.
  2312. function indentString(string, spaces) {
  2313. var ind = common.repeat(' ', spaces),
  2314. position = 0,
  2315. next = -1,
  2316. result = '',
  2317. line,
  2318. length = string.length;
  2319. while (position < length) {
  2320. next = string.indexOf('\n', position);
  2321. if (next === -1) {
  2322. line = string.slice(position);
  2323. position = length;
  2324. } else {
  2325. line = string.slice(position, next + 1);
  2326. position = next + 1;
  2327. }
  2328. if (line.length && line !== '\n') result += ind;
  2329. result += line;
  2330. }
  2331. return result;
  2332. }
  2333. function generateNextLine(state, level) {
  2334. return '\n' + common.repeat(' ', state.indent * level);
  2335. }
  2336. function testImplicitResolving(state, str) {
  2337. var index, length, type;
  2338. for (index = 0, length = state.implicitTypes.length; index < length; index += 1) {
  2339. type = state.implicitTypes[index];
  2340. if (type.resolve(str)) {
  2341. return true;
  2342. }
  2343. }
  2344. return false;
  2345. }
  2346. // [33] s-white ::= s-space | s-tab
  2347. function isWhitespace(c) {
  2348. return c === CHAR_SPACE || c === CHAR_TAB;
  2349. }
  2350. // Returns true if the character can be printed without escaping.
  2351. // From YAML 1.2: "any allowed characters known to be non-printable
  2352. // should also be escaped. [However,] This isn’t mandatory"
  2353. // Derived from nb-char - \t - #x85 - #xA0 - #x2028 - #x2029.
  2354. function isPrintable(c) {
  2355. return (0x00020 <= c && c <= 0x00007E)
  2356. || ((0x000A1 <= c && c <= 0x00D7FF) && c !== 0x2028 && c !== 0x2029)
  2357. || ((0x0E000 <= c && c <= 0x00FFFD) && c !== CHAR_BOM)
  2358. || (0x10000 <= c && c <= 0x10FFFF);
  2359. }
  2360. // [34] ns-char ::= nb-char - s-white
  2361. // [27] nb-char ::= c-printable - b-char - c-byte-order-mark
  2362. // [26] b-char ::= b-line-feed | b-carriage-return
  2363. // Including s-white (for some reason, examples doesn't match specs in this aspect)
  2364. // ns-char ::= c-printable - b-line-feed - b-carriage-return - c-byte-order-mark
  2365. function isNsCharOrWhitespace(c) {
  2366. return isPrintable(c)
  2367. && c !== CHAR_BOM
  2368. // - b-char
  2369. && c !== CHAR_CARRIAGE_RETURN
  2370. && c !== CHAR_LINE_FEED;
  2371. }
  2372. // [127] ns-plain-safe(c) ::= c = flow-out ⇒ ns-plain-safe-out
  2373. // c = flow-in ⇒ ns-plain-safe-in
  2374. // c = block-key ⇒ ns-plain-safe-out
  2375. // c = flow-key ⇒ ns-plain-safe-in
  2376. // [128] ns-plain-safe-out ::= ns-char
  2377. // [129] ns-plain-safe-in ::= ns-char - c-flow-indicator
  2378. // [130] ns-plain-char(c) ::= ( ns-plain-safe(c) - “:” - “#” )
  2379. // | ( /* An ns-char preceding */ “#” )
  2380. // | ( “:” /* Followed by an ns-plain-safe(c) */ )
  2381. function isPlainSafe(c, prev, inblock) {
  2382. var cIsNsCharOrWhitespace = isNsCharOrWhitespace(c);
  2383. var cIsNsChar = cIsNsCharOrWhitespace && !isWhitespace(c);
  2384. return (
  2385. // ns-plain-safe
  2386. inblock ? // c = flow-in
  2387. cIsNsCharOrWhitespace
  2388. : cIsNsCharOrWhitespace
  2389. // - c-flow-indicator
  2390. && c !== CHAR_COMMA
  2391. && c !== CHAR_LEFT_SQUARE_BRACKET
  2392. && c !== CHAR_RIGHT_SQUARE_BRACKET
  2393. && c !== CHAR_LEFT_CURLY_BRACKET
  2394. && c !== CHAR_RIGHT_CURLY_BRACKET
  2395. )
  2396. // ns-plain-char
  2397. && c !== CHAR_SHARP // false on '#'
  2398. && !(prev === CHAR_COLON && !cIsNsChar) // false on ': '
  2399. || (isNsCharOrWhitespace(prev) && !isWhitespace(prev) && c === CHAR_SHARP) // change to true on '[^ ]#'
  2400. || (prev === CHAR_COLON && cIsNsChar); // change to true on ':[^ ]'
  2401. }
  2402. // Simplified test for values allowed as the first character in plain style.
  2403. function isPlainSafeFirst(c) {
  2404. // Uses a subset of ns-char - c-indicator
  2405. // where ns-char = nb-char - s-white.
  2406. // No support of ( ( “?” | “:” | “-” ) /* Followed by an ns-plain-safe(c)) */ ) part
  2407. return isPrintable(c) && c !== CHAR_BOM
  2408. && !isWhitespace(c) // - s-white
  2409. // - (c-indicator ::=
  2410. // “-” | “?” | “:” | “,” | “[” | “]” | “{” | “}”
  2411. && c !== CHAR_MINUS
  2412. && c !== CHAR_QUESTION
  2413. && c !== CHAR_COLON
  2414. && c !== CHAR_COMMA
  2415. && c !== CHAR_LEFT_SQUARE_BRACKET
  2416. && c !== CHAR_RIGHT_SQUARE_BRACKET
  2417. && c !== CHAR_LEFT_CURLY_BRACKET
  2418. && c !== CHAR_RIGHT_CURLY_BRACKET
  2419. // | “#” | “&” | “*” | “!” | “|” | “=” | “>” | “'” | “"”
  2420. && c !== CHAR_SHARP
  2421. && c !== CHAR_AMPERSAND
  2422. && c !== CHAR_ASTERISK
  2423. && c !== CHAR_EXCLAMATION
  2424. && c !== CHAR_VERTICAL_LINE
  2425. && c !== CHAR_EQUALS
  2426. && c !== CHAR_GREATER_THAN
  2427. && c !== CHAR_SINGLE_QUOTE
  2428. && c !== CHAR_DOUBLE_QUOTE
  2429. // | “%” | “@” | “`”)
  2430. && c !== CHAR_PERCENT
  2431. && c !== CHAR_COMMERCIAL_AT
  2432. && c !== CHAR_GRAVE_ACCENT;
  2433. }
  2434. // Simplified test for values allowed as the last character in plain style.
  2435. function isPlainSafeLast(c) {
  2436. // just not whitespace or colon, it will be checked to be plain character later
  2437. return !isWhitespace(c) && c !== CHAR_COLON;
  2438. }
  2439. // Same as 'string'.codePointAt(pos), but works in older browsers.
  2440. function codePointAt(string, pos) {
  2441. var first = string.charCodeAt(pos), second;
  2442. if (first >= 0xD800 && first <= 0xDBFF && pos + 1 < string.length) {
  2443. second = string.charCodeAt(pos + 1);
  2444. if (second >= 0xDC00 && second <= 0xDFFF) {
  2445. // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
  2446. return (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
  2447. }
  2448. }
  2449. return first;
  2450. }
  2451. // Determines whether block indentation indicator is required.
  2452. function needIndentIndicator(string) {
  2453. var leadingSpaceRe = /^\n* /;
  2454. return leadingSpaceRe.test(string);
  2455. }
  2456. var STYLE_PLAIN = 1,
  2457. STYLE_SINGLE = 2,
  2458. STYLE_LITERAL = 3,
  2459. STYLE_FOLDED = 4,
  2460. STYLE_DOUBLE = 5;
  2461. // Determines which scalar styles are possible and returns the preferred style.
  2462. // lineWidth = -1 => no limit.
  2463. // Pre-conditions: str.length > 0.
  2464. // Post-conditions:
  2465. // STYLE_PLAIN or STYLE_SINGLE => no \n are in the string.
  2466. // STYLE_LITERAL => no lines are suitable for folding (or lineWidth is -1).
  2467. // STYLE_FOLDED => a line > lineWidth and can be folded (and lineWidth != -1).
  2468. function chooseScalarStyle(string, singleLineOnly, indentPerLevel, lineWidth,
  2469. testAmbiguousType, quotingType, forceQuotes, inblock) {
  2470. var i;
  2471. var char = 0;
  2472. var prevChar = null;
  2473. var hasLineBreak = false;
  2474. var hasFoldableLine = false; // only checked if shouldTrackWidth
  2475. var shouldTrackWidth = lineWidth !== -1;
  2476. var previousLineBreak = -1; // count the first line correctly
  2477. var plain = isPlainSafeFirst(codePointAt(string, 0))
  2478. && isPlainSafeLast(codePointAt(string, string.length - 1));
  2479. if (singleLineOnly || forceQuotes) {
  2480. // Case: no block styles.
  2481. // Check for disallowed characters to rule out plain and single.
  2482. for (i = 0; i < string.length; char >= 0x10000 ? i += 2 : i++) {
  2483. char = codePointAt(string, i);
  2484. if (!isPrintable(char)) {
  2485. return STYLE_DOUBLE;
  2486. }
  2487. plain = plain && isPlainSafe(char, prevChar, inblock);
  2488. prevChar = char;
  2489. }
  2490. } else {
  2491. // Case: block styles permitted.
  2492. for (i = 0; i < string.length; char >= 0x10000 ? i += 2 : i++) {
  2493. char = codePointAt(string, i);
  2494. if (char === CHAR_LINE_FEED) {
  2495. hasLineBreak = true;
  2496. // Check if any line can be folded.
  2497. if (shouldTrackWidth) {
  2498. hasFoldableLine = hasFoldableLine ||
  2499. // Foldable line = too long, and not more-indented.
  2500. (i - previousLineBreak - 1 > lineWidth &&
  2501. string[previousLineBreak + 1] !== ' ');
  2502. previousLineBreak = i;
  2503. }
  2504. } else if (!isPrintable(char)) {
  2505. return STYLE_DOUBLE;
  2506. }
  2507. plain = plain && isPlainSafe(char, prevChar, inblock);
  2508. prevChar = char;
  2509. }
  2510. // in case the end is missing a \n
  2511. hasFoldableLine = hasFoldableLine || (shouldTrackWidth &&
  2512. (i - previousLineBreak - 1 > lineWidth &&
  2513. string[previousLineBreak + 1] !== ' '));
  2514. }
  2515. // Although every style can represent \n without escaping, prefer block styles
  2516. // for multiline, since they're more readable and they don't add empty lines.
  2517. // Also prefer folding a super-long line.
  2518. if (!hasLineBreak && !hasFoldableLine) {
  2519. // Strings interpretable as another type have to be quoted;
  2520. // e.g. the string 'true' vs. the boolean true.
  2521. if (plain && !forceQuotes && !testAmbiguousType(string)) {
  2522. return STYLE_PLAIN;
  2523. }
  2524. return quotingType === QUOTING_TYPE_DOUBLE ? STYLE_DOUBLE : STYLE_SINGLE;
  2525. }
  2526. // Edge case: block indentation indicator can only have one digit.
  2527. if (indentPerLevel > 9 && needIndentIndicator(string)) {
  2528. return STYLE_DOUBLE;
  2529. }
  2530. // At this point we know block styles are valid.
  2531. // Prefer literal style unless we want to fold.
  2532. if (!forceQuotes) {
  2533. return hasFoldableLine ? STYLE_FOLDED : STYLE_LITERAL;
  2534. }
  2535. return quotingType === QUOTING_TYPE_DOUBLE ? STYLE_DOUBLE : STYLE_SINGLE;
  2536. }
  2537. // Note: line breaking/folding is implemented for only the folded style.
  2538. // NB. We drop the last trailing newline (if any) of a returned block scalar
  2539. // since the dumper adds its own newline. This always works:
  2540. // • No ending newline => unaffected; already using strip "-" chomping.
  2541. // • Ending newline => removed then restored.
  2542. // Importantly, this keeps the "+" chomp indicator from gaining an extra line.
  2543. function writeScalar(state, string, level, iskey, inblock) {
  2544. state.dump = (function () {
  2545. if (string.length === 0) {
  2546. return state.quotingType === QUOTING_TYPE_DOUBLE ? '""' : "''";
  2547. }
  2548. if (!state.noCompatMode) {
  2549. if (DEPRECATED_BOOLEANS_SYNTAX.indexOf(string) !== -1 || DEPRECATED_BASE60_SYNTAX.test(string)) {
  2550. return state.quotingType === QUOTING_TYPE_DOUBLE ? ('"' + string + '"') : ("'" + string + "'");
  2551. }
  2552. }
  2553. var indent = state.indent * Math.max(1, level); // no 0-indent scalars
  2554. // As indentation gets deeper, let the width decrease monotonically
  2555. // to the lower bound min(state.lineWidth, 40).
  2556. // Note that this implies
  2557. // state.lineWidth ≤ 40 + state.indent: width is fixed at the lower bound.
  2558. // state.lineWidth > 40 + state.indent: width decreases until the lower bound.
  2559. // This behaves better than a constant minimum width which disallows narrower options,
  2560. // or an indent threshold which causes the width to suddenly increase.
  2561. var lineWidth = state.lineWidth === -1
  2562. ? -1 : Math.max(Math.min(state.lineWidth, 40), state.lineWidth - indent);
  2563. // Without knowing if keys are implicit/explicit, assume implicit for safety.
  2564. var singleLineOnly = iskey
  2565. // No block styles in flow mode.
  2566. || (state.flowLevel > -1 && level >= state.flowLevel);
  2567. function testAmbiguity(string) {
  2568. return testImplicitResolving(state, string);
  2569. }
  2570. switch (chooseScalarStyle(string, singleLineOnly, state.indent, lineWidth,
  2571. testAmbiguity, state.quotingType, state.forceQuotes && !iskey, inblock)) {
  2572. case STYLE_PLAIN:
  2573. return string;
  2574. case STYLE_SINGLE:
  2575. return "'" + string.replace(/'/g, "''") + "'";
  2576. case STYLE_LITERAL:
  2577. return '|' + blockHeader(string, state.indent)
  2578. + dropEndingNewline(indentString(string, indent));
  2579. case STYLE_FOLDED:
  2580. return '>' + blockHeader(string, state.indent)
  2581. + dropEndingNewline(indentString(foldString(string, lineWidth), indent));
  2582. case STYLE_DOUBLE:
  2583. return '"' + escapeString(string) + '"';
  2584. default:
  2585. throw new exception('impossible error: invalid scalar style');
  2586. }
  2587. }());
  2588. }
  2589. // Pre-conditions: string is valid for a block scalar, 1 <= indentPerLevel <= 9.
  2590. function blockHeader(string, indentPerLevel) {
  2591. var indentIndicator = needIndentIndicator(string) ? String(indentPerLevel) : '';
  2592. // note the special case: the string '\n' counts as a "trailing" empty line.
  2593. var clip = string[string.length - 1] === '\n';
  2594. var keep = clip && (string[string.length - 2] === '\n' || string === '\n');
  2595. var chomp = keep ? '+' : (clip ? '' : '-');
  2596. return indentIndicator + chomp + '\n';
  2597. }
  2598. // (See the note for writeScalar.)
  2599. function dropEndingNewline(string) {
  2600. return string[string.length - 1] === '\n' ? string.slice(0, -1) : string;
  2601. }
  2602. // Note: a long line without a suitable break point will exceed the width limit.
  2603. // Pre-conditions: every char in str isPrintable, str.length > 0, width > 0.
  2604. function foldString(string, width) {
  2605. // In folded style, $k$ consecutive newlines output as $k+1$ newlines—
  2606. // unless they're before or after a more-indented line, or at the very
  2607. // beginning or end, in which case $k$ maps to $k$.
  2608. // Therefore, parse each chunk as newline(s) followed by a content line.
  2609. var lineRe = /(\n+)([^\n]*)/g;
  2610. // first line (possibly an empty line)
  2611. var result = (function () {
  2612. var nextLF = string.indexOf('\n');
  2613. nextLF = nextLF !== -1 ? nextLF : string.length;
  2614. lineRe.lastIndex = nextLF;
  2615. return foldLine(string.slice(0, nextLF), width);
  2616. }());
  2617. // If we haven't reached the first content line yet, don't add an extra \n.
  2618. var prevMoreIndented = string[0] === '\n' || string[0] === ' ';
  2619. var moreIndented;
  2620. // rest of the lines
  2621. var match;
  2622. while ((match = lineRe.exec(string))) {
  2623. var prefix = match[1], line = match[2];
  2624. moreIndented = (line[0] === ' ');
  2625. result += prefix
  2626. + (!prevMoreIndented && !moreIndented && line !== ''
  2627. ? '\n' : '')
  2628. + foldLine(line, width);
  2629. prevMoreIndented = moreIndented;
  2630. }
  2631. return result;
  2632. }
  2633. // Greedy line breaking.
  2634. // Picks the longest line under the limit each time,
  2635. // otherwise settles for the shortest line over the limit.
  2636. // NB. More-indented lines *cannot* be folded, as that would add an extra \n.
  2637. function foldLine(line, width) {
  2638. if (line === '' || line[0] === ' ') return line;
  2639. // Since a more-indented line adds a \n, breaks can't be followed by a space.
  2640. var breakRe = / [^ ]/g; // note: the match index will always be <= length-2.
  2641. var match;
  2642. // start is an inclusive index. end, curr, and next are exclusive.
  2643. var start = 0, end, curr = 0, next = 0;
  2644. var result = '';
  2645. // Invariants: 0 <= start <= length-1.
  2646. // 0 <= curr <= next <= max(0, length-2). curr - start <= width.
  2647. // Inside the loop:
  2648. // A match implies length >= 2, so curr and next are <= length-2.
  2649. while ((match = breakRe.exec(line))) {
  2650. next = match.index;
  2651. // maintain invariant: curr - start <= width
  2652. if (next - start > width) {
  2653. end = (curr > start) ? curr : next; // derive end <= length-2
  2654. result += '\n' + line.slice(start, end);
  2655. // skip the space that was output as \n
  2656. start = end + 1; // derive start <= length-1
  2657. }
  2658. curr = next;
  2659. }
  2660. // By the invariants, start <= length-1, so there is something left over.
  2661. // It is either the whole string or a part starting from non-whitespace.
  2662. result += '\n';
  2663. // Insert a break if the remainder is too long and there is a break available.
  2664. if (line.length - start > width && curr > start) {
  2665. result += line.slice(start, curr) + '\n' + line.slice(curr + 1);
  2666. } else {
  2667. result += line.slice(start);
  2668. }
  2669. return result.slice(1); // drop extra \n joiner
  2670. }
  2671. // Escapes a double-quoted string.
  2672. function escapeString(string) {
  2673. var result = '';
  2674. var char = 0;
  2675. var escapeSeq;
  2676. for (var i = 0; i < string.length; char >= 0x10000 ? i += 2 : i++) {
  2677. char = codePointAt(string, i);
  2678. escapeSeq = ESCAPE_SEQUENCES[char];
  2679. if (!escapeSeq && isPrintable(char)) {
  2680. result += string[i];
  2681. if (char >= 0x10000) result += string[i + 1];
  2682. } else {
  2683. result += escapeSeq || encodeHex(char);
  2684. }
  2685. }
  2686. return result;
  2687. }
  2688. function writeFlowSequence(state, level, object) {
  2689. var _result = '',
  2690. _tag = state.tag,
  2691. index,
  2692. length,
  2693. value;
  2694. for (index = 0, length = object.length; index < length; index += 1) {
  2695. value = object[index];
  2696. if (state.replacer) {
  2697. value = state.replacer.call(object, String(index), value);
  2698. }
  2699. // Write only valid elements, put null instead of invalid elements.
  2700. if (writeNode(state, level, value, false, false) ||
  2701. (typeof value === 'undefined' &&
  2702. writeNode(state, level, null, false, false))) {
  2703. if (_result !== '') _result += ',' + (!state.condenseFlow ? ' ' : '');
  2704. _result += state.dump;
  2705. }
  2706. }
  2707. state.tag = _tag;
  2708. state.dump = '[' + _result + ']';
  2709. }
  2710. function writeBlockSequence(state, level, object, compact) {
  2711. var _result = '',
  2712. _tag = state.tag,
  2713. index,
  2714. length,
  2715. value;
  2716. for (index = 0, length = object.length; index < length; index += 1) {
  2717. value = object[index];
  2718. if (state.replacer) {
  2719. value = state.replacer.call(object, String(index), value);
  2720. }
  2721. // Write only valid elements, put null instead of invalid elements.
  2722. if (writeNode(state, level + 1, value, true, true, false, true) ||
  2723. (typeof value === 'undefined' &&
  2724. writeNode(state, level + 1, null, true, true, false, true))) {
  2725. if (!compact || _result !== '') {
  2726. _result += generateNextLine(state, level);
  2727. }
  2728. if (state.dump && CHAR_LINE_FEED === state.dump.charCodeAt(0)) {
  2729. _result += '-';
  2730. } else {
  2731. _result += '- ';
  2732. }
  2733. _result += state.dump;
  2734. }
  2735. }
  2736. state.tag = _tag;
  2737. state.dump = _result || '[]'; // Empty sequence if no valid values.
  2738. }
  2739. function writeFlowMapping(state, level, object) {
  2740. var _result = '',
  2741. _tag = state.tag,
  2742. objectKeyList = Object.keys(object),
  2743. index,
  2744. length,
  2745. objectKey,
  2746. objectValue,
  2747. pairBuffer;
  2748. for (index = 0, length = objectKeyList.length; index < length; index += 1) {
  2749. pairBuffer = '';
  2750. if (_result !== '') pairBuffer += ', ';
  2751. if (state.condenseFlow) pairBuffer += '"';
  2752. objectKey = objectKeyList[index];
  2753. objectValue = object[objectKey];
  2754. if (state.replacer) {
  2755. objectValue = state.replacer.call(object, objectKey, objectValue);
  2756. }
  2757. if (!writeNode(state, level, objectKey, false, false)) {
  2758. continue; // Skip this pair because of invalid key;
  2759. }
  2760. if (state.dump.length > 1024) pairBuffer += '? ';
  2761. pairBuffer += state.dump + (state.condenseFlow ? '"' : '') + ':' + (state.condenseFlow ? '' : ' ');
  2762. if (!writeNode(state, level, objectValue, false, false)) {
  2763. continue; // Skip this pair because of invalid value.
  2764. }
  2765. pairBuffer += state.dump;
  2766. // Both key and value are valid.
  2767. _result += pairBuffer;
  2768. }
  2769. state.tag = _tag;
  2770. state.dump = '{' + _result + '}';
  2771. }
  2772. function writeBlockMapping(state, level, object, compact) {
  2773. var _result = '',
  2774. _tag = state.tag,
  2775. objectKeyList = Object.keys(object),
  2776. index,
  2777. length,
  2778. objectKey,
  2779. objectValue,
  2780. explicitPair,
  2781. pairBuffer;
  2782. // Allow sorting keys so that the output file is deterministic
  2783. if (state.sortKeys === true) {
  2784. // Default sorting
  2785. objectKeyList.sort();
  2786. } else if (typeof state.sortKeys === 'function') {
  2787. // Custom sort function
  2788. objectKeyList.sort(state.sortKeys);
  2789. } else if (state.sortKeys) {
  2790. // Something is wrong
  2791. throw new exception('sortKeys must be a boolean or a function');
  2792. }
  2793. for (index = 0, length = objectKeyList.length; index < length; index += 1) {
  2794. pairBuffer = '';
  2795. if (!compact || _result !== '') {
  2796. pairBuffer += generateNextLine(state, level);
  2797. }
  2798. objectKey = objectKeyList[index];
  2799. objectValue = object[objectKey];
  2800. if (state.replacer) {
  2801. objectValue = state.replacer.call(object, objectKey, objectValue);
  2802. }
  2803. if (!writeNode(state, level + 1, objectKey, true, true, true)) {
  2804. continue; // Skip this pair because of invalid key.
  2805. }
  2806. explicitPair = (state.tag !== null && state.tag !== '?') ||
  2807. (state.dump && state.dump.length > 1024);
  2808. if (explicitPair) {
  2809. if (state.dump && CHAR_LINE_FEED === state.dump.charCodeAt(0)) {
  2810. pairBuffer += '?';
  2811. } else {
  2812. pairBuffer += '? ';
  2813. }
  2814. }
  2815. pairBuffer += state.dump;
  2816. if (explicitPair) {
  2817. pairBuffer += generateNextLine(state, level);
  2818. }
  2819. if (!writeNode(state, level + 1, objectValue, true, explicitPair)) {
  2820. continue; // Skip this pair because of invalid value.
  2821. }
  2822. if (state.dump && CHAR_LINE_FEED === state.dump.charCodeAt(0)) {
  2823. pairBuffer += ':';
  2824. } else {
  2825. pairBuffer += ': ';
  2826. }
  2827. pairBuffer += state.dump;
  2828. // Both key and value are valid.
  2829. _result += pairBuffer;
  2830. }
  2831. state.tag = _tag;
  2832. state.dump = _result || '{}'; // Empty mapping if no valid pairs.
  2833. }
  2834. function detectType(state, object, explicit) {
  2835. var _result, typeList, index, length, type, style;
  2836. typeList = explicit ? state.explicitTypes : state.implicitTypes;
  2837. for (index = 0, length = typeList.length; index < length; index += 1) {
  2838. type = typeList[index];
  2839. if ((type.instanceOf || type.predicate) &&
  2840. (!type.instanceOf || ((typeof object === 'object') && (object instanceof type.instanceOf))) &&
  2841. (!type.predicate || type.predicate(object))) {
  2842. if (explicit) {
  2843. if (type.multi && type.representName) {
  2844. state.tag = type.representName(object);
  2845. } else {
  2846. state.tag = type.tag;
  2847. }
  2848. } else {
  2849. state.tag = '?';
  2850. }
  2851. if (type.represent) {
  2852. style = state.styleMap[type.tag] || type.defaultStyle;
  2853. if (_toString.call(type.represent) === '[object Function]') {
  2854. _result = type.represent(object, style);
  2855. } else if (_hasOwnProperty.call(type.represent, style)) {
  2856. _result = type.represent[style](object, style);
  2857. } else {
  2858. throw new exception('!<' + type.tag + '> tag resolver accepts not "' + style + '" style');
  2859. }
  2860. state.dump = _result;
  2861. }
  2862. return true;
  2863. }
  2864. }
  2865. return false;
  2866. }
  2867. // Serializes `object` and writes it to global `result`.
  2868. // Returns true on success, or false on invalid object.
  2869. //
  2870. function writeNode(state, level, object, block, compact, iskey, isblockseq) {
  2871. state.tag = null;
  2872. state.dump = object;
  2873. if (!detectType(state, object, false)) {
  2874. detectType(state, object, true);
  2875. }
  2876. var type = _toString.call(state.dump);
  2877. var inblock = block;
  2878. var tagStr;
  2879. if (block) {
  2880. block = (state.flowLevel < 0 || state.flowLevel > level);
  2881. }
  2882. var objectOrArray = type === '[object Object]' || type === '[object Array]',
  2883. duplicateIndex,
  2884. duplicate;
  2885. if (objectOrArray) {
  2886. duplicateIndex = state.duplicates.indexOf(object);
  2887. duplicate = duplicateIndex !== -1;
  2888. }
  2889. if ((state.tag !== null && state.tag !== '?') || duplicate || (state.indent !== 2 && level > 0)) {
  2890. compact = false;
  2891. }
  2892. if (duplicate && state.usedDuplicates[duplicateIndex]) {
  2893. state.dump = '*ref_' + duplicateIndex;
  2894. } else {
  2895. if (objectOrArray && duplicate && !state.usedDuplicates[duplicateIndex]) {
  2896. state.usedDuplicates[duplicateIndex] = true;
  2897. }
  2898. if (type === '[object Object]') {
  2899. if (block && (Object.keys(state.dump).length !== 0)) {
  2900. writeBlockMapping(state, level, state.dump, compact);
  2901. if (duplicate) {
  2902. state.dump = '&ref_' + duplicateIndex + state.dump;
  2903. }
  2904. } else {
  2905. writeFlowMapping(state, level, state.dump);
  2906. if (duplicate) {
  2907. state.dump = '&ref_' + duplicateIndex + ' ' + state.dump;
  2908. }
  2909. }
  2910. } else if (type === '[object Array]') {
  2911. if (block && (state.dump.length !== 0)) {
  2912. if (state.noArrayIndent && !isblockseq && level > 0) {
  2913. writeBlockSequence(state, level - 1, state.dump, compact);
  2914. } else {
  2915. writeBlockSequence(state, level, state.dump, compact);
  2916. }
  2917. if (duplicate) {
  2918. state.dump = '&ref_' + duplicateIndex + state.dump;
  2919. }
  2920. } else {
  2921. writeFlowSequence(state, level, state.dump);
  2922. if (duplicate) {
  2923. state.dump = '&ref_' + duplicateIndex + ' ' + state.dump;
  2924. }
  2925. }
  2926. } else if (type === '[object String]') {
  2927. if (state.tag !== '?') {
  2928. writeScalar(state, state.dump, level, iskey, inblock);
  2929. }
  2930. } else if (type === '[object Undefined]') {
  2931. return false;
  2932. } else {
  2933. if (state.skipInvalid) return false;
  2934. throw new exception('unacceptable kind of an object to dump ' + type);
  2935. }
  2936. if (state.tag !== null && state.tag !== '?') {
  2937. // Need to encode all characters except those allowed by the spec:
  2938. //
  2939. // [35] ns-dec-digit ::= [#x30-#x39] /* 0-9 */
  2940. // [36] ns-hex-digit ::= ns-dec-digit
  2941. // | [#x41-#x46] /* A-F */ | [#x61-#x66] /* a-f */
  2942. // [37] ns-ascii-letter ::= [#x41-#x5A] /* A-Z */ | [#x61-#x7A] /* a-z */
  2943. // [38] ns-word-char ::= ns-dec-digit | ns-ascii-letter | “-”
  2944. // [39] ns-uri-char ::= “%” ns-hex-digit ns-hex-digit | ns-word-char | “#”
  2945. // | “;” | “/” | “?” | “:” | “@” | “&” | “=” | “+” | “$” | “,”
  2946. // | “_” | “.” | “!” | “~” | “*” | “'” | “(” | “)” | “[” | “]”
  2947. //
  2948. // Also need to encode '!' because it has special meaning (end of tag prefix).
  2949. //
  2950. tagStr = encodeURI(
  2951. state.tag[0] === '!' ? state.tag.slice(1) : state.tag
  2952. ).replace(/!/g, '%21');
  2953. if (state.tag[0] === '!') {
  2954. tagStr = '!' + tagStr;
  2955. } else if (tagStr.slice(0, 18) === 'tag:yaml.org,2002:') {
  2956. tagStr = '!!' + tagStr.slice(18);
  2957. } else {
  2958. tagStr = '!<' + tagStr + '>';
  2959. }
  2960. state.dump = tagStr + ' ' + state.dump;
  2961. }
  2962. }
  2963. return true;
  2964. }
  2965. function getDuplicateReferences(object, state) {
  2966. var objects = [],
  2967. duplicatesIndexes = [],
  2968. index,
  2969. length;
  2970. inspectNode(object, objects, duplicatesIndexes);
  2971. for (index = 0, length = duplicatesIndexes.length; index < length; index += 1) {
  2972. state.duplicates.push(objects[duplicatesIndexes[index]]);
  2973. }
  2974. state.usedDuplicates = new Array(length);
  2975. }
  2976. function inspectNode(object, objects, duplicatesIndexes) {
  2977. var objectKeyList,
  2978. index,
  2979. length;
  2980. if (object !== null && typeof object === 'object') {
  2981. index = objects.indexOf(object);
  2982. if (index !== -1) {
  2983. if (duplicatesIndexes.indexOf(index) === -1) {
  2984. duplicatesIndexes.push(index);
  2985. }
  2986. } else {
  2987. objects.push(object);
  2988. if (Array.isArray(object)) {
  2989. for (index = 0, length = object.length; index < length; index += 1) {
  2990. inspectNode(object[index], objects, duplicatesIndexes);
  2991. }
  2992. } else {
  2993. objectKeyList = Object.keys(object);
  2994. for (index = 0, length = objectKeyList.length; index < length; index += 1) {
  2995. inspectNode(object[objectKeyList[index]], objects, duplicatesIndexes);
  2996. }
  2997. }
  2998. }
  2999. }
  3000. }
  3001. function dump$1(input, options) {
  3002. options = options || {};
  3003. var state = new State(options);
  3004. if (!state.noRefs) getDuplicateReferences(input, state);
  3005. var value = input;
  3006. if (state.replacer) {
  3007. value = state.replacer.call({ '': value }, '', value);
  3008. }
  3009. if (writeNode(state, 0, value, true, true)) return state.dump + '\n';
  3010. return '';
  3011. }
  3012. var dump_1 = dump$1;
  3013. var dumper = {
  3014. dump: dump_1
  3015. };
  3016. function renamed(from, to) {
  3017. return function () {
  3018. throw new Error('Function yaml.' + from + ' is removed in js-yaml 4. ' +
  3019. 'Use yaml.' + to + ' instead, which is now safe by default.');
  3020. };
  3021. }
  3022. var Type = type;
  3023. var Schema = schema;
  3024. var FAILSAFE_SCHEMA = failsafe;
  3025. var JSON_SCHEMA = json;
  3026. var CORE_SCHEMA = core;
  3027. var DEFAULT_SCHEMA = _default;
  3028. var load = loader.load;
  3029. var loadAll = loader.loadAll;
  3030. var dump = dumper.dump;
  3031. var YAMLException = exception;
  3032. // Re-export all types in case user wants to create custom schema
  3033. var types = {
  3034. binary: binary,
  3035. float: float,
  3036. map: map,
  3037. null: _null,
  3038. pairs: pairs,
  3039. set: set,
  3040. timestamp: timestamp,
  3041. bool: bool,
  3042. int: int,
  3043. merge: merge,
  3044. omap: omap,
  3045. seq: seq,
  3046. str: str
  3047. };
  3048. // Removed functions from JS-YAML 3.0.x
  3049. var safeLoad = renamed('safeLoad', 'load');
  3050. var safeLoadAll = renamed('safeLoadAll', 'loadAll');
  3051. var safeDump = renamed('safeDump', 'dump');
  3052. var jsYaml = {
  3053. Type: Type,
  3054. Schema: Schema,
  3055. FAILSAFE_SCHEMA: FAILSAFE_SCHEMA,
  3056. JSON_SCHEMA: JSON_SCHEMA,
  3057. CORE_SCHEMA: CORE_SCHEMA,
  3058. DEFAULT_SCHEMA: DEFAULT_SCHEMA,
  3059. load: load,
  3060. loadAll: loadAll,
  3061. dump: dump,
  3062. YAMLException: YAMLException,
  3063. types: types,
  3064. safeLoad: safeLoad,
  3065. safeLoadAll: safeLoadAll,
  3066. safeDump: safeDump
  3067. };
  3068. exports.CORE_SCHEMA = CORE_SCHEMA;
  3069. exports.DEFAULT_SCHEMA = DEFAULT_SCHEMA;
  3070. exports.FAILSAFE_SCHEMA = FAILSAFE_SCHEMA;
  3071. exports.JSON_SCHEMA = JSON_SCHEMA;
  3072. exports.Schema = Schema;
  3073. exports.Type = Type;
  3074. exports.YAMLException = YAMLException;
  3075. exports["default"] = jsYaml;
  3076. exports.dump = dump;
  3077. exports.load = load;
  3078. exports.loadAll = loadAll;
  3079. exports.safeDump = safeDump;
  3080. exports.safeLoad = safeLoad;
  3081. exports.safeLoadAll = safeLoadAll;
  3082. exports.types = types;
  3083. Object.defineProperty(exports, '__esModule', { value: true });
  3084. }));