Tinymath escape characters and empty named args (#98559) (#98660)

This commit is contained in:
Wylie Conlon 2021-04-28 15:43:43 -04:00 committed by GitHub
parent 6ea81a35fd
commit 5967d0a9be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 5 deletions

View file

@ -43,7 +43,7 @@ Literal "literal"
// Quoted variables are interpreted as strings
// but unquoted variables are more restrictive
Variable
= _ Quote chars:(ValidChar / Space)* Quote _ {
= _ [\'] chars:(ValidChar / Space / [\"])* [\'] _ {
return {
type: 'variable',
value: chars.join(''),
@ -51,6 +51,14 @@ Variable
text: text()
};
}
/ _ [\"] chars:(ValidChar / Space / [\'])* [\"] _ {
return {
type: 'variable',
value: chars.join(''),
location: simpleLocation(location()),
text: text()
};
}
/ _ rest:ValidChar+ _ {
return {
type: 'variable',
@ -103,10 +111,9 @@ Argument_List "arguments"
}
String
= [\"] value:(ValidChar)+ [\"] { return value.join(''); }
/ [\'] value:(ValidChar)+ [\'] { return value.join(''); }
/ value:(ValidChar)+ { return value.join(''); }
= '"' chars:("\\\"" { return "\""; } / [^"])* '"' { return chars.join(''); }
/ "'" chars:("\\\'" { return "\'"; } / [^'])* "'" { return chars.join(''); }
/ chars:(ValidChar)+ { return chars.join(''); }
Argument
= name:[a-zA-Z_]+ _ '=' _ value:(Number / String) _ {

View file

@ -73,6 +73,7 @@ describe('Parser', () => {
expect(parse('"foo bar"')).toEqual(variableEqual('foo bar'));
expect(parse('"foo bar fizz buzz"')).toEqual(variableEqual('foo bar fizz buzz'));
expect(parse('"foo bar baby"')).toEqual(variableEqual('foo bar baby'));
expect(parse(`"f'oo"`)).toEqual(variableEqual(`f'oo`));
});
it('strings with single quotes', () => {
@ -88,6 +89,7 @@ describe('Parser', () => {
expect(parse("' foo bar'")).toEqual(variableEqual(" foo bar"));
expect(parse("'foo bar '")).toEqual(variableEqual("foo bar "));
expect(parse("'0foo'")).toEqual(variableEqual("0foo"));
expect(parse(`'f"oo'`)).toEqual(variableEqual(`f"oo`));
/* eslint-enable prettier/prettier */
});
@ -138,10 +140,18 @@ describe('Parser', () => {
);
});
it('named argument is empty string', () => {
expect(parse('foo(q="")')).toEqual(functionEqual('foo', [namedArgumentEqual('q', '')]));
expect(parse(`foo(q='')`)).toEqual(functionEqual('foo', [namedArgumentEqual('q', '')]));
});
it('named and positional', () => {
expect(parse('foo(ref, q="bar")')).toEqual(
functionEqual('foo', [variableEqual('ref'), namedArgumentEqual('q', 'bar')])
);
expect(parse(`foo(ref, q='ba"r')`)).toEqual(
functionEqual('foo', [variableEqual('ref'), namedArgumentEqual('q', `ba"r`)])
);
});
it('numerically named', () => {
@ -182,6 +192,21 @@ describe('Parser', () => {
it('invalid named', () => {
expect(() => parse('foo(offset-type="1d")')).toThrow('but "(" found');
});
it('named with complex strings', () => {
expect(parse(`foo(filter='😀 > "\ttab"')`)).toEqual(
functionEqual('foo', [namedArgumentEqual('filter', `😀 > "\ttab"`)])
);
});
it('named with escape characters', () => {
expect(parse(`foo(filter='Women\\'s Clothing')`)).toEqual(
functionEqual('foo', [namedArgumentEqual('filter', `Women's Clothing`)])
);
expect(parse(`foo(filter="\\"Quoted inner string\\"")`)).toEqual(
functionEqual('foo', [namedArgumentEqual('filter', `"Quoted inner string"`)])
);
});
});
it('Missing expression', () => {