Skip to content

Commit

Permalink
fix: 根元素为组件时,s-data 不正确的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Jul 27, 2020
1 parent c3341e6 commit fd87852
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bin/auto-complete
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ function _test {
_arguments -C \
"-h[Show help information]" \
"--h[Show help information]" \
"1: :($(ls test/cases))"
"1: :($(ls node_modules/san-html-cases/src))"
}
11 changes: 10 additions & 1 deletion src/runtime/underscore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const BASE_PROPS = {
'id': 1
}

interface Context {
owner?: Context
}

function extend (target: object, source: object) {
if (!source) return target
Object.keys(source).forEach(function (key) {
Expand Down Expand Up @@ -109,6 +113,11 @@ function createFromPrototype (proto: object) {
return new (Creator as any)()
}

function getRootCtx (ctx: Context) {
while (ctx.owner) ctx = ctx.owner
return ctx
}

export const _ = {
escapeHTML, defaultStyleFilter, boolAttrFilter, attrFilter, extend, includes, _classFilter, _styleFilter, _xstyleFilter, _xclassFilter, createFromPrototype
escapeHTML, defaultStyleFilter, boolAttrFilter, attrFilter, extend, includes, _classFilter, _styleFilter, _xstyleFilter, _xclassFilter, createFromPrototype, getRootCtx
}
4 changes: 2 additions & 2 deletions src/target-js/compilers/expr-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ function tertiary (e: ExprTertiaryNode) {
}

// 生成数据访问表达式代码
export function dataAccess (accessorExpr?: ExprAccessorNode): string {
let code = 'ctx.data'
export function dataAccess (accessorExpr?: ExprAccessorNode, contextVariableName: string = 'ctx'): string {
let code = `${contextVariableName}.data`
if (!accessorExpr) return code
for (const path of accessorExpr.paths) {
code += TypeGuards.isExprStringNode(path) && isValidIdentifier(path.value)
Expand Down
1 change: 1 addition & 0 deletions src/target-js/compilers/renderer-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class RendererCompiler {
emitter.writeLine('var html = "";')

this.genComponentContextCode(info)
// TODO remove
emitter.writeLine(`var currentCtx = ctx;`)

// instance preraration
Expand Down
3 changes: 2 additions & 1 deletion src/target-js/js-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export class JSEmitter extends Emitter {
}

public writeDataComment () {
this.writeHTMLExpression(`"<!--s-data:" + JSON.stringify(${dataAccess()}) + "-->"`)
this.writeLine('var rootCtx = _.getRootCtx(ctx);')
this.writeHTMLExpression(`"<!--s-data:" + JSON.stringify(${dataAccess(undefined, 'rootCtx')}) + "-->"`)
}

public writeHTMLLiteral (str: string) {
Expand Down

0 comments on commit fd87852

Please sign in to comment.