Skip to content

Commit

Permalink
fb55#545: Add tests for Tokenizer.ts to cover new cases
Browse files Browse the repository at this point in the history
  • Loading branch information
alexprey committed Aug 20, 2020
1 parent b9e39b6 commit 5162479
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 1 deletion.
165 changes: 165 additions & 0 deletions src/Tokenizer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,169 @@ describe('Tokenizer', () => {
tokenizer.reset();
logger.log = [];
});

test('should support attributes', () => {
const logger = new CallbackLogger();
const tokenizer = new Tokenizer({
xmlMode: false,
decodeEntities: false
}, logger);

const input = '<div width=100></div>';
const expectedOutput = [
"onopentagname: 'div'",
"onattribname: 'width'",
"onattribdata: '100'",
'onattribend',
'onopentagend',
"onclosetag: 'div'",
'onend'
];

tokenizer.write(input);
tokenizer.end();
expect(logger.log).toEqual(expectedOutput);
});

test('should support double quoted attributes', () => {
const logger = new CallbackLogger();
const tokenizer = new Tokenizer({
xmlMode: false,
decodeEntities: false
}, logger);

const input = '<div title="title attribute text"></div>';
const expectedOutput = [
"onopentagname: 'div'",
"onattribname: 'title'",
"onattribdata: 'title attribute text'",
'onattribend',
'onopentagend',
"onclosetag: 'div'",
'onend'
];

tokenizer.write(input);
tokenizer.end();
expect(logger.log).toEqual(expectedOutput);
});

test('should support single quoted attributes', () => {
const logger = new CallbackLogger();
const tokenizer = new Tokenizer({
xmlMode: false,
decodeEntities: false
}, logger);

const input = '<div title=\'title attribute text\'></div>';
const expectedOutput = [
"onopentagname: 'div'",
"onattribname: 'title'",
"onattribdata: 'title attribute text'",
'onattribend',
'onopentagend',
"onclosetag: 'div'",
'onend'
];

tokenizer.write(input);
tokenizer.end();
expect(logger.log).toEqual(expectedOutput);
});

test('should support curly braced attributes', () => {
const logger = new CallbackLogger();
const tokenizer = new Tokenizer({
xmlMode: false,
decodeEntities: false,
curlyBracesInAttributes: true
}, logger);

const input = '<div on:click={() => handleClick()}></div>';
const expectedOutput = [
"onopentagname: 'div'",
"onattribname: 'on:click'",
"onattribdata: '() => handleClick()'",
'onattribend',
'onopentagend',
"onclosetag: 'div'",
'onend'
];

tokenizer.write(input);
tokenizer.end();
expect(logger.log).toEqual(expectedOutput);
});

test('should support curly braced attributes with escaping in JS strings', () => {
const logger = new CallbackLogger();
const tokenizer = new Tokenizer({
xmlMode: false,
decodeEntities: false,
curlyBracesInAttributes: true
}, logger);

const input = '<div on:click={() => console.log(\'}\' + "}" + `}`)}></div>';
const expectedOutput = [
"onopentagname: 'div'",
"onattribname: 'on:click'",
"onattribdata: '() => console.log(\'}\' + \"}\" + `}`)'",
'onattribend',
'onopentagend',
"onclosetag: 'div'",
'onend'
];

tokenizer.write(input);
tokenizer.end();
expect(logger.log).toEqual(expectedOutput);
});

test('should support curly braced attributes with escaping by inline JS comments', () => {
const logger = new CallbackLogger();
const tokenizer = new Tokenizer({
xmlMode: false,
decodeEntities: false,
curlyBracesInAttributes: true
}, logger);

const input = '<div on:click={() => console.log("c") /* } */}></div>';
const expectedOutput = [
"onopentagname: 'div'",
"onattribname: 'on:click'",
"onattribdata: '() => console.log(\"c\") /* } */'",
'onattribend',
'onopentagend',
"onclosetag: 'div'",
'onend'
];

tokenizer.write(input);
tokenizer.end();
expect(logger.log).toEqual(expectedOutput);
});

test('should support curly braced attributes with nesting scopes', () => {
const logger = new CallbackLogger();
const tokenizer = new Tokenizer({
xmlMode: false,
decodeEntities: false,
curlyBracesInAttributes: true
}, logger);

const input = '<div styles={{ width: 100, height: 200 }}></div>';
const expectedOutput = [
"onopentagname: 'div'",
"onattribname: 'on:click'",
"onattribdata: '{ width: 100, height: 200 }'",
'onattribend',
'onopentagend',
"onclosetag: 'div'",
'onend'
];

tokenizer.write(input);
tokenizer.end();
expect(logger.log).toEqual(expectedOutput);
});
});
2 changes: 1 addition & 1 deletion src/Tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ export default class Tokenizer {
_decodeEntities: boolean;

constructor(
options: { xmlMode?: boolean; decodeEntities?: boolean } | null,
options: { xmlMode?: boolean; decodeEntities?: boolean, curlyBracesInAttributes?: boolean } | null,
cbs: Callbacks
) {
this._cbs = cbs;
Expand Down

0 comments on commit 5162479

Please sign in to comment.