Skip to content

Commit

Permalink
feat(syntax-interpreter): single quoted strings in options attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
AshleyGrant committed Mar 9, 2016
1 parent 589d107 commit f26cd40
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/syntax-interpreter.js
Expand Up @@ -111,11 +111,13 @@ export class SyntaxInterpreter {
let current;
let i;
let ii;
let inString = false;
let inEscape = false

for (i = 0, ii = attrValue.length; i < ii; ++i) {
current = attrValue[i];

if (current === ';') {
if (current === ';' && !inString) {
info = language.inspectAttribute(resources, name, target.trim());
language.createAttributeInstruction(resources, element, info, instruction, context);

Expand All @@ -128,9 +130,19 @@ export class SyntaxInterpreter {
} else if (current === ':' && name === null) {
name = target.trim();
target = '';
} else if (current === '\\') {
target += current;
inEscape = true;
continue;
} else {
target += current;

if(name !== null && inEscape === false && current === '\'') {
inString = !inString;
}
}

inEscape = false;
}

if (name !== null) {
Expand Down
43 changes: 43 additions & 0 deletions test/syntax-interpreter.spec.js
Expand Up @@ -152,6 +152,49 @@ describe('SyntaxInterpreter', () => {
expect(instruction.attributes.key).toBe('foo');
expect(instruction.attributes.value).toBe('bar');
});
});

describe('options attributes', () => {
var interpreter, info;

beforeAll(() => {
interpreter = new SyntaxInterpreter(new Parser(), new ObserverLocator(), new EventManager());

interpreter.language = {
inspectAttribute(resources, attrName, attrValue) {
return {
attrName: attrName,
attrValue: attrValue
};
},
createAttributeInstruction() {
return;
}
}

info = {
attrName: 'custom',
command: null,
defaultBindingMode: 1
};
});

it('handles a semicolon inside a string of an options attribute', () => {
info.attrValue = "foo: 'bar;';";
var instruction = interpreter.options({}, null, info, null);
expect(instruction.attributes['foo']).toBe("'bar;'");
});

it('handles an escaped single quote inside a string of an options attribute', () => {
info.attrValue = "foo: 'bar\\'';";
var instruction = interpreter.options({}, null, info, null);
expect(instruction.attributes['foo']).toBe("'bar\\''");
});

it('handles an escaped single quote and a semicolon inside a string of an options attribute', () => {
info.attrValue = "foo: 'bar\\';';";
var instruction = interpreter.options({}, null, info, null);
expect(instruction.attributes['foo']).toBe("'bar\\';'");
});
});
});

0 comments on commit f26cd40

Please sign in to comment.