From 3ea06a4b0b66b367513541b9903e8d422e8d87ce Mon Sep 17 00:00:00 2001 From: harttle Date: Fri, 20 Nov 2020 13:35:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20script=20=E5=86=85=E5=AE=B9=E8=B7=B3?= =?UTF-8?q?=E8=BF=87=E8=BD=AC=E4=B9=89=EF=BC=8C=E8=A7=81=20#73?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +++ package-lock.json | 6 ++--- package.json | 2 +- src/target-js/compilers/anode-compiler.ts | 25 +++++++++++-------- .../target-js/compilers/expr-compiler.spec.ts | 10 ++++++++ 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index c3ff3786..69814dd1 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,10 @@ writeFileSync('ssr.php', targetCode) npm i san-ssr san-ssr-target-php ``` +## 已知问题 + +- script 元素内容不可在组件间传递。即 script 内不可引用组件,也不可包含 slot。 + ## 贡献指南 ### 开发起步 diff --git a/package-lock.json b/package-lock.json index 0c9f9dda..e6a937fd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11775,9 +11775,9 @@ "dev": true }, "san-html-cases": { - "version": "3.10.2", - "resolved": "https://registry.npmjs.org/san-html-cases/-/san-html-cases-3.10.2.tgz", - "integrity": "sha512-dXrQ5hzHr/CI4eoqcIwpp7ZqkCEOWXunzFjwrO0LlXrLsNUtFQv51Hkq3xIq69mj8KKkSIl6RGZ6vGzE32D91g==", + "version": "3.10.4", + "resolved": "https://registry.npmjs.org/san-html-cases/-/san-html-cases-3.10.4.tgz", + "integrity": "sha512-36a8LbGxs13xKitx6A65iNqnj7+kccWXdHlyf1bZogB9vT50LnlZKZ89oD8ZYjsYia/XDeqlGV0dOi2esI6VlQ==", "dev": true }, "san-ssr-target-fake-cmd": { diff --git a/package.json b/package.json index f90f2264..56343358 100644 --- a/package.json +++ b/package.json @@ -77,7 +77,7 @@ "jest": "^26.2.2", "mustache": "^4.0.1", "san": "^3.10.0", - "san-html-cases": "^3.10.2", + "san-html-cases": "^3.10.4", "san-ssr-target-fake-cmd": "^1.0.0", "san-ssr-target-fake-esm": "^1.0.0", "semantic-release": "^17.1.1", diff --git a/src/target-js/compilers/anode-compiler.ts b/src/target-js/compilers/anode-compiler.ts index 2cfe70be..71b80d68 100644 --- a/src/target-js/compilers/anode-compiler.ts +++ b/src/target-js/compilers/anode-compiler.ts @@ -1,4 +1,5 @@ import { expr } from './expr-compiler' +import assert from 'assert' import { camelCase } from 'lodash' import { JSEmitter } from '../js-emitter' import { ANode, AIfNode, AForNode, ASlotNode, ATemplateNode, AFragmentNode, ATextNode } from 'san' @@ -16,6 +17,7 @@ import * as TypeGuards from '../../utils/type-guards' export class ANodeCompiler { private ssrIndex = 0 private elementCompiler: ElementCompiler + private inScript = false /** * @param componentInfo 要被编译的节点所在组件的信息 @@ -58,10 +60,11 @@ export class ANodeCompiler { private compileText (aNode: ATextNode) { const { emitter } = this - const shouldEmitComment = TypeGuards.isExprTextNode(aNode.textExpr) && aNode.textExpr.original && !this.ssrOnly + const shouldEmitComment = TypeGuards.isExprTextNode(aNode.textExpr) && aNode.textExpr.original && !this.ssrOnly && !this.inScript + const outputType = this.inScript ? 'rawhtml' : 'html' if (shouldEmitComment) emitter.writeHTMLLiteral('') - emitter.writeHTMLExpression(expr(aNode.textExpr, 'html')) + emitter.writeHTMLExpression(expr(aNode.textExpr, outputType)) if (shouldEmitComment) emitter.writeHTMLLiteral('') } @@ -72,11 +75,11 @@ export class ANodeCompiler { } compileFragment (aNode: AFragmentNode) { - if (TypeGuards.isATextNode(aNode.children[0]) && !this.ssrOnly) { + if (TypeGuards.isATextNode(aNode.children[0]) && !this.ssrOnly && !this.inScript) { this.emitter.writeHTMLLiteral('') } this.elementCompiler.inner(aNode) - if (TypeGuards.isATextNode(aNode.children[aNode.children.length - 1]) && !this.ssrOnly) { + if (TypeGuards.isATextNode(aNode.children[aNode.children.length - 1]) && !this.ssrOnly && !this.inScript) { this.emitter.writeHTMLLiteral('') } } @@ -143,6 +146,7 @@ export class ANodeCompiler { private compileSlot (aNode: ASlotNode) { const { emitter } = this + assert(!this.inScript, ' is not allowed inside