@lark.js/mvc 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14721,7 +14721,7 @@ function convertArtSyntax(source, debug) {
14721
14721
  }
14722
14722
  if (blockStack.length > 0) {
14723
14723
  const unclosed = blockStack.map((b) => `"${b.ctrl}" at line ${b.line}`).join(", ");
14724
- throw new Error(`[@lark/mvc error] unclosed block(s): ${unclosed}`);
14724
+ throw new Error(`[@lark.js/mvc error] unclosed block(s): ${unclosed}`);
14725
14725
  }
14726
14726
  return result.join("");
14727
14727
  }
@@ -14798,7 +14798,7 @@ function convertArtExpression(code, debug, lineNo, blockStack = []) {
14798
14798
  return `${debugPrefix}<%for(${forExpr}){%>`;
14799
14799
  }
14800
14800
  const tokens = code.split(/\s+/);
14801
- const keyword = tokens.shift();
14801
+ const keyword = tokens.shift() ?? "";
14802
14802
  switch (keyword) {
14803
14803
  case "if": {
14804
14804
  blockStack.push({ ctrl: "if", line: lineNo });
@@ -14815,12 +14815,12 @@ function convertArtExpression(code, debug, lineNo, blockStack = []) {
14815
14815
  }
14816
14816
  return `${debugPrefix}<%}else{%>`;
14817
14817
  }
14818
- case "each": {
14819
- blockStack.push({ ctrl: "each", line: lineNo });
14818
+ case "forOf": {
14819
+ blockStack.push({ ctrl: "forOf", line: lineNo });
14820
14820
  const object = tokens[0];
14821
14821
  if (tokens.length > 1 && tokens[1] !== "as") {
14822
14822
  throw new Error(
14823
- `[@lark/mvc error] bad each syntax: {{${code}}}. Expected "as" keyword, got "${tokens[1]}". Usage: {{each list as item [index]}}`
14823
+ `[@lark.js/mvc error] bad forOf syntax: {{${code}}}. Expected "as" keyword, got "${tokens[1]}". Usage: {{forOf list as item [index]}}`
14824
14824
  );
14825
14825
  }
14826
14826
  const restTokens = tokens.slice(2);
@@ -14842,12 +14842,12 @@ function convertArtExpression(code, debug, lineNo, blockStack = []) {
14842
14842
  }
14843
14843
  return `${debugPrefix}<%for(let ${index}=0${refExpr},${refObjCount}=${refObj}.length${lastCount};${index}<${refObjCount};${index}++){${firstAndLast}${valueDecl}%>`;
14844
14844
  }
14845
- case "parse": {
14846
- blockStack.push({ ctrl: "parse", line: lineNo });
14845
+ case "forIn": {
14846
+ blockStack.push({ ctrl: "forIn", line: lineNo });
14847
14847
  const object = tokens[0];
14848
14848
  if (tokens.length > 1 && tokens[1] !== "as") {
14849
14849
  throw new Error(
14850
- `[@lark/mvc error] bad parse syntax: {{${code}}}. Expected "as" keyword, got "${tokens[1]}". Usage: {{for-in obj as val [key]}}`
14850
+ `[@lark.js/mvc error] bad forIn syntax: {{${code}}}. Expected "as" keyword, got "${tokens[1]}". Usage: {{for-in obj as val [key]}}`
14851
14851
  );
14852
14852
  }
14853
14853
  const restTokens2 = tokens.slice(2);
@@ -14867,19 +14867,19 @@ function convertArtExpression(code, debug, lineNo, blockStack = []) {
14867
14867
  case "set":
14868
14868
  return `${debugPrefix}<%let ${tokens.join(" ")};%>`;
14869
14869
  case "/if":
14870
- case "/each":
14871
- case "/parse":
14870
+ case "/forOf":
14871
+ case "/forIn":
14872
14872
  case "/for": {
14873
14873
  const expectedCtrl = keyword.substring(1);
14874
14874
  const last = blockStack.pop();
14875
14875
  if (!last) {
14876
14876
  throw new Error(
14877
- `[@lark/mvc error] unexpected {{${code}}}: no matching open block`
14877
+ `[@lark.js/mvc error] unexpected {{${code}}}: no matching open block`
14878
14878
  );
14879
14879
  }
14880
14880
  if (last.ctrl !== expectedCtrl) {
14881
14881
  throw new Error(
14882
- `[@lark/mvc error] unexpected {{${code}}}: expected {{/${last.ctrl}}} to close block opened at line ${last.line}`
14882
+ `[@lark.js/mvc error] unexpected {{${code}}}: expected {{/${last.ctrl}}} to close block opened at line ${last.line}`
14883
14883
  );
14884
14884
  }
14885
14885
  return `${debugPrefix}<%}%>`;
@@ -14943,7 +14943,7 @@ function parseAsExpr(expr) {
14943
14943
  function compileToFunction(source, debug, file) {
14944
14944
  const matcher = /<%([@=!:])?([\s\S]*?)%>|$/g;
14945
14945
  let index = 0;
14946
- let funcSource = `$p+='`;
14946
+ let funcSource = `$out+='`;
14947
14947
  let hasAtRule = false;
14948
14948
  const escapeSlashRegExp = /\\|'/g;
14949
14949
  const escapeBreakReturnRegExp = /\r|\n/g;
@@ -14969,17 +14969,17 @@ function compileToFunction(source, debug, file) {
14969
14969
  }
14970
14970
  if (operate === "@") {
14971
14971
  hasAtRule = true;
14972
- funcSource += `'+($expr='<%${operate + expr}%>',$i($$ref,${content}))+'`;
14972
+ funcSource += `'+($dbgExpr='<%${operate + expr}%>',$refFn($refAlt,${content}))+'`;
14973
14973
  } else if (operate === "=" || operate === ":") {
14974
- funcSource += `'+($expr='<%${operate + expr}%>',$e(${content}))+'`;
14974
+ funcSource += `'+($dbgExpr='<%${operate + expr}%>',$encHtml(${content}))+'`;
14975
14975
  } else if (operate === "!") {
14976
- if (!content.startsWith("$eu(") || !content.endsWith(")")) {
14977
- content = `$n(${content})`;
14976
+ if (!content.startsWith("$encUri(") || !content.endsWith(")")) {
14977
+ content = `$strSafe(${content})`;
14978
14978
  }
14979
- funcSource += `'+($expr='<%${operate + expr}%>',${content})+'`;
14979
+ funcSource += `'+($dbgExpr='<%${operate + expr}%>',${content})+'`;
14980
14980
  } else if (content) {
14981
14981
  if (line > -1) {
14982
- funcSource += `';$line=${line};$art='${art}';`;
14982
+ funcSource += `';$dbgLine=${line};$dbgArt='${art}';`;
14983
14983
  content = "";
14984
14984
  } else {
14985
14985
  funcSource += `';`;
@@ -14988,19 +14988,19 @@ function compileToFunction(source, debug, file) {
14988
14988
  funcSource = funcSource.substring(0, funcSource.length - 4) + ";";
14989
14989
  }
14990
14990
  if (expr) {
14991
- funcSource += `$expr='<%${expr}%>';`;
14991
+ funcSource += `$dbgExpr='<%${expr}%>';`;
14992
14992
  }
14993
- funcSource += content + `;$p+='`;
14993
+ funcSource += content + `;$out+='`;
14994
14994
  }
14995
14995
  } else {
14996
14996
  if (operate === "@") {
14997
14997
  hasAtRule = true;
14998
- funcSource += `'+$i($$ref,${content})+'`;
14998
+ funcSource += `'+$refFn($refAlt,${content})+'`;
14999
14999
  } else if (operate === "=" || operate === ":") {
15000
- funcSource += `'+$e(${content})+'`;
15000
+ funcSource += `'+$encHtml(${content})+'`;
15001
15001
  } else if (operate === "!") {
15002
- if (!content.startsWith("$eu(") || !content.endsWith(")")) {
15003
- content = `$n(${content})`;
15002
+ if (!content.startsWith("$encUri(") || !content.endsWith(")")) {
15003
+ content = `$strSafe(${content})`;
15004
15004
  }
15005
15005
  funcSource += `'+${content}+'`;
15006
15006
  } else if (content) {
@@ -15008,28 +15008,24 @@ function compileToFunction(source, debug, file) {
15008
15008
  if (funcSource.endsWith(`+'';`)) {
15009
15009
  funcSource = funcSource.substring(0, funcSource.length - 4) + ";";
15010
15010
  }
15011
- funcSource += `${content};$p+='`;
15011
+ funcSource += `${content};$out+='`;
15012
15012
  }
15013
15013
  }
15014
15014
  return match;
15015
15015
  });
15016
15016
  funcSource += `';`;
15017
- funcSource = funcSource.replace(/\$p\+='';/g, "");
15018
- funcSource = funcSource.replace(/\$p\+=''\+/g, "$p+=");
15017
+ funcSource = funcSource.replace(/\$out\+='';/g, "");
15018
+ funcSource = funcSource.replace(/\$out\+=''\+/g, "$out+=");
15019
15019
  if (debug) {
15020
15020
  const filePart = file ? `\\r\\n\\tat file:${file}` : "";
15021
- funcSource = `let $expr,$art,$line;try{${funcSource}}catch(ex){let msg='render view error:'+(ex.message||ex);if($art)msg+='\\r\\n\\tsrc art:{{'+$art+'}}\\r\\n\\tat line:'+$line;msg+='\\r\\n\\t'+($art?'translate to:':'expr:');msg+=$expr+'${filePart}';throw msg;}`;
15021
+ funcSource = `let $dbgExpr,$dbgArt,$dbgLine;try{${funcSource}}catch(ex){let msg='render view error:'+(ex.message||ex);if($dbgArt)msg+='\\r\\n\\tsrc art:{{'+$dbgArt+'}}\\r\\n\\tat line:'+$dbgLine;msg+='\\r\\n\\t'+($dbgArt?'translate to:':'expr:');msg+=$dbgExpr+'${filePart}';throw msg;}`;
15022
15022
  }
15023
15023
  const viewIdRegExp = new RegExp(String.fromCharCode(31), "g");
15024
15024
  funcSource = funcSource.replace(viewIdRegExp, `'+$viewId+'`);
15025
- const atRule = hasAtRule ? `if(!$i){$i=(ref,v,k,f)=>{for(f=ref[$g];--f;)if(ref[k=$g+f]===v)return k;ref[k=$g+ref[$g]++]=v;return k;}}` : "";
15026
- const encode = `if(!$n){let $em={'&':'amp','<':'lt','>':'gt','"':'#34','\\'':'#39','\`':'#96'},$er=/[&<>"'\`]/g,$ef=m=>'&'+$em[m]+';';$n=v=>''+(v==null?'':v);$e=v=>$n(v).replace($er,$ef)}`;
15027
- const encodeURIMore = `if(!$eu){let $um={'!':'%21','\\'':'%27','(':'%28',')':'%29','*':'%2A'},$uf=m=>$um[m],$uq=/[!')(*]/g;$eu=v=>encodeURIComponent($n(v)).replace($uq,$uf)}`;
15028
- const encodeQuote = `if(!$eq){let $qr=/['"\\\\]/g;$eq=v=>$n(v).replace($qr,'\\\\$&')}`;
15029
- const refFallback = "if(!$$ref)$$ref=$$;";
15030
- const fns = `${refFallback}${encode}${encodeURIMore}${encodeQuote}${atRule};`;
15031
- const fullSource = `${fns}let $g='\\x1e',$_temp,$p=''{{VARS}};${funcSource}return $p`;
15032
- return `($$,$viewId,$$ref,$e,$n,$eu,$i,$eq)=>{${fullSource}}`;
15025
+ void hasAtRule;
15026
+ const refFallback = "if(!$refAlt)$refAlt=$data;";
15027
+ const fullSource = `${refFallback}let $splitter='\\x1e',$tmp,$out=''{{VARS}};${funcSource}return $out`;
15028
+ return `($data,$viewId,$refAlt,$encHtml,$strSafe,$encUri,$refFn,$encQuote)=>{${fullSource}}`;
15033
15029
  }
15034
15030
  function compileTemplate(source, options = {}) {
15035
15031
  const { debug = false, globalVars = [], file } = options;
@@ -15038,17 +15034,14 @@ function compileTemplate(source, options = {}) {
15038
15034
  const viewEventProcessed = processViewEvents(converted);
15039
15035
  const finalSource = restoreComments(viewEventProcessed, comments);
15040
15036
  const funcBody = compileToFunction(finalSource, debug, file);
15041
- const varDeclarations = globalVars.map((key) => `,${key}=$$.${key}`).join("");
15037
+ const varDeclarations = globalVars.map((key) => `,${key}=$data.${key}`).join("");
15042
15038
  const funcWithVars = funcBody.replace("{{VARS}}", () => varDeclarations);
15043
- return `export default function(data, selfId, refData) {
15044
- let $$ = data || {},
15045
- $viewId = selfId || '';
15046
- return (${funcWithVars})($$, $viewId, refData,
15047
- /* $e */ v => String(v == null ? '' : v).replace(/[&<>"'\`]/g, m => '&' + ({'&':'amp','<':'lt','>':'gt','"':'#34',"'":'#39','\`':'#96'})[m] + ';'),
15048
- /* $n */ v => String(v == null ? '' : v),
15049
- /* $eu */ null,
15050
- /* $i */ null,
15051
- /* $eq */ null
15039
+ return `import { encHtml as __larkEncHtml, strSafe as __larkStrSafe, encUri as __larkEncUri, encQuote as __larkEncQuote, refFn as __larkRefFn } from "@lark.js/mvc/runtime";
15040
+ export default function(data, viewId, refData) {
15041
+ let $data = data || {},
15042
+ $viewId = viewId || '';
15043
+ return (${funcWithVars})($data, $viewId, refData,
15044
+ __larkEncHtml, __larkStrSafe, __larkEncUri, __larkRefFn, __larkEncQuote
15052
15045
  );
15053
15046
  }`;
15054
15047
  }
@@ -15157,7 +15150,7 @@ function fallbackExtractVariables(source) {
15157
15150
  while ((m = outputRegExp.exec(source)) !== null) {
15158
15151
  vars.add(m[1]);
15159
15152
  }
15160
- const eachRegExp = /\{\{each\s+([a-zA-Z_$][\w$]*)\s+as/g;
15153
+ const eachRegExp = /\{\{forOf\s+([a-zA-Z_$][\w$]*)\s+as/g;
15161
15154
  while ((m = eachRegExp.exec(source)) !== null) {
15162
15155
  vars.add(m[1]);
15163
15156
  }
@@ -15173,82 +15166,100 @@ function walkAst(ast, visitors) {
15173
15166
  if (visitors[type]) {
15174
15167
  visitors[type](node);
15175
15168
  }
15169
+ const bag = node;
15176
15170
  for (const key of Object.keys(node)) {
15177
15171
  if (key === "type" || key === "start" || key === "end" || key === "loc" || key === "range")
15178
15172
  continue;
15179
- if (type === "MemberExpression" && key === "property" && !node.computed)
15180
- continue;
15181
- if (type === "ObjectProperty" && key === "key" && !node.computed) {
15182
- continue;
15173
+ if (type === "MemberExpression" && key === "property") {
15174
+ const me = node;
15175
+ if (!me.computed) continue;
15183
15176
  }
15184
- if (type === "ObjectMethod" && key === "key" && !node.computed) {
15185
- continue;
15177
+ if (type === "ObjectProperty" && key === "key") {
15178
+ const op = node;
15179
+ if (!op.computed) continue;
15186
15180
  }
15187
- const child = node[key];
15181
+ if (type === "ObjectMethod" && key === "key") {
15182
+ const om = node;
15183
+ if (!om.computed) continue;
15184
+ }
15185
+ const child = bag[key];
15188
15186
  if (Array.isArray(child)) {
15189
15187
  for (const item of child) {
15190
- if (item && typeof item === "object" && typeof item.type === "string") {
15191
- visit(item);
15192
- }
15188
+ if (isAstNode(item)) visit(item);
15193
15189
  }
15194
- } else if (child && typeof child === "object" && typeof child.type === "string") {
15190
+ } else if (isAstNode(child)) {
15195
15191
  visit(child);
15196
15192
  }
15197
15193
  }
15198
15194
  }
15199
15195
  visit(ast);
15200
15196
  }
15197
+ function isAstNode(v) {
15198
+ return !!v && typeof v === "object" && typeof v.type === "string";
15199
+ }
15201
15200
  var BUILTIN_GLOBALS = {
15202
15201
  // ─── Template runtime helpers (injected by compileToFunction) ───────
15203
15202
  //
15204
15203
  // These variables appear in the generated template function signature
15205
15204
  // or body. They must be excluded from extractGlobalVars() so that
15206
- // they are not mistaken for user data variables and destructured from $$.
15205
+ // they are not mistaken for user data variables and destructured from $data.
15207
15206
  // SPLITTER character constant (same as \x1e), used as namespace separator
15208
15207
  // for refData keys, event attribute encoding, and internal data structures.
15209
- // Declared as: let $g='\x1e'
15210
- $g: 1,
15211
- // refData — the data object passed from Updater to the template function.
15212
- // User variables are destructured from $$ at the top of the function:
15213
- // let {name, age} = $$;
15208
+ // Declared as: let $splitter='\x1e'
15209
+ $splitter: 1,
15210
+ // Data — the data object passed from Updater to the template function.
15211
+ // User variables are destructured from $data at the top of the function:
15212
+ // let {name, age} = $data;
15214
15213
  // This is the first parameter of the generated arrow function.
15215
- $$: 1,
15214
+ $data: 1,
15216
15215
  // Null-safe toString: v => '' + (v == null ? '' : v)
15217
15216
  // Converts null/undefined to empty string, otherwise calls toString().
15218
15217
  // Wraps every {{!raw}} output to prevent "null" / "undefined" rendering.
15219
- $n: 1,
15220
- // HTML entity encoder: v => $n(v).replace(/[&<>"'`]/g, entityMap)
15218
+ $strSafe: 1,
15219
+ // HTML entity encoder: v => $strSafe(v).replace(/[&<>"'`]/g, entityMap)
15221
15220
  // Encodes &, <, >, ", ', ` to HTML entities (&amp; &lt; etc.)
15222
15221
  // Applied to all {{=escaped}} and {{:binding}} outputs.
15223
- $e: 1,
15224
- // HTML entity map — internal object used by $e:
15222
+ $encHtml: 1,
15223
+ // HTML entity map — internal object used by $encHtml:
15225
15224
  // {'&':'amp','<':'gt','>':'gt','"':'#34','\'':'#39','`':'#96'}
15226
- // Not a standalone function; referenced inside $e's closure.
15227
- $em: 1,
15228
- // HTML entity RegExp — internal regexp used by $e:
15225
+ // Not a standalone function; referenced inside $encHtml's closure.
15226
+ $entMap: 1,
15227
+ // HTML entity RegExp — internal regexp used by $encHtml:
15229
15228
  // /[&<>"'`]/g
15230
- $er: 1,
15231
- // HTML entity replacer function — internal helper used by $e:
15232
- // m => '&' + $em[m] + ';'
15233
- // Maps each matched character to its entity string.
15234
- $ef: 1,
15229
+ $entReg: 1,
15230
+ // HTML entity replacer function — internal helper used by $encHtml:
15231
+ // m => '&' + $entMap[m] + ';'
15232
+ // Maps matched character to its entity string.
15233
+ $entFn: 1,
15235
15234
  // Output buffer — the string accumulator for rendered HTML.
15236
- // All template output is appended via $p += '...'.
15237
- // Declared as: let $p = ''
15238
- $p: 1,
15235
+ // All template output is appended via $out += '...'.
15236
+ // Declared as: let $out = ''
15237
+ $out: 1,
15239
15238
  // Reference lookup: (refData, value) => key
15240
15239
  // Finds or allocates a SPLITTER-prefixed key in refData for a given
15241
15240
  // object reference. Used by {{@ref}} operator for passing object
15242
15241
  // references to child views via v-lark attributes.
15243
- $i: 1,
15244
- // URI encoder: v => encodeURIComponent($n(v)).replace(/[!')(*]/g, extraMap)
15242
+ $refFn: 1,
15243
+ // URI encoder: v => encodeURIComponent($strSafe(v)).replace(/[!')(*]/g, extraMap)
15245
15244
  // Extends encodeURIComponent with encoding of ! ' ( ) *.
15246
15245
  // Applied to values in @event URL parameters and {{!uri}} contexts.
15247
- $eu: 1,
15248
- // Quote encoder: v => $n(v).replace(/['"\\]/g, '\\$&')
15246
+ $encUri: 1,
15247
+ // URI encode map internal object used by $encUri:
15248
+ // {'!':'%21','\'':'%27','(':'%28',')':'%29','*':'%2A'}
15249
+ $uriMap: 1,
15250
+ // URI encode replacer — internal helper used by $encUri:
15251
+ // m => $uriMap[m]
15252
+ $uriFn: 1,
15253
+ // URI encode regexp — internal regexp used by $encUri:
15254
+ // /[!')(*]/g
15255
+ $uriReg: 1,
15256
+ // Quote encoder: v => $strSafe(v).replace(/['"\\]/g, '\\$&')
15249
15257
  // Escapes quotes and backslashes for safe embedding in HTML attribute
15250
15258
  // values (e.g. data-json='...').
15251
- $eq: 1,
15259
+ $encQuote: 1,
15260
+ // Quote encode regexp — internal regexp used by $encQuote:
15261
+ // /['"\\]/g
15262
+ $qReg: 1,
15252
15263
  // View ID — the unique identifier of the owning View instance.
15253
15264
  // Injected into @event attribute values at render time so that
15254
15265
  // EventDelegator can dispatch events to the correct View handler.
@@ -15256,23 +15267,23 @@ var BUILTIN_GLOBALS = {
15256
15267
  $viewId: 1,
15257
15268
  // Debug: current expression text — stores the template expression being
15258
15269
  // evaluated, for error reporting. Only present in debug mode.
15259
- // e.g. $expr='<%=user.name%>'
15260
- $expr: 1,
15270
+ // e.g. $dbgExpr='<%=user.name%>'
15271
+ $dbgExpr: 1,
15261
15272
  // Debug: original art syntax — stores the {{}} template syntax before
15262
15273
  // conversion, for error reporting. Only present in debug mode.
15263
- // e.g. $art='{{=user.name}}'
15264
- $art: 1,
15274
+ // e.g. $dbgArt='{{=user.name}}'
15275
+ $dbgArt: 1,
15265
15276
  // Debug: source line number — tracks the current line in the template
15266
15277
  // source, for error reporting. Only present in debug mode.
15267
- $line: 1,
15268
- // refData alias — fallback reference lookup table.
15269
- // Defaults to $$ when no explicit $$ref is provided.
15270
- // Ensures $i() does not crash when @ operator is used without refData.
15271
- $$ref: 1,
15278
+ $dbgLine: 1,
15279
+ // RefData alias — fallback reference lookup table.
15280
+ // Defaults to $data when no explicit $refAlt is provided.
15281
+ // Ensures $refFn() does not crash when @ operator is used without refData.
15282
+ $refAlt: 1,
15272
15283
  // Temporary variable — used by the compiler for intermediate
15273
15284
  // expression results in generated code (e.g. loop variables,
15274
- // conditional branches). Declared as: let $_temp
15275
- $_temp: 1,
15285
+ // conditional branches). Declared as: let $tmp
15286
+ $tmp: 1,
15276
15287
  // JS literals
15277
15288
  undefined: 1,
15278
15289
  null: 1,