Skip to content

Commit da39c64

Browse files
committed
glayzzle#120 - fix inline raw attribute
1 parent b26659b commit da39c64

File tree

5 files changed

+41
-21
lines changed

5 files changed

+41
-21
lines changed

dist/php-parser.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7250,12 +7250,18 @@ module.exports = {
72507250

72517251
case this.tok.T_INLINE_HTML:
72527252
{
7253-
var start = this.lexer.yylloc.prev_line != this.lexer.yylloc.first_line ? this.lexer.yylloc.prev_offset : this.lexer.yylloc.first_offset;
7254-
result = this.node("inline");
72557253
var value = this.text();
7254+
var prevChar = this.lexer.yylloc.first_offset > 0 ? this.lexer._input[this.lexer.yylloc.first_offset - 1] : null;
7255+
var fixFirstLine = prevChar === "\r" || prevChar === "\n";
7256+
// revert back the first stripped line
7257+
if (fixFirstLine) {
7258+
if (prevChar === "\n" && this.lexer.yylloc.first_offset > 1 && this.lexer._input[this.lexer.yylloc.first_offset - 2] === "\r") {
7259+
prevChar = "\r\n";
7260+
}
7261+
}
7262+
result = this.node("inline");
72567263
this.next();
7257-
var raw = this.lexer._input.substring(start, this.lexer.yylloc.prev_offset);
7258-
return result(value, raw);
7264+
return result(value, fixFirstLine ? prevChar + value : value);
72597265
}
72607266

72617267
case this.tok.T_UNSET:

dist/php-parser.min.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/php-parser.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/parser/statement.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -274,18 +274,25 @@ module.exports = {
274274
}
275275

276276
case this.tok.T_INLINE_HTML: {
277-
const start =
278-
this.lexer.yylloc.prev_line != this.lexer.yylloc.first_line
279-
? this.lexer.yylloc.prev_offset
280-
: this.lexer.yylloc.first_offset;
281-
result = this.node("inline");
282277
const value = this.text();
278+
let prevChar =
279+
this.lexer.yylloc.first_offset > 0
280+
? this.lexer._input[this.lexer.yylloc.first_offset - 1]
281+
: null;
282+
const fixFirstLine = prevChar === "\r" || prevChar === "\n";
283+
// revert back the first stripped line
284+
if (fixFirstLine) {
285+
if (
286+
prevChar === "\n" &&
287+
this.lexer.yylloc.first_offset > 1 &&
288+
this.lexer._input[this.lexer.yylloc.first_offset - 2] === "\r"
289+
) {
290+
prevChar = "\r\n";
291+
}
292+
}
293+
result = this.node("inline");
283294
this.next();
284-
const raw = this.lexer._input.substring(
285-
start,
286-
this.lexer.yylloc.prev_offset
287-
);
288-
return result(value, raw);
295+
return result(value, fixFirstLine ? prevChar + value : value);
289296
}
290297

291298
case this.tok.T_UNSET:

test/astTests.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,18 @@ describe("Test AST structure", function() {
3131
});
3232

3333
it("test inline", function() {
34-
var ast = parser.parseCode('Hello <?php echo "World"; ?> !');
34+
const ast = parser.parseCode("Hello <?php echo 'World'; ?>\n !");
3535
ast.children[0].kind.should.be.exactly("inline");
3636
ast.children[2].kind.should.be.exactly("inline");
3737
ast.children[0].value.should.be.exactly("Hello ");
3838
ast.children[2].value.should.be.exactly(" !");
39+
ast.children[2].raw.should.be.exactly("\n !");
40+
});
41+
42+
it("fix #120", function() {
43+
const ast = parser.parseCode("<?php echo 'World'; ?>\r\n !");
44+
ast.children[1].value.should.be.exactly(" !");
45+
ast.children[1].raw.should.be.exactly("\r\n !");
3946
});
4047

4148
it("test magics", function() {

0 commit comments

Comments
 (0)