Skip to content
This repository was archived by the owner on Jan 20, 2024. It is now read-only.

Commit 84445e2

Browse files
Merge pull request #6 from menduz/feature/scalar-raw-value
Feature/scalar raw value
2 parents 78fae94 + d690e05 commit 84445e2

3 files changed

Lines changed: 21 additions & 7 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"devInstall": "dev-env-installer install"
1111
},
1212
"dependencies": {
13-
"underscore": "^1.8.3"
13+
1414
},
1515
"typings":"dist/index.d.ts",
1616
"repository": {

src/loader.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,16 @@ function charFromCodepoint(c) {
119119

120120
var simpleEscapeCheck = new Array(256); // integer, for fast access
121121
var simpleEscapeMap = new Array(256);
122+
var customEscapeCheck = new Array(256); // integer, for fast access
123+
var customEscapeMap = new Array(256);
122124
for (var i = 0; i < 256; i++) {
123-
simpleEscapeCheck[i] = simpleEscapeSequence(i) ? 1 : 0;
124-
simpleEscapeMap[i] = simpleEscapeSequence(i);
125+
customEscapeMap[i] = simpleEscapeMap[i] = simpleEscapeSequence(i);
126+
simpleEscapeCheck[i] = simpleEscapeMap[i] ? 1 : 0;
127+
customEscapeCheck[i] = 1;
128+
129+
if (!simpleEscapeCheck[i]) {
130+
customEscapeMap[i] = '\\' + String.fromCharCode(i);
131+
}
125132
}
126133

127134

@@ -151,14 +158,16 @@ class State{
151158
tagMap:any
152159
version:string
153160
checkLineBreaks:boolean
161+
allowAnyEscape:boolean
154162

155163
constructor(input:string,options:any){
156164
this.input = input;
157165

158166
this.filename = options['filename'] || null;
159167
this.schema = options['schema'] || DEFAULT_FULL_SCHEMA;
160168
this.onWarning = options['onWarning'] || null;
161-
this.legacy = options['legacy'] || false;
169+
this.legacy = options['legacy'] || false;
170+
this.allowAnyEscape = options['allowAnyEscape'] || false;
162171

163172
this.implicitTypes = this.schema.compiledImplicit;
164173
this.typeMap = this.schema.compiledTypeMap;
@@ -571,6 +580,7 @@ function readPlainScalar(state:State, nodeIndent, withinFlowCollection) {
571580
captureSegment(state, captureStart, captureEnd, false);
572581

573582
if (state.result.startPosition!=-1) {
583+
state_result.rawValue = state.input.substring(state_result.startPosition, state_result.endPosition);
574584
return true;
575585
}
576586

@@ -655,6 +665,7 @@ function readDoubleQuotedScalar(state:State, nodeIndent:number) {
655665
captureSegment(state, captureStart, state.position, true);
656666
state.position++;
657667
scalar.endPosition=state.position;
668+
scalar.rawValue = state.input.substring(scalar.startPosition, scalar.endPosition);
658669
return true;
659670

660671
} else if (0x5C/* \ */ === ch) {
@@ -665,8 +676,8 @@ function readDoubleQuotedScalar(state:State, nodeIndent:number) {
665676
skipSeparationSpace(state, false, nodeIndent);
666677

667678
// TODO: rework to inline fn with no type cast?
668-
} else if (ch < 256 && simpleEscapeCheck[ch]) {
669-
scalar.value += simpleEscapeMap[ch];
679+
} else if (ch < 256 && (state.allowAnyEscape ? customEscapeCheck[ch] : simpleEscapeCheck[ch])) {
680+
scalar.value += (state.allowAnyEscape ? customEscapeMap[ch] : simpleEscapeMap[ch]);
670681
state.position++;
671682

672683
} else if ((tmp = escapedHexLen(ch)) > 0) {
@@ -981,6 +992,7 @@ function readBlockScalar(state:State, nodeIndent) {
981992

982993
}
983994
sc.endPosition=i;
995+
sc.rawValue = state.input.substring(sc.startPosition, sc.endPosition);
984996
return true;
985997
}
986998

src/yamlAST.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export interface YAMLScalar extends YAMLNode{
3838
value:string
3939
doubleQuoted?:boolean
4040
plainScalar?:boolean
41+
rawValue:string
4142
}
4243

4344
export interface YAMLMapping extends YAMLNode{
@@ -83,7 +84,8 @@ export function newScalar(v:string=""):YAMLScalar{
8384
value:v,
8485
kind:Kind.SCALAR,
8586
parent:null,
86-
doubleQuoted:false
87+
doubleQuoted:false,
88+
rawValue:v
8789
}
8890
}
8991
export function newItems():YAMLSequence{

0 commit comments

Comments
 (0)