vsn 0.1.56 → 0.1.59

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 (58) hide show
  1. package/demo/demo.html +186 -62
  2. package/demo/vsn.js +2 -1
  3. package/demo/xhr.vsn +10 -4
  4. package/dist/AST/FunctionNode.d.ts +17 -0
  5. package/dist/AST/FunctionNode.js +128 -0
  6. package/dist/AST/FunctionNode.js.map +1 -1
  7. package/dist/AST.d.ts +47 -46
  8. package/dist/AST.js +57 -46
  9. package/dist/AST.js.map +1 -1
  10. package/dist/DOM/DOMObject.d.ts +6 -1
  11. package/dist/DOM/DOMObject.js.map +1 -1
  12. package/dist/Tag.d.ts +2 -2
  13. package/dist/Tag.js +22 -14
  14. package/dist/Tag.js.map +1 -1
  15. package/dist/Types.js +1 -1
  16. package/dist/Types.js.map +1 -1
  17. package/dist/attributes/Bind.js.map +1 -1
  18. package/dist/attributes/Exec.d.ts +1 -0
  19. package/dist/attributes/Exec.js +8 -1
  20. package/dist/attributes/Exec.js.map +1 -1
  21. package/dist/attributes/KeyAbstract.js +1 -1
  22. package/dist/attributes/KeyAbstract.js.map +1 -1
  23. package/dist/attributes/KeyDown.js +1 -1
  24. package/dist/attributes/KeyDown.js.map +1 -1
  25. package/dist/attributes/KeyUp.js +1 -1
  26. package/dist/attributes/KeyUp.js.map +1 -1
  27. package/dist/attributes/LazyAttribute.js +2 -2
  28. package/dist/attributes/LazyAttribute.js.map +1 -1
  29. package/dist/attributes/On.js +1 -1
  30. package/dist/attributes/On.js.map +1 -1
  31. package/dist/attributes/ScriptAttribute.d.ts +4 -0
  32. package/dist/attributes/ScriptAttribute.js +45 -0
  33. package/dist/attributes/ScriptAttribute.js.map +1 -0
  34. package/dist/attributes/_imports.d.ts +1 -0
  35. package/dist/attributes/_imports.js +3 -1
  36. package/dist/attributes/_imports.js.map +1 -1
  37. package/dist/vsn.d.ts +1 -1
  38. package/dist/vsn.js +2 -2
  39. package/dist/vsn.js.map +1 -1
  40. package/dist/vsn.min.js +2 -0
  41. package/package.json +3 -4
  42. package/src/AST/FunctionNode.ts +50 -0
  43. package/src/AST.ts +10 -0
  44. package/src/DOM/DOMObject.ts +7 -1
  45. package/src/Tag.ts +21 -15
  46. package/src/Types.ts +1 -1
  47. package/src/attributes/Bind.ts +1 -2
  48. package/src/attributes/Exec.ts +5 -1
  49. package/src/attributes/KeyAbstract.ts +1 -1
  50. package/src/attributes/KeyDown.ts +1 -1
  51. package/src/attributes/KeyUp.ts +1 -1
  52. package/src/attributes/LazyAttribute.ts +2 -2
  53. package/src/attributes/On.ts +1 -1
  54. package/src/attributes/ScriptAttribute.ts +9 -0
  55. package/src/attributes/_imports.ts +1 -0
  56. package/src/vsn.ts +2 -2
  57. package/test/AST/FunctionNode.spec.ts +14 -0
  58. package/webpack.config.js +10 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vsn",
3
- "version": "0.1.56",
3
+ "version": "0.1.59",
4
4
  "description": "SEO Friendly Javascript/Typescript Framework",
5
5
  "keywords": [
6
6
  "framework",
@@ -15,11 +15,10 @@
15
15
  "scripts": {
16
16
  "build": "rm -rf ./dist/ && tsc",
17
17
  "build_dev": "rm -rf ./dist/ && webpack --env BUILD=development BENCHMARK=1",
18
- "demo": "rm -rf ./dist/ && webpack --env BUILD=production BENCHMARK=1 && cp ./dist/vsn.min.js ./demo/vsn.js",
19
- "clean": "rm -rf ./dist/",
18
+ "demo": "webpack --env BUILD=production BENCHMARK=1 && cp ./dist/vsn.min.js ./demo/vsn.js",
20
19
  "test": "karma start --single-run",
21
20
  "posttest": "codecov",
22
- "sample": "rm -rf sample/dist/ && tsc -p sample/ && node sample/dist/sample/index.js"
21
+ "clean": "rm -rf ./dist/"
23
22
  },
24
23
  "license": "MIT",
25
24
  "dependencies": {},
@@ -0,0 +1,50 @@
1
+ import {Scope} from "../Scope";
2
+ import {DOM} from "../DOM";
3
+ import {Tag} from "../Tag";
4
+ import {Token, Tree, TreeNode} from "../AST";
5
+ import {Node} from "./Node";
6
+ import {BlockNode} from "./BlockNode";
7
+
8
+ export class FunctionNode extends Node implements TreeNode {
9
+ protected requiresPrep: boolean = true;
10
+
11
+ constructor(
12
+ public readonly name: string,
13
+ public readonly args: string[],
14
+ public readonly block: BlockNode
15
+ ) {
16
+ super();
17
+ }
18
+
19
+ protected _getChildNodes(): Node[] {
20
+ return [
21
+ this.block
22
+ ];
23
+ }
24
+
25
+ public async prepare(scope: Scope, dom: DOM, tag: Tag = null) {
26
+ scope.set(this.name, async (...args) => {
27
+ const functionScope = new Scope(scope);
28
+ for (const arg of this.args) {
29
+ functionScope.set(arg, args.shift());
30
+ }
31
+ return await this.evaluate(functionScope, dom, tag);
32
+ });
33
+ }
34
+
35
+ public async evaluate(scope: Scope, dom: DOM, tag: Tag = null) {
36
+ return await this.block.evaluate(scope, dom, tag);
37
+ }
38
+
39
+ public static parse(lastNode, token, tokens: Token[]): FunctionNode {
40
+ tokens.shift(); // skip 'func'
41
+ const name = tokens.shift();
42
+ const argTokens = Tree.getBlockTokens(tokens);
43
+ const funcArgs: string[] = [];
44
+ for (const t of argTokens) {
45
+ funcArgs.push(t[0].value);
46
+ }
47
+ const block = Tree.processTokens(Tree.getBlockTokens(tokens, null)[0]);
48
+ return new FunctionNode(name.value, funcArgs, block);
49
+ }
50
+ }
package/src/AST.ts CHANGED
@@ -26,6 +26,7 @@ import {BooleanLiteralNode} from "./AST/BooleanLiteralNode";
26
26
  import {NotNode} from "./AST/NotNode";
27
27
  import {XHRNode} from "./AST/XHRNode";
28
28
  import {StringFormatNode} from "./AST/StringFormatNode";
29
+ import {FunctionNode} from "./AST/FunctionNode";
29
30
 
30
31
  function lower(str: string): string {
31
32
  return str ? str.toLowerCase() : null;
@@ -65,6 +66,7 @@ export enum TokenType {
65
66
  IF,
66
67
  ELSE_IF,
67
68
  ELSE,
69
+ FUNC,
68
70
  NAME,
69
71
  L_BRACE,
70
72
  R_BRACE,
@@ -194,6 +196,10 @@ const TOKEN_PATTERNS: TokenPattern[] = [
194
196
  type: TokenType.ELSE,
195
197
  pattern: /^else\s?(?=\{)/
196
198
  },
199
+ {
200
+ type: TokenType.FUNC,
201
+ pattern: /^func\s/
202
+ },
197
203
  {
198
204
  type: TokenType.ELEMENT_ATTRIBUTE,
199
205
  pattern: /^\.?@[-_a-zA-Z0-9]*/
@@ -482,6 +488,10 @@ export class Tree {
482
488
  node = ForStatementNode.parse(node, token, tokens);
483
489
  blockNodes.push(node);
484
490
  node = null;
491
+ } else if (token.type === TokenType.FUNC) {
492
+ node = FunctionNode.parse(node, token, tokens);
493
+ blockNodes.push(node);
494
+ node = null;
485
495
  } else if (StringFormatNode.match(tokens)) {
486
496
  node = StringFormatNode.parse(node, tokens[0], tokens);
487
497
  } else if (token.type === TokenType.STRING_LITERAL) {
@@ -1,9 +1,15 @@
1
1
  import {Scope} from "../Scope";
2
2
  import {EventDispatcher} from "../EventDispatcher";
3
3
 
4
+ export interface IEventHandler {
5
+ event: string;
6
+ handler: (...args: any[]) => any;
7
+ context?: any;
8
+ }
9
+
4
10
  export abstract class DOMObject extends EventDispatcher {
5
11
  protected _scope: Scope;
6
- protected onEventHandlers: {[key:string]:any[]};
12
+ protected onEventHandlers: {[key:string]: IEventHandler[]};
7
13
  protected _uniqueScope: boolean = false;
8
14
 
9
15
  constructor(
package/src/Tag.ts CHANGED
@@ -271,7 +271,7 @@ export class Tag extends DOMObject {
271
271
  return this._scope;
272
272
 
273
273
  if (this.uniqueScope)
274
- return this.createScope();
274
+ return this.createScope(true);
275
275
 
276
276
  if (!!this.parentTag)
277
277
  return this.parentTag.scope;
@@ -491,7 +491,7 @@ export class Tag extends DOMObject {
491
491
 
492
492
  public async connectAttributes() {
493
493
  if (this.isInput) {
494
- this.addEventHandler('input', [], this.inputMutation.bind(this));
494
+ this.addEventHandler('input', [], this.inputMutation, this);
495
495
  }
496
496
 
497
497
  for (const attr of this.nonDeferredAttributes) {
@@ -515,9 +515,12 @@ export class Tag extends DOMObject {
515
515
  option.removeAttribute('selected');
516
516
  }
517
517
  }
518
- this.element.setAttribute('value', values.join(','));
518
+ //this.element.setAttribute('value', );
519
+ this.value = values.join(',');
519
520
  } else {
520
- this.element.setAttribute('value', e.target.value);
521
+ //this.element.setAttribute('value', e.target.value);
522
+ //(this.element as any).value = e.target.value;
523
+ this.value = e.target.value;
521
524
  }
522
525
  }
523
526
 
@@ -538,13 +541,14 @@ export class Tag extends DOMObject {
538
541
  protected handleEvent(eventType: string, e) {
539
542
  if (e)
540
543
  e.stopPropagation();
544
+
541
545
  if (!this.onEventHandlers[eventType])
542
546
  return;
543
547
 
544
548
  this.scope.set('$event', e);
545
549
  this.scope.set('$value', this.value);
546
550
  for (const handler of this.onEventHandlers[eventType]) {
547
- handler(e);
551
+ handler.handler.call(handler.context, e);
548
552
  }
549
553
  }
550
554
 
@@ -556,7 +560,7 @@ export class Tag extends DOMObject {
556
560
  return attribute.replace(`|${modifier}`, '');
557
561
  }
558
562
 
559
- public addEventHandler(eventType: string, modifiers: string[], handler) {
563
+ public addEventHandler(eventType: string, modifiers: string[], handler, context: any = null) {
560
564
  let passiveValue: boolean = null;
561
565
  if (modifiers.indexOf('active') > -1) {
562
566
  passiveValue = false;
@@ -574,16 +578,20 @@ export class Tag extends DOMObject {
574
578
  element.addEventListener(eventType, this.handleEvent.bind(this, eventType), opts);
575
579
  }
576
580
 
577
- this.onEventHandlers[eventType].push(handler);
581
+ this.onEventHandlers[eventType].push({
582
+ handler: handler,
583
+ event: eventType,
584
+ context: context,
585
+ });
578
586
  }
579
587
 
580
- public removeEventHandler(eventType: string, handler) {
588
+ public removeEventHandler(eventType: string, handler, context: any = null) {
581
589
  if (!this.onEventHandlers[eventType])
582
590
  return;
583
591
 
584
- const index = this.onEventHandlers[eventType].indexOf(handler);
585
- if (index > -1) {
586
- this.onEventHandlers[eventType].splice(index, 1);
592
+ const _handler = this.onEventHandlers[eventType].find(h => h.handler === handler && h.context === context);
593
+ if (_handler) {
594
+ this.onEventHandlers[eventType].splice(this.onEventHandlers[eventType].indexOf(_handler), 1);
587
595
  if (this.onEventHandlers[eventType].length === 0) {
588
596
  this.element.removeEventListener(eventType, this.handleEvent.bind(this, eventType));
589
597
  }
@@ -593,18 +601,16 @@ export class Tag extends DOMObject {
593
601
  public createScope(force: boolean = false): Scope {
594
602
  // Standard attribute requires a unique scope
595
603
  // @todo: Does this cause any issues with attribute bindings on the parent scope prior to having its own scope? hmm...
596
- if ((!this.uniqueScope && force) || this.uniqueScope) {
604
+ if (!this._scope && (force || this.uniqueScope)) {
597
605
  this._uniqueScope = true;
598
606
  this._scope = new Scope();
599
607
 
600
608
  if (this.parentTag) {
601
609
  this.scope.parentScope = this.parentTag.scope;
602
610
  }
603
-
604
- return this._scope;
605
611
  }
606
612
 
607
- return null;
613
+ return this._scope;
608
614
  }
609
615
 
610
616
  async watchAttribute(attributeName: string) {
package/src/Types.ts CHANGED
@@ -18,7 +18,7 @@ export class Types {
18
18
 
19
19
  @Registry.type('float')
20
20
  public static float(value: string) {
21
- value = `${value}`.replace(/[^0-9.]+/, '');
21
+ value = `${value}`.replace(/[^\d.]+/, '');
22
22
  return parseFloat(value);
23
23
  }
24
24
 
@@ -44,7 +44,6 @@ export class Bind extends Attribute {
44
44
 
45
45
  this.key = await ref.getKey();
46
46
  this.boundScope = await ref.getScope();
47
-
48
47
  if (!!this.valueFromElement)
49
48
  this.updateFrom();
50
49
 
@@ -80,12 +79,12 @@ export class Bind extends Attribute {
80
79
 
81
80
  get valueFromElement(): string {
82
81
  let value;
82
+
83
83
  if (this.property) {
84
84
  value = this.tag.getElementAttribute(this.property);
85
85
  } else {
86
86
  value = this.tag.value;
87
87
  }
88
-
89
88
  return value;
90
89
  }
91
90
 
@@ -7,8 +7,12 @@ export class Exec extends Attribute {
7
7
  public static readonly canDefer: boolean = false;
8
8
  protected tree: Tree;
9
9
 
10
+ public get code() {
11
+ return this.getAttributeValue();
12
+ }
13
+
10
14
  public async compile() {
11
- this.tree = new Tree(this.getAttributeValue());
15
+ this.tree = new Tree(this.code);
12
16
  await this.tree.prepare(this.tag.scope, this.tag.dom, this.tag);
13
17
  await super.compile();
14
18
  }
@@ -9,7 +9,7 @@ export abstract class KeyAbstract extends On {
9
9
  }
10
10
 
11
11
  public async connect() {
12
- this.tag.addEventHandler('keydown', this.getAttributeModifiers(), this.handleEvent.bind(this));
12
+ this.tag.addEventHandler('keydown', this.getAttributeModifiers(), this.handleEvent, this);
13
13
  await super.connect();
14
14
  }
15
15
 
@@ -4,7 +4,7 @@ import {Registry} from "../Registry";
4
4
  @Registry.attribute('vsn-key-down')
5
5
  export class KeyDown extends KeyAbstract {
6
6
  public async connect() {
7
- this.tag.addEventHandler('keydown', this.getAttributeModifiers(), this.handleEvent.bind(this));
7
+ this.tag.addEventHandler('keydown', this.getAttributeModifiers(), this.handleEvent, this);
8
8
  await super.connect();
9
9
  }
10
10
  }
@@ -4,7 +4,7 @@ import {Registry} from "../Registry";
4
4
  @Registry.attribute('vsn-key-up')
5
5
  export class KeyUp extends KeyAbstract {
6
6
  public async connect() {
7
- this.tag.addEventHandler('keyup', this.getAttributeModifiers(), this.handleEvent.bind(this));
7
+ this.tag.addEventHandler('keyup', this.getAttributeModifiers(), this.handleEvent, this);
8
8
  await super.connect();
9
9
  }
10
10
  }
@@ -12,7 +12,7 @@ export class LazyAttribute extends On {
12
12
  }
13
13
 
14
14
  public async connect() {
15
- this.tag.addEventHandler('scroll', ['passive'], this.handleEvent.bind(this));
15
+ this.tag.addEventHandler('scroll', ['passive'], this.handleEvent, this);
16
16
  await this.handleEvent();
17
17
  }
18
18
 
@@ -20,7 +20,7 @@ export class LazyAttribute extends On {
20
20
  if (!this.loaded && window.scrollY + window.outerHeight >= this.eleTop) {
21
21
  this.loaded = true;
22
22
  await this.handler.evaluate(this.tag.scope, this.tag.dom, this.tag);
23
- this.tag.removeEventHandler('scroll', this.handleEvent.bind(this));
23
+ this.tag.removeEventHandler('scroll', this.handleEvent, this);
24
24
  }
25
25
  }
26
26
  }
@@ -32,7 +32,7 @@ export abstract class On extends Attribute {
32
32
  }
33
33
 
34
34
  public async connect() {
35
- this.tag.addEventHandler(this.getAttributeBinding(), this.getAttributeModifiers(), this.handleEvent.bind(this));
35
+ this.tag.addEventHandler(this.getAttributeBinding(), this.getAttributeModifiers(), this.handleEvent, this);
36
36
  await super.connect();
37
37
  }
38
38
  }
@@ -0,0 +1,9 @@
1
+ import {Registry} from "../Registry";
2
+ import {Exec} from "./Exec";
3
+
4
+ @Registry.attribute('vsn-script')
5
+ export class ScriptAttribute extends Exec {
6
+ public get code() {
7
+ return this.tag.element.innerText;
8
+ }
9
+ }
@@ -19,6 +19,7 @@ export {Referenced} from "./Referenced";
19
19
  export {RootAttribute} from "./RootAttribute";
20
20
  export {ScopeAttribute} from "./ScopeAttribute";
21
21
  export {ScopeChange} from "./ScopeChange";
22
+ export {ScriptAttribute} from "./ScriptAttribute";
22
23
  export {SetAttribute} from "./SetAttribute";
23
24
  export {StandardAttribute} from "./StandardAttribute";
24
25
  export {StyleAttribute} from "./StyleAttribute";
package/src/vsn.ts CHANGED
@@ -43,7 +43,7 @@ export class Vision extends EventDispatcher {
43
43
  return this._dom;
44
44
  }
45
45
 
46
- public async eval(code: string) {
46
+ public async exec(code: string) {
47
47
  return await this._dom.exec(code);
48
48
  }
49
49
 
@@ -55,7 +55,7 @@ export class Vision extends EventDispatcher {
55
55
  await this._dom.buildFrom(document, true);
56
56
  const now = (new Date()).getTime();
57
57
  const setupTime = now - startTime;
58
- console.warn(`Took ${setupTime}ms to start up VisionJS. https://www.vsnjs.com/`);
58
+ console.warn(`Took ${setupTime}ms to start up VisionJS. https://www.vsnjs.com/`, window ? `v${window['VSN_VERSION']}` : null);
59
59
  }
60
60
 
61
61
  public static get instance() {
@@ -0,0 +1,14 @@
1
+ import {Tree} from "../../src/AST";
2
+ import {Scope} from "../../src/Scope";
3
+
4
+
5
+ describe('FunctionNode', () => {
6
+ it("properly define a simple function", async () => {
7
+ const tree = new Tree(`func add(a, b) { return a + b; }; add(1, 2);`);
8
+ const scope = new Scope();
9
+ await tree.prepare(scope, null, null);
10
+ expect(scope.get('add')).toBeDefined();
11
+ const v = await tree.evaluate(scope, null, null);
12
+ expect(v).toBe(3);
13
+ });
14
+ });
package/webpack.config.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const path = require('path');
2
2
  const webpack = require("webpack");
3
+ const TerserPlugin = require("terser-webpack-plugin");
3
4
 
4
5
  const defaultConfiguration = {
5
6
  entry: './src/vsn.ts',
@@ -20,6 +21,15 @@ const defaultConfiguration = {
20
21
  filename: 'vsn.min.js',
21
22
  path: path.resolve(__dirname, 'dist'),
22
23
  },
24
+ optimization: {
25
+ minimizer: [new TerserPlugin({
26
+ terserOptions: {
27
+ format: {
28
+ preamble: `/* Copyright ${new Date().getUTCFullYear()}, VSNjs. ${require('./package.json').name} ${require('./package.json').version} (${new Date().toUTCString()}) */if (window) window['VSN_VERSION']='${require('./package.json').version}';`
29
+ }
30
+ }
31
+ })],
32
+ }
23
33
  };
24
34
 
25
35
  function buildConfig(env) {