Comparing version 0.1.3 to 0.1.4
39
hjsx.js
@@ -161,2 +161,20 @@ // util/normalize-attribute-name.ts | ||
var regex = new RegExp(`[${Object.keys(escapeMap).join("")}]`, "g"); | ||
var SELF_CLOSING_TAGS = [ | ||
"area", | ||
"base", | ||
"br", | ||
"col", | ||
"command", | ||
"embed", | ||
"hr", | ||
"img", | ||
"input", | ||
"keygen", | ||
"link", | ||
"meta", | ||
"param", | ||
"source", | ||
"track", | ||
"wbr" | ||
]; | ||
@@ -192,20 +210,2 @@ // hjsx.ts | ||
}; | ||
var SELF_CLOSING_TAGS = [ | ||
"area", | ||
"base", | ||
"br", | ||
"col", | ||
"command", | ||
"embed", | ||
"hr", | ||
"img", | ||
"input", | ||
"keygen", | ||
"link", | ||
"meta", | ||
"param", | ||
"source", | ||
"track", | ||
"wbr" | ||
]; | ||
var renderToString = (component) => { | ||
@@ -224,2 +224,3 @@ if (isPrimitive(component)) | ||
let { type, props, children } = component; | ||
props = props ?? {}; | ||
if (typeof type === "function") { | ||
@@ -230,3 +231,3 @@ const componentInstance = isClassConstructor(type) ? new type({ ...props, children }) : type({ ...props, children }); | ||
const innerHTML = props.dangerouslySetInnerHTML ? dangerouslySetInnerHTML(props.dangerouslySetInnerHTML) : null; | ||
const propsString = Object.entries(props).filter(([key, value]) => key !== "dangerouslySetInnerHTML" && value !== false && value != null).map(([key, value]) => { | ||
const propsString = Object.entries(props).filter(([key, value]) => key !== "u.dangerouslySetInnerHTML" && value !== false && value != null).map(([key, value]) => { | ||
const normalizedKey = normalizeAttributeName(key); | ||
@@ -233,0 +234,0 @@ const normalizedValue = escapeHtml(String(key === "style" ? handleStyle(value) : value)); |
65
hjsx.ts
@@ -1,32 +0,3 @@ | ||
import { | ||
escapeHtml, | ||
normalizeAttributeName, | ||
isNullish, | ||
isPrimitive, | ||
dangerouslySetInnerHTML, | ||
handleStyle, | ||
isIterable, | ||
isClassConstructor, | ||
isObject, | ||
} from "./util"; | ||
import * as u from "./util"; | ||
const SELF_CLOSING_TAGS = [ | ||
"area", | ||
"base", | ||
"br", | ||
"col", | ||
"command", | ||
"embed", | ||
"hr", | ||
"img", | ||
"input", | ||
"keygen", | ||
"link", | ||
"meta", | ||
"param", | ||
"source", | ||
"track", | ||
"wbr", | ||
]; | ||
export function hjsx( | ||
@@ -57,6 +28,6 @@ type: hjsx.Element["type"], | ||
export const renderToString = (component?: unknown): string => { | ||
if (isPrimitive(component)) return escapeHtml(String(component)); | ||
if (isNullish(component)) return ""; | ||
if (isIterable(component)) return renderChildren({ children: component }); | ||
if (!isObject(component)) return ""; | ||
if (u.isPrimitive(component)) return u.escapeHtml(String(component)); | ||
if (u.isNullish(component)) return ""; | ||
if (u.isIterable(component)) return renderChildren({ children: component }); | ||
if (!u.isObject(component)) return ""; | ||
if (!("children" in component)) return renderChildren({ children: component }); | ||
@@ -66,4 +37,5 @@ validateElement(component); | ||
let { type, props, children } = component; | ||
props = props ?? {}; | ||
if (typeof type === "function") { | ||
const componentInstance = isClassConstructor(type) | ||
const componentInstance = u.isClassConstructor(type) | ||
? new type({ ...props, children }) | ||
@@ -75,12 +47,13 @@ : type({ ...props, children }); | ||
const innerHTML = props.dangerouslySetInnerHTML | ||
? dangerouslySetInnerHTML(props.dangerouslySetInnerHTML) | ||
? u.dangerouslySetInnerHTML(props.dangerouslySetInnerHTML) | ||
: null; | ||
const propsString = Object.entries(props) | ||
.filter( | ||
([key, value]) => key !== "dangerouslySetInnerHTML" && value !== false && value != null, | ||
([key, value]) => | ||
key !== "u.dangerouslySetInnerHTML" && value !== false && value != null, | ||
) | ||
.map(([key, value]) => { | ||
const normalizedKey = normalizeAttributeName(key); | ||
const normalizedValue = escapeHtml( | ||
String(key === "style" ? handleStyle(value) : value), | ||
const normalizedKey = u.normalizeAttributeName(key); | ||
const normalizedValue = u.escapeHtml( | ||
String(key === "style" ? u.handleStyle(value) : value), | ||
); | ||
@@ -97,3 +70,3 @@ return value === true ? normalizedKey : `${normalizedKey}="${normalizedValue}"`; | ||
return SELF_CLOSING_TAGS.includes(type) | ||
return u.SELF_CLOSING_TAGS.includes(type) | ||
? `<${type} ${propsString} />` | ||
@@ -105,9 +78,9 @@ : `<${type} ${propsString}>${childrenString}</${type}>`; | ||
const renderChildren = (args: unknown): string => { | ||
if (isNullish(args)) return ""; | ||
if (isPrimitive(args)) return escapeHtml(String(args)); | ||
if (!isObject(args)) return ""; | ||
if (u.isNullish(args)) return ""; | ||
if (u.isPrimitive(args)) return u.escapeHtml(String(args)); | ||
if (!u.isObject(args)) return ""; | ||
const { children } = args; | ||
const childrenArray = Array.isArray(children) ? children : [children]; | ||
return childrenArray | ||
.filter((child) => !isNullish(child)) | ||
.filter((child) => !u.isNullish(child)) | ||
.map(renderToString) | ||
@@ -120,3 +93,3 @@ .join(""); | ||
): asserts element is hjsx.PropsWithChildren<hjsx.Element> { | ||
if (!isObject(element)) { | ||
if (!u.isObject(element)) { | ||
throw new Error("Element must be an object"); | ||
@@ -123,0 +96,0 @@ } |
{ | ||
"name": "hjsx", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"module": "hjsx.ts", | ||
@@ -5,0 +5,0 @@ "main": "hjsx.js", |
@@ -89,1 +89,20 @@ export { normalizeAttributeName } from "./normalize-attribute-name.ts"; | ||
} | ||
export const SELF_CLOSING_TAGS = [ | ||
"area", | ||
"base", | ||
"br", | ||
"col", | ||
"command", | ||
"embed", | ||
"hr", | ||
"img", | ||
"input", | ||
"keygen", | ||
"link", | ||
"meta", | ||
"param", | ||
"source", | ||
"track", | ||
"wbr", | ||
]; |
Sorry, the diff of this file is not supported yet
92851
2046