Skip to content

Commit

Permalink
fix: 插值运算为 + 时,进行了局部转义,应为整体转义
Browse files Browse the repository at this point in the history
  • Loading branch information
meixg committed Jan 18, 2022
1 parent 690dce3 commit a6d4622
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ test/cases/*/ssr.php
test/cases/*/output
.vscode
/example
bin/sample
bin/output.js
bin/component*.js
bin/dist
2 changes: 1 addition & 1 deletion bin/debug-local.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function compile (componentPath, externalPath, outputPath) {
fs.writeFileSync(path.resolve(__dirname, outputPath), res)
}

compile('./component', './component2', './dist/component')
compile('./sample/component', './component2', './dist/component.js')
// compile('./component2', './component', './dist/component2')

// // online
Expand Down
15 changes: 13 additions & 2 deletions src/compilers/san-expr-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as TypeGuards from '../ast/san-ast-type-guards'
import { _ } from '../runtime/underscore'
import { EncodeURIComponent, MapLiteral, HelperCall, ArrayLiteral, FilterCall, FunctionCall, Identifier, ConditionalExpression, BinaryExpression, UnaryExpression, Expression } from '../ast/renderer-ast-dfn'
import { CTX_DATA, L, I, NULL } from '../ast/renderer-ast-util'
import { AccessorExpr, BinaryExpr, CallExpr, InterpExpr, StringLiteral, TertiaryExpr, TextExpr, UnaryExpr, ArrayLiteral as ArrayLiteralType, ObjectLiteral, Expr } from 'san'
import { AccessorExpr, BinaryExpr, CallExpr, InterpExpr, StringLiteral, TertiaryExpr, TextExpr, UnaryExpr, ArrayLiteral as ArrayLiteralType, ObjectLiteral, Expr, ExprType } from 'san'

// 输出类型
export enum OutputType {
Expand Down Expand Up @@ -61,8 +61,19 @@ function unary (e: UnaryExpr) {
throw new Error(`unexpected unary operator "${String.fromCharCode(e.operator)}"`)
}
function binary (e: BinaryExpr, output: OutputType) {
const lhs = sanExpr(e.segs[0], output)
const op = binaryOp[e.operator]

// + 的时候,不确定是字符串相加还是数字相加
// 因此只能在外层就转义
if (op === '+' && output === OutputType.ESCAPE_HTML) {
return interp({
type: ExprType.INTERP,
expr: e,
filters: []
}, output)
}

const lhs = sanExpr(e.segs[0], output)
const rhs = sanExpr(e.segs[1], output)
return new BinaryExpression(lhs, op, rhs)
}
Expand Down
44 changes: 42 additions & 2 deletions test/unit/compilers/san-expr-compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('compilers/san-expr-compiler', () => {
}))
})
it('should escape a binary expression', () => {
const e = parseExpr('a + b')
const e = parseExpr('a || b')
const dataItem = (value: string) => ({
kind: SyntaxKind.HelperCall,
name: 'output',
Expand All @@ -42,10 +42,50 @@ describe('compilers/san-expr-compiler', () => {
expect(res).toEqual(expect.objectContaining({
kind: SyntaxKind.BinaryExpression,
lhs: dataItem('a'),
op: '+',
op: '||',
rhs: dataItem('b')
}))
})
it('should escape a + binary expression outside', () => {
const e = parseExpr('a + b')
const dataItem = (value: string) => ({
kind: SyntaxKind.BinaryExpression,
lhs: {
kind: SyntaxKind.BinaryExpression,
lhs: {
kind: SyntaxKind.Identifier,
name: 'ctx'
},
op: '.',
rhs: {
kind: SyntaxKind.Identifier,
name: 'data'
}
},
op: '[]',
rhs: {
kind: SyntaxKind.Literal,
value
}
})
const res = expr(e, OutputType.ESCAPE_HTML)
expect(res).toMatchObject({
args: [
{
kind: SyntaxKind.BinaryExpression,
lhs: dataItem('a'),
op: '+',
rhs: dataItem('b')
},
{
kind: SyntaxKind.Literal,
value: true
}
],
kind: SyntaxKind.HelperCall,
name: 'output'
})
})
it('should compile unary expression', () => {
const e = parseExpr('+num === 123')
expect(expr(e)).toMatchObject({
Expand Down

0 comments on commit a6d4622

Please sign in to comment.