halfcab 14.0.2 → 14.0.5

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 +50 -4
  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'
@@ -72,7 +75,7 @@ let html = (strings, ...values) => {
72
75
  // Check if it's a CSJS object (has custom toString and isn't a TemplateResult)
73
76
  // TemplateResult usually has 'strings' and 'values' or '_$litType$'
74
77
  // DirectiveResult (unsafeHTML) uses default toString, so we shouldn't call it.
75
- if (value && typeof value.toString === 'function' && value.toString !== Object.prototype.toString && !value.strings && !value._$litType$) {
78
+ if (value && typeof value !== 'function' && !Array.isArray(value) && typeof value.toString === 'function' && value.toString !== Object.prototype.toString && !value.strings && !value._$litType$) {
76
79
  return value.toString()
77
80
  }
78
81
  return value
@@ -81,7 +84,16 @@ 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
  }
@@ -99,12 +111,46 @@ function resolveTemplate (value) {
99
111
  }
100
112
 
101
113
  if (value['_$litDirective$']) {
102
- const directive = value['_$litDirective$']
103
- if (directive.directiveName === 'unsafeHTML' && value.values && value.values.length > 0) {
114
+ const directiveClass = value['_$litDirective$']
115
+ if (directiveClass.directiveName === 'unsafeHTML' && value.values && value.values.length > 0) {
104
116
  return String(value.values[0])
105
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
+ }
106
147
  }
107
148
  }
149
+
150
+ if (typeof value === 'function') {
151
+ return ''
152
+ }
153
+
108
154
  return value === undefined || value === null ? '' : String(value)
109
155
  }
110
156
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "halfcab",
3
- "version": "14.0.2",
3
+ "version": "14.0.5",
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",