solely 0.1.7 → 0.1.9

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.
Files changed (76) hide show
  1. package/README.md +160 -1
  2. package/dist/compiler/index.cjs +1 -0
  3. package/dist/compiler/index.js +1 -0
  4. package/dist/compiler/ir/buildIR.cjs +1 -0
  5. package/dist/compiler/ir/buildIR.js +1 -0
  6. package/dist/compiler/parser/parser.cjs +1 -0
  7. package/dist/compiler/parser/parser.js +1 -0
  8. package/dist/index.cjs +1 -0
  9. package/dist/index.d.ts +1 -0
  10. package/dist/index.js +1 -1885
  11. package/dist/plugin.cjs +1 -0
  12. package/dist/plugin.d.ts +39 -1
  13. package/dist/plugin.js +1 -0
  14. package/dist/plugins/index.d.ts +7 -1
  15. package/dist/plugins/solely-vite-plugin.cjs +1 -0
  16. package/dist/plugins/solely-vite-plugin.d.ts +28 -0
  17. package/dist/plugins/solely-vite-plugin.js +1 -0
  18. package/dist/router/core.cjs +1 -0
  19. package/dist/{runtime/router → router}/core.d.ts +1 -0
  20. package/dist/router/core.js +1 -0
  21. package/dist/router/index.cjs +1 -0
  22. package/dist/router/index.js +1 -0
  23. package/dist/router/router-link.cjs +1 -0
  24. package/dist/{runtime/router → router}/router-link.d.ts +1 -1
  25. package/dist/router/router-link.js +1 -0
  26. package/dist/router/router-view.cjs +1 -0
  27. package/dist/{runtime/router → router}/router-view.d.ts +1 -1
  28. package/dist/router/router-view.js +1 -0
  29. package/dist/router/routes copy.d.ts +6 -0
  30. package/dist/runtime/component/base-element.cjs +1 -0
  31. package/dist/runtime/component/base-element.js +1 -0
  32. package/dist/runtime/component/decorators.cjs +1 -0
  33. package/dist/runtime/component/decorators.d.ts +7 -1
  34. package/dist/runtime/component/decorators.js +1 -0
  35. package/dist/runtime/index.cjs +1 -0
  36. package/dist/runtime/index.d.ts +0 -1
  37. package/dist/runtime/index.js +1 -0
  38. package/dist/runtime/reactivity/observe.cjs +1 -0
  39. package/dist/runtime/reactivity/observe.js +1 -0
  40. package/dist/runtime/renderer/renderIR.cjs +1 -0
  41. package/dist/runtime/renderer/renderIR.js +1 -0
  42. package/dist/shared/env.cjs +1 -0
  43. package/dist/shared/env.d.ts +4 -0
  44. package/dist/shared/env.js +1 -0
  45. package/dist/shared/event-listeners.cjs +1 -0
  46. package/dist/shared/event-listeners.js +1 -0
  47. package/dist/shared/functions.cjs +1 -0
  48. package/dist/shared/functions.d.ts +2 -1
  49. package/dist/shared/functions.js +1 -0
  50. package/dist/shared/gen-function copy.d.ts +6 -0
  51. package/dist/shared/gen-function.cjs +1 -0
  52. package/dist/shared/gen-function.d.ts +3 -7
  53. package/dist/shared/gen-function.js +1 -0
  54. package/dist/shared/index.cjs +1 -0
  55. package/dist/shared/index.d.ts +1 -0
  56. package/dist/shared/index.js +1 -0
  57. package/dist/shared/is-object.cjs +1 -0
  58. package/dist/shared/is-object.js +1 -0
  59. package/dist/shared/templateError.cjs +1 -0
  60. package/dist/shared/templateError.js +1 -0
  61. package/dist/shared/url.cjs +1 -0
  62. package/dist/shared/url.js +1 -0
  63. package/dist/types/ast.cjs +1 -0
  64. package/dist/types/ast.d.ts +6 -0
  65. package/dist/types/ast.js +1 -0
  66. package/dist/types/index.cjs +1 -0
  67. package/dist/types/index.js +1 -0
  68. package/dist/types/ir.d.ts +81 -38
  69. package/dist/types/lifecycle.cjs +1 -0
  70. package/dist/types/lifecycle.js +1 -0
  71. package/package.json +219 -58
  72. package/dist/index.umd.cjs +0 -46
  73. package/dist/plugins/html-parse-precompile.d.ts +0 -1
  74. package/dist/plugins/html-precompile.d.ts +0 -1
  75. /package/dist/{runtime/router → router}/index.d.ts +0 -0
  76. /package/dist/{runtime/router → router}/types.d.ts +0 -0
package/README.md CHANGED
@@ -19,6 +19,7 @@
19
19
  - **🎨 强大模板**:基于 AST 的模板编译,支持条件渲染和列表渲染
20
20
  - **⚡ 响应式系统**:高效的响应式数据绑定,自动追踪依赖
21
21
  - **🧩 Web 组件**:原生 Web 组件支持,可自定义元素
22
+ - **🛡️ 安全可靠**:编译时模板固定,自动 HTML 转义,内置 XSS 防护机制
22
23
  - **📦 插件系统**:灵活的插件扩展机制
23
24
  - **🧪 测试完善**:完整的单元测试覆盖
24
25
  - **🗺️ 路由支持**:内置路由系统,支持单页应用开发
@@ -28,6 +29,9 @@
28
29
  - [快速开始指南](./docs/guide.md) - 从零开始学习 Solely
29
30
  - [API 文档](./docs/api.md) - 完整的 API 参考
30
31
  - [示例项目](./examples/) - 实际应用示例
32
+ - [插件系统](#插件系统) - 了解 Vite 插件的使用方法
33
+ - [安全机制说明](#安全机制) - 了解框架的 XSS 防护机制
34
+ - [安全演示](./examples/security-demo/) - 查看 XSS 防护实际效果
31
35
 
32
36
  ## 📦 安装
33
37
 
@@ -324,7 +328,161 @@ solely/
324
328
  4. **路由系统**:支持单页应用的路由管理,包括路由定义、导航和组件挂载
325
329
  5. **插件系统**:提供扩展机制,支持自定义功能
326
330
 
327
- ## 🔧 API 参考
331
+ ## 🔌 插件系统
332
+
333
+ Solely 提供了强大的插件系统,支持构建时扩展。目前内置了 Vite 插件,用于模板预编译和构建优化。
334
+
335
+ ### Vite 插件
336
+
337
+ Solely 提供了官方的 Vite 插件 `solelyVitePlugin`,用于在构建时将 HTML 模板预编译为高效的 IR(中间表示),提升运行时性能。
338
+
339
+ #### 安装
340
+
341
+ ```bash
342
+ npm install solely
343
+ ```
344
+
345
+ #### 基本配置
346
+
347
+ ```typescript
348
+ // vite.config.ts
349
+ import { defineConfig } from 'vite';
350
+ import { solelyVitePlugin } from 'solely/plugin';
351
+
352
+ export default defineConfig({
353
+ plugins: [
354
+ solelyVitePlugin({
355
+ precompile: true, // 启用预编译(默认 true)
356
+ sourceMap: true, // 生成源码映射(默认 true)
357
+ minify: false, // 压缩生成的代码(默认 false)
358
+ debug: false, // 调试模式(默认 false)
359
+ })
360
+ ]
361
+ });
362
+ ```
363
+
364
+ #### 配置选项
365
+
366
+ | 选项 | 类型 | 默认值 | 说明 |
367
+ |------|------|--------|------|
368
+ | `precompile` | `boolean` | `true` | 是否启用模板预编译 |
369
+ | `sourceMap` | `boolean` | `true` | 是否生成源码映射 |
370
+ | `minify` | `boolean` | `false` | 是否压缩生成的代码 |
371
+ | `include` | `string \| RegExp \| Array<string \| RegExp>` | `/\.html(?:\?\|$)/` | 包含的文件匹配模式 |
372
+ | `exclude` | `string \| RegExp \| Array<string \| RegExp>` | `/node_modules/` | 排除的文件匹配模式 |
373
+ | `debug` | `boolean` | `false` | 调试模式,输出详细日志 |
374
+
375
+ #### 在组件中使用
376
+
377
+ 使用 `?solely` 查询参数导入 HTML 模板,插件会自动将其预编译为 IR:
378
+
379
+ ```typescript
380
+ // my-component.ts
381
+ import { BaseElement, CustomElement } from 'solely';
382
+ import template from './template.html?solely'; // 使用 ?solely 查询参数
383
+
384
+ @CustomElement({
385
+ tagName: 'my-component',
386
+ template, // 直接使用预编译的 IR
387
+ })
388
+ class MyComponent extends BaseElement {
389
+ // ...
390
+ }
391
+ ```
392
+
393
+ #### 预编译 vs 运行时编译
394
+
395
+ | 特性 | 预编译模式 (`?solely`) | 运行时编译模式 (`?raw`) |
396
+ |------|----------------------|----------------------|
397
+ | 构建时 | 模板编译为 IR | 保持原始 HTML 字符串 |
398
+ | 运行时 | 直接执行 IR,无解析开销 | 需要解析 HTML 并构建 IR |
399
+ | 性能 | 更高(推荐生产环境) | 较低(适合开发环境) |
400
+ | 包体积 | 较小(IR 比 HTML 更紧凑) | 较大(包含原始 HTML) |
401
+
402
+ **开发环境**(使用运行时编译):
403
+ ```typescript
404
+ import template from './template.html?raw'; // 运行时编译
405
+ ```
406
+
407
+ **生产环境**(使用预编译):
408
+ ```typescript
409
+ import template from './template.html?solely'; // 预编译
410
+ ```
411
+
412
+ #### 热更新支持
413
+
414
+ 插件支持 Vite 的热模块替换(HMR),当模板文件修改时会自动触发页面刷新:
415
+
416
+ ```typescript
417
+ // vite.config.ts
418
+ export default defineConfig({
419
+ plugins: [
420
+ solelyVitePlugin({
421
+ debug: true // 启用调试模式查看 HMR 日志
422
+ })
423
+ ]
424
+ });
425
+ ```
426
+
427
+ #### 错误处理
428
+
429
+ 插件具有健壮的错误处理机制:
430
+
431
+ - 编译失败时会自动回退到原始模板
432
+ - 在控制台输出详细的错误信息
433
+ - 不会中断构建流程
434
+
435
+ ```typescript
436
+ // vite.config.ts
437
+ export default defineConfig({
438
+ plugins: [
439
+ solelyVitePlugin({
440
+ debug: true // 查看详细的编译日志和错误信息
441
+ })
442
+ ]
443
+ });
444
+ ```
445
+
446
+ ## 🛡️ 安全机制
447
+
448
+ Solely 框架内置了多层安全防护机制,有效防止常见的 XSS(跨站脚本攻击):
449
+
450
+ ### 编译时安全
451
+
452
+ - **模板编译时固定**:所有模板在构建阶段编译为中间表示(IR),运行时无法更改模板结构
453
+ - **无运行时解析**:不存在 `eval` 或动态模板解析,杜绝代码注入
454
+
455
+ ### 自动 HTML 转义
456
+
457
+ - **文本插值自动转义**:使用 `{{ }}` 插值时,框架自动进行 HTML 实体编码
458
+
459
+ ```html
460
+ <!-- 用户输入: <script>alert('XSS')</script> -->
461
+ <div>{{ $data.userInput }}</div>
462
+ <!-- 渲染结果: &lt;script&gt;alert('XSS')&lt;/script&gt; -->
463
+ ```
464
+
465
+ ### 安全最佳实践
466
+
467
+ | ✅ 推荐做法 | ❌ 避免做法 |
468
+ |---|---|
469
+ | 使用 `{{ }}` 插值显示用户内容 | 将用户输入绑定到 `:innerHTML` |
470
+ | 对用户输入进行验证和清理 | 直接操作 DOM 插入不可信内容 |
471
+ | 使用 CSP (Content Security Policy) | 信任未经检验的第三方 API 数据 |
472
+ | 富文本使用白名单过滤 | 动态构造模板字符串 |
473
+
474
+ ### 注意事项
475
+
476
+ ⚠️ **如果框架支持 `:innerHTML` 指令**:
477
+
478
+ - 永远不要将用户输入绑定到 `:innerHTML`
479
+ - 永远不要将第三方 API 数据直接绑定到 `:innerHTML`
480
+ - 仅对完全可信的静态内容使用 `:innerHTML`
481
+ - 如需渲染富文本,请使用专门的富文本组件并进行白名单过滤
482
+
483
+ 查看 [安全演示示例](./examples/security-demo/) 了解框架如何防护 XSS 攻击。
484
+
485
+ ## � API 参考
328
486
 
329
487
  ### CustomElement 装饰器
330
488
 
@@ -933,6 +1091,7 @@ beforeEach: (to, from) => {
933
1091
  - **待办事项示例** (`examples/todo/`) - 列表管理和状态示例
934
1092
  - **表单示例** (`examples/form/`) - 表单处理和验证示例
935
1093
  - **天气示例** (`examples/weather/`) - 异步数据获取示例
1094
+ - **安全演示** (`examples/security-demo/`) - XSS 防护机制演示
936
1095
 
937
1096
  ### 运行示例
938
1097
 
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./ir/buildIR.cjs"),r=require("./parser/parser.cjs");exports.buildIR=e.buildIR,exports.clearIRCache=e.clearIRCache,exports.transformModel=e.transformModel,exports.parseHtml=r.parseHtml;
@@ -0,0 +1 @@
1
+ import{buildIR as r,clearIRCache as o,transformModel as p}from"./ir/buildIR.js";import{parseHtml as i}from"./parser/parser.js";export{r as buildIR,o as clearIRCache,i as parseHtml,p as transformModel};
@@ -0,0 +1 @@
1
+ "use strict";var e=Object.defineProperty,n=(n,t,o)=>((n,t,o)=>t in n?e(n,t,{enumerable:!0,configurable:!0,writable:!0,value:o}):n[t]=o)(n,"symbol"!=typeof t?t+"":t,o);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("../../shared/gen-function.cjs"),o=require("../../shared/env.cjs"),i=require("../../types/ast.cjs"),s=require("../../types/lifecycle.cjs"),c=/\{\{[\s\S]*?\}\}/,l=new class{constructor(){n(this,"compilers",new Map)}get(e="anonymous"){let n=this.compilers.get(e);return n||(n=new r,this.compilers.set(e,n)),n}clear(e){var n;if(e)null==(n=this.compilers.get(e))||n.clear(),this.compilers.delete(e);else{for(const e of this.compilers.values())e.clear();this.compilers.clear()}}};class r{constructor(){n(this,"cache",new Map),n(this,"functions",[])}makeCacheKey(e,n,t){return 0===t.length?`${e}:${n}`:`${e}:${n}:${t.map(e=>Object.keys(e).sort().join(",")).join(";")}`}compile(e,n,i){const s=null==n?void 0:n.trim();if(!s)return 0;const c=this.makeCacheKey(e,s,i),l=this.cache.get(c);if(void 0!==l)return l;let r;try{r=t.genFunction(s,e,i)}catch(u){o.IS_DEV,r=()=>""}const a=this.functions.push(r);return this.cache.set(c,a),a}getFunctions(){return this.functions}getStats(){return{total:this.functions.length,cached:this.cache.size}}clear(){this.cache.clear(),this.functions=[]}}function a(e){const n=e[0];return":"===n?{isDynamic:!0,type:"expression"}:"@"===n?{isDynamic:!0,type:"handler"}:s.isLifecycleKind(e)?{isDynamic:!0,type:"lifecycle"}:{isDynamic:!1,type:null}}function u(e,n,t){o.IS_DEV&&(e.o={expr:n??"",loc:t??null})}function d(e){if(!e.attrs)return;const n=e.attrs.find(e=>"s-model"===e.key);if(!n)return;const t="$data."+n.value.replace("this.","").replace("$data.",""),o=n.loc,i=(e.tag||"").toLowerCase(),s=e.attrs.find(e=>"type"===e.key),c=(null==s?void 0:s.value)??"text";e.attrs=e.attrs.filter(e=>e!==n);const l=(n,t,i)=>{var s;null==(s=e.attrs)||s.push({key:n,value:t,role:i,loc:o})};if("input"===i){if("checkbox"===c){const n=e.attrs.find(e=>"value"===e.key),o=n?JSON.stringify(n.value):'"on"';return l(":checked",`Array.isArray(${t}) ? ${t}.includes(${o}) : ${t}`),void l("@change",`\n const _v = ${o};\n if(Array.isArray(${t})) {\n const _i = ${t}.indexOf(_v);\n if(checked && _i === -1) ${t}.push(_v);\n else if(!checked && _i > -1) ${t}.splice(_i,1);\n } else {\n ${t} = checked;\n }`,"model")}if("radio"===c){const n=e.attrs.find(e=>"value"===e.key),o=(null==n?void 0:n.value)??"null";return l(":checked",`${t} == ${JSON.stringify(o)}`),void l("@change",`if (checked) ${t} = ${JSON.stringify(o)}`,"model")}return l(":value",t),void l("@input",`${t} = value`,"model")}if("textarea"===i)return l(":value",t),void l("@input",`${t} = value`,"model");"select"!==i?(l(":value",t),l("@input",`${t} = $event`,"model")):e.attrs.some(e=>"multiple"===e.key)?(l("updated",`\n const values = Array.isArray(${t}) ? ${t} : ${t} != null ? [${t}] : [];\n for (const opt of el.options) {\n opt.selected = values.includes(opt.value);\n }`),l("@change",`${t} = Array.from(event.target.selectedOptions).map(o => o.value)`,"model")):(l(":value",t),l("@change",`${t} = value`,"model"))}function f(e,n,t,o=null){if(!e.attrs||0===e.attrs.length)return[];const i=[];for(const s of e.attrs){if(o&&o.has(s.key))continue;const{key:c,value:l,role:r}=s,{isDynamic:d,type:f}=a(c);if(d&&f){const o={k:c,f:t.compile(f,l??"",n),d:1,r:r};u(o,l,s.loc??e.loc),i.push(o)}else i.push({k:c,v:l,d:0,r:r})}return i}function v(e,n,t){var s,l,r,v,p,m;const y={t:e.type,d:0};let $=!1;var k;switch(e.type===i.ASTType.Text?(k=e.content)&&c.test(k)&&($=!0):((null==(s=e.attrs)?void 0:s.some(e=>"s-model"===e.key||a(e.key).isDynamic))&&($=!0),e.type===i.ASTType.For&&($=!0)),y.d=$?1:0,e.type){case i.ASTType.Text:{const o=e.content??"";$?(y.f=t.compile("template",o,n),u(y,o,e.loc)):y.x=o;break}case i.ASTType.Element:y.g=e.tag,d(e),y.a=f(e,n,t),e.children&&(y.c=h(e.children,n,t));break;case i.ASTType.For:{const o=null==(l=e.attrs)?void 0:l.find(e=>"each"===e.key),i=(null==o?void 0:o.value)??"";y.f=t.compile("expression",i,n),y.i=(null==(v=null==(r=e.attrs)?void 0:r.find(e=>"item"===e.key))?void 0:v.value)??"item",y.n=(null==(m=null==(p=e.attrs)?void 0:p.find(e=>"index"===e.key))?void 0:m.value)??"index",u(y,i,(null==o?void 0:o.loc)??e.loc);const s=new Set(["each","item","index"]);y.a=f(e,n,t,s);const c=[...n,{i:y.i,x:y.n}];e.children&&(y.c=h(e.children,c,t));break}case i.ASTType.If:case i.ASTType.ElseIf:case i.ASTType.Else:o.IS_DEV,y.a=f(e,n,t),e.children&&(y.c=h(e.children,n,t));break;case i.ASTType.Comment:y.x=e.content??""}return y}function h(e,n,t){var s;const c=[];let l=0;for(;l<e.length;){const r=e[l];if(r.type===i.ASTType.If){const a=[];let u=l;for(;u<e.length;){const c=e[u],l=c.type;if(l!==i.ASTType.If&&l!==i.ASTType.ElseIf&&l!==i.ASTType.Else)break;if(l===i.ASTType.If&&a.length>0)break;let r,d=null,v="";if(l!==i.ASTType.Else){const e=null==(s=c.attrs)?void 0:s.find(e=>"test"===e.key||"condition"===e.key);v=(null==e?void 0:e.value)??"false",d=t.compile("expression",v,n),r=(null==e?void 0:e.loc)??c.loc}const p=c.children?h(c.children,n,t):[],m=f(c,n,t,new Set(["test","condition"]));if(a.push({f:d,c:p,a:m.length>0?m:void 0,...o.IS_DEV&&{o:{expr:v,loc:r}}}),u++,l===i.ASTType.Else)break}c.push({t:i.ASTType.Conditional,d:1,b:a,...o.IS_DEV&&{o:{loc:r.loc}}}),l=u}else c.push(v(r,n,t)),l++}return c}exports.buildIR=function(e,n={}){const{filename:t="anonymous",source:i}=n,s=l.get(t);s.clear(),performance.now();const c=h(e,[],s),r=(performance.now(),s.getStats()),a=c.filter(e=>1===e.d).length,u={t:"root",v:"1.1.0",fns:s.getFunctions(),n:c,s:{tf:r.total,cf:r.cached,dn:a,tn:c.length},m:{t:(new Date).toISOString(),as:e.length,fn:t,...i&&{src:i}}};return o.IS_DEV,u},exports.clearIRCache=function(e){l.clear(e)},exports.transformModel=d;
@@ -0,0 +1 @@
1
+ var e=Object.defineProperty,n=(n,t,o)=>((n,t,o)=>t in n?e(n,t,{enumerable:!0,configurable:!0,writable:!0,value:o}):n[t]=o)(n,"symbol"!=typeof t?t+"":t,o);import{genFunction as t}from"../../shared/gen-function.js";import{IS_DEV as o}from"../../shared/env.js";import{ASTType as i}from"../../types/ast.js";import{isLifecycleKind as s}from"../../types/lifecycle.js";const c=/\{\{[\s\S]*?\}\}/,l=new class{constructor(){n(this,"compilers",new Map)}get(e="anonymous"){let n=this.compilers.get(e);return n||(n=new r,this.compilers.set(e,n)),n}clear(e){var n;if(e)null==(n=this.compilers.get(e))||n.clear(),this.compilers.delete(e);else{for(const e of this.compilers.values())e.clear();this.compilers.clear()}}};class r{constructor(){n(this,"cache",new Map),n(this,"functions",[])}makeCacheKey(e,n,t){return 0===t.length?`${e}:${n}`:`${e}:${n}:${t.map(e=>Object.keys(e).sort().join(",")).join(";")}`}compile(e,n,o){const i=null==n?void 0:n.trim();if(!i)return 0;const s=this.makeCacheKey(e,i,o),c=this.cache.get(s);if(void 0!==c)return c;let l;try{l=t(i,e,o)}catch(a){l=()=>""}const r=this.functions.push(l);return this.cache.set(s,r),r}getFunctions(){return this.functions}getStats(){return{total:this.functions.length,cached:this.cache.size}}clear(){this.cache.clear(),this.functions=[]}}function a(e){const n=e[0];return":"===n?{isDynamic:!0,type:"expression"}:"@"===n?{isDynamic:!0,type:"handler"}:s(e)?{isDynamic:!0,type:"lifecycle"}:{isDynamic:!1,type:null}}function u(e,n,t){o&&(e.o={expr:n??"",loc:t??null})}function d(e){if(!e.attrs)return;const n=e.attrs.find(e=>"s-model"===e.key);if(!n)return;const t="$data."+n.value.replace("this.","").replace("$data.",""),o=n.loc,i=(e.tag||"").toLowerCase(),s=e.attrs.find(e=>"type"===e.key),c=(null==s?void 0:s.value)??"text";e.attrs=e.attrs.filter(e=>e!==n);const l=(n,t,i)=>{var s;null==(s=e.attrs)||s.push({key:n,value:t,role:i,loc:o})};if("input"===i){if("checkbox"===c){const n=e.attrs.find(e=>"value"===e.key),o=n?JSON.stringify(n.value):'"on"';return l(":checked",`Array.isArray(${t}) ? ${t}.includes(${o}) : ${t}`),void l("@change",`\n const _v = ${o};\n if(Array.isArray(${t})) {\n const _i = ${t}.indexOf(_v);\n if(checked && _i === -1) ${t}.push(_v);\n else if(!checked && _i > -1) ${t}.splice(_i,1);\n } else {\n ${t} = checked;\n }`,"model")}if("radio"===c){const n=e.attrs.find(e=>"value"===e.key),o=(null==n?void 0:n.value)??"null";return l(":checked",`${t} == ${JSON.stringify(o)}`),void l("@change",`if (checked) ${t} = ${JSON.stringify(o)}`,"model")}return l(":value",t),void l("@input",`${t} = value`,"model")}if("textarea"===i)return l(":value",t),void l("@input",`${t} = value`,"model");"select"!==i?(l(":value",t),l("@input",`${t} = $event`,"model")):e.attrs.some(e=>"multiple"===e.key)?(l("updated",`\n const values = Array.isArray(${t}) ? ${t} : ${t} != null ? [${t}] : [];\n for (const opt of el.options) {\n opt.selected = values.includes(opt.value);\n }`),l("@change",`${t} = Array.from(event.target.selectedOptions).map(o => o.value)`,"model")):(l(":value",t),l("@change",`${t} = value`,"model"))}function f(e,n,t,o=null){if(!e.attrs||0===e.attrs.length)return[];const i=[];for(const s of e.attrs){if(o&&o.has(s.key))continue;const{key:c,value:l,role:r}=s,{isDynamic:d,type:f}=a(c);if(d&&f){const o={k:c,f:t.compile(f,l??"",n),d:1,r:r};u(o,l,s.loc??e.loc),i.push(o)}else i.push({k:c,v:l,d:0,r:r})}return i}function v(e,n,t){var o,s,l,r,v,h;const p={t:e.type,d:0};let $=!1;var y;switch(e.type===i.Text?(y=e.content)&&c.test(y)&&($=!0):((null==(o=e.attrs)?void 0:o.some(e=>"s-model"===e.key||a(e.key).isDynamic))&&($=!0),e.type===i.For&&($=!0)),p.d=$?1:0,e.type){case i.Text:{const o=e.content??"";$?(p.f=t.compile("template",o,n),u(p,o,e.loc)):p.x=o;break}case i.Element:p.g=e.tag,d(e),p.a=f(e,n,t),e.children&&(p.c=m(e.children,n,t));break;case i.For:{const o=null==(s=e.attrs)?void 0:s.find(e=>"each"===e.key),i=(null==o?void 0:o.value)??"";p.f=t.compile("expression",i,n),p.i=(null==(r=null==(l=e.attrs)?void 0:l.find(e=>"item"===e.key))?void 0:r.value)??"item",p.n=(null==(h=null==(v=e.attrs)?void 0:v.find(e=>"index"===e.key))?void 0:h.value)??"index",u(p,i,(null==o?void 0:o.loc)??e.loc);const c=new Set(["each","item","index"]);p.a=f(e,n,t,c);const a=[...n,{i:p.i,x:p.n}];e.children&&(p.c=m(e.children,a,t));break}case i.If:case i.ElseIf:case i.Else:p.a=f(e,n,t),e.children&&(p.c=m(e.children,n,t));break;case i.Comment:p.x=e.content??""}return p}function m(e,n,t){var s;const c=[];let l=0;for(;l<e.length;){const r=e[l];if(r.type===i.If){const a=[];let u=l;for(;u<e.length;){const c=e[u],l=c.type;if(l!==i.If&&l!==i.ElseIf&&l!==i.Else)break;if(l===i.If&&a.length>0)break;let r,d=null,v="";if(l!==i.Else){const e=null==(s=c.attrs)?void 0:s.find(e=>"test"===e.key||"condition"===e.key);v=(null==e?void 0:e.value)??"false",d=t.compile("expression",v,n),r=(null==e?void 0:e.loc)??c.loc}const h=c.children?m(c.children,n,t):[],p=f(c,n,t,new Set(["test","condition"]));if(a.push({f:d,c:h,a:p.length>0?p:void 0,...o&&{o:{expr:v,loc:r}}}),u++,l===i.Else)break}c.push({t:i.Conditional,d:1,b:a,...o&&{o:{loc:r.loc}}}),l=u}else c.push(v(r,n,t)),l++}return c}function h(e,n={}){const{filename:t="anonymous",source:o}=n,i=l.get(t);i.clear(),performance.now();const s=m(e,[],i),c=(performance.now(),i.getStats()),r=s.filter(e=>1===e.d).length;return{t:"root",v:"1.1.0",fns:i.getFunctions(),n:s,s:{tf:c.total,cf:c.cached,dn:r,tn:s.length},m:{t:(new Date).toISOString(),as:e.length,fn:t,...o&&{src:o}}}}function p(e){l.clear(e)}export{h as buildIR,p as clearIRCache,d as transformModel};
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("../../shared/event-listeners.cjs"),e=require("../../types/ast.cjs"),n=require("../../types/lifecycle.cjs"),c=/<\/?([A-Za-z][A-Za-z0-9\-\:]*)[>\s\/]/,o=new Set(["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"]),r=/\s*([^\s"'/>=]+)(?:=(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/gs;function s(o,s){var i;const l=(null==(i=(o.match(c)||[])[1])?void 0:i.toLowerCase())??"",f=function(e,c){const o=[];let s;const i=e.replace(/^\s*[^\s"'/>]+/,""),l=e.length-i.length;for(;null!==(s=r.exec(i));){const r=s[1];if(!r)continue;const i=s[2]??s[3]??s[4]??"";let f=r;t.default.has(r)&&(f=f.replace(/^on/,"@")),"s-model"!==f&&(f=f.replace(/^on-/,"@").replace(/^s-/,":")),f=n.mapAttrKeyToLifecycleKind(f)||f;const u=s[0],a=u.indexOf("=");let d;if(-1===a)d=u.indexOf(r)+r.length;else{let t=a+1;for(;t<u.length&&/\s/.test(u[t]);)t++;if('"'===u[t]||"'"===u[t]){let e=t+1;for(;e<u.length&&/\s/.test(u[e]);)e++;d=e}else{let e=t;for(;e<u.length&&/\s/.test(u[e]);)e++;d=e}}let p=s.index+d+l;const y=e.slice(0,p).split("\n"),m=c[0]+y.length-1,g=y.length>1?y[y.length-1].length+1:c[1]+p;o.push({key:f,value:i,loc:[m,g]})}return o}(o,s);return{type:{for:e.ASTType.For,if:e.ASTType.If,elseif:e.ASTType.ElseIf,else:e.ASTType.Else}[l]??e.ASTType.Element,tag:l,loc:s,children:[],attrs:f}}function i(t,e){const n=t.length;if(t.startsWith("!--",e)){const n=t.indexOf("--\x3e",e+3);return-1===n?-1:n+2}if(t.startsWith("![CDATA[",e)){const n=t.indexOf("]]>",e+8);return-1===n?-1:n+2}if("!"===t[e])return t.indexOf(">",e+1);if("?"===t[e]){const n=t.indexOf("?>",e+1);return-1===n?-1:n+1}let c=!1,o=!1,r=e;for(;r<n;){const e=t[r];if("'"!==e||o)if('"'!==e||c){if(">"===e&&!c&&!o)return r}else o=!o;else c=!c;r++}return-1}exports.parseHtml=function(t){const n=[];if(!t)return[];const c=[],r=[],l=()=>{var t;return 0===c.length?n:(t=c[c.length-1]).children||(t.children=[])};let f=0;const u=t.length,a=[0];for(let e=0;e<u;e++)"\n"===t[e]&&a.push(e+1);function d(t){let e=0;for(let n=1;n<a.length&&!(a[n]>t);n++)e=n;return[e+1,t-a[e]+1]}for(;f<u;){const n=t.indexOf("<",f);if(-1===n||n>f){const c=t.substring(f,-1===n?void 0:n),o=c.trim();if(o){const t=d(f+c.indexOf(o));l().push({type:e.ASTType.Text,tag:"text",content:o,loc:t,children:[]})}if(-1===n)break;f=n}const a=i(t,f+1);if(-1===a){const n=t.substring(f);n.trim()&&l().push({type:e.ASTType.Text,tag:"text",content:n.trim(),children:[],loc:d(f)});break}const p=t.substring(f,a+1),y=l(),m=d(f);if(p.startsWith("\x3c!--")){y.push({type:e.ASTType.Comment,tag:"comment",content:p.slice(4,-3),loc:m,children:[]}),f=a+1;continue}if(p.startsWith("<![CDATA[")){y.push({type:e.ASTType.Text,tag:"text",content:p.slice(9,-3),loc:m,children:[]}),f=a+1;continue}if(p.startsWith("<!")&&!p.startsWith("\x3c!--")){y.push({type:e.ASTType.Comment,tag:"comment",content:p.slice(2,-1),loc:m,children:[]}),f=a+1;continue}if(p.startsWith("<?")){y.push({type:e.ASTType.Comment,tag:"comment",content:p.slice(2,-2),loc:m,children:[]}),f=a+1;continue}if(p.startsWith("</")){const t=r.pop();for(;c.length>0;){if((c[c.length-1].tag||"").toLowerCase()===(null==t?void 0:t.toLowerCase())){c.pop();break}c.pop()}f=a+1;continue}const g=s(p,m);y.push(g);const h=g.tag||"",x=h.toLowerCase().startsWith("script"),b=h.toLowerCase().startsWith("style"),v=p.endsWith("/>")||o.has(h.toLowerCase());if((x||b)&&!v){const n=`</${h}>`,c=t.toLowerCase().indexOf(n,a+1);if(-1!==c){const o=t.substring(a+1,c);o.length&&g.children.push({type:e.ASTType.Text,tag:"text",content:o,loc:d(a+1),children:[]}),f=c+n.length;continue}{const n=t.substring(a+1);n.length&&g.children.push({type:e.ASTType.Text,tag:"text",content:n,loc:d(a+1),children:[]}),f=u;continue}}v||g.type===e.ASTType.Text||g.type===e.ASTType.Comment||(c.push(g),r.push(h)),f=a+1}return n};
@@ -0,0 +1 @@
1
+ import t from"../../shared/event-listeners.js";import{ASTType as n}from"../../types/ast.js";import{mapAttrKeyToLifecycleKind as e}from"../../types/lifecycle.js";const o=/<\/?([A-Za-z][A-Za-z0-9\-\:]*)[>\s\/]/,c=new Set(["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"]),r=/\s*([^\s"'/>=]+)(?:=(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/gs;function s(c,s){var i;const l=(null==(i=(c.match(o)||[])[1])?void 0:i.toLowerCase())??"",f=function(n,o){const c=[];let s;const i=n.replace(/^\s*[^\s"'/>]+/,""),l=n.length-i.length;for(;null!==(s=r.exec(i));){const r=s[1];if(!r)continue;const i=s[2]??s[3]??s[4]??"";let f=r;t.has(r)&&(f=f.replace(/^on/,"@")),"s-model"!==f&&(f=f.replace(/^on-/,"@").replace(/^s-/,":")),f=e(f)||f;const u=s[0],a=u.indexOf("=");let p;if(-1===a)p=u.indexOf(r)+r.length;else{let t=a+1;for(;t<u.length&&/\s/.test(u[t]);)t++;if('"'===u[t]||"'"===u[t]){let n=t+1;for(;n<u.length&&/\s/.test(u[n]);)n++;p=n}else{let n=t;for(;n<u.length&&/\s/.test(u[n]);)n++;p=n}}let m=s.index+p+l;const d=n.slice(0,m).split("\n"),y=o[0]+d.length-1,g=d.length>1?d[d.length-1].length+1:o[1]+m;c.push({key:f,value:i,loc:[y,g]})}return c}(c,s);return{type:{for:n.For,if:n.If,elseif:n.ElseIf,else:n.Else}[l]??n.Element,tag:l,loc:s,children:[],attrs:f}}function i(t,n){const e=t.length;if(t.startsWith("!--",n)){const e=t.indexOf("--\x3e",n+3);return-1===e?-1:e+2}if(t.startsWith("![CDATA[",n)){const e=t.indexOf("]]>",n+8);return-1===e?-1:e+2}if("!"===t[n])return t.indexOf(">",n+1);if("?"===t[n]){const e=t.indexOf("?>",n+1);return-1===e?-1:e+1}let o=!1,c=!1,r=n;for(;r<e;){const n=t[r];if("'"!==n||c)if('"'!==n||o){if(">"===n&&!o&&!c)return r}else c=!c;else o=!o;r++}return-1}function l(t){const e=[];if(!t)return[];const o=[],r=[],l=()=>{var t;return 0===o.length?e:(t=o[o.length-1]).children||(t.children=[])};let f=0;const u=t.length,a=[0];for(let n=0;n<u;n++)"\n"===t[n]&&a.push(n+1);function p(t){let n=0;for(let e=1;e<a.length&&!(a[e]>t);e++)n=e;return[n+1,t-a[n]+1]}for(;f<u;){const e=t.indexOf("<",f);if(-1===e||e>f){const o=t.substring(f,-1===e?void 0:e),c=o.trim();if(c){const t=p(f+o.indexOf(c));l().push({type:n.Text,tag:"text",content:c,loc:t,children:[]})}if(-1===e)break;f=e}const a=i(t,f+1);if(-1===a){const e=t.substring(f);e.trim()&&l().push({type:n.Text,tag:"text",content:e.trim(),children:[],loc:p(f)});break}const m=t.substring(f,a+1),d=l(),y=p(f);if(m.startsWith("\x3c!--")){d.push({type:n.Comment,tag:"comment",content:m.slice(4,-3),loc:y,children:[]}),f=a+1;continue}if(m.startsWith("<![CDATA[")){d.push({type:n.Text,tag:"text",content:m.slice(9,-3),loc:y,children:[]}),f=a+1;continue}if(m.startsWith("<!")&&!m.startsWith("\x3c!--")){d.push({type:n.Comment,tag:"comment",content:m.slice(2,-1),loc:y,children:[]}),f=a+1;continue}if(m.startsWith("<?")){d.push({type:n.Comment,tag:"comment",content:m.slice(2,-2),loc:y,children:[]}),f=a+1;continue}if(m.startsWith("</")){const t=r.pop();for(;o.length>0;){if((o[o.length-1].tag||"").toLowerCase()===(null==t?void 0:t.toLowerCase())){o.pop();break}o.pop()}f=a+1;continue}const g=s(m,y);d.push(g);const h=g.tag||"",x=h.toLowerCase().startsWith("script"),b=h.toLowerCase().startsWith("style"),v=m.endsWith("/>")||c.has(h.toLowerCase());if((x||b)&&!v){const e=`</${h}>`,o=t.toLowerCase().indexOf(e,a+1);if(-1!==o){const c=t.substring(a+1,o);c.length&&g.children.push({type:n.Text,tag:"text",content:c,loc:p(a+1),children:[]}),f=o+e.length;continue}{const e=t.substring(a+1);e.length&&g.children.push({type:n.Text,tag:"text",content:e,loc:p(a+1),children:[]}),f=u;continue}}v||g.type===n.Text||g.type===n.Comment||(o.push(g),r.push(h)),f=a+1}return e}export{l as parseHtml};
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./router/core.cjs"),r=require("./router/router-view.cjs"),s=require("./router/router-link.cjs"),t=require("./shared/templateError.cjs"),o=require("./shared/gen-function.cjs"),p=require("./shared/functions.cjs"),i=require("./shared/event-listeners.cjs"),u=require("./shared/is-object.cjs"),c=require("./shared/url.cjs"),x=require("./shared/env.cjs"),n=require("./types/ast.cjs"),j=require("./types/lifecycle.cjs"),q=require("./runtime/component/base-element.cjs"),a=require("./compiler/ir/buildIR.cjs"),l=require("./compiler/parser/parser.cjs"),d=require("./runtime/reactivity/observe.cjs"),m=require("./runtime/component/decorators.cjs"),h=require("./runtime/renderer/renderIR.cjs");exports.Router=e.Router,exports.createRouter=e.createRouter,exports.getRouter=e.getRouter,exports.routerReady=e.routerReady,exports.RouterView=r.default,exports.RouterLink=s.default,exports.showTemplateError=t.showTemplateError,exports.genFunction=o.genFunction,exports.createFunction=p.createFunction,exports.listeners=i.default,exports.isObject=u.isObject,exports.parseHashUrl=c.parseHashUrl,exports.IS_DEV=x.IS_DEV,exports.IS_PROD=x.IS_PROD,exports.ASTType=n.ASTType,exports.LIFECYCLE_ATTRS_MAP=j.LIFECYCLE_ATTRS_MAP,exports.isLifecycleKind=j.isLifecycleKind,exports.mapAttrKeyToLifecycleKind=j.mapAttrKeyToLifecycleKind,exports.BaseElement=q.default,exports.buildIR=a.buildIR,exports.clearIRCache=a.clearIRCache,exports.transformModel=a.transformModel,exports.parseHtml=l.parseHtml,exports.isProxy=d.isProxy,exports.observe=d.observe,exports.toRaw=d.toRaw,exports.CustomElement=m.CustomElement,exports.IRRenderer=h.IRRenderer,exports.createRender=h.createRender;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './compiler';
2
+ export * from './router';
2
3
  export * from './runtime';
3
4
  export * from './shared';
4
5
  export * from './types';