Skip to content

Commit ec614c9

Browse files
committed
Add hasLeadingNewline attribute to InlineHTML
Use this attribute to not print an extra newline if the original code did not have it.
1 parent 21b18eb commit ec614c9

File tree

7 files changed

+39
-8
lines changed

7 files changed

+39
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
Version 3.0.0-dev
22
-----------------
33

4-
Nothing yet.
4+
### Added
5+
6+
* The `InlineHTML` node now has an `hasLeadingNewline` attribute, that specifies whether the
7+
preceding closing tag contained a newline. The pretty printer honors this attribute.
58

69
Version 3.0.0-alpha1 (2016-07-25)
710
---------------------------------

lib/PhpParser/Lexer.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Lexer
1212
protected $pos;
1313
protected $line;
1414
protected $filePos;
15+
protected $prevCloseTagHasNewline;
1516

1617
protected $tokenMap;
1718
protected $dropTokens;
@@ -67,6 +68,10 @@ public function startLexing($code) {
6768
$this->pos = -1;
6869
$this->line = 1;
6970
$this->filePos = 0;
71+
72+
// If inline HTML occurs without preceding code, treat it as if it had a leading newline.
73+
// This ensures proper composability, because having a newline is the "safe" assumption.
74+
$this->prevCloseTagHasNewline = true;
7075
}
7176

7277
protected function resetErrors() {
@@ -166,6 +171,11 @@ public function getNextToken(&$value = null, &$startAttributes = null, &$endAttr
166171
} elseif (!isset($this->dropTokens[$token[0]])) {
167172
$value = $token[1];
168173
$id = $this->tokenMap[$token[0]];
174+
if (T_CLOSE_TAG === $token[0]) {
175+
$this->prevCloseTagHasNewline = false !== strpos($token[1], "\n");
176+
} else if (T_INLINE_HTML === $token[0]) {
177+
$startAttributes['hasLeadingNewline'] = $this->prevCloseTagHasNewline;
178+
}
169179

170180
$this->line += substr_count($value, "\n");
171181
$this->filePos += \strlen($value);

lib/PhpParser/PrettyPrinter/Standard.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,8 @@ protected function pStmt_Unset(Stmt\Unset_ $node) {
789789
}
790790

791791
protected function pStmt_InlineHTML(Stmt\InlineHTML $node) {
792-
return '?>' . $this->pNoIndent("\n" . $node->value) . '<?php ';
792+
$newline = $node->getAttribute('hasLeadingNewline', true) ? "\n" : '';
793+
return '?>' . $this->pNoIndent($newline . $node->value) . '<?php ';
793794
}
794795

795796
protected function pStmt_HaltCompiler(Stmt\HaltCompiler $node) {

test/PhpParser/LexerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public function provideTestLex() {
7272
),
7373
array(
7474
Tokens::T_INLINE_HTML, 'plaintext',
75-
array('startLine' => 1), array('endLine' => 1)
75+
array('startLine' => 1, 'hasLeadingNewline' => false),
76+
array('endLine' => 1)
7677
),
7778
)
7879
),

test/PhpParser/ParserTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ public function testInvalidToken() {
110110
}
111111

112112
/**
113-
* @dataProvider provideTestKindAttributes
113+
* @dataProvider provideTestExtraAttributes
114114
*/
115-
public function testKindAttributes($code, $expectedAttributes) {
115+
public function testExtraAttributes($code, $expectedAttributes) {
116116
$parser = $this->getParser(new Lexer);
117117
$stmts = $parser->parse("<?php $code;");
118118
$attributes = $stmts[0]->getAttributes();
@@ -121,7 +121,7 @@ public function testKindAttributes($code, $expectedAttributes) {
121121
}
122122
}
123123

124-
public function provideTestKindAttributes() {
124+
public function provideTestExtraAttributes() {
125125
return array(
126126
array('0', ['kind' => Scalar\LNumber::KIND_DEC]),
127127
array('9', ['kind' => Scalar\LNumber::KIND_DEC]),
@@ -158,6 +158,8 @@ public function provideTestKindAttributes() {
158158
array("die('done')", ['kind' => Expr\Exit_::KIND_DIE]),
159159
array("exit", ['kind' => Expr\Exit_::KIND_EXIT]),
160160
array("exit(1)", ['kind' => Expr\Exit_::KIND_EXIT]),
161+
array("?>Foo", ['hasLeadingNewline' => false]),
162+
array("?>\nFoo", ['hasLeadingNewline' => true]),
161163
);
162164
}
163165
}

test/code/prettyPrinter/inlineHTMLandPHPtest.file-test

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,10 @@ HTML
4949
<?php
5050
echo 'PHP';
5151
?>
52-
HTML
52+
HTML
53+
-----
54+
HTML<?php echo 'PHP'; ?>HTML
55+
-----
56+
HTML<?php
57+
echo 'PHP';
58+
?>HTML

test/code/prettyPrinter/onlyInlineHTML.file-test

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ World Hallo
88
Hallo World
99
Foo Bar
1010
Bar Foo
11-
World Hallo
11+
World Hallo
12+
-----
13+
14+
15+
Test
16+
-----
17+
18+
19+
Test

0 commit comments

Comments
 (0)