You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
svelte/compiler/parse/__test__.js

147 lines
2.3 KiB

8 years ago
import * as assert from 'assert';
import parse from './index.js';
describe( 'parse', () => {
it( 'is a function', () => {
assert.equal( typeof parse, 'function' );
});
8 years ago
it( 'parses a self-closing element', () => {
const template = '<div/>';
assert.deepEqual( parse( template ), {
html: {
start: 0,
end: 6,
type: 'Fragment',
children: [
{
start: 0,
end: 6,
type: 'Element',
name: 'div',
attributes: [],
children: []
}
]
},
css: null,
js: null
});
});
it( 'parses an element with text', () => {
8 years ago
const template = `<span>test</span>`;
assert.deepEqual( parse( template ), {
8 years ago
html: {
start: 0,
end: 17,
type: 'Fragment',
children: [
{
start: 0,
end: 17,
type: 'Element',
name: 'span',
attributes: [],
children: [
{
start: 6,
end: 10,
type: 'Text',
data: 'test'
}
]
}
]
},
css: null,
js: null
});
});
it( 'parses an element with a mustache tag', () => {
const template = `<h1>hello {{name}}!</h1>`;
assert.deepEqual( parse( template ), {
html: {
start: 0,
end: 24,
type: 'Fragment',
children: [
{
start: 0,
end: 24,
type: 'Element',
name: 'h1',
attributes: [],
children: [
{
start: 4,
end: 10,
type: 'Text',
data: 'hello '
},
{
start: 10,
end: 18,
type: 'MustacheTag',
expression: {
start: 12,
end: 16,
type: 'Identifier',
name: 'name'
}
},
{
start: 18,
end: 19,
type: 'Text',
data: '!'
}
]
}
]
},
css: null,
js: null
8 years ago
});
});
8 years ago
it( 'parses an {{#if}}...{{/if}} block', () => {
const template = `{{#if foo}}bar{{/if}}`;
assert.deepEqual( parse( template ), {
html: {
start: 0,
end: 21,
type: 'Fragment',
children: [
{
start: 0,
end: 21,
type: 'IfBlock',
expression: {
start: 6,
end: 9,
type: 'Identifier',
name: 'foo'
},
children: [
{
start: 11,
end: 14,
type: 'Text',
data: 'bar'
}
]
}
]
},
css: null,
js: null
});
});
8 years ago
});