-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.js
236 lines (191 loc) · 7.24 KB
/
render.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
const $j = typeof window !== "undefined" ? require("jquery") : null
const { isFunction } = require('lodash')
// Store DOM state in a module-level object (only for client-side)
const domState = typeof window !== "undefined" ? {} : null
function saveState(target) {
if (!domState) return // Skip in Node.js
const $target = $j(target)
const id = $target.attr("id") || $target[0].tagName
if (!id) return // Ensure the element has an identifier
// Save input values and scroll positions
domState[id] = {
scrollTop: $target.scrollTop(),
inputs: {},
scrollChildren: {},
}
// Save scroll position of child elements with the "custom-scrollbar" class
$target.find(".custom-scrollbar").each((index, el) => {
const $el = $j(el)
domState[id].scrollChildren[index] = $el.scrollTop()
})
$target.find("input, select, textarea").each((_, el) => {
const $el = $j(el)
const nameOrId = $el.attr("id") || $el.attr("name")
if (nameOrId) {
domState[id].inputs[nameOrId] = $el.val()
}
})
}
function restoreState(target) {
if (!domState) return // Skip in Node.js
const $target = $j(target)
const id = $target.attr("id") || $target[0].tagName
if (!domState[id]) return // No saved state for this element
const state = domState[id]
// Restore input values
$target.find("input, select, textarea").each((_, el) => {
const $el = $j(el)
const nameOrId = $el.attr("id") || $el.attr("name")
if (nameOrId && state.inputs[nameOrId] !== undefined) {
$el.val(state.inputs[nameOrId])
}
})
// Restore scroll position of the main container
$target.scrollTop(state.scrollTop)
// Restore scroll positions of child elements with the "custom-scrollbar" class
$target.find(".custom-scrollbar").each((index, el) => {
const $el = $j(el)
if (state.scrollChildren[index] !== undefined) {
$el.scrollTop(state.scrollChildren[index])
}
})
}
function processAttributes(str, context = {}) {
Object.assign(globalThis, context)
// First extract style tags and replace with placeholders
const styles = []
const withoutStyles = str.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, (match) => {
styles.push(match)
return `%%STYLE_${styles.length - 1}%%`
})
// Process attributes in the non-style content
let processed = withoutStyles
// Process nested template literals in ternaries
processed = processed.replace(/\${([^}]+)}/g, (match, expr) => {
try {
if (expr.includes('`')) {
const processedExpr = expr.includes('?')
? eval(expr)
: expr
if (typeof processedExpr === 'string') {
return processAttributes(processedExpr, context)
}
return processedExpr
}
return '${' + expr + '}'
} catch (e) {
console.error('Error processing nested template:', expr)
return match
}
})
// First handle template expressions including base64 images
processed = processed.replace(/(\w+(?:-\w+)*)=\${(.+?)}/g, (match, attr, expr) => {
try {
const value = eval(expr)
// Remove attribute if value is falsey
if (!value) {
return ''
} else if (typeof value === 'boolean') {
return value ? attr : ''
} else {
// Special handling for src attributes to ensure proper data URL format
if (attr === 'src') {
const valueStr = value.toString()
// If it's a base64 string without proper prefix, add it
if (valueStr.match(/^[A-Za-z0-9+/=]+$/)) {
return `src="data:image/png;base64,${valueStr}"`
}
return `src="${valueStr}"`
}
// Special handling for class to preserve all class names
if (attr === 'class') {
const trimmedValue = value.toString().trim()
if (!trimmedValue) return ''
return `class="${trimmedValue}"`
}
return `${attr}="${value.toString().trim()}"`
}
} catch (e) {
console.error('Error evaluating:', expr)
return ''
}
})
// Then handle raw class attributes with or without spaces
processed = processed.replace(/class\s*=\s*(?!")([\s\S]*?)(?=\s+(?:\w+(?:-\w+)*)?=|\/?>|>)/g, (match, value) => {
const trimmedValue = value.trim()
if (!trimmedValue) return ''
return `class="${trimmedValue}"`
})
// Handle any remaining attribute quotes properly
processed = processed.replace(/=\s*"\s*([^"]*?)\s*"\s*=\s*""\s*(?=>)/g, '="$1"')
// Clean up any orphaned ="" that might have been added
processed = processed.replace(/\s*=\s*""\s*(?=>)/g, '')
// Remove any empty attributes
processed = processed.replace(/\s*(\w+(?:-\w+)*)=\s*(?=\s|\/?>|>|\n)/g, '')
// Remove any empty attributes
processed = processed.replace(/\s*(\w+(?:-\w+)*)=\s*(?=\s|\/?>|>|\n)/g, '')
// Handle remaining unquoted attributes
processed = processed.replace(/(\w+(?:-\w+)*)=([^"\s>][^>]*?)(?=\s+(?:\w+(?:-\w+)*)?=|\/?>|>)/g, (match, attr, value) => {
if (value.startsWith('"')) return match
if (attr === 'class' || attr === 'src') return match // Skip already handled attributes
const normalizedValue = value.trim()
if (!normalizedValue) return ''
return `${attr}="${normalizedValue}"`
})
// Then handle class attributes with multi-line support
processed = processed.replace(/class=(?!")([\s\S]*?)(?=\s+(?:\w+(?:-\w+)*)?=|\/?>|>)/g, (match, value) => {
if (value.startsWith('"')) return match
const normalizedValue = value
.replace(/\s+/g, ' ')
.trim()
return `class="${normalizedValue}"`
})
// Finally handle any remaining unquoted attributes
processed = processed.replace(/(\w+(?:-\w+)*)=([^"\s>][^>]*?)(?=\s+(?:\w+(?:-\w+)*)?=|\/?>|>)/g, (match, attr, value) => {
if (value.startsWith('"')) return match
if (attr === 'class') return match // Skip class attributes as they're already handled
// Split the value at the first occurrence of a new attribute
const valueParts = value.split(/\s+(?=\w+(?:-\w+)*=)/)
const normalizedValue = valueParts[0].replace(/\s+/g, ' ').trim()
return `${attr}="${normalizedValue}"`
})
// Restore style tags
processed = processed.replace(/%%STYLE_(\d+)%%/g, (match, index) => styles[index])
return processed
}
function expandSelfClosingTags(html) {
if (typeof html !== 'string') return html
// Match tags with optional attributes ending in />
return html.replace(/<(\w+)([^>]*?)\s*\/>/g, '<$1$2></$1>')
}
function render(templateFnOrString, target) {
if (typeof window === "undefined") {
// Node.js environment
let result = templateFnOrString
if (isFunction(templateFnOrString)) {
result = templateFnOrString()
}
result = processAttributes(result)
result = expandSelfClosingTags(result)
return result // Directly return the HTML string
} else {
// Browser environment
let result = templateFnOrString
if (isFunction(templateFnOrString)) {
result = templateFnOrString()
}
result = processAttributes(result)
result = expandSelfClosingTags(result)
if (target) {
// Save current state of the DOM element
saveState(target)
// Render the new content
$j(target).html(result)
// Restore the state
restoreState(target)
return
}
return result
}
}
module.exports = render