diff --git a/CHANGELOG.md b/CHANGELOG.md index eab602bb..c16c0d1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [3.14.2](https://github.com/baidu/san-ssr/compare/nv3.14.1...nv3.14.2) (2024-06-24) + + +### Bug Fixes + +* attr inherit case in expression ([#176](https://github.com/baidu/san-ssr/issues/176)) ([096742c](https://github.com/baidu/san-ssr/commit/096742c00d4682bc3ccadbdb782fc829e0afe123)) + ## [3.14.1](https://github.com/baidu/san-ssr/compare/nv3.14.0...nv3.14.1) (2024-01-26) diff --git a/package-lock.json b/package-lock.json index 7a43d928..3dc09c2c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "san-ssr", - "version": "3.14.1", + "version": "3.14.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "san-ssr", - "version": "3.14.1", + "version": "3.14.2", "license": "MIT", "dependencies": { "@types/estree": "0.0.45", @@ -54,7 +54,7 @@ "mkdirp": "^1.0.4", "mustache": "^4.0.1", "san": "^3.14.1", - "san-html-cases": "^3.14.5", + "san-html-cases": "^3.14.6", "san-ssr-target-fake-cmd": "^1.0.0", "san-ssr-target-fake-esm": "^1.0.0", "source-map-support": "^0.5.19", @@ -15413,9 +15413,9 @@ "dev": true }, "node_modules/san-html-cases": { - "version": "3.14.5", - "resolved": "https://registry.npmmirror.com/san-html-cases/-/san-html-cases-3.14.5.tgz", - "integrity": "sha512-wr09yYp2OD811sHATw1W2dAMz5KuR+Dhh1s4bRo8dH33ayJdcL2tE3a/P/1v/82/O1erjc1ySa2B0w5g5pc0Lw==", + "version": "3.14.6", + "resolved": "https://registry.npmmirror.com/san-html-cases/-/san-html-cases-3.14.6.tgz", + "integrity": "sha512-7Je1IpcPVcUgjVICnszxxoWFy++fE/cuJ54513062XUNy+Ky+ZWDCZxd9tBqrSMFPOZW9k/QBvcWzM/7AqDIqA==", "dev": true }, "node_modules/san-ssr-target-fake-cmd": { @@ -29791,9 +29791,9 @@ "dev": true }, "san-html-cases": { - "version": "3.14.5", - "resolved": "https://registry.npmmirror.com/san-html-cases/-/san-html-cases-3.14.5.tgz", - "integrity": "sha512-wr09yYp2OD811sHATw1W2dAMz5KuR+Dhh1s4bRo8dH33ayJdcL2tE3a/P/1v/82/O1erjc1ySa2B0w5g5pc0Lw==", + "version": "3.14.6", + "resolved": "https://registry.npmmirror.com/san-html-cases/-/san-html-cases-3.14.6.tgz", + "integrity": "sha512-7Je1IpcPVcUgjVICnszxxoWFy++fE/cuJ54513062XUNy+Ky+ZWDCZxd9tBqrSMFPOZW9k/QBvcWzM/7AqDIqA==", "dev": true }, "san-ssr-target-fake-cmd": { diff --git a/package.json b/package.json index 11d4f419..fce46700 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "san-ssr", - "version": "3.14.1", + "version": "3.14.3", "description": "San server-side-render framework and utils", "main": "dist/index.js", "types": "dist/index.d.ts", @@ -72,7 +72,7 @@ "mkdirp": "^1.0.4", "mustache": "^4.0.1", "san": "^3.14.1", - "san-html-cases": "^3.14.5", + "san-html-cases": "^3.14.6", "san-ssr-target-fake-cmd": "^1.0.0", "san-ssr-target-fake-esm": "^1.0.0", "source-map-support": "^0.5.19", diff --git a/src/compilers/anode-compiler.ts b/src/compilers/anode-compiler.ts index 1fd5157f..c8ad96df 100644 --- a/src/compilers/anode-compiler.ts +++ b/src/compilers/anode-compiler.ts @@ -20,7 +20,8 @@ import { ConditionalExpression, Typeof, AssignmentStatement, - ArrayLiteral + ArrayLiteral, + HelperCall } from '../ast/renderer-ast-dfn' import { CTX_DATA, @@ -333,26 +334,33 @@ export class ANodeCompiler { const childSlots = I(this.id.next('childSlots')) yield DEF(childSlots.name, new MapLiteral([])) + // joinAttr用于处理属性拼接 + const attrsNamed = I(this.id.next('joinAttr')) + const inheritAttrs = this.componentInfo.inheritAttrs && aNode.attrs && aNode.attrs.length // 处理属性合并 - if (this.componentInfo.inheritAttrs && aNode.attrs && aNode.attrs.length) { + if (inheritAttrs && aNode.attrs) { const attrList = [] const attrListMap = [] for (const attr of aNode.attrs) { + const attrValue = new HelperCall('escapeHTML', [sanExpr(attr.expr)]) const result = TypeGuards.isExprBoolNode(attr.expr) || attr.expr.value === '' ? L(attr.name) - : BINARY(BINARY(L(`${attr.name}="`), '+', sanExpr(attr.expr)), '+', L('"')) + : BINARY(BINARY(L(`${attr.name}="`), '+', attrValue), '+', L('"')) attrList.push([result, false]) attrListMap.push([L(attr.name), L(1)]) } - yield DEF('selfAttrs', new ArrayLiteral(attrList as any)) - yield DEF('attrListMap', new MapLiteral(attrListMap as any)) - yield DEF('filteredParentAttrs', new ArrayLiteral([])) + const selfAttrsNamed = I(this.id.next('selfAttrs')) + const attrListMapNamed = I(this.id.next('attrListMap')) + const filteredParentAttrsNamed = I(this.id.next('filteredParentAttrs')) + yield DEF(selfAttrsNamed.name, new ArrayLiteral(attrList as any)) + yield DEF(attrListMapNamed.name, new MapLiteral(attrListMap as any)) + yield DEF(filteredParentAttrsNamed.name, new ArrayLiteral([])) yield new Foreach(I('key'), I('val'), I('attrs'), [ // 如果props已经存在对应属性,则attr重复的属性需要被删除 new If( BINARY( - I('attrListMap'), + I(attrListMapNamed.name), '[]', // val值示例:a=1,通过split获取到key值 BINARY(new FunctionCall(BINARY(I('val'), '.', I('split')), [L('=')]), '[]', L(0)) @@ -361,11 +369,12 @@ export class ANodeCompiler { STATEMENT(I('continue')) ] ), - STATEMENT(new FunctionCall(BINARY(I('filteredParentAttrs'), '.', I('push')), [I('val')])) + STATEMENT(new FunctionCall(BINARY(I(filteredParentAttrsNamed.name), '.', I('push')), [I('val')])) ]) - yield ASSIGN( - I('attrs'), - new FunctionCall(BINARY(I('selfAttrs'), '.', I('concat')), [I('filteredParentAttrs')]) + + yield DEF( + attrsNamed.name, + new FunctionCall(BINARY(I(selfAttrsNamed.name), '.', I('concat')), [I(filteredParentAttrsNamed.name)]) ) } @@ -413,7 +422,7 @@ export class ANodeCompiler { // 传入attrs数据到下一个组件 if (isRootElement || aNode.attrs) { - mapItems.push([I('attrs'), I('attrs')]) + mapItems.push([I('attrs'), I(inheritAttrs ? attrsNamed.name : 'attrs')]) } if (isRootElement) {