Skip to content

Commit

Permalink
#120 - fix inline raw attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
ichiriac committed Mar 5, 2018
1 parent b26659b commit da39c64
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 21 deletions.
14 changes: 10 additions & 4 deletions dist/php-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7250,12 +7250,18 @@ module.exports = {

case this.tok.T_INLINE_HTML:
{
var start = this.lexer.yylloc.prev_line != this.lexer.yylloc.first_line ? this.lexer.yylloc.prev_offset : this.lexer.yylloc.first_offset;
result = this.node("inline");
var value = this.text();
var prevChar = this.lexer.yylloc.first_offset > 0 ? this.lexer._input[this.lexer.yylloc.first_offset - 1] : null;
var fixFirstLine = prevChar === "\r" || prevChar === "\n";
// revert back the first stripped line
if (fixFirstLine) {
if (prevChar === "\n" && this.lexer.yylloc.first_offset > 1 && this.lexer._input[this.lexer.yylloc.first_offset - 2] === "\r") {
prevChar = "\r\n";
}
}
result = this.node("inline");
this.next();
var raw = this.lexer._input.substring(start, this.lexer.yylloc.prev_offset);
return result(value, raw);
return result(value, fixFirstLine ? prevChar + value : value);
}

case this.tok.T_UNSET:
Expand Down
10 changes: 5 additions & 5 deletions dist/php-parser.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/php-parser.min.js.map

Large diffs are not rendered by default.

27 changes: 17 additions & 10 deletions src/parser/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,18 +274,25 @@ module.exports = {
}

case this.tok.T_INLINE_HTML: {
const start =
this.lexer.yylloc.prev_line != this.lexer.yylloc.first_line
? this.lexer.yylloc.prev_offset
: this.lexer.yylloc.first_offset;
result = this.node("inline");
const value = this.text();
let prevChar =
this.lexer.yylloc.first_offset > 0
? this.lexer._input[this.lexer.yylloc.first_offset - 1]
: null;
const fixFirstLine = prevChar === "\r" || prevChar === "\n";
// revert back the first stripped line
if (fixFirstLine) {
if (
prevChar === "\n" &&
this.lexer.yylloc.first_offset > 1 &&
this.lexer._input[this.lexer.yylloc.first_offset - 2] === "\r"
) {
prevChar = "\r\n";
}
}
result = this.node("inline");
this.next();
const raw = this.lexer._input.substring(
start,
this.lexer.yylloc.prev_offset
);
return result(value, raw);
return result(value, fixFirstLine ? prevChar + value : value);
}

case this.tok.T_UNSET:
Expand Down
9 changes: 8 additions & 1 deletion test/astTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,18 @@ describe("Test AST structure", function() {
});

it("test inline", function() {
var ast = parser.parseCode('Hello <?php echo "World"; ?> !');
const ast = parser.parseCode("Hello <?php echo 'World'; ?>\n !");
ast.children[0].kind.should.be.exactly("inline");
ast.children[2].kind.should.be.exactly("inline");
ast.children[0].value.should.be.exactly("Hello ");
ast.children[2].value.should.be.exactly(" !");
ast.children[2].raw.should.be.exactly("\n !");
});

it("fix #120", function() {
const ast = parser.parseCode("<?php echo 'World'; ?>\r\n !");
ast.children[1].value.should.be.exactly(" !");
ast.children[1].raw.should.be.exactly("\r\n !");
});

it("test magics", function() {
Expand Down

0 comments on commit da39c64

Please sign in to comment.