Skip to content

Commit a33accd

Browse files
committed
Make the regex for the cycle tag less greedy
1 parent 906686c commit a33accd

File tree

2 files changed

+14
-33
lines changed

2 files changed

+14
-33
lines changed

src/Liquid/Tag/TagCycle.php

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,15 @@ class TagCycle extends AbstractTag
5757
*/
5858
public function __construct($markup, array &$tokens, FileSystem $fileSystem = null)
5959
{
60-
$simpleSyntax = new Regexp("/" . Liquid::get('QUOTED_FRAGMENT') . "/");
61-
$namedSyntax = new Regexp("/(" . Liquid::get('QUOTED_FRAGMENT') . ")\s*\:\s*(.*)/");
62-
63-
if ($namedSyntax->match($markup)) {
64-
$this->variables = $this->variablesFromString($namedSyntax->matches[2]);
65-
$this->name = $namedSyntax->matches[1];
66-
} elseif ($simpleSyntax->match($markup)) {
67-
$this->variables = $this->variablesFromString($markup);
60+
$simpleSyntax = preg_match_all('/(["\'])?(?(1)(?:(?!\1).)*+\1|[^\s,:|\'"]++)/', $markup, $matches);
61+
$namedSyntax = preg_match('/^(["\'])?(?(1)(?:(?!\1).)*+\1|[^\s,:|\'"]++)\s*\:/', $markup, $namedMatches);
62+
63+
if ($namedSyntax) {
64+
$this->name = $matches[0][0];
65+
array_shift($matches[0]);
66+
$this->variables = $matches[0];
67+
} elseif ($simpleSyntax) {
68+
$this->variables = $matches[0];
6869
$this->name = "'" . implode($this->variables) . "'";
6970
} else {
7071
throw new ParseException("Syntax Error in 'cycle' - Valid syntax: cycle [name :] var [, var2, var3 ...]");
@@ -103,28 +104,4 @@ public function render(Context $context)
103104

104105
return $result;
105106
}
106-
107-
/**
108-
* Extract variables from a string of markup
109-
*
110-
* @param string $markup
111-
*
112-
* @return array;
113-
*/
114-
private function variablesFromString($markup)
115-
{
116-
$regexp = new Regexp('/\s*(' . Liquid::get('QUOTED_FRAGMENT') . ')\s*/');
117-
$parts = explode(',', $markup);
118-
$result = array();
119-
120-
foreach ($parts as $part) {
121-
$regexp->match($part);
122-
123-
if (!empty($regexp->matches[1])) {
124-
$result[] = $regexp->matches[1];
125-
}
126-
}
127-
128-
return $result;
129-
}
130107
}

tests/Liquid/Tag/TagCycleTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,15 @@ public function testCycle()
3636
public function testMultipleCycles()
3737
{
3838
$this->assertTemplateResult('1 2 1 1 2 3 1', '{%cycle 1,2%} {%cycle 1,2%} {%cycle 1,2%} {%cycle 1,2,3%} {%cycle 1,2,3%} {%cycle 1,2,3%} {%cycle 1,2,3%}');
39+
$this->assertTemplateResult('onetwo:,three', '{%cycle "one","two:,three"%}{%cycle "one","two:,three"%}');
3940
}
4041

4142
public function testMultipleNamedCycles()
4243
{
43-
$this->assertTemplateResult('one one two two one one', '{%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %}');
44+
$this->assertTemplateResult('one one two two one one', '{%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1 : "one", "two" %} {%cycle 2 : "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %}');
45+
$this->assertTemplateResult('one one two two one one', '{%cycle "first": "one", "two" %} {%cycle "second": "one", "two" %} {%cycle "first" : "one", "two" %} {%cycle "second" : "one", "two" %} {%cycle "first": "one", "two" %} {%cycle "second": "one", "two" %}');
46+
$this->assertTemplateResult('one one two two one one', '{%cycle 1: \'one\', \'two\' %} {%cycle 2: \'one\', \'two\' %} {%cycle 1 : \'one\', \'two\' %} {%cycle 2 : \'one\', \'two\' %} {%cycle 1: \'one\', \'two\' %} {%cycle 2: \'one\', \'two\' %}');
47+
$this->assertTemplateResult('one one two two one one', '{%cycle \'first\': \'one\', \'two\' %} {%cycle \'second\': \'one\', \'two\' %} {%cycle \'first\' : \'one\', \'two\' %} {%cycle \'second\' : \'one\', \'two\' %} {%cycle \'first\': \'one\', \'two\' %} {%cycle \'second\': \'one\', \'two\' %}');
4448
}
4549

4650
public function testMultipleNamedCyclesWithNamesFromContext()

0 commit comments

Comments
 (0)