/*
* @author acrazing joking.young@gmail.com
* @since 2017-08-20 17:32:24
*/
import * as assert from 'node:assert';
import { parse } from './parse';
import { SyntaxKind } from './types';
import type { IAttribute, IAttributeValue, INode, ITag, IText } from './types';
export let index = 0;
export function text(input: string, start = index): IText {
return {
type: SyntaxKind.Text,
start: start,
end: (index = input.length + start),
value: input,
};
}
export function tag(
input: string,
name: string,
open: IText,
attributes: IAttribute[],
body: INode[] | undefined | null,
close: IText | undefined | null,
start: number,
rawName = name,
): ITag {
return {
start: start,
end: (index = start + input.length),
type: SyntaxKind.Tag,
open: open,
name: name,
rawName: rawName,
attributes: attributes,
attributeMap: undefined,
body: body,
close: close,
};
}
function attr(name: IText, value?: IAttributeValue): IAttribute {
return {
start: name.start,
end: (index = value ? value.end : name.end),
name: name,
value: value,
};
}
function value(input: string, quote: undefined | "'" | '"', start = index): IAttributeValue {
return {
start: start,
end: (index = start + (quote === void 0 ? 0 : 2) + input.length),
value: input,
quote: quote,
};
}
const scenes: Array<{
name: string;
input: string;
nodes: INode[];
}> = [
{
name: 'text',
input: 'hello world',
nodes: [text('hello world', 0)],
},
{
name: 'text twice',
input: 'hello < world',
nodes: [text('hello < world', 0)],
},
{
name: 'single tag',
input: '
', 1),
[attr(text('id', 6), value('1', '"', 9))],
[
text('\n hello world\n ', 13),
tag(
'
h1
',
'h1',
text('
', 30),
[attr(text('id', 34), value('h1', '"', 37))],
[text('h1', 42)],
text('
', 44),
30,
),
text('\n ', 49),
tag(
'

',
'img',
text('

', 52),
[attr(text('src', 57), value('/src/index.ts', '"', 61))],
void 0,
null,
52,
),
text('\n ', 77),
tag('
', 'input', text('
', 80), [], void 0, null, 80),
text('\n ', 89),
tag(
`
`,
'div',
text('
', 92),
[attr(text('id', 97), value('2', '"', 100))],
[
text('\n ', 104),
tag(
`
span
`,
'div',
text('
', 109),
[attr(text('id', 114), value('3', '"', 117))],
[
text('\n ', 121),
tag(
'span',
'span',
text('', 128),
[],
[text('span', 134)],
text('', 138),
128,
),
text('\n ', 145),
tag(
'',
'empty',
text('', 152),
[],
[],
text('', 159),
152,
),
text('\n ', 167),
],
text('
', 172),
109,
),
text('\n ', 178),
],
text('
', 181),
92,
),
text('\n', 187),
],
text('
', 188),
1,
),
text('\n ', 194),
],
},
{
name: 'doctype',
input: '',
nodes: [
tag(
'',
'!doctype',
text('', 0),
[attr(text('html', 10))],
void 0,
null,
0,
),
tag('', 'html', text('', 15), [], [], text('', 21), 15),
],
},
{
name: 'comments',
input:
' qm comment ?>',
nodes: [
tag(
'',
'!--',
text('', 20),
0,
),
tag(
'',
'!',
text('', 42),
23,
),
tag(
'',
'!',
text('', 62),
43,
),
tag(
' qm comment ?>',
'',
text('<', 63),
[],
[text('? qm comment ?', 64)],
text('>', 78),
63,
),
tag(
'',
'!',
text('', 97),
79,
),
],
},
{
name: 'normal comment special',
input: '',
nodes: [
tag(
'',
'!--',
text('', 14),
0,
),
],
},
{
name: 'script',
input: '',
nodes: [
tag(
'',
'script',
text('', 22),
0,
),
],
},
{
name: 'script',
input: '',
nodes: [
tag(
'',
'style',
text('', 20),
0,
),
],
},
{
name: 'tag name',
input: '