Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support v-for="(item, index)" #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,21 @@ private function handleFor( DOMNode $node, array $data ) {
list( $itemName, $listName ) = explode( ' in ', $node->getAttribute( 'v-for' ) );
$node->removeAttribute( 'v-for' );

foreach ( $data[$listName] as $item ) {
if ( preg_match( '/\((?P<itemName>[^,]+)\s*,\s*(?P<keyName>[^\)]+)\)/', $itemName, $matches ) ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this uses an extremely crude regex to match variable names; I’m not sure if this library has a better regex (snippet) for that elsewhere, though.

$itemName = $matches['itemName'];
$keyName = $matches['keyName'];
} else {
$keyName = null;
}

foreach ( $data[$listName] as $key => $item ) {
$newNode = $node->cloneNode( true );
$node->parentNode->insertBefore( $newNode, $node );
$this->handleNode( $newNode, array_merge( $data, [ $itemName => $item ] ) );
$newData = array_merge( $data, [ $itemName => $item ] );
if ( $keyName !== null ) {
$newData[$keyName] = $key;
}
$this->handleNode( $newNode, $newData );
}

$this->removeNode( $node );
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/fixture/gloss_widget.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
<div class="wikibase-lexeme-sense-glosses-list">
<table class="wikibase-lexeme-sense-glosses-table">
<tbody>
<tr v-for="gloss in glosses" class="wikibase-lexeme-sense-gloss">
<tr v-for="(gloss, index) in glosses" class="wikibase-lexeme-sense-gloss">
<td class="wikibase-lexeme-sense-gloss-language">
<span v-if="!inEditMode">{{gloss.language}}</span>
<input v-else="" class="wikibase-lexeme-sense-gloss-language-input" v-model="gloss.language" :disabled="isSaving">
<input v-else class="wikibase-lexeme-sense-gloss-language-input" v-model="gloss.language" :disabled="isSaving">
</td>
<td class="wikibase-lexeme-sense-gloss-value">
<span v-if="!inEditMode" :dir="gloss.language|directionality" :lang="gloss.language">{{gloss.value}} <span class="wikibase-lexeme-sense-glosses-sense-id">({{senseId}})</span></span>
<input v-else="" class="wikibase-lexeme-sense-gloss-value-input" v-model="gloss.value" :disabled="isSaving">
<input v-else class="wikibase-lexeme-sense-gloss-value-input" v-model="gloss.value" :disabled="isSaving" :autofocus="index == glosses.length - 1">
</td>
</tr>
</tbody>
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/fixture/lemma_widget.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<span class="lemma-widget_lemma-language">{{lemma.language}}</span>
</li>
</ul>
<div v-else="">
<div v-else>
<div class="lemma-widget_edit-area">
<ul class="lemma-widget_lemma-list">
<li v-for="lemma in lemmas" class="lemma-widget_lemma-edit-box">
Expand Down
18 changes: 18 additions & 0 deletions tests/php/TemplatingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,24 @@ public function testTemplateWithForLoopMustache_RendersCorrectValues() {
assertThat( $result, is( equalTo( '<p><a>1</a><a>2</a></p>' ) ) );
}

public function testTemplateWithForLoopWithIndex_RendersValuesAndIndices() {
$result = $this->createAndRender(
'<p><a v-for="(item, index) in list">{{index}}: {{item}}</a></p>',
[ 'list' => [ 10, 20 ] ]
);

assertThat( $result, is( equalTo( '<p><a>0: 10</a><a>1: 20</a></p>' ) ) );
}

public function testTemplateWithForLoopWithKey_RendersValuesAndKeys() {
$result = $this->createAndRender(
'<p><a v-for="(item, key) in list">{{key}}: {{item}}</a></p>',
[ 'list' => [ 'ten' => 10, 'twenty' => 20 ] ]
);

assertThat( $result, is( equalTo( '<p><a>ten: 10</a><a>twenty: 20</a></p>' ) ) );
}

public function testTemplateWithAttributeBinding_ConditionIsFalse_AttributeIsNotRendered() {
$result = $this->createAndRender( '<p :attr1="condition"></p>', [ 'condition' => false ] );

Expand Down