Skip to content

Commit

Permalink
Merge pull request #12 from pug-php/fix-issue-11
Browse files Browse the repository at this point in the history
Fix issue #11
  • Loading branch information
kylekatarnls authored May 4, 2017
2 parents e43c283 + 772d327 commit 315b407
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
composer.lock
composer.phar
*debug.log*
.idea
/vendor/
/coverage/
33 changes: 25 additions & 8 deletions src/Jade/JadeSymfonyEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,7 @@ protected function crawlDirectories($srcDir, $appDir, &$assetsDirectories)
return $baseDir ?: $appDir . '/Resources/views';
}

/**
* Pug code transformation to do before Pug render.
*
* @param string $pugCode code input
*
* @return string
*/
public function preRender($pugCode)
protected function replaceCode($pugCode)
{
$helperPattern = $this->getOption('expressionLanguage') === 'js'
? 'view.%s.%s'
Expand Down Expand Up @@ -113,6 +106,30 @@ public function preRender($pugCode)
return $pugCode;
}

/**
* Pug code transformation to do before Pug render.
*
* @param string $pugCode code input
*
* @return string
*/
public function preRender($pugCode)
{
$newCode = '';
while (mb_strlen($pugCode)) {
if (!preg_match('/^(.*)("(?:\\\\[\\s\\S]|[^"\\\\])*"|\'(?:\\\\[\\s\\S]|[^\'\\\\])*\')/U', $pugCode, $match)) {
$newCode .= $this->replaceCode($pugCode);

break;
}

$newCode .= $this->replaceCode($match[1]) . $match[2];
$pugCode = mb_substr($pugCode, mb_strlen($match[0]));
}

return $newCode;
}

protected function registerHelpers(ContainerInterface $services, $helpers)
{
$this->helpers = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testCommand()

$output = $commandTester->getDisplay();

$this->assertContains('8 templates cached', $output, 'All templates can be cached except filter.pug as the upper filter does not exists.');
$this->assertContains('9 templates cached', $output, 'All templates can be cached except filter.pug as the upper filter does not exists.');
$this->assertContains('1 templates failed to be cached', $output, 'filter.pug fails as the upper filter does not exists.');
}
}
9 changes: 9 additions & 0 deletions tests/Pug/PugSymfonyEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,13 @@ public function testForbidView()
{
(new PugSymfonyEngine(self::$kernel))->render('p.pug', ['view' => 42]);
}

public function testIssue11BackgroundImage()
{
$pugSymfony = new PugSymfonyEngine(self::$kernel);
$pugSymfony->setOption('expressionLanguage', 'js');
$html = trim($pugSymfony->render('background-image.pug', ['image' => 'foo']));

self::assertSame('<div style="background-image: url(foo);" class="slide"></div>', $html);
}
}
1 change: 1 addition & 0 deletions tests/project/app/Resources/views/background-image.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.slide(style="background-image: url(" + image + ");")

0 comments on commit 315b407

Please sign in to comment.