diff --git a/src/compilers/php-render-compiler.ts b/src/compilers/php-render-compiler.ts index 573c2c2a..2633fe58 100644 --- a/src/compilers/php-render-compiler.ts +++ b/src/compilers/php-render-compiler.ts @@ -650,33 +650,30 @@ const elementSourceCompiler = { }) if (bindDirective) { - emitter.writeLine( - '(function ($bindObj) use (&$html){foreach ($bindObj as $key => $value) {' - ) + emitter.nextLine('(') + emitter.writeAnonymousFunction(['$bindObj'], ['&$html'], () => { + emitter.writeForeach('$bindObj as $key => $value', () => { + if (tagName === 'textarea') { + emitter.writeIf('$key == "value"', () => emitter.writeContinue()) + } - if (tagName === 'textarea') { - emitter.writeIf('$key == "value"', () => { - emitter.writeLine('continue;') + emitter.writeSwitch('$key', () => { + emitter.writeCase('"readonly"') + emitter.writeCase('"disabled"') + emitter.writeCase('"multiple"', () => { + emitter.writeLine('$html .= _::boolAttrFilter($key, _::escapeHTML($value));') + emitter.writeBreak() + }) + emitter.writeDefault(() => { + emitter.writeLine('$html .= _::attrFilter($key, _::escapeHTML($value));') + }) + }) }) - } - - emitter.writeLine('switch ($key) {\n' + - 'case "readonly":\n' + - 'case "disabled":\n' + - 'case "multiple":\n' + - 'case "multiple":\n' + - '$html .= _::boolAttrFilter($key, _::escapeHTML($value));\n' + - 'break;\n' + - 'default:\n' + - '$html .= _::attrFilter($key, _::escapeHTML($value));' + - '}' - ) + }) - emitter.writeLine( - '}})(' + - ExpressionEmitter.expr(bindDirective.value) + - ');' - ) + emitter.write(')(') + emitter.write(ExpressionEmitter.expr(bindDirective.value)) + emitter.feedLine(');') } emitter.bufferHTMLLiteral('>') @@ -1182,7 +1179,7 @@ function genComponentContextCode (component, emitter) { emitter.writeLine('"slotRenderers" => []') emitter.unindent() - emitter.feedLine('];') + emitter.writeLine('];') emitter.writeLine('$ctx->instance = _::createComponent($ctx);') } diff --git a/src/emitters/php-emitter.ts b/src/emitters/php-emitter.ts index 2c60809e..05700886 100644 --- a/src/emitters/php-emitter.ts +++ b/src/emitters/php-emitter.ts @@ -52,6 +52,35 @@ export class PHPEmitter extends Emitter { this.writeHTML(ExpressionEmitter.stringLiteralize(buffer)) } + /** + * switch + */ + public writeSwitch (expr: string, body: Function) { + this.writeLine(`switch (${expr}) {`) + this.indent() + body() + this.unindent() + this.writeLine('}') + } + + public writeCase (expr: string, body: Function = () => null) { + this.writeLine(`case ${expr}:`) + this.indent() + body() + this.unindent() + } + + public writeBreak () { + this.writeLine('break;') + } + + public writeDefault (body: Function = () => null) { + this.writeLine('default:') + this.indent() + body() + this.unindent() + } + /** * function */ @@ -108,6 +137,9 @@ export class PHPEmitter extends Emitter { public endForeach () { this.endBlock() } + public writeContinue () { + this.writeLine('continue;') + } /** * block