Skip to content

Commit 54822b0

Browse files
authoredMar 17, 2017
Merge pull request #384 from sveltejs/shorthand-attributes
implement :shorthand attributes
2 parents 3aa1814 + e6d088d commit 54822b0

File tree

8 files changed

+115
-2
lines changed

8 files changed

+115
-2
lines changed
 

‎src/parse/state/tag.js

+26-2
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ function readTagName ( parser ) {
204204
function readAttribute ( parser, uniqueNames ) {
205205
const start = parser.index;
206206

207-
const name = parser.readUntil( /(\s|=|\/|>)/ );
207+
let name = parser.readUntil( /(\s|=|\/|>)/ );
208208
if ( !name ) return null;
209209
if ( uniqueNames.has( name ) ) {
210210
parser.error( 'Attributes need to be unique', start );
@@ -232,7 +232,15 @@ function readAttribute ( parser, uniqueNames ) {
232232
};
233233
}
234234

235-
const value = parser.eat( '=' ) ? readAttributeValue( parser ) : true;
235+
let value;
236+
237+
// :foo is shorthand for foo='{{foo}}'
238+
if ( /^:\w+$/.test( name ) ) {
239+
name = name.slice( 1 );
240+
value = getShorthandValue( start + 1, name );
241+
} else {
242+
value = parser.eat( '=' ) ? readAttributeValue( parser ) : true;
243+
}
236244

237245
return {
238246
start,
@@ -312,3 +320,19 @@ function readAttributeValue ( parser ) {
312320

313321
parser.error( `Unexpected end of input` );
314322
}
323+
324+
function getShorthandValue ( start, name ) {
325+
const end = start + name.length;
326+
327+
return [{
328+
type: 'AttributeShorthand',
329+
start,
330+
end,
331+
expression: {
332+
type: 'Identifier',
333+
start,
334+
end,
335+
name
336+
}
337+
}];
338+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default {
2+
html: `<div id="foo"></div>`,
3+
4+
test ( assert, component, target ) {
5+
component.set({ id: 'bar' });
6+
assert.equal( target.innerHTML, `<div id="bar"></div>` );
7+
8+
component.teardown();
9+
}
10+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<div :id/>
2+
3+
<script>
4+
export default {
5+
data: () => ({
6+
id: 'foo'
7+
})
8+
};
9+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>foo: {{foo}}</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export default {
2+
data: {
3+
foo: 42
4+
},
5+
6+
html: `<div><p>foo: 42</p></div>`,
7+
8+
test ( assert, component, target ) {
9+
component.set({
10+
foo: 99
11+
});
12+
13+
assert.equal( target.innerHTML, `<div><p>foo: 99</p></div>` );
14+
15+
component.teardown();
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<div>
2+
<Widget :foo/>
3+
</div>
4+
5+
<script>
6+
import Widget from './Widget.html';
7+
8+
export default {
9+
components: { Widget }
10+
};
11+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div :id/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"hash": 4120363214,
3+
"html": {
4+
"start": 0,
5+
"end": 10,
6+
"type": "Fragment",
7+
"children": [
8+
{
9+
"start": 0,
10+
"end": 10,
11+
"type": "Element",
12+
"name": "div",
13+
"attributes": [
14+
{
15+
"start": 5,
16+
"end": 8,
17+
"type": "Attribute",
18+
"name": "id",
19+
"value": [
20+
{
21+
"start": 6,
22+
"end": 8,
23+
"type": "AttributeShorthand",
24+
"expression": {
25+
"type": "Identifier",
26+
"start": 6,
27+
"end": 8,
28+
"name": "id"
29+
}
30+
}
31+
]
32+
}
33+
],
34+
"children": []
35+
}
36+
]
37+
},
38+
"css": null,
39+
"js": null
40+
}

0 commit comments

Comments
 (0)