parse attributes

pull/31/head
Rich-Harris 8 years ago
parent 9fd6c3d408
commit e3e91d45ea

@ -93,9 +93,7 @@ export default function generate ( parsed, template ) {
return `root.${flattened.keypath}`; return `root.${flattened.keypath}`;
} }
console.log( `node, contexts`, node, contexts ) return 'TODO';
return 'TODO'
throw new Error( 'TODO expressions' );
} }
parsed.html.children.forEach( child => { parsed.html.children.forEach( child => {

@ -1,13 +1,13 @@
import { tokenizer, tokTypes, parseExpressionAt } from 'acorn'; import { tokenizer, tokTypes, parseExpressionAt } from 'acorn';
export function readEventHandlerDirective ( parser, name ) { export function readEventHandlerDirective ( parser, start, name ) {
const quoteMark = ( const quoteMark = (
parser.eat( `'` ) ? `'` : parser.eat( `'` ) ? `'` :
parser.eat( `"` ) ? `"` : parser.eat( `"` ) ? `"` :
null null
); );
const start = parser.index; const expressionStart = parser.index;
let end = null; let end = null;
let depth = 0; let depth = 0;
@ -16,17 +16,17 @@ export function readEventHandlerDirective ( parser, name ) {
if ( token.type === tokTypes.parenR ) { if ( token.type === tokTypes.parenR ) {
depth -= 1; depth -= 1;
if ( depth === 0 ) { if ( depth === 0 ) {
end = start + token.end; end = expressionStart + token.end;
break; break;
} }
} }
} }
const expression = parseExpressionAt( parser.template.slice( 0, end ), start ); const expression = parseExpressionAt( parser.template.slice( 0, end ), expressionStart );
parser.index = expression.end; parser.index = expression.end;
if ( expression.type !== 'CallExpression' ) { if ( expression.type !== 'CallExpression' ) {
parser.error( `Expected call expression`, start ); parser.error( `Expected call expression`, expressionStart );
} }
if ( quoteMark ) { if ( quoteMark ) {
@ -34,6 +34,8 @@ export function readEventHandlerDirective ( parser, name ) {
} }
return { return {
start,
end: parser.index,
type: 'EventHandler', type: 'EventHandler',
name, name,
expression expression

@ -0,0 +1,3 @@
export default function readStyle () {
throw new Error( 'TODO <style>' );
}

@ -117,6 +117,8 @@ function readTagName ( parser ) {
} }
function readAttribute ( parser ) { function readAttribute ( parser ) {
const start = parser.index;
const name = parser.readUntil( /(\s|=|\/|>)/ ); const name = parser.readUntil( /(\s|=|\/|>)/ );
if ( !name ) return null; if ( !name ) return null;
@ -124,12 +126,14 @@ function readAttribute ( parser ) {
if ( /^on:/.test( name ) ) { if ( /^on:/.test( name ) ) {
parser.eat( '=', true ); parser.eat( '=', true );
return readEventHandlerDirective( parser, name.slice( 3 ) ); return readEventHandlerDirective( parser, start, name.slice( 3 ) );
} }
const value = parser.eat( '=' ) ? readAttributeValue( parser ) : true; const value = parser.eat( '=' ) ? readAttributeValue( parser ) : true;
return { return {
start,
end: parser.index,
type: 'Attribute', type: 'Attribute',
name, name,
value value
@ -147,7 +151,7 @@ function readQuotedAttributeValue ( parser, quoteMark ) {
let currentChunk = { let currentChunk = {
start: parser.index, start: parser.index,
end: null, end: null,
type: 'AttributeText', type: 'Text',
data: '' data: ''
}; };
@ -185,21 +189,27 @@ function readQuotedAttributeValue ( parser, quoteMark ) {
currentChunk = { currentChunk = {
start: parser.index, start: parser.index,
end: null, end: null,
type: 'AttributeText', type: 'Text',
data: '' data: ''
}; };
} }
else if ( parser.match( '\\' ) ) { else if ( parser.eat( '\\' ) ) {
escaped = true; escaped = true;
} }
else if ( parser.match( quoteMark ) ) { else if ( parser.match( quoteMark ) ) {
currentChunk.end = parser.index++;
if ( currentChunk.data ) { if ( currentChunk.data ) {
chunks.push( currentChunk ); chunks.push( currentChunk );
return chunks; return chunks;
} }
} }
else {
currentChunk.data += parser.template[ parser.index++ ];
}
} }
} }

@ -0,0 +1,34 @@
{
"html": {
"start": 0,
"end": 23,
"type": "Fragment",
"children": [
{
"start": 0,
"end": 23,
"type": "Element",
"name": "div",
"attributes": [
{
"start": 5,
"end": 16,
"type": "Attribute",
"name": "class",
"value": [
{
"start": 12,
"end": 15,
"type": "Text",
"data": "foo"
}
]
}
],
"children": []
}
]
},
"css": null,
"js": null
}

@ -11,6 +11,8 @@
"name": "button", "name": "button",
"attributes": [ "attributes": [
{ {
"start": 8,
"end": 45,
"type": "EventHandler", "type": "EventHandler",
"name": "click", "name": "click",
"expression": { "expression": {

Loading…
Cancel
Save