parse :Switch tags

pull/971/head
Rich Harris 8 years ago
parent 6641684bcf
commit 4f991536d4

@ -15,9 +15,10 @@ import { Node } from '../../interfaces';
const validTagName = /^\!?[a-zA-Z]{1,}:?[a-zA-Z0-9\-]*/;
const SELF = ':Self';
const SWITCH = ':Switch';
const metaTags = {
':Window': true,
':Window': true
};
const specials = new Map([
@ -104,6 +105,15 @@ export default function tag(parser: Parser) {
}
}
const element: Node = {
start,
end: null, // filled in later
type: 'Element',
name,
attributes: [],
children: [],
};
parser.allowWhitespace();
if (isClosingTag) {
@ -156,12 +166,18 @@ export default function tag(parser: Parser) {
}
}
const attributes = [];
if (name === SWITCH) {
parser.eat('{', true);
element.expression = readExpression(parser);
parser.allowWhitespace();
parser.eat('}', true);
}
const uniqueNames = new Set();
let attribute;
while ((attribute = readAttribute(parser, uniqueNames))) {
attributes.push(attribute);
element.attributes.push(attribute);
parser.allowWhitespace();
}
@ -179,19 +195,10 @@ export default function tag(parser: Parser) {
}
parser.eat('>', true);
parser[special.property] = special.read(parser, start, attributes);
parser[special.property] = special.read(parser, start, element.attributes);
return;
}
const element: Node = {
start,
end: null, // filled in later
type: 'Element',
name,
attributes,
children: [],
};
parser.current().children.push(element);
const selfClosing = parser.eat('/') || isVoidElementName(name);
@ -242,6 +249,8 @@ function readTagName(parser: Parser) {
return SELF;
}
if (parser.eat(SWITCH)) return SWITCH;
const name = parser.readUntil(/(\s|\/|>)/);
if (name in metaTags) return name;

@ -0,0 +1 @@
<:Switch {foo ? Foo : Bar}></:Switch>

@ -0,0 +1,43 @@
{
"hash": 755548012,
"html": {
"start": 0,
"end": 37,
"type": "Fragment",
"children": [
{
"start": 0,
"end": 37,
"type": "Element",
"name": ":Switch",
"attributes": [],
"children": [],
"expression": {
"type": "ConditionalExpression",
"start": 10,
"end": 25,
"test": {
"type": "Identifier",
"start": 10,
"end": 13,
"name": "foo"
},
"consequent": {
"type": "Identifier",
"start": 16,
"end": 19,
"name": "Foo"
},
"alternate": {
"type": "Identifier",
"start": 22,
"end": 25,
"name": "Bar"
}
}
}
]
},
"css": null,
"js": null
}
Loading…
Cancel
Save