halfcab 14.0.1 → 14.0.4

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 (2) hide show
  1. package/halfcab.mjs +64 -9
  2. package/package.json +1 -1
package/halfcab.mjs CHANGED
@@ -2,8 +2,11 @@ import shiftyRouterModule from 'shifty-router'
2
2
  import hrefModule from 'shifty-router/href.js'
3
3
  import historyModule from 'shifty-router/history.js'
4
4
  import createLocation from 'shifty-router/create-location.js'
5
- import { html as litHtml, render } from 'lit-html'
5
+ import { html as litHtml, render, nothing, noChange } from 'lit-html'
6
6
  import { unsafeHTML } from 'lit-html/directives/unsafe-html.js'
7
+ import { repeat } from 'lit-html/directives/repeat.js'
8
+ import { classMap } from 'lit-html/directives/class-map.js'
9
+ import { styleMap } from 'lit-html/directives/style-map.js'
7
10
  import axios from 'axios'
8
11
  import cssInject from 'csjs-inject'
9
12
  import merge from 'deepmerge'
@@ -81,21 +84,73 @@ let html = (strings, ...values) => {
81
84
  return litHtml(strings, ...values)
82
85
  }
83
86
 
87
+ // Capture directive classes for SSR identification
88
+ const RepeatDirective = repeat([], () => {})['_$litDirective$']
89
+ const ClassMapDirective = classMap({})['_$litDirective$']
90
+ const StyleMapDirective = styleMap({})['_$litDirective$']
91
+
84
92
  function resolveTemplate (value) {
93
+ if (value === nothing || value === noChange) {
94
+ return ''
95
+ }
96
+
85
97
  if (Array.isArray(value)) {
86
98
  return value.map(resolveTemplate).join('')
87
99
  }
88
- if (value && typeof value === 'object' && value.strings) {
89
- let result = ''
90
- const { strings, values } = value
91
- for (let i = 0; i < strings.length; i++) {
92
- result += strings[i]
93
- if (i < values.length) {
94
- result += resolveTemplate(values[i])
100
+ if (value && typeof value === 'object') {
101
+ if (value.strings) {
102
+ let result = ''
103
+ const { strings, values } = value
104
+ for (let i = 0; i < strings.length; i++) {
105
+ result += strings[i]
106
+ if (i < values.length) {
107
+ result += resolveTemplate(values[i])
108
+ }
95
109
  }
110
+ return result
96
111
  }
97
- return result
112
+
113
+ if (value['_$litDirective$']) {
114
+ const directiveClass = value['_$litDirective$']
115
+ if (directiveClass.directiveName === 'unsafeHTML' && value.values && value.values.length > 0) {
116
+ return String(value.values[0])
117
+ }
118
+
119
+ if (directiveClass === RepeatDirective) {
120
+ const items = value.values[0]
121
+ const templateFn = value.values[value.values.length - 1]
122
+ if (items && typeof templateFn === 'function') {
123
+ return Array.from(items).map((item, index) => resolveTemplate(templateFn(item, index))).join('')
124
+ }
125
+ return ''
126
+ }
127
+
128
+ if (directiveClass === ClassMapDirective) {
129
+ const classObj = value.values[0]
130
+ if (typeof classObj === 'object') {
131
+ return Object.keys(classObj)
132
+ .filter(key => classObj[key])
133
+ .join(' ')
134
+ }
135
+ return ''
136
+ }
137
+
138
+ if (directiveClass === StyleMapDirective) {
139
+ const styleObj = value.values[0]
140
+ if (typeof styleObj === 'object') {
141
+ return Object.keys(styleObj)
142
+ .map(key => `${key}:${styleObj[key]}`)
143
+ .join(';')
144
+ }
145
+ return ''
146
+ }
147
+ }
148
+ }
149
+
150
+ if (typeof value === 'function') {
151
+ return ''
98
152
  }
153
+
99
154
  return value === undefined || value === null ? '' : String(value)
100
155
  }
101
156
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "halfcab",
3
- "version": "14.0.1",
3
+ "version": "14.0.4",
4
4
  "type": "module",
5
5
  "description": "A simple universal JavaScript framework focused on making use of es2015 template strings to build components.",
6
6
  "main": "halfcab.mjs",