jaxs 0.2.0 → 0.3.0

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 (32) hide show
  1. package/cypress/e2e/svg-renders.cy.js +10 -0
  2. package/cypress/jaxs-apps/dist/add-remove-root-children.3bb9b3f5.js +71 -50
  3. package/cypress/jaxs-apps/dist/add-remove-root-children.3bb9b3f5.js.map +1 -1
  4. package/cypress/jaxs-apps/dist/svg.04290504.js +644 -0
  5. package/cypress/jaxs-apps/dist/svg.04290504.js.map +1 -0
  6. package/cypress/jaxs-apps/dist/svg.html +11 -0
  7. package/cypress/jaxs-apps/svg.html +11 -0
  8. package/cypress/jaxs-apps/svg.jsx +15 -0
  9. package/dist/jaxs.js +67 -34
  10. package/package.json +3 -2
  11. package/src/debugging.js +3 -3
  12. package/src/rendering/change/instructions/attributes.ts +5 -2
  13. package/src/rendering/change/instructions/children.ts +22 -23
  14. package/src/rendering/change/instructions/element.ts +8 -1
  15. package/src/rendering/change/instructions/events.ts +1 -1
  16. package/src/rendering/change/instructions/generate.ts +2 -2
  17. package/src/rendering/change/instructions/node.ts +19 -2
  18. package/src/rendering/change/instructions/text.ts +1 -1
  19. package/src/rendering/change.ts +17 -9
  20. package/src/rendering/dom/attributesAndEvents.ts +1 -1
  21. package/src/rendering/dom/create.ts +1 -1
  22. package/src/rendering/dom/svg.ts +18 -0
  23. package/src/rendering/templates/children.ts +10 -2
  24. package/src/rendering/templates/tag.ts +23 -1
  25. package/src/types.ts +4 -1
  26. package/.parcel-cache/6f14daf302269614-BundleGraph-0 +0 -0
  27. package/.parcel-cache/7d3d872b02d671a6-AssetGraph-0 +0 -0
  28. package/.parcel-cache/8e029bb14f8992df-RequestGraph-0 +0 -0
  29. package/.parcel-cache/a5394978e4ece10b-AssetGraph-0 +0 -0
  30. package/.parcel-cache/b5686051ae060930.txt +0 -2
  31. package/.parcel-cache/data.mdb +0 -0
  32. package/.parcel-cache/lock.mdb +0 -0
@@ -12,7 +12,7 @@ import type {
12
12
  } from '../types';
13
13
  import { ChangeInstructions } from '../types'
14
14
  import { compileChange } from './change/compile';
15
- import { debug } from '../debugging';
15
+ // import { debug } from '../debugging';
16
16
 
17
17
  export const change = (
18
18
  source: HtmlChildren,
@@ -21,7 +21,7 @@ export const change = (
21
21
  ) => {
22
22
  const instructions = compileChange(source, target, parent);
23
23
 
24
- debug('instructions', instructions.map((instruction) => instruction.type))
24
+ // debug('instructions', instructions.map((instruction) => instruction.type))
25
25
 
26
26
  instructions.forEach((instruction) => {
27
27
  performInstruction(instruction);
@@ -43,7 +43,7 @@ const changeText: Updater = (instruction: Instruction) => {
43
43
  const removeNode: Updater = (instruction: Instruction) => {
44
44
  const { source } = instruction;
45
45
  source.remove();
46
- debug('removeNode called on', source.nodeName)
46
+ // debug('removeNode called on', source.nodeName)
47
47
  };
48
48
 
49
49
  const insertNode: Updater = (instruction: Instruction) => {
@@ -61,22 +61,30 @@ const insertNode: Updater = (instruction: Instruction) => {
61
61
  const replaceNode: Updater = (instruction: Instruction) => {
62
62
  const { source, target } = instruction;
63
63
  source.replaceWith(target);
64
- debug('replaceNode called on', source.nodeName, 'with', target.nodeName)
65
- debug('parent', source.parentElement)
64
+ // debug('replaceNode called on', source.nodeName, 'with', target.nodeName)
65
+ // debug('parent', source.parentElement)
66
66
  };
67
67
 
68
68
  const removeAttribute: Updater = (instruction: Instruction) => {
69
69
  const { source, data } = instruction;
70
- const { name } = data as RemoveInstructionData;
70
+ const { name, isSvg } = data as RemoveInstructionData;
71
71
 
72
- (source as ExpandedElement).removeAttribute(name);
72
+ if (isSvg) {
73
+ (source as SVGElement).removeAttributeNS(null, name)
74
+ } else {
75
+ (source as ExpandedElement).removeAttribute(name);
76
+ }
73
77
  };
74
78
 
75
79
  const addAttribute: Updater = (instruction: Instruction) => {
76
80
  const { source, data } = instruction;
77
- const { name, value } = data as AttributeInstructionData;
81
+ const { name, value, isSvg } = data as AttributeInstructionData;
78
82
 
79
- (source as ExpandedElement).setAttribute(name, value);
83
+ if (isSvg) {
84
+ (source as SVGElement).setAttributeNS(null, name, value)
85
+ } else {
86
+ (source as ExpandedElement).setAttribute(name, value);
87
+ }
80
88
  };
81
89
 
82
90
  const updateAttribute: Updater = (instruction: Instruction) => {
@@ -1,4 +1,4 @@
1
- import { Attributes, AttributesAndEvents } from '../../types';
1
+ import type { Attributes, AttributesAndEvents } from '../../types';
2
2
 
3
3
  export const separateAttrsAndEvents = (
4
4
  combined: Attributes,
@@ -1,4 +1,4 @@
1
- import {
1
+ import type {
2
2
  Attributes,
3
3
  DomEventPublisher,
4
4
  EventAttributes,
@@ -0,0 +1,18 @@
1
+ import type { RenderKit, Attributes, ExpandedElement } from "../../types"
2
+
3
+ export const namespace = 'http://www.w3.org/2000/svg'
4
+ export const isSvgTag = (tagType: string) => tagType === 'svg'
5
+ export const isSvg = (element: SVGElement) => element.namespaceURI === namespace
6
+
7
+ export const createSvgNode = (type: string, attributes: Attributes, renderKit: RenderKit) => {
8
+ const document = renderKit && renderKit.document || window.document;
9
+ const node = document.createElementNS(namespace, type)
10
+
11
+ for (const key in attributes) {
12
+ if (key === '__self' || key === 'xmlns') continue;
13
+ // adding namespace in as first argument makes it not really render!
14
+ node.setAttributeNS(null, key, attributes[key]);
15
+ }
16
+
17
+ return node as unknown as ExpandedElement
18
+ }
@@ -61,14 +61,22 @@ const textNode = (content: TextValue) => {
61
61
  return new TextTemplate(content);
62
62
  };
63
63
 
64
+ const withSvgFlag = (isSvg: boolean) => (template: Template) => {
65
+ template && (template.isSvg = template.isSvg || isSvg)
66
+ return template
67
+ }
68
+
64
69
  export class Children implements Template {
65
70
  collection: Template[];
66
71
  parentElement: Element | undefined;
72
+ isSvg: boolean;
67
73
 
68
- constructor(jsxChildren: Template[]) {
69
- this.collection = ensureArray(jsxChildren);
74
+ constructor(jsxChildren: Template[], isSvg = false) {
75
+ this.collection = ensureArray(jsxChildren)
70
76
  this.collection = this.collection.map(replaceTextNodes) as Template[];
71
77
  this.collection = this.collection.flat() as Template[];
78
+ this.collection = this.collection.map(withSvgFlag(isSvg))
79
+ this.isSvg = isSvg;
72
80
  }
73
81
 
74
82
  render(renderKit: RenderKit, parentElement: Element | undefined) {
@@ -8,6 +8,7 @@ import type {
8
8
 
9
9
  import { createDecoratedNode } from '../dom/create';
10
10
  import { separateAttrsAndEvents } from '../dom/attributesAndEvents';
11
+ import { isSvgTag, createSvgNode } from '../dom/svg'
11
12
  import { Children } from './children';
12
13
 
13
14
  export class Tag implements Template {
@@ -15,11 +16,13 @@ export class Tag implements Template {
15
16
  events: EventAttributes;
16
17
  attributes: Attributes;
17
18
  children: Children;
19
+ isSvg: boolean;
18
20
 
19
21
  constructor(
20
22
  tagType: string,
21
23
  combinedAttributes: Attributes,
22
24
  children: Template[],
25
+ isSvg = false
23
26
  ) {
24
27
  this.type = tagType;
25
28
 
@@ -27,7 +30,8 @@ export class Tag implements Template {
27
30
  this.events = events;
28
31
  this.attributes = attributes;
29
32
 
30
- this.children = new Children(children);
33
+ this.isSvg = isSvg || isSvgTag(this.type);
34
+ this.children = new Children(children, this.isSvg);
31
35
  }
32
36
 
33
37
  render(renderKit: RenderKit): DomCollection {
@@ -39,6 +43,14 @@ export class Tag implements Template {
39
43
  }
40
44
 
41
45
  generateDom(renderKit: RenderKit) {
46
+ if (this.isSvg) {
47
+ return this.generateSvnDom(renderKit)
48
+ } else {
49
+ return this.generateHtmlDom(renderKit)
50
+ }
51
+ }
52
+
53
+ generateHtmlDom(renderKit: RenderKit) {
42
54
  const node = createDecoratedNode(
43
55
  this.type,
44
56
  this.attributes,
@@ -49,6 +61,16 @@ export class Tag implements Template {
49
61
  return node;
50
62
  }
51
63
 
64
+ generateSvnDom(renderKit: RenderKit) {
65
+ const node = createSvgNode(
66
+ this.type,
67
+ this.attributes,
68
+ renderKit,
69
+ );
70
+ node.__jsx = this.key();
71
+ return node;
72
+ }
73
+
52
74
  key() {
53
75
  return this.attributes.key || this.source() || this.createKey();
54
76
  }
package/src/types.ts CHANGED
@@ -20,7 +20,7 @@ export interface InputElement extends ExpandedElement {
20
20
  // deno-lint-ignore no-explicit-any
21
21
  value: any;
22
22
  }
23
- export type Dom = Text | ExpandedElement;
23
+ export type Dom = Text | ExpandedElement | SVGElement;
24
24
  export type DomCollection = Dom[];
25
25
  export type HtmlChildren =
26
26
  | HTMLCollection
@@ -45,6 +45,7 @@ export type AttributesAndEvents = {
45
45
  export type TemplateEventListeners = Record<string, EventListener>;
46
46
 
47
47
  export interface Template {
48
+ isSvg?: boolean,
48
49
  render: (
49
50
  renderKit: RenderKit,
50
51
  parentElement?: Element,
@@ -80,11 +81,13 @@ export type BusListenersMap = Record<BusEventName, BusPayload>;
80
81
  // Change instructions
81
82
  export type RemoveInstructionData = {
82
83
  name: string;
84
+ isSvg?: boolean;
83
85
  };
84
86
 
85
87
  export type AttributeInstructionData = {
86
88
  name: string;
87
89
  value: string;
90
+ isSvg?: boolean;
88
91
  };
89
92
 
90
93
  export type EventInstructionData = {
@@ -1,2 +0,0 @@
1
- 536441516
2
- 1705626586315608000
Binary file
Binary file