@sitecore-feaas/clientside 0.3.9 → 0.3.10

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.
package/src/ui/react.tsx CHANGED
@@ -10,17 +10,16 @@ import React from 'react'
10
10
  import { renderHTMLContent, HTMLNode, parseHTMLContent } from '../dom/html.js'
11
11
  import { DataSettings } from '../utils/settings.js'
12
12
  export { renderHTMLContent }
13
- import { FEAASExternalProps, getComponent } from './FEAASExternal.js'
13
+ import { getComponent } from './FEAASExternal.js'
14
14
  export { External, linkComponents, registerComponent, getComponent } from './FEAASExternal.js'
15
15
  export * from './index.js'
16
16
  export * from '../headless.js'
17
17
 
18
18
  export type FEAASReactComponentProps = FEAASComponentProps & {
19
- src?: string
20
19
  preload?: boolean
21
20
  lastModified?: Date | string
22
21
  }
23
- export const ServerComponent = async (props: FEAASComponentProps) => {
22
+ export const ServerComponent = async (props: FEAASReactComponentProps) => {
24
23
  const fetchedData = await DataSettings.fetch(props?.data || {})
25
24
 
26
25
  const p: FEAASReactComponentProps = { ...props }
@@ -46,8 +45,7 @@ export function FEAASReactComponent({ data, ...props }: FEAASReactComponentProps
46
45
  node,
47
46
  (node) => {
48
47
  if (node.localName == 'feaas-external' && getComponent(node.getAttribute('data-external-id'))) {
49
- const { dataExternalId, ...props } = FEAASExternalProps(node as any)
50
- return [FEAASReactExternal, props]
48
+ return [FEAASReactExternal, {}]
51
49
  }
52
50
  },
53
51
  index
@@ -100,10 +98,23 @@ export function HTMLNodeToReact(
100
98
  }
101
99
  const children = Array.from(node.childNodes).map((child, index) => HTMLNodeToReact(child, callback, index))
102
100
 
103
- const attributes: { [key: string]: string } = {}
101
+ const attributes: { [key: string]: string | Record<string, string> } = {}
104
102
  for (let i = 0; i < node.attributes.length; i++) {
105
103
  const { name, value } = node.attributes[i]
106
- attributes[name] = value
104
+ if (name == 'style') {
105
+ attributes[name] = String(value || '')
106
+ .trim()
107
+ .split(/\s*;\s*/)
108
+ .reduce((object, pair) => {
109
+ const [k, v] = pair.split(':')
110
+ return {
111
+ ...object,
112
+ [k]: v
113
+ }
114
+ }, {})
115
+ } else {
116
+ attributes[name] = value
117
+ }
107
118
  }
108
119
 
109
120
  const reactNode = callback?.(node)
@@ -84,18 +84,10 @@ describe('React', () => {
84
84
  '<section><feaas-component library="library" component="test" data="{&quot;abc&quot;:321}"><div data-path-src="abc"></div></feaas-component></section>'
85
85
  )
86
86
  })
87
- it('render template with data with src and stringy data', async () => {
87
+ it('render template with style attribute', async () => {
88
88
  test(
89
89
  <section>
90
- <Component src='library/test' template={'<div data-path-src="abc">'} data={'{"abc":321}'}></Component>
91
- </section>,
92
- '<section><feaas-component library="library" component="test" src="library/test" data="{&quot;abc&quot;:321}"><div data-path-src="abc"></div></feaas-component></section>'
93
- )
94
- })
95
- it('render template with data and template', async () => {
96
- test(
97
- <section>
98
- <Component template={'<div data-path-src="abc">'} data={'{"abc":321}'}></Component>
90
+ <Component template={'<div data-path-src="abc">'} data={{ abc: 321 }}></Component>
99
91
  </section>,
100
92
  '<section><feaas-component data="{&quot;abc&quot;:321}"><div data-path-src="abc"></div></feaas-component></section>'
101
93
  )
@@ -53,6 +53,20 @@ describe('React Server', () => {
53
53
  `<feaas-component library="library" component="test" src="library/test" data="{&quot;value&quot;:0}">Hey: <var data-path="value">0</var>!</feaas-component>`
54
54
  )
55
55
  })
56
+ it('pre-render data with style attribute', async () => {
57
+ vi.spyOn(global, 'fetch').mockImplementation(
58
+ async (url: string, options: Parameters<typeof fetch>[1] = {}) =>
59
+ new Response(
60
+ '<p style="text-align: left; color: red;;"><b style="cursor: 1">Hey:</b> <var data-path="value"></var>!</p>'
61
+ )
62
+ )
63
+
64
+ const Component = await ServerComponent({ src: 'library/test', data: { value: 0 } })
65
+ await test(
66
+ <>{Component}</>,
67
+ `<feaas-component library="library" component="test" src="library/test" data="{&quot;value&quot;:0}"><p style="text-align: left; color: red;"><b style="cursor: 1;">Hey:</b> <var data-path="value">0</var>!</p></feaas-component>`
68
+ )
69
+ })
56
70
  it('render external component', async () => {
57
71
  function MyComponent() {
58
72
  return <h1>My Component</h1>