@teleporthq/teleport-plugin-common 0.45.3-alpha.0 → 0.45.13
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/__tests__/builders/html-builders.ts +9 -2
- package/__tests__/builders/style-builders.ts +135 -1
- package/__tests__/node-handlers/node-to-html/index.ts +2 -0
- package/__tests__/node-handlers/node-to-jsx/attribute-value-mapper.ts +77 -0
- package/__tests__/node-handlers/node-to-jsx/calendar-events-reviver.ts +176 -0
- package/__tests__/node-handlers/node-to-jsx/chart-data-guard.ts +101 -0
- package/__tests__/node-handlers/node-to-jsx/cms-list-repeater-ecommerce-source.ts +79 -0
- package/__tests__/node-handlers/node-to-jsx/cms-list-repeater-loading.ts +122 -0
- package/__tests__/node-handlers/node-to-jsx/index.ts +719 -3
- package/__tests__/node-handlers/node-to-jsx/url-search-params-expression.ts +59 -0
- package/__tests__/node-handlers/node-to-jsx/utils.ts +635 -3
- package/__tests__/utils/ast-utils.ts +188 -0
- package/__tests__/utils/content-property-sanitiser.ts +156 -0
- package/__tests__/utils/page-seeds-state-from-data-source.ts +70 -0
- package/__tests__/utils/parse-string-with-template-expressions.ts +82 -0
- package/__tests__/utils/resource-auth-scheme.ts +55 -0
- package/__tests__/utils/route-utils.ts +118 -0
- package/__tests__/utils/style-utils.ts +59 -0
- package/__tests__/utils/template-expression-balance.ts +83 -0
- package/__tests__/utils/url-query-writer.ts +239 -0
- package/__tests__/utils/url-search-param-sync.ts +138 -0
- package/__tests__/utils/url-search-params.ts +48 -0
- package/dist/cjs/builders/ast-builders.d.ts +2 -0
- package/dist/cjs/builders/ast-builders.d.ts.map +1 -1
- package/dist/cjs/builders/ast-builders.js +41 -3
- package/dist/cjs/builders/ast-builders.js.map +1 -1
- package/dist/cjs/builders/hast-builders.d.ts +1 -0
- package/dist/cjs/builders/hast-builders.d.ts.map +1 -1
- package/dist/cjs/builders/hast-builders.js +11 -1
- package/dist/cjs/builders/hast-builders.js.map +1 -1
- package/dist/cjs/builders/style-builders.d.ts +8 -2
- package/dist/cjs/builders/style-builders.d.ts.map +1 -1
- package/dist/cjs/builders/style-builders.js +80 -33
- package/dist/cjs/builders/style-builders.js.map +1 -1
- package/dist/cjs/index.d.ts +5 -1
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +10 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/constants.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/constants.js +2 -0
- package/dist/cjs/node-handlers/node-to-html/constants.js.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/index.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/index.js +69 -1
- package/dist/cjs/node-handlers/node-to-html/index.js.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/types.d.ts +5 -1
- package/dist/cjs/node-handlers/node-to-html/types.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/utils.d.ts +2 -1
- package/dist/cjs/node-handlers/node-to-html/utils.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/utils.js +174 -13
- package/dist/cjs/node-handlers/node-to-html/utils.js.map +1 -1
- package/dist/cjs/node-handlers/node-to-jsx/index.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-jsx/index.js +1443 -45
- package/dist/cjs/node-handlers/node-to-jsx/index.js.map +1 -1
- package/dist/cjs/node-handlers/node-to-jsx/types.d.ts +60 -6
- package/dist/cjs/node-handlers/node-to-jsx/types.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-jsx/utils.d.ts +80 -7
- package/dist/cjs/node-handlers/node-to-jsx/utils.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-jsx/utils.js +883 -72
- package/dist/cjs/node-handlers/node-to-jsx/utils.js.map +1 -1
- package/dist/cjs/tsconfig.tsbuildinfo +1 -1
- package/dist/cjs/utils/ast-utils.d.ts +163 -11
- package/dist/cjs/utils/ast-utils.d.ts.map +1 -1
- package/dist/cjs/utils/ast-utils.js +1013 -75
- package/dist/cjs/utils/ast-utils.js.map +1 -1
- package/dist/cjs/utils/hast-utils.d.ts +8 -6
- package/dist/cjs/utils/hast-utils.d.ts.map +1 -1
- package/dist/cjs/utils/hast-utils.js +28 -4
- package/dist/cjs/utils/hast-utils.js.map +1 -1
- package/dist/cjs/utils/nested-selector-commas.d.ts +2 -0
- package/dist/cjs/utils/nested-selector-commas.d.ts.map +1 -0
- package/dist/cjs/utils/nested-selector-commas.js +81 -0
- package/dist/cjs/utils/nested-selector-commas.js.map +1 -0
- package/dist/cjs/utils/route-utils.d.ts +117 -0
- package/dist/cjs/utils/route-utils.d.ts.map +1 -0
- package/dist/cjs/utils/route-utils.js +212 -0
- package/dist/cjs/utils/route-utils.js.map +1 -0
- package/dist/cjs/utils/style-utils.d.ts +1 -0
- package/dist/cjs/utils/style-utils.d.ts.map +1 -1
- package/dist/cjs/utils/style-utils.js +181 -2
- package/dist/cjs/utils/style-utils.js.map +1 -1
- package/dist/cjs/utils/template-expression-balance.d.ts +58 -0
- package/dist/cjs/utils/template-expression-balance.d.ts.map +1 -0
- package/dist/cjs/utils/template-expression-balance.js +181 -0
- package/dist/cjs/utils/template-expression-balance.js.map +1 -0
- package/dist/cjs/utils/types.d.ts +3 -0
- package/dist/cjs/utils/types.d.ts.map +1 -0
- package/dist/cjs/utils/types.js +3 -0
- package/dist/cjs/utils/types.js.map +1 -0
- package/dist/cjs/utils/url-query-writer.d.ts +123 -0
- package/dist/cjs/utils/url-query-writer.d.ts.map +1 -0
- package/dist/cjs/utils/url-query-writer.js +260 -0
- package/dist/cjs/utils/url-query-writer.js.map +1 -0
- package/dist/cjs/utils/url-search-param-sync.d.ts +4 -0
- package/dist/cjs/utils/url-search-param-sync.d.ts.map +1 -0
- package/dist/cjs/utils/url-search-param-sync.js +241 -0
- package/dist/cjs/utils/url-search-param-sync.js.map +1 -0
- package/dist/cjs/utils/url-search-params.d.ts +48 -0
- package/dist/cjs/utils/url-search-params.d.ts.map +1 -0
- package/dist/cjs/utils/url-search-params.js +109 -0
- package/dist/cjs/utils/url-search-params.js.map +1 -0
- package/dist/esm/builders/ast-builders.d.ts +2 -0
- package/dist/esm/builders/ast-builders.d.ts.map +1 -1
- package/dist/esm/builders/ast-builders.js +40 -4
- package/dist/esm/builders/ast-builders.js.map +1 -1
- package/dist/esm/builders/hast-builders.d.ts +1 -0
- package/dist/esm/builders/hast-builders.d.ts.map +1 -1
- package/dist/esm/builders/hast-builders.js +9 -0
- package/dist/esm/builders/hast-builders.js.map +1 -1
- package/dist/esm/builders/style-builders.d.ts +8 -2
- package/dist/esm/builders/style-builders.d.ts.map +1 -1
- package/dist/esm/builders/style-builders.js +78 -32
- package/dist/esm/builders/style-builders.js.map +1 -1
- package/dist/esm/index.d.ts +5 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +5 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/node-handlers/node-to-html/constants.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-html/constants.js +2 -0
- package/dist/esm/node-handlers/node-to-html/constants.js.map +1 -1
- package/dist/esm/node-handlers/node-to-html/index.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-html/index.js +71 -3
- package/dist/esm/node-handlers/node-to-html/index.js.map +1 -1
- package/dist/esm/node-handlers/node-to-html/types.d.ts +5 -1
- package/dist/esm/node-handlers/node-to-html/types.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-html/utils.d.ts +2 -1
- package/dist/esm/node-handlers/node-to-html/utils.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-html/utils.js +170 -13
- package/dist/esm/node-handlers/node-to-html/utils.js.map +1 -1
- package/dist/esm/node-handlers/node-to-jsx/index.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-jsx/index.js +1442 -47
- package/dist/esm/node-handlers/node-to-jsx/index.js.map +1 -1
- package/dist/esm/node-handlers/node-to-jsx/types.d.ts +60 -6
- package/dist/esm/node-handlers/node-to-jsx/types.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-jsx/utils.d.ts +80 -7
- package/dist/esm/node-handlers/node-to-jsx/utils.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-jsx/utils.js +875 -70
- package/dist/esm/node-handlers/node-to-jsx/utils.js.map +1 -1
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/dist/esm/utils/ast-utils.d.ts +163 -11
- package/dist/esm/utils/ast-utils.d.ts.map +1 -1
- package/dist/esm/utils/ast-utils.js +989 -75
- package/dist/esm/utils/ast-utils.js.map +1 -1
- package/dist/esm/utils/hast-utils.d.ts +8 -6
- package/dist/esm/utils/hast-utils.d.ts.map +1 -1
- package/dist/esm/utils/hast-utils.js +26 -3
- package/dist/esm/utils/hast-utils.js.map +1 -1
- package/dist/esm/utils/nested-selector-commas.d.ts +2 -0
- package/dist/esm/utils/nested-selector-commas.d.ts.map +1 -0
- package/dist/esm/utils/nested-selector-commas.js +77 -0
- package/dist/esm/utils/nested-selector-commas.js.map +1 -0
- package/dist/esm/utils/route-utils.d.ts +117 -0
- package/dist/esm/utils/route-utils.d.ts.map +1 -0
- package/dist/esm/utils/route-utils.js +179 -0
- package/dist/esm/utils/route-utils.js.map +1 -0
- package/dist/esm/utils/style-utils.d.ts +1 -0
- package/dist/esm/utils/style-utils.d.ts.map +1 -1
- package/dist/esm/utils/style-utils.js +179 -1
- package/dist/esm/utils/style-utils.js.map +1 -1
- package/dist/esm/utils/template-expression-balance.d.ts +58 -0
- package/dist/esm/utils/template-expression-balance.d.ts.map +1 -0
- package/dist/esm/utils/template-expression-balance.js +174 -0
- package/dist/esm/utils/template-expression-balance.js.map +1 -0
- package/dist/esm/utils/types.d.ts +3 -0
- package/dist/esm/utils/types.d.ts.map +1 -0
- package/dist/esm/utils/types.js +2 -0
- package/dist/esm/utils/types.js.map +1 -0
- package/dist/esm/utils/url-query-writer.d.ts +123 -0
- package/dist/esm/utils/url-query-writer.d.ts.map +1 -0
- package/dist/esm/utils/url-query-writer.js +229 -0
- package/dist/esm/utils/url-query-writer.js.map +1 -0
- package/dist/esm/utils/url-search-param-sync.d.ts +4 -0
- package/dist/esm/utils/url-search-param-sync.d.ts.map +1 -0
- package/dist/esm/utils/url-search-param-sync.js +213 -0
- package/dist/esm/utils/url-search-param-sync.js.map +1 -0
- package/dist/esm/utils/url-search-params.d.ts +48 -0
- package/dist/esm/utils/url-search-params.d.ts.map +1 -0
- package/dist/esm/utils/url-search-params.js +81 -0
- package/dist/esm/utils/url-search-params.js.map +1 -0
- package/package.json +4 -4
- package/src/builders/ast-builders.ts +80 -3
- package/src/builders/hast-builders.ts +11 -0
- package/src/builders/style-builders.ts +121 -43
- package/src/index.ts +22 -1
- package/src/node-handlers/node-to-html/constants.ts +2 -0
- package/src/node-handlers/node-to-html/index.ts +126 -6
- package/src/node-handlers/node-to-html/types.ts +6 -0
- package/src/node-handlers/node-to-html/utils.ts +228 -14
- package/src/node-handlers/node-to-jsx/index.ts +2135 -62
- package/src/node-handlers/node-to-jsx/types.ts +75 -32
- package/src/node-handlers/node-to-jsx/utils.ts +1173 -84
- package/src/utils/ast-utils.ts +1316 -127
- package/src/utils/hast-utils.ts +37 -9
- package/src/utils/nested-selector-commas.ts +88 -0
- package/src/utils/route-utils.ts +214 -0
- package/src/utils/style-utils.ts +183 -1
- package/src/utils/template-expression-balance.ts +196 -0
- package/src/utils/types.ts +25 -0
- package/src/utils/url-query-writer.ts +318 -0
- package/src/utils/url-search-param-sync.ts +303 -0
- package/src/utils/url-search-params.ts +122 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as hastUtils from '../../utils/hast-utils'
|
|
2
|
-
import { createHTMLNode } from '../../builders/hast-builders'
|
|
2
|
+
import { createComment, createHTMLNode } from '../../builders/hast-builders'
|
|
3
3
|
import { UIDLUtils, StringUtils } from '@teleporthq/teleport-shared'
|
|
4
4
|
import {
|
|
5
5
|
UIDLRepeatNode,
|
|
@@ -8,8 +8,16 @@ import {
|
|
|
8
8
|
UIDLNode,
|
|
9
9
|
UIDLElementNode,
|
|
10
10
|
HastNode,
|
|
11
|
+
HastText,
|
|
12
|
+
UIDLDynamicReference,
|
|
13
|
+
UIDLRawValue,
|
|
11
14
|
} from '@teleporthq/teleport-types'
|
|
12
|
-
import {
|
|
15
|
+
import {
|
|
16
|
+
createConditionalStatement,
|
|
17
|
+
createInlineConditionalStatement,
|
|
18
|
+
handleAttribute,
|
|
19
|
+
handleEvent,
|
|
20
|
+
} from './utils'
|
|
13
21
|
import { NodeToHTML } from './types'
|
|
14
22
|
import { DEFAULT_TEMPLATE_SYNTAX } from './constants'
|
|
15
23
|
|
|
@@ -80,13 +88,28 @@ const generateElementNode: NodeToHTML<UIDLElementNode, HastNode> = (node, params
|
|
|
80
88
|
})
|
|
81
89
|
}
|
|
82
90
|
|
|
91
|
+
const renderingConditions = node.content.renderingConditions
|
|
92
|
+
if (renderingConditions) {
|
|
93
|
+
const statement = createInlineConditionalStatement(
|
|
94
|
+
renderingConditions.reference,
|
|
95
|
+
renderingConditions.condition
|
|
96
|
+
)
|
|
97
|
+
if (statement) {
|
|
98
|
+
hastUtils.addAttributeToNode(htmlNode, templateSyntax.conditionalAttr, statement)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
83
102
|
templateLookup[key] = htmlNode
|
|
84
103
|
return htmlNode
|
|
85
104
|
}
|
|
86
105
|
|
|
87
106
|
export default generateElementNode
|
|
88
107
|
|
|
89
|
-
const generateNode: NodeToHTML<UIDLNode, HastNode | string> = (
|
|
108
|
+
const generateNode: NodeToHTML<UIDLNode, HastNode | HastText | string> = (
|
|
109
|
+
node,
|
|
110
|
+
params,
|
|
111
|
+
templateSyntax
|
|
112
|
+
) => {
|
|
90
113
|
switch (node.type) {
|
|
91
114
|
case 'inject':
|
|
92
115
|
if (node?.dependency) {
|
|
@@ -102,7 +125,7 @@ const generateNode: NodeToHTML<UIDLNode, HastNode | string> = (node, params, tem
|
|
|
102
125
|
return StringUtils.encode(node.content.toString())
|
|
103
126
|
|
|
104
127
|
case 'dynamic':
|
|
105
|
-
return
|
|
128
|
+
return generateDynamicNode(node, params, templateSyntax)
|
|
106
129
|
|
|
107
130
|
case 'element':
|
|
108
131
|
return generateElementNode(node, params, templateSyntax)
|
|
@@ -116,6 +139,10 @@ const generateNode: NodeToHTML<UIDLNode, HastNode | string> = (node, params, tem
|
|
|
116
139
|
case 'slot':
|
|
117
140
|
return generateSlotNode(node, params, templateSyntax)
|
|
118
141
|
|
|
142
|
+
case 'expr':
|
|
143
|
+
return 'Expression nodes are not supported'
|
|
144
|
+
case 'cms-list-repeater':
|
|
145
|
+
return 'CMS repeater nodes are not supported'
|
|
119
146
|
default:
|
|
120
147
|
throw new Error(
|
|
121
148
|
`generateHTMLSyntax encountered a node of unsupported type: ${JSON.stringify(
|
|
@@ -127,9 +154,90 @@ const generateNode: NodeToHTML<UIDLNode, HastNode | string> = (node, params, tem
|
|
|
127
154
|
}
|
|
128
155
|
}
|
|
129
156
|
|
|
157
|
+
const generateDynamicNode: NodeToHTML<UIDLDynamicReference, HastNode | HastText | string> = (
|
|
158
|
+
node,
|
|
159
|
+
params,
|
|
160
|
+
templateSyntax
|
|
161
|
+
) => {
|
|
162
|
+
if (
|
|
163
|
+
node.content.referenceType === 'global' ||
|
|
164
|
+
(node.content.referenceType as string) === 'globalState'
|
|
165
|
+
) {
|
|
166
|
+
const spanNode = createHTMLNode('span')
|
|
167
|
+
return spanNode
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const usedPropType = params.propDefinitions[node.content.id]
|
|
171
|
+
if (usedPropType && usedPropType.type === 'element') {
|
|
172
|
+
let slotNode = createHTMLNode('slot')
|
|
173
|
+
const commentNode = createComment(`Default content for ${node.content.id}`)
|
|
174
|
+
hastUtils.addChildNode(slotNode, commentNode)
|
|
175
|
+
|
|
176
|
+
// Named slot handling for VueJS
|
|
177
|
+
if (templateSyntax.slotBinding === 'v-slot') {
|
|
178
|
+
hastUtils.addAttributeToNode(slotNode, 'name', node.content.id)
|
|
179
|
+
if (usedPropType.defaultValue) {
|
|
180
|
+
const defaultSlotContent = generateNode(
|
|
181
|
+
usedPropType.defaultValue as UIDLElementNode,
|
|
182
|
+
params,
|
|
183
|
+
templateSyntax
|
|
184
|
+
) as HastNode
|
|
185
|
+
hastUtils.addChildNode(slotNode, defaultSlotContent)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return slotNode
|
|
189
|
+
} else {
|
|
190
|
+
const slotWrapper = createHTMLNode('ng-container')
|
|
191
|
+
slotNode = createHTMLNode('ng-container')
|
|
192
|
+
|
|
193
|
+
if (usedPropType.defaultValue) {
|
|
194
|
+
const defaultSlotTemplateName = StringUtils.dashCaseToCamelCase(
|
|
195
|
+
`default-${node.content.id}`
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
hastUtils.addAttributeToNode(
|
|
199
|
+
slotNode,
|
|
200
|
+
'*ngTemplateOutlet',
|
|
201
|
+
`${node.content.id} ? ${node.content.id} : ${defaultSlotTemplateName}`
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
const templateNode = createHTMLNode('ng-template')
|
|
205
|
+
hastUtils.addBooleanAttributeToNode(templateNode, `#${defaultSlotTemplateName}`)
|
|
206
|
+
|
|
207
|
+
const defaultSlotContent = generateNode(
|
|
208
|
+
usedPropType.defaultValue as UIDLElementNode,
|
|
209
|
+
params,
|
|
210
|
+
templateSyntax
|
|
211
|
+
) as HastNode
|
|
212
|
+
hastUtils.addChildNode(templateNode, defaultSlotContent)
|
|
213
|
+
hastUtils.addChildNode(slotWrapper, templateNode)
|
|
214
|
+
} else {
|
|
215
|
+
hastUtils.addAttributeToNode(slotNode, '*ngTemplateOutlet', node.content.id)
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
hastUtils.addChildNode(slotWrapper, slotNode)
|
|
219
|
+
return slotWrapper
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return templateSyntax.interpolation(node.content.id)
|
|
224
|
+
}
|
|
225
|
+
|
|
130
226
|
const generateRawHTMLNode: NodeToHTML<UIDLNode, HastNode> = (node, params, templateSyntax) => {
|
|
131
227
|
const attrKey = templateSyntax.domHTMLInjection ? templateSyntax.domHTMLInjection : 'innerHTML'
|
|
132
228
|
const htmlNode = createHTMLNode('span')
|
|
229
|
+
const rawNode = node as UIDLRawValue
|
|
230
|
+
|
|
231
|
+
if (rawNode.dynamic) {
|
|
232
|
+
const { id, refPath } = rawNode.dynamic.content
|
|
233
|
+
const idWithPath = UIDLUtils.generateIdWithRefPath(id, refPath)
|
|
234
|
+
const fallbackContent = rawNode.fallback || rawNode.content
|
|
235
|
+
const fallbackVarName = `htmlFallback${StringUtils.generateRandomString()}`
|
|
236
|
+
params.dataObject[fallbackVarName] = fallbackContent
|
|
237
|
+
hastUtils.addAttributeToNode(htmlNode, attrKey, `${idWithPath} || ${fallbackVarName}`)
|
|
238
|
+
return htmlNode
|
|
239
|
+
}
|
|
240
|
+
|
|
133
241
|
const dataObjName = `${node.type}${StringUtils.generateRandomString()}`
|
|
134
242
|
hastUtils.addAttributeToNode(htmlNode, attrKey, dataObjName)
|
|
135
243
|
params.dataObject[dataObjName] = node.content.toString()
|
|
@@ -159,12 +267,20 @@ const generateRepeatNode: NodeToHTML<UIDLRepeatNode, HastNode> = (node, params,
|
|
|
159
267
|
return repeatContentTag
|
|
160
268
|
}
|
|
161
269
|
|
|
162
|
-
const generateConditionalNode: NodeToHTML<UIDLConditionalNode, HastNode> = (
|
|
270
|
+
const generateConditionalNode: NodeToHTML<UIDLConditionalNode, HastNode | HastText> = (
|
|
163
271
|
node,
|
|
164
272
|
params,
|
|
165
273
|
templateSyntax
|
|
166
274
|
) => {
|
|
167
275
|
let conditionalTag = generateNode(node.content.node, params, templateSyntax)
|
|
276
|
+
|
|
277
|
+
// We can add attributes only to tags, so in case of a text node we wrap it with a 'span'
|
|
278
|
+
if (typeof conditionalTag === 'object' && !('properties' in conditionalTag)) {
|
|
279
|
+
const wrappingSpan = createHTMLNode('span')
|
|
280
|
+
hastUtils.addTextNode(wrappingSpan, conditionalTag.value)
|
|
281
|
+
conditionalTag = wrappingSpan
|
|
282
|
+
}
|
|
283
|
+
|
|
168
284
|
// conditional attribute needs to be added on a tag, so in case of a text node we wrap it with
|
|
169
285
|
// a 'span' which is the less intrusive of all
|
|
170
286
|
if (typeof conditionalTag === 'string') {
|
|
@@ -178,7 +294,11 @@ const generateConditionalNode: NodeToHTML<UIDLConditionalNode, HastNode> = (
|
|
|
178
294
|
return conditionalTag
|
|
179
295
|
}
|
|
180
296
|
|
|
181
|
-
const generateSlotNode: NodeToHTML<UIDLSlotNode, HastNode> = (
|
|
297
|
+
const generateSlotNode: NodeToHTML<UIDLSlotNode, HastNode | HastText> = (
|
|
298
|
+
node,
|
|
299
|
+
params,
|
|
300
|
+
templateSyntax
|
|
301
|
+
) => {
|
|
182
302
|
const slotNode = createHTMLNode('slot')
|
|
183
303
|
|
|
184
304
|
if (node.content.name) {
|
|
@@ -2,6 +2,8 @@ import {
|
|
|
2
2
|
UIDLDependency,
|
|
3
3
|
UIDLEventHandlerStatement,
|
|
4
4
|
UIDLElementNode,
|
|
5
|
+
UIDLPropDefinition,
|
|
6
|
+
UIDLStateDefinition,
|
|
5
7
|
} from '@teleporthq/teleport-types'
|
|
6
8
|
|
|
7
9
|
export interface HTMLTemplateGenerationParams {
|
|
@@ -11,6 +13,8 @@ export interface HTMLTemplateGenerationParams {
|
|
|
11
13
|
// tslint:disable-next-line no-any
|
|
12
14
|
dataObject: Record<string, any>
|
|
13
15
|
methodsObject: Record<string, UIDLEventHandlerStatement[]>
|
|
16
|
+
propDefinitions: Record<string, UIDLPropDefinition>
|
|
17
|
+
stateDefinitions: Record<string, UIDLStateDefinition>
|
|
14
18
|
}
|
|
15
19
|
|
|
16
20
|
export interface HTMLTemplateSyntax {
|
|
@@ -25,6 +29,8 @@ export interface HTMLTemplateSyntax {
|
|
|
25
29
|
customElementTagName?: (value: string) => string
|
|
26
30
|
dependencyHandling?: 'import' | 'ignore'
|
|
27
31
|
domHTMLInjection?: string
|
|
32
|
+
slotBinding?: string
|
|
33
|
+
slotTagName?: string
|
|
28
34
|
}
|
|
29
35
|
|
|
30
36
|
export type NodeToHTML<NodeType, ReturnType> = (
|
|
@@ -1,14 +1,21 @@
|
|
|
1
1
|
import * as hastUtils from '../../utils/hast-utils'
|
|
2
|
-
import { StringUtils } from '@teleporthq/teleport-shared'
|
|
2
|
+
import { StringUtils, UIDLUtils } from '@teleporthq/teleport-shared'
|
|
3
3
|
import {
|
|
4
4
|
UIDLConditionalExpression,
|
|
5
|
+
UIDLConditionExpressionEntry,
|
|
6
|
+
UIDLConditionExpressionGroup,
|
|
5
7
|
UIDLConditionalNode,
|
|
6
8
|
HastNode,
|
|
7
9
|
UIDLAttributeValue,
|
|
8
10
|
UIDLEventHandlerStatement,
|
|
9
11
|
UIDLElementNode,
|
|
12
|
+
UIDLExpressionValue,
|
|
13
|
+
UIDLDynamicReference,
|
|
14
|
+
UIDLRawValue,
|
|
10
15
|
} from '@teleporthq/teleport-types'
|
|
11
16
|
import { HTMLTemplateGenerationParams, HTMLTemplateSyntax } from './types'
|
|
17
|
+
import { createHTMLNode } from '../../builders/hast-builders'
|
|
18
|
+
import generateElementNode from '../node-to-html'
|
|
12
19
|
|
|
13
20
|
export const handleAttribute = (
|
|
14
21
|
htmlNode: HastNode,
|
|
@@ -33,9 +40,15 @@ export const handleAttribute = (
|
|
|
33
40
|
StringUtils.encode(attrValue.content.toString())
|
|
34
41
|
)
|
|
35
42
|
break
|
|
36
|
-
case 'raw':
|
|
43
|
+
case 'raw': {
|
|
44
|
+
const rawValue = attrValue as UIDLRawValue
|
|
45
|
+
if (rawValue.dynamic) {
|
|
46
|
+
applyDynamicHtmlDirective(htmlNode, rawValue, params, templateSyntax, node)
|
|
47
|
+
break
|
|
48
|
+
}
|
|
37
49
|
hastUtils.addAttributeToNode(htmlNode, attrKey, attrValue.content.toString())
|
|
38
50
|
break
|
|
51
|
+
}
|
|
39
52
|
case 'static':
|
|
40
53
|
if (Array.isArray(attrValue.content)) {
|
|
41
54
|
// This handles the cases when arrays are sent as props or passed as attributes
|
|
@@ -58,6 +71,35 @@ export const handleAttribute = (
|
|
|
58
71
|
hastUtils.addAttributeToNode(htmlNode, dynamicAttrKey, attrValue.content.toString())
|
|
59
72
|
}
|
|
60
73
|
break
|
|
74
|
+
|
|
75
|
+
case 'element':
|
|
76
|
+
const templateNode = createHTMLNode(templateSyntax.slotTagName)
|
|
77
|
+
const templateContent = generateElementNode(attrValue, params, templateSyntax)
|
|
78
|
+
|
|
79
|
+
if (templateSyntax.slotBinding === 'v-slot') {
|
|
80
|
+
hastUtils.addBooleanAttributeToNode(
|
|
81
|
+
templateNode,
|
|
82
|
+
`${templateSyntax.slotBinding}:${attrKey}`
|
|
83
|
+
)
|
|
84
|
+
} else {
|
|
85
|
+
hastUtils.addBooleanAttributeToNode(templateNode, `${templateSyntax.slotBinding}${attrKey}`)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
hastUtils.addChildNode(templateNode, templateContent)
|
|
89
|
+
hastUtils.addChildNode(htmlNode, templateNode)
|
|
90
|
+
break
|
|
91
|
+
|
|
92
|
+
case 'object': {
|
|
93
|
+
dataObject[attrKey] = attrValue.content
|
|
94
|
+
hastUtils.addAttributeToNode(htmlNode, dynamicAttrKey, attrKey)
|
|
95
|
+
break
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
case 'expr':
|
|
99
|
+
// TODO: Check this in the future. Not throwing an error for now
|
|
100
|
+
console.info(`Expressions are not supported in HTML templates`)
|
|
101
|
+
break
|
|
102
|
+
|
|
61
103
|
default:
|
|
62
104
|
throw new Error(
|
|
63
105
|
`generateElementNode could not generate code for attribute of type ${JSON.stringify(
|
|
@@ -112,13 +154,33 @@ export const createConditionalStatement = (node: UIDLConditionalNode): string =>
|
|
|
112
154
|
const { node: childNode, reference, value, condition } = node.content
|
|
113
155
|
|
|
114
156
|
const expression = standardizeUIDLConditionalExpression(value, condition)
|
|
115
|
-
|
|
157
|
+
if (reference.type === 'dynamic') {
|
|
158
|
+
const statement = createConditional(reference.content.id, expression)
|
|
159
|
+
|
|
160
|
+
if (childNode.type === 'conditional') {
|
|
161
|
+
return `${statement} && ${createConditionalStatement(childNode)}`
|
|
162
|
+
}
|
|
116
163
|
|
|
117
|
-
|
|
118
|
-
return `${statement} && ${createConditionalStatement(childNode)}`
|
|
164
|
+
return statement
|
|
119
165
|
}
|
|
120
166
|
|
|
121
|
-
|
|
167
|
+
// expression nodes are not supported in html based templates.
|
|
168
|
+
if (reference.type === 'expr') {
|
|
169
|
+
return ''
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Used by the inline `renderingConditions` path on UIDLElement — identical
|
|
174
|
+
// semantics to `createConditionalStatement` except the reference / condition
|
|
175
|
+
// are supplied directly rather than extracted from a UIDLConditionalNode.
|
|
176
|
+
export const createInlineConditionalStatement = (
|
|
177
|
+
reference: UIDLConditionalNode['content']['reference'],
|
|
178
|
+
condition: UIDLConditionalExpression
|
|
179
|
+
): string => {
|
|
180
|
+
if (reference.type === 'dynamic') {
|
|
181
|
+
return createConditional(reference.content.id, condition)
|
|
182
|
+
}
|
|
183
|
+
return ''
|
|
122
184
|
}
|
|
123
185
|
|
|
124
186
|
const standardizeUIDLConditionalExpression = (
|
|
@@ -132,30 +194,145 @@ const standardizeUIDLConditionalExpression = (
|
|
|
132
194
|
return conditionalExpression
|
|
133
195
|
}
|
|
134
196
|
|
|
197
|
+
// An entry carrying its own `reference` compares that reference instead of the
|
|
198
|
+
// expression's inherited key. Dynamic refs contribute their id; expr refs
|
|
199
|
+
// contribute their raw expression, parenthesized (this flavor works in string
|
|
200
|
+
// space, so the expression text slots in as the left side directly).
|
|
201
|
+
const resolveConditionEntryKey = (
|
|
202
|
+
entry: UIDLConditionExpressionEntry,
|
|
203
|
+
inheritedKey: string
|
|
204
|
+
): string => {
|
|
205
|
+
if (!entry.reference) {
|
|
206
|
+
return inheritedKey
|
|
207
|
+
}
|
|
208
|
+
if (entry.reference.type === 'dynamic') {
|
|
209
|
+
return entry.reference.content.id
|
|
210
|
+
}
|
|
211
|
+
return `(${entry.reference.content})`
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// A group entry recurses with its own matchingCriteria and comes back already
|
|
215
|
+
// parenthesized; a leaf stringifies its single comparison unwrapped.
|
|
216
|
+
const stringifyConditionEntry = (
|
|
217
|
+
entry: UIDLConditionExpressionEntry | UIDLConditionExpressionGroup,
|
|
218
|
+
inheritedKey: string
|
|
219
|
+
): string => {
|
|
220
|
+
if (UIDLUtils.isUIDLConditionGroup(entry)) {
|
|
221
|
+
return `(${createConditional(inheritedKey, entry)})`
|
|
222
|
+
}
|
|
223
|
+
return stringifyConditionalExpression(
|
|
224
|
+
resolveConditionEntryKey(entry, inheritedKey),
|
|
225
|
+
entry.operation,
|
|
226
|
+
entry.operand,
|
|
227
|
+
entry.containsField
|
|
228
|
+
)
|
|
229
|
+
}
|
|
230
|
+
|
|
135
231
|
const createConditional = (
|
|
136
232
|
conditionalKey: string,
|
|
137
|
-
conditionalExpression: UIDLConditionalExpression
|
|
138
|
-
) => {
|
|
233
|
+
conditionalExpression: UIDLConditionalExpression | UIDLConditionExpressionGroup
|
|
234
|
+
): string => {
|
|
139
235
|
const { matchingCriteria, conditions } = conditionalExpression
|
|
140
236
|
if (conditions.length === 1) {
|
|
141
|
-
|
|
142
|
-
const { operation, operand } = conditions[0]
|
|
143
|
-
return stringifyConditionalExpression(conditionalKey, operation, operand)
|
|
237
|
+
return stringifyConditionEntry(conditions[0], conditionalKey)
|
|
144
238
|
}
|
|
145
239
|
|
|
146
|
-
const stringConditions = conditions.map((
|
|
147
|
-
|
|
240
|
+
const stringConditions = conditions.map((conditionEntry) => {
|
|
241
|
+
const entryText = stringifyConditionEntry(conditionEntry, conditionalKey)
|
|
242
|
+
// Groups arrive parenthesized already; leaves get wrapped here.
|
|
243
|
+
return UIDLUtils.isUIDLConditionGroup(conditionEntry) ? entryText : `(${entryText})`
|
|
148
244
|
})
|
|
149
245
|
|
|
150
246
|
const joinOperator = matchingCriteria === 'all' ? '&&' : '||'
|
|
151
247
|
return stringConditions.join(` ${joinOperator} `)
|
|
152
248
|
}
|
|
153
249
|
|
|
250
|
+
const stringifyOperandValue = (
|
|
251
|
+
value: string | number | boolean | UIDLDynamicReference | UIDLExpressionValue
|
|
252
|
+
): string => {
|
|
253
|
+
if (typeof value === 'string') {
|
|
254
|
+
return `'${value}'`
|
|
255
|
+
}
|
|
256
|
+
if (typeof value === 'number' || typeof value === 'boolean') {
|
|
257
|
+
return String(value)
|
|
258
|
+
}
|
|
259
|
+
if (typeof value === 'object' && 'type' in value) {
|
|
260
|
+
if (value.type === 'dynamic') {
|
|
261
|
+
return value.content.id
|
|
262
|
+
}
|
|
263
|
+
if (value.type === 'expr') {
|
|
264
|
+
return value.content
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return String(value)
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Collection operators dereference `.length` / `.includes` on the bound value,
|
|
272
|
+
* which is nullish far more often than it looks (an optional CMS column, a
|
|
273
|
+
* category without an image, a not-yet-hydrated state). Coercing to an empty
|
|
274
|
+
* collection first keeps a hide-this-node condition from throwing
|
|
275
|
+
* `Cannot read properties of null` mid-render. A non-empty string stays
|
|
276
|
+
* truthy, so string semantics are preserved. Mirrors `createNullSafeCollection`
|
|
277
|
+
* in the JSX handler — keep the two in sync.
|
|
278
|
+
*/
|
|
279
|
+
const nullSafeCollection = (identifier: string, empty: '[]' | '{}' = '[]') =>
|
|
280
|
+
`(${identifier} || ${empty})`
|
|
281
|
+
|
|
154
282
|
const stringifyConditionalExpression = (
|
|
155
283
|
identifier: string,
|
|
156
284
|
operation: string,
|
|
157
|
-
value: string | number | boolean
|
|
285
|
+
value: string | number | boolean | UIDLDynamicReference | UIDLExpressionValue,
|
|
286
|
+
containsField?: string
|
|
158
287
|
) => {
|
|
288
|
+
// Array/object operators
|
|
289
|
+
if (operation === 'isEmpty') {
|
|
290
|
+
return `${nullSafeCollection(identifier)}.length === 0`
|
|
291
|
+
}
|
|
292
|
+
if (operation === 'isNotEmpty') {
|
|
293
|
+
return `${nullSafeCollection(identifier)}.length > 0`
|
|
294
|
+
}
|
|
295
|
+
if (operation === 'lengthEquals') {
|
|
296
|
+
return `${nullSafeCollection(identifier)}.length === ${stringifyOperandValue(value)}`
|
|
297
|
+
}
|
|
298
|
+
if (operation === 'lengthGreaterThan') {
|
|
299
|
+
return `${nullSafeCollection(identifier)}.length > ${stringifyOperandValue(value)}`
|
|
300
|
+
}
|
|
301
|
+
if (operation === 'lengthLessThan') {
|
|
302
|
+
return `${nullSafeCollection(identifier)}.length < ${stringifyOperandValue(value)}`
|
|
303
|
+
}
|
|
304
|
+
if (operation === 'contains') {
|
|
305
|
+
const operandStr = stringifyOperandValue(value)
|
|
306
|
+
if (containsField) {
|
|
307
|
+
return `${nullSafeCollection(
|
|
308
|
+
identifier
|
|
309
|
+
)}.some(item => item.${containsField} === ${operandStr})`
|
|
310
|
+
}
|
|
311
|
+
return `${nullSafeCollection(identifier)}.includes(${operandStr})`
|
|
312
|
+
}
|
|
313
|
+
if (operation === 'notContains') {
|
|
314
|
+
const operandStr = stringifyOperandValue(value)
|
|
315
|
+
if (containsField) {
|
|
316
|
+
return `!${nullSafeCollection(
|
|
317
|
+
identifier
|
|
318
|
+
)}.some(item => item.${containsField} === ${operandStr})`
|
|
319
|
+
}
|
|
320
|
+
return `!${nullSafeCollection(identifier)}.includes(${operandStr})`
|
|
321
|
+
}
|
|
322
|
+
if (operation === 'hasKey') {
|
|
323
|
+
return `Object.prototype.hasOwnProperty.call(${nullSafeCollection(
|
|
324
|
+
identifier,
|
|
325
|
+
'{}'
|
|
326
|
+
)}, ${stringifyOperandValue(value)})`
|
|
327
|
+
}
|
|
328
|
+
if (operation === 'notHasKey') {
|
|
329
|
+
return `!Object.prototype.hasOwnProperty.call(${nullSafeCollection(
|
|
330
|
+
identifier,
|
|
331
|
+
'{}'
|
|
332
|
+
)}, ${stringifyOperandValue(value)})`
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// Standard operators
|
|
159
336
|
if (typeof value === 'boolean') {
|
|
160
337
|
return `${value ? '' : '!'}${identifier}`
|
|
161
338
|
}
|
|
@@ -164,5 +341,42 @@ const stringifyConditionalExpression = (
|
|
|
164
341
|
return `${identifier} ${operation} '${value}'`
|
|
165
342
|
}
|
|
166
343
|
|
|
344
|
+
if (typeof value === 'number') {
|
|
345
|
+
return `${identifier} ${operation} ${value}`
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
if (value.type === 'dynamic') {
|
|
349
|
+
return `${identifier} ${operation} ${value.content.id}`
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
if (value.type === 'expr') {
|
|
353
|
+
return `${identifier} ${operation} ${value.content}`
|
|
354
|
+
}
|
|
355
|
+
|
|
167
356
|
return `${identifier} ${operation} ${value}`
|
|
168
357
|
}
|
|
358
|
+
|
|
359
|
+
const applyDynamicHtmlDirective = (
|
|
360
|
+
htmlNode: HastNode,
|
|
361
|
+
rawValue: UIDLRawValue,
|
|
362
|
+
params: HTMLTemplateGenerationParams,
|
|
363
|
+
templateSyntax: HTMLTemplateSyntax,
|
|
364
|
+
node: UIDLElementNode
|
|
365
|
+
) => {
|
|
366
|
+
const dynamicRef = rawValue.dynamic
|
|
367
|
+
const { id, refPath } = dynamicRef.content
|
|
368
|
+
const idWithPath = UIDLUtils.generateIdWithRefPath(id, refPath)
|
|
369
|
+
const fallbackContent = rawValue.fallback || rawValue.content
|
|
370
|
+
const fallbackVarName = `htmlFallback${StringUtils.generateRandomString()}`
|
|
371
|
+
params.dataObject[fallbackVarName] = fallbackContent
|
|
372
|
+
const expression = `${idWithPath} || ${fallbackVarName}`
|
|
373
|
+
|
|
374
|
+
const directiveKey = templateSyntax.domHTMLInjection || 'innerHTML'
|
|
375
|
+
htmlNode.tagName = 'span'
|
|
376
|
+
hastUtils.addAttributeToNode(htmlNode, directiveKey, expression)
|
|
377
|
+
|
|
378
|
+
const elementType = node.content.elementType
|
|
379
|
+
if (params.dependencies[elementType]?.path?.includes('dangerous-html')) {
|
|
380
|
+
delete params.dependencies[elementType]
|
|
381
|
+
}
|
|
382
|
+
}
|