Skip to content

Commit b27a385

Browse files
dansloTomasVotruba
andauthored
Format and print a file that starts with inline HTML (#15)
* Format and print a file that starts with inline HTML The formatter closed PHP code before every inline HTML statement and gave its token the layout whitespace and an open tag, so a file starting with HTML came out as <?php ?><?php followed by the HTML in PHP code. The printer likewise wrote ?> before inline HTML that followed HTML. Both now close PHP code only when it is open. * Fold the HTML state check, add a printer test for adjacent inline HTML, update the open tag expectation --------- Co-authored-by: Tomas Votruba <tomas.vot@gmail.com>
1 parent d27cecd commit b27a385

4 files changed

Lines changed: 88 additions & 12 deletions

File tree

‎pkg/visitor/formatter/formatter.go‎

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,18 @@ func (formatter *formatter) formatStmts(list *[]ast.Vertex) {
111111
formatter.lastSemiColon = nil
112112

113113
if _, ok := statement.(*ast.StmtInlineHtml); ok {
114-
if formatter.lastSemiColon != nil {
115-
formatter.lastSemiColon.Value = append(formatter.lastSemiColon.Value, '?', '>')
116-
} else {
117-
*list = insert(*list, i+insertCounter, &ast.StmtNop{
118-
SemiColonTkn: &token.Token{
119-
Value: []byte("?>"),
120-
},
121-
})
122-
insertCounter++
114+
// Only open PHP code needs closing: not the start of a file, nor more HTML.
115+
if formatter.state != FormatterStateHTML {
116+
if formatter.lastSemiColon != nil {
117+
formatter.lastSemiColon.Value = append(formatter.lastSemiColon.Value, '?', '>')
118+
} else {
119+
*list = insert(*list, i+insertCounter, &ast.StmtNop{
120+
SemiColonTkn: &token.Token{
121+
Value: []byte("?>"),
122+
},
123+
})
124+
insertCounter++
125+
}
123126
}
124127
} else {
125128
formatter.addFreeFloating(token.T_WHITESPACE, []byte("\n"))
@@ -748,7 +751,17 @@ func (formatter *formatter) StmtIf(node *ast.StmtIf) {
748751
}
749752

750753
func (formatter *formatter) StmtInlineHtml(node *ast.StmtInlineHtml) {
751-
node.InlineHtmlTkn = formatter.newToken(token.T_STRING, node.Value)
754+
// In HTML, layout whitespace would be output and needs no open tag.
755+
freeFloating := formatter.freeFloating
756+
if formatter.state == FormatterStateHTML {
757+
freeFloating = nil
758+
}
759+
formatter.resetFreeFloating()
760+
node.InlineHtmlTkn = &token.Token{
761+
ID: token.T_INLINE_HTML,
762+
Value: node.Value,
763+
FreeFloating: freeFloating,
764+
}
752765
formatter.state = FormatterStateHTML
753766
}
754767

‎pkg/visitor/formatter/formatter_test.go‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,6 +1811,47 @@ func TestFormatter_StmtInlineHtml(test *testing.T) {
18111811
}
18121812
}
18131813

1814+
func TestFormatter_RootStartingWithInlineHtml(test *testing.T) {
1815+
buffer := bytes.NewBufferString("")
1816+
1817+
node := &ast.Root{
1818+
Stmts: []ast.Vertex{
1819+
&ast.StmtInlineHtml{
1820+
Value: []byte("<b>"),
1821+
},
1822+
&ast.StmtInlineHtml{
1823+
Value: []byte("<i>"),
1824+
},
1825+
&ast.StmtEcho{
1826+
Exprs: []ast.Vertex{
1827+
&ast.ExprVariable{
1828+
Name: &ast.Identifier{
1829+
Value: []byte("$foo"),
1830+
},
1831+
},
1832+
},
1833+
},
1834+
&ast.StmtInlineHtml{
1835+
Value: []byte("</b>"),
1836+
},
1837+
},
1838+
}
1839+
1840+
formatterValue := formatter.NewFormatter()
1841+
node.Accept(formatterValue)
1842+
1843+
printerValue := printer.NewPrinter(buffer)
1844+
node.Accept(printerValue)
1845+
1846+
expected := `<b><i><?php
1847+
echo $foo;?></b>`
1848+
actual := buffer.String()
1849+
1850+
if expected != actual {
1851+
test.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
1852+
}
1853+
}
1854+
18141855
func TestFormatter_StmtInterface(test *testing.T) {
18151856
buffer := bytes.NewBufferString("")
18161857

‎pkg/visitor/printer/printer.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,10 +458,12 @@ func (printer *printer) StmtIf(node *ast.StmtIf) {
458458
}
459459

460460
func (printer *printer) StmtInlineHtml(node *ast.StmtInlineHtml) {
461-
printer.state = PrinterStatePHP
462-
if printer.last != nil && !bytes.HasSuffix(printer.last, []byte("?>")) && !bytes.HasSuffix(printer.last, []byte("?>\n")) {
461+
// Only open PHP code needs closing: not the start of a file, nor HTML.
462+
if printer.state == PrinterStatePHP && printer.last != nil && !bytes.HasSuffix(printer.last, []byte("?>")) && !bytes.HasSuffix(printer.last, []byte("?>\n")) {
463463
printer.write([]byte("?>"))
464464
}
465+
// The HTML itself is written as is, without an open tag.
466+
printer.state = PrinterStatePHP
465467

466468
printer.printToken(node.InlineHtmlTkn, node.Value)
467469
printer.state = PrinterStateHTML

‎pkg/visitor/printer/printer_test.go‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,26 @@ func TestPrinterPrintFileInlineHtml(test *testing.T) {
9696
}
9797
}
9898

99+
func TestPrinterPrintFileAdjacentInlineHtml(test *testing.T) {
100+
buffer := bytes.NewBufferString("")
101+
102+
printerValue := printer.NewPrinter(buffer).WithState(printer.PrinterStatePHP)
103+
node := &ast.Root{
104+
Stmts: []ast.Vertex{
105+
&ast.StmtInlineHtml{Value: []byte("<b>")},
106+
&ast.StmtInlineHtml{Value: []byte("</b>")},
107+
},
108+
}
109+
node.Accept(printerValue)
110+
111+
expected := `<b></b>`
112+
actual := buffer.String()
113+
114+
if expected != actual {
115+
test.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
116+
}
117+
}
118+
99119
// node
100120

101121
func TestPrinterPrintIdentifier(test *testing.T) {

0 commit comments

Comments
 (0)