diff --git a/pkg/visitor/formatter/formatter.go b/pkg/visitor/formatter/formatter.go
index c82a741..6d81125 100644
--- a/pkg/visitor/formatter/formatter.go
+++ b/pkg/visitor/formatter/formatter.go
@@ -102,7 +102,10 @@ func (formatter *formatter) formatStmts(list *[]ast.Vertex) {
formatter.lastSemiColon = nil
if _, ok := statement.(*ast.StmtInlineHtml); ok {
- if formatter.lastSemiColon != nil {
+ if formatter.state == FormatterStateHTML {
+ // Nothing to close: the file starts with HTML, or more HTML
+ // follows.
+ } else if formatter.lastSemiColon != nil {
formatter.lastSemiColon.Value = append(formatter.lastSemiColon.Value, '?', '>')
} else {
*list = insert(*list, i+insertCounter, &ast.StmtNop{
@@ -710,7 +713,17 @@ func (formatter *formatter) StmtIf(node *ast.StmtIf) {
}
func (formatter *formatter) StmtInlineHtml(node *ast.StmtInlineHtml) {
- node.InlineHtmlTkn = formatter.newToken(token.T_STRING, node.Value)
+ // In HTML, layout whitespace would be output and needs no open tag.
+ freeFloating := formatter.freeFloating
+ if formatter.state == FormatterStateHTML {
+ freeFloating = nil
+ }
+ formatter.resetFreeFloating()
+ node.InlineHtmlTkn = &token.Token{
+ ID: token.T_INLINE_HTML,
+ Value: node.Value,
+ FreeFloating: freeFloating,
+ }
formatter.state = FormatterStateHTML
}
diff --git a/pkg/visitor/formatter/formatter_test.go b/pkg/visitor/formatter/formatter_test.go
index 6e501bb..de422cb 100644
--- a/pkg/visitor/formatter/formatter_test.go
+++ b/pkg/visitor/formatter/formatter_test.go
@@ -1780,6 +1780,47 @@ func TestFormatter_StmtInlineHtml(test *testing.T) {
}
}
+func TestFormatter_RootStartingWithInlineHtml(test *testing.T) {
+ buffer := bytes.NewBufferString("")
+
+ node := &ast.Root{
+ Stmts: []ast.Vertex{
+ &ast.StmtInlineHtml{
+ Value: []byte(""),
+ },
+ &ast.StmtInlineHtml{
+ Value: []byte(""),
+ },
+ &ast.StmtEcho{
+ Exprs: []ast.Vertex{
+ &ast.ExprVariable{
+ Name: &ast.Identifier{
+ Value: []byte("$foo"),
+ },
+ },
+ },
+ },
+ &ast.StmtInlineHtml{
+ Value: []byte(""),
+ },
+ },
+ }
+
+ formatterValue := formatter.NewFormatter()
+ node.Accept(formatterValue)
+
+ printerValue := printer.NewPrinter(buffer)
+ node.Accept(printerValue)
+
+ expected := ``
+ actual := buffer.String()
+
+ if expected != actual {
+ test.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
+ }
+}
+
func TestFormatter_StmtInterface(test *testing.T) {
buffer := bytes.NewBufferString("")
diff --git a/pkg/visitor/printer/printer.go b/pkg/visitor/printer/printer.go
index 2df692a..ae33463 100644
--- a/pkg/visitor/printer/printer.go
+++ b/pkg/visitor/printer/printer.go
@@ -458,10 +458,12 @@ func (printer *printer) StmtIf(node *ast.StmtIf) {
}
func (printer *printer) StmtInlineHtml(node *ast.StmtInlineHtml) {
- printer.state = PrinterStatePHP
- if printer.last != nil && !bytes.HasSuffix(printer.last, []byte("?>")) && !bytes.HasSuffix(printer.last, []byte("?>\n")) {
+ // Only open PHP code needs closing: not the start of a file, nor HTML.
+ if printer.state == PrinterStatePHP && printer.last != nil && !bytes.HasSuffix(printer.last, []byte("?>")) && !bytes.HasSuffix(printer.last, []byte("?>\n")) {
printer.write([]byte("?>"))
}
+ // The HTML itself is written as is, without an open tag.
+ printer.state = PrinterStatePHP
printer.printToken(node.InlineHtmlTkn, node.Value)
printer.state = PrinterStateHTML