Skip to content

Commit a72ae19

Browse files
committed
Fix CRLF handling in raw strings
1 parent a173903 commit a72ae19

5 files changed

Lines changed: 71 additions & 11 deletions

File tree

build/index.cjs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,20 @@ function parse(source) {
9191
if (ch !== "`") return;
9292
let str = "", more = !1;
9393
do {
94-
for (; next(), !(ch === "\n" || done); )
94+
let newline = "\n";
95+
for (; ; ) {
96+
if (next(), ch === "\r")
97+
if (next(), ch === "\n") {
98+
newline = "\r\n";
99+
break;
100+
} else
101+
str += "\r";
102+
else if (ch === "\n" || done)
103+
break;
95104
str += ch;
105+
}
96106
for (next(); isWhitespace(ch); ) next();
97-
more = ch === "`", more && (str += "\n");
107+
more = ch === "`", more && (str += newline);
98108
} while (more);
99109
return str;
100110
}

build/index.js

Lines changed: 16 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/maml.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/parse.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,31 @@ export function parse(source: string): any {
110110

111111
function parseRawString() {
112112
if (ch !== '`') return
113-
let str = '',
114-
more = false
113+
let str = ''
114+
let more = false
115115
do {
116+
let newline = '\n'
116117
while (true) {
117118
next()
118-
if ((ch as string) === '\n' || done) break
119+
if ((ch as string) === '\r') {
120+
next()
121+
if ((ch as string) === '\n') {
122+
newline = '\r\n'
123+
break
124+
} else {
125+
str += '\r'
126+
}
127+
} else if ((ch as string) === '\n' || done) {
128+
break
129+
}
119130
str += ch
120131
}
121132
next()
122133
while (isWhitespace(ch)) next()
123134
more = (ch as string) === '`'
124-
if (more) str += '\n'
135+
if (more) {
136+
str += newline
137+
}
125138
} while (more)
126139
return str
127140
}

test/parse.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,29 @@ describe('extra', () => {
6969
const output = parse('` ')
7070
expect(output).toStrictEqual(' ')
7171
})
72+
73+
test('raw string with CRLF newlines', () => {
74+
const output = parse('`line1\r\n`line2\r\n`line3')
75+
expect(output).toStrictEqual('line1\r\nline2\r\nline3')
76+
})
77+
78+
test('raw string with mixed CRLF and LF newlines', () => {
79+
const output = parse('`line1\r\n`line2\n`line3\r\n`')
80+
expect(output).toStrictEqual('line1\r\nline2\nline3\r\n')
81+
})
82+
83+
test('single-line raw string with CRLF newline', () => {
84+
const output = parse('`simgle line\r\n')
85+
expect(output).toStrictEqual('simgle line')
86+
})
87+
88+
test('single-line raw string with CR inside and CRLF newline', () => {
89+
const output = parse('`the \r char\r\n')
90+
expect(output).toStrictEqual('the \r char')
91+
})
92+
93+
test('single-line raw string with CR at the end', () => {
94+
const output = parse('`string\r')
95+
expect(output).toStrictEqual('string\r')
96+
})
7297
})

0 commit comments

Comments
 (0)