From 2741eee8bfc31e3f5959555ebd9df00c4d236c9f Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Tue, 22 Nov 2016 16:52:04 -0500 Subject: [PATCH] =?UTF-8?q?bind:foo=20shorthand=20=E2=80=93=C2=A0closes=20?= =?UTF-8?q?#20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- compiler/parse/read/directives.js | 22 ++++++++++-------- compiler/parse/state/tag.js | 1 - test/parser/binding-shorthand/input.html | 1 + test/parser/binding-shorthand/output.json | 27 +++++++++++++++++++++++ 4 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 test/parser/binding-shorthand/input.html create mode 100644 test/parser/binding-shorthand/output.json diff --git a/compiler/parse/read/directives.js b/compiler/parse/read/directives.js index e1bd3f6c30..32bcf39e68 100644 --- a/compiler/parse/read/directives.js +++ b/compiler/parse/read/directives.js @@ -43,17 +43,21 @@ export function readEventHandlerDirective ( parser, start, name ) { } export function readBindingDirective ( parser, start, name ) { - const quoteMark = ( - parser.eat( `'` ) ? `'` : - parser.eat( `"` ) ? `"` : - null - ); + let value = name; // shorthand – bind:foo equivalent to bind:foo='foo' - const value = parser.read( /([a-zA-Z_$][a-zA-Z0-9_$]*)(\.[a-zA-Z_$][a-zA-Z0-9_$]*)*/ ); - if ( !value ) parser.error( `Expected valid property name` ); + if ( parser.eat( '=' ) ) { + const quoteMark = ( + parser.eat( `'` ) ? `'` : + parser.eat( `"` ) ? `"` : + null + ); - if ( quoteMark ) { - parser.eat( quoteMark, true ); + value = parser.read( /([a-zA-Z_$][a-zA-Z0-9_$]*)(\.[a-zA-Z_$][a-zA-Z0-9_$]*)*/ ); + if ( !value ) parser.error( `Expected valid property name` ); + + if ( quoteMark ) { + parser.eat( quoteMark, true ); + } } return { diff --git a/compiler/parse/state/tag.js b/compiler/parse/state/tag.js index 15e65d8cea..e8c627c93a 100644 --- a/compiler/parse/state/tag.js +++ b/compiler/parse/state/tag.js @@ -147,7 +147,6 @@ function readAttribute ( parser ) { } if ( /^bind:/.test( name ) ) { - parser.eat( '=', true ); return readBindingDirective( parser, start, name.slice( 5 ) ); } diff --git a/test/parser/binding-shorthand/input.html b/test/parser/binding-shorthand/input.html new file mode 100644 index 0000000000..31f9c872b0 --- /dev/null +++ b/test/parser/binding-shorthand/input.html @@ -0,0 +1 @@ + diff --git a/test/parser/binding-shorthand/output.json b/test/parser/binding-shorthand/output.json new file mode 100644 index 0000000000..61c24975a2 --- /dev/null +++ b/test/parser/binding-shorthand/output.json @@ -0,0 +1,27 @@ +{ + "html": { + "start": 0, + "end": 18, + "type": "Fragment", + "children": [ + { + "start": 0, + "end": 18, + "type": "Element", + "name": "Widget", + "attributes": [ + { + "start": 8, + "end": 16, + "type": "Binding", + "name": "foo", + "value": "foo" + } + ], + "children": [] + } + ] + }, + "css": null, + "js": null +}