Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions pkg/visitor/formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
}

Expand Down
41 changes: 41 additions & 0 deletions pkg/visitor/formatter/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("<b>"),
},
&ast.StmtInlineHtml{
Value: []byte("<i>"),
},
&ast.StmtEcho{
Exprs: []ast.Vertex{
&ast.ExprVariable{
Name: &ast.Identifier{
Value: []byte("$foo"),
},
},
},
},
&ast.StmtInlineHtml{
Value: []byte("</b>"),
},
},
}

formatterValue := formatter.NewFormatter()
node.Accept(formatterValue)

printerValue := printer.NewPrinter(buffer)
node.Accept(printerValue)

expected := `<b><i><?php
echo $foo;?></b>`
actual := buffer.String()

if expected != actual {
test.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}

func TestFormatter_StmtInterface(test *testing.T) {
buffer := bytes.NewBufferString("")

Expand Down
6 changes: 4 additions & 2 deletions pkg/visitor/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down