Skip to content

Commit

Permalink
Merge pull request #52 from Talesoft/dev-master
Browse files Browse the repository at this point in the history
Improved lexer space handling, there should be no way to trigger exceptions anymore
  • Loading branch information
TorbenKoehn committed Jan 16, 2016
2 parents 8a03371 + 574ee9d commit f65f096
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 25 deletions.
48 changes: 25 additions & 23 deletions Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -864,33 +864,35 @@ protected function scanIndent()
$tabs = $this->strpos($indent, "\t") !== false;
$mixed = $spaces && $tabs;

//Don't allow mixed indentation, this will just confuse the lexer
if ($mixed || ($this->_indentStyle === self::INDENT_SPACE && $tabs)) {

//Well, let's try a conversion if were using spaces and have an indentWidth already
if ($this->_indentStyle === self::INDENT_SPACE && $this->_indentWidth !== null) {

//We replace all tabs (\t) by indentWidth * spaces
$spaces = str_replace("\t", str_repeat(self::INDENT_SPACE, $this->_indentWidth), $spaces);
$tabs = false;
$mixed = false;
} else {

$this->throwException(
"Mixed indentation style encountered. "
."Dont mix tabs and spaces. Stick to one of both."
);
if ($mixed) {

switch ($this->_indentStyle) {
case self::INDENT_SPACE:
default:

//Convert tabs to spaces based on indentWidth
$spaces = str_replace(self::INDENT_TAB, str_repeat(
self::INDENT_SPACE,
$this->_indentWidth ? $this->_indentWidth : 4
), $spaces);
$tabs = false;
$mixed = false;
break;
case self::INDENT_TAB:

//Convert spaces to tabs
$spaces = str_replace(self::INDENT_SPACE, str_repeat(
self::INDENT_TAB,
$this->_indentWidth ? $this->_indentWidth : 1
), $spaces);
$spaces = false;
$mixed = false;
break;
}
}

//Validate the indentation style
$indentStyle = $tabs ? self::INDENT_TAB : self::INDENT_SPACE;
if ($this->_indentStyle && $this->_indentStyle !== $indentStyle)
$this->throwException(
"Mixed indentation style encountered. "
."You used another indentation style in this line than in "
."previous lines. Dont do that."
);
$this->_indentStyle = $tabs ? self::INDENT_TAB : self::INDENT_SPACE;

//Validate the indentation width
if (!$this->_indentWidth)
Expand Down
21 changes: 20 additions & 1 deletion Test/IssueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ public function testIssue44()
doctype html
JADE;


$renderer = new Renderer([
'adapterOptions' => [
'path' => __DIR__.'/cache/issues'
],
'pretty' => false,
'paths' => [__DIR__.'/views/issues']
]);

$this->assertEquals('<?php /** * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) */?><!DOCTYPE html>', $renderer->compile($jade));
$this->assertEquals('<?php /*** CakePHP(tm) : Rapid Development Framework (http://cakephp.org) * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) */?><!DOCTYPE html><!DOCTYPE html> <html> <head><?=$view->Html->charset()?> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">', $renderer->compileFile('issue-44'));

$this->assertEquals('<ul class="right hide-on-med-and-down"><li><?=$view->Html->link(__(\'Dashboard\'), [\'controller\' => \'Users\', \'action\' => \'index\'])?></li><li><?=$view->Html->link(__(\'Log Out\'), [\'controller\' => \'Users\', \'action\' => \'logout\'])?></li></ul><ul id="nav-mobile" class="side-nav"><li><?=$view->Html->link(__(\'Dashboard\'), [\'controller\' => \'Users\', \'action\' => \'index\'])?></li><li><?=$view->Html->link(__(\'Log Out\'), [\'controller\' => \'Users\', \'action\' => \'logout\'])?></li></ul><a class="button-collapse" href="#" data-activates="nav-mobile"><i class="material-icons">menu</i></a><div class="progress -main"><div class="indeterminate"></div></div>', $this->_renderer->compileFile('issue-44/for_members.ctp.1'));
$this->assertEquals('<ul class="right hide-on-med-and-down"><li><?=$view->Html->link(__(\'Dashboard\'), [\'controller\' => \'Users\', \'action\' => \'index\'])?></li><li><?=$view->Html->link(__(\'Log Out\'), [\'controller\' => \'Users\', \'action\' => \'logout\'])?></li></ul><ul id="nav-mobile" class="side-nav"><li><?=$view->Html->link(__(\'Dashboard\'), [\'controller\' => \'Users\', \'action\' => \'index\'])?></li><li><?=$view->Html->link(__(\'Log Out\'), [\'controller\' => \'Users\', \'action\' => \'logout\'])?></li></ul><a class="button-collapse" href="#" data-activates="nav-mobile"><i class="material-icons">menu</i></a><div class="progress -main"><div class="indeterminate"></div></div>', $this->_renderer->compileFile('issue-44/for_members.ctp.2'));


$renderer = new Renderer([
'adapterOptions' => [
'path' => __DIR__.'/cache/issues'
Expand All @@ -89,6 +105,9 @@ public function testIssue44()
]);

$this->assertEquals('<?php /** * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) */?><!DOCTYPE html>', $renderer->compile($jade));
$this->assertEquals('<?php /** * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) */?><!DOCTYPE html>', $renderer->compileFile('issue-44'));
$this->assertEquals('<?php /** * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) */?><!DOCTYPE html><!DOCTYPE html> <html> <head><?=$view->Html->charset()?> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">', $renderer->compileFile('issue-44'));

$this->assertEquals('<ul class="right hide-on-med-and-down"><li><?=$view->Html->link(__(\'Dashboard\'), [\'controller\' => \'Users\', \'action\' => \'index\'])?></li><li><?=$view->Html->link(__(\'Log Out\'), [\'controller\' => \'Users\', \'action\' => \'logout\'])?></li></ul><ul id="nav-mobile" class="side-nav"><li><?=$view->Html->link(__(\'Dashboard\'), [\'controller\' => \'Users\', \'action\' => \'index\'])?></li><li><?=$view->Html->link(__(\'Log Out\'), [\'controller\' => \'Users\', \'action\' => \'logout\'])?></li></ul><a class="button-collapse" href="#" data-activates="nav-mobile"><i class="material-icons">menu</i></a><div class="progress -main"><div class="indeterminate"></div></div>', $this->_renderer->compileFile('issue-44/for_members.ctp.1'));
$this->assertEquals('<ul class="right hide-on-med-and-down"><li><?=$view->Html->link(__(\'Dashboard\'), [\'controller\' => \'Users\', \'action\' => \'index\'])?></li><li><?=$view->Html->link(__(\'Log Out\'), [\'controller\' => \'Users\', \'action\' => \'logout\'])?></li></ul><ul id="nav-mobile" class="side-nav"><li><?=$view->Html->link(__(\'Dashboard\'), [\'controller\' => \'Users\', \'action\' => \'index\'])?></li><li><?=$view->Html->link(__(\'Log Out\'), [\'controller\' => \'Users\', \'action\' => \'logout\'])?></li></ul><a class="button-collapse" href="#" data-activates="nav-mobile"><i class="material-icons">menu</i></a><div class="progress -main"><div class="indeterminate"></div></div>', $this->_renderer->compileFile('issue-44/for_members.ctp.2'));
}
}
9 changes: 8 additions & 1 deletion Test/views/issues/issue-44.jade
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*/
doctype html
doctype html


doctype html
<html>
<head>
!= $view->Html->charset()
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
3 changes: 3 additions & 0 deletions Test/views/issues/issue-44/default.ctp.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

block nav-wrapper-content
block next-to-header
15 changes: 15 additions & 0 deletions Test/views/issues/issue-44/for_members.ctp.1.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
extends default.ctp.jade

block nav-wrapper-content
ul.right.hide-on-med-and-down
li!= $view->Html->link(__('Dashboard'), ['controller' => 'Users', 'action' => 'index'])
li!= $view->Html->link(__('Log Out'), ['controller' => 'Users', 'action' => 'logout'])

ul#nav-mobile.side-nav
li!= $view->Html->link(__('Dashboard'), ['controller' => 'Users', 'action' => 'index'])
li!= $view->Html->link(__('Log Out'), ['controller' => 'Users', 'action' => 'logout'])
a.button-collapse(href="#" data-activates="nav-mobile"): i.material-icons menu

block next-to-header
.progress(class='-main')
.indeterminate
15 changes: 15 additions & 0 deletions Test/views/issues/issue-44/for_members.ctp.2.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
extends default.ctp.jade

block nav-wrapper-content
ul.right.hide-on-med-and-down
li!= $view->Html->link(__('Dashboard'), ['controller' => 'Users', 'action' => 'index'])
li!= $view->Html->link(__('Log Out'), ['controller' => 'Users', 'action' => 'logout'])

ul#nav-mobile.side-nav
li!= $view->Html->link(__('Dashboard'), ['controller' => 'Users', 'action' => 'index'])
li!= $view->Html->link(__('Log Out'), ['controller' => 'Users', 'action' => 'logout'])
a.button-collapse(href="#" data-activates="nav-mobile"): i.material-icons menu

block next-to-header
.progress(class='-main')
.indeterminate

0 comments on commit f65f096

Please sign in to comment.