1
+ "use strict" ;
2
+
3
+ Object . defineProperty ( exports , "__esModule" , {
4
+ value : true
5
+ } ) ;
6
+ exports . default = exports . Chomp = void 0 ;
7
+
8
+ var _Node2 = _interopRequireWildcard ( require ( "./Node" ) ) ;
9
+
10
+ var _Range = _interopRequireDefault ( require ( "./Range" ) ) ;
11
+
12
+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
13
+
14
+ function _interopRequireWildcard ( obj ) { if ( obj && obj . __esModule ) { return obj ; } else { var newObj = { } ; if ( obj != null ) { for ( var key in obj ) { if ( Object . prototype . hasOwnProperty . call ( obj , key ) ) { var desc = Object . defineProperty && Object . getOwnPropertyDescriptor ? Object . getOwnPropertyDescriptor ( obj , key ) : { } ; if ( desc . get || desc . set ) { Object . defineProperty ( newObj , key , desc ) ; } else { newObj [ key ] = obj [ key ] ; } } } } newObj . default = obj ; return newObj ; } }
15
+
16
+ function _typeof ( obj ) { if ( typeof Symbol === "function" && typeof Symbol . iterator === "symbol" ) { _typeof = function _typeof ( obj ) { return typeof obj ; } ; } else { _typeof = function _typeof ( obj ) { return obj && typeof Symbol === "function" && obj . constructor === Symbol && obj !== Symbol . prototype ? "symbol" : typeof obj ; } ; } return _typeof ( obj ) ; }
17
+
18
+ function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
19
+
20
+ function _inherits ( subClass , superClass ) { if ( typeof superClass !== "function" && superClass !== null ) { throw new TypeError ( "Super expression must either be null or a function" ) ; } _setPrototypeOf ( subClass . prototype , superClass && superClass . prototype ) ; if ( superClass ) _setPrototypeOf ( subClass , superClass ) ; }
21
+
22
+ function _setPrototypeOf ( o , p ) { _setPrototypeOf = Object . setPrototypeOf || function _setPrototypeOf ( o , p ) { o . __proto__ = p ; return o ; } ; return _setPrototypeOf ( o , p ) ; }
23
+
24
+ function _defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( "value" in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } }
25
+
26
+ function _createClass ( Constructor , protoProps , staticProps ) { if ( protoProps ) _defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) _defineProperties ( Constructor , staticProps ) ; return Constructor ; }
27
+
28
+ function _possibleConstructorReturn ( self , call ) { if ( call && ( _typeof ( call ) === "object" || typeof call === "function" ) ) { return call ; } return _assertThisInitialized ( self ) ; }
29
+
30
+ function _assertThisInitialized ( self ) { if ( self === void 0 ) { throw new ReferenceError ( "this hasn't been initialised - super() hasn't been called" ) ; } return self ; }
31
+
32
+ function _getPrototypeOf ( o ) { _getPrototypeOf = Object . getPrototypeOf || function _getPrototypeOf ( o ) { return o . __proto__ ; } ; return _getPrototypeOf ( o ) ; }
33
+
34
+ var Chomp = {
35
+ CLIP : 'CLIP' ,
36
+ KEEP : 'KEEP' ,
37
+ STRIP : 'STRIP'
38
+ } ;
39
+ exports . Chomp = Chomp ;
40
+
41
+ var BlockValue =
42
+ /*#__PURE__*/
43
+ function ( _Node ) {
44
+ function BlockValue ( type , props ) {
45
+ var _this ;
46
+
47
+ _classCallCheck ( this , BlockValue ) ;
48
+
49
+ _this = _possibleConstructorReturn ( this , _getPrototypeOf ( BlockValue ) . call ( this , type , props ) ) ;
50
+ _this . blockIndent = null ;
51
+ _this . chomping = Chomp . CLIP ;
52
+ _this . header = null ;
53
+ return _this ;
54
+ }
55
+
56
+ _createClass ( BlockValue , [ {
57
+ key : "parseBlockHeader" ,
58
+ value : function parseBlockHeader ( start ) {
59
+ var src = this . context . src ;
60
+ var offset = start + 1 ;
61
+ var bi = '' ;
62
+
63
+ while ( true ) {
64
+ var ch = src [ offset ] ;
65
+
66
+ switch ( ch ) {
67
+ case '-' :
68
+ this . chomping = Chomp . STRIP ;
69
+ break ;
70
+
71
+ case '+' :
72
+ this . chomping = Chomp . KEEP ;
73
+ break ;
74
+
75
+ case '0' :
76
+ case '1' :
77
+ case '2' :
78
+ case '3' :
79
+ case '4' :
80
+ case '5' :
81
+ case '6' :
82
+ case '7' :
83
+ case '8' :
84
+ case '9' :
85
+ bi += ch ;
86
+ break ;
87
+
88
+ default :
89
+ this . blockIndent = Number ( bi ) || null ;
90
+ this . header = new _Range . default ( start , offset ) ;
91
+ return offset ;
92
+ }
93
+
94
+ offset += 1 ;
95
+ }
96
+ }
97
+ } , {
98
+ key : "parseBlockValue" ,
99
+ value : function parseBlockValue ( start ) {
100
+ var _this$context = this . context ,
101
+ indent = _this$context . indent ,
102
+ inFlow = _this$context . inFlow ,
103
+ src = _this$context . src ;
104
+ var offset = start ;
105
+ var bi = this . blockIndent ? indent + this . blockIndent - 1 : indent ;
106
+ var minBlockIndent = 1 ;
107
+
108
+ for ( var ch = src [ offset ] ; ch === '\n' ; ch = src [ offset ] ) {
109
+ offset += 1 ;
110
+ if ( _Node2 . default . atDocumentBoundary ( src , offset ) ) break ;
111
+
112
+ var end = _Node2 . default . endOfBlockIndent ( src , bi , offset ) ; // should not include tab?
113
+
114
+
115
+ if ( end === null ) break ;
116
+
117
+ if ( ! this . blockIndent ) {
118
+ // no explicit block indent, none yet detected
119
+ var lineIndent = end - ( offset + indent ) ;
120
+
121
+ if ( src [ end ] !== '\n' ) {
122
+ // first line with non-whitespace content
123
+ if ( lineIndent < minBlockIndent ) {
124
+ offset -= 1 ;
125
+ break ;
126
+ }
127
+
128
+ this . blockIndent = lineIndent ;
129
+ bi = indent + this . blockIndent - 1 ;
130
+ } else if ( lineIndent > minBlockIndent ) {
131
+ // empty line with more whitespace
132
+ minBlockIndent = lineIndent ;
133
+ }
134
+ }
135
+
136
+ offset = _Node2 . default . endOfLine ( src , end ) ;
137
+ }
138
+
139
+ this . valueRange = new _Range . default ( start + 1 , offset ) ;
140
+ return offset ;
141
+ }
142
+ /**
143
+ * Parses a block value from the source
144
+ *
145
+ * Accepted forms are:
146
+ * ```
147
+ * BS
148
+ * block
149
+ * lines
150
+ *
151
+ * BS #comment
152
+ * block
153
+ * lines
154
+ * ```
155
+ * where the block style BS matches the regexp `[|>][-+1-9]*` and block lines
156
+ * are empty or have an indent level greater than `indent`.
157
+ *
158
+ * @param {ParseContext } context
159
+ * @param {number } start - Index of first character
160
+ * @returns {number } - Index of the character after this block
161
+ */
162
+
163
+ } , {
164
+ key : "parse" ,
165
+ value : function parse ( context , start ) {
166
+ this . context = context ;
167
+ var src = context . src ;
168
+ var offset = this . parseBlockHeader ( start ) ;
169
+ offset = _Node2 . default . endOfWhiteSpace ( src , offset ) ;
170
+ offset = this . parseComment ( offset ) ;
171
+ offset = this . parseBlockValue ( offset ) ;
172
+ return offset ;
173
+ }
174
+ } , {
175
+ key : "strValue" ,
176
+ get : function get ( ) {
177
+ if ( ! this . valueRange || ! this . context ) return null ;
178
+ var _this$valueRange = this . valueRange ,
179
+ start = _this$valueRange . start ,
180
+ end = _this$valueRange . end ;
181
+ var _this$context2 = this . context ,
182
+ indent = _this$context2 . indent ,
183
+ src = _this$context2 . src ;
184
+ if ( this . valueRange . isEmpty ) return '' ;
185
+ var lastNewLine = null ;
186
+ var ch = src [ end - 1 ] ;
187
+
188
+ while ( ch === '\n' || ch === '\t' || ch === ' ' ) {
189
+ end -= 1 ;
190
+
191
+ if ( end <= start ) {
192
+ if ( this . chomping === Chomp . KEEP ) break ; else return '' ;
193
+ }
194
+
195
+ if ( ch === '\n' ) lastNewLine = end ;
196
+ ch = src [ end - 1 ] ;
197
+ }
198
+
199
+ var keepStart = end + 1 ;
200
+
201
+ if ( lastNewLine ) {
202
+ if ( this . chomping === Chomp . KEEP ) {
203
+ keepStart = lastNewLine ;
204
+ end = this . valueRange . end ;
205
+ } else {
206
+ end = lastNewLine ;
207
+ }
208
+ }
209
+
210
+ var bi = indent + this . blockIndent ;
211
+ var folded = this . type === _Node2 . Type . BLOCK_FOLDED ;
212
+ var str = '' ;
213
+ var sep = '' ;
214
+ var prevMoreIndented = false ;
215
+
216
+ for ( var i = start ; i < end ; ++ i ) {
217
+ for ( var j = 0 ; j < bi ; ++ j ) {
218
+ if ( src [ i ] !== ' ' ) break ;
219
+ i += 1 ;
220
+ }
221
+
222
+ var _ch = src [ i ] ;
223
+
224
+ if ( _ch === '\n' ) {
225
+ if ( sep === '\n' ) str += '\n' ; else sep = '\n' ;
226
+ } else {
227
+ var lineEnd = _Node2 . default . endOfLine ( src , i ) ;
228
+
229
+ var line = src . slice ( i , lineEnd ) ;
230
+ i = lineEnd ;
231
+
232
+ if ( folded && ( _ch === ' ' || _ch === '\t' ) && i < keepStart ) {
233
+ if ( sep === ' ' ) sep = '\n' ; else if ( ! prevMoreIndented && sep === '\n' ) sep = '\n\n' ;
234
+ str += sep + line ; //+ ((lineEnd < end && src[lineEnd]) || '')
235
+
236
+ sep = lineEnd < end && src [ lineEnd ] || '' ;
237
+ prevMoreIndented = true ;
238
+ } else {
239
+ str += sep + line ;
240
+ sep = folded && i < keepStart ? ' ' : '\n' ;
241
+ prevMoreIndented = false ;
242
+ }
243
+ }
244
+ }
245
+
246
+ return this . chomping === Chomp . STRIP ? str : str + '\n' ;
247
+ }
248
+ } ] ) ;
249
+
250
+ _inherits ( BlockValue , _Node ) ;
251
+
252
+ return BlockValue ;
253
+ } ( _Node2 . default ) ;
254
+
255
+ exports . default = BlockValue ;
0 commit comments