diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..346125f --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,31 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +### Changed + +- Render nulls, undefined, zeroes and false correctly and more in line with how template strings do. + +### [3.0.0] - 2023-07-07 + +- This version marks the fork from [vhtml](https://www.npmjs.com/package/vhtml) + +### Added + +- Typescript support (no additional @types/ package is required) (thanks to [pastelmind](https://github.com/pastelmind) for his type definitions for vhtml, these are simply adapted from his by @odinhb) +- New way to skip sanitization by passing a string with `_stringjsx_sanitized = true` set. ([remziatay](https://github.com/remziatay) had the idea [here](https://github.com/developit/vhtml/issues/34#issuecomment-1460436783)) + +### Changed + +- Returns a `String` (object, not primitive) ([remziatay](https://github.com/remziatay) had the idea [here](https://github.com/developit/vhtml/issues/34#issuecomment-1460436783)) +- Rewrote the source code itself (@odinhb) + +### Fixed + +- Fix [injection possibility](https://github.com/developit/vhtml/issues/34) +- Fix [memory leak](https://github.com/developit/vhtml/issues/20) diff --git a/src/stringjsx.js b/src/stringjsx.js index 7ae48ed..b309671 100644 --- a/src/stringjsx.js +++ b/src/stringjsx.js @@ -87,8 +87,9 @@ function render(tagName, attributes) { const children = extractChildren(arguments); let result = ''; + const isFragment = tagName !== null && tagName !== undefined; - if (tagName) { // null is passed when rendering a fragment + if (isFragment) { result += '<'; result += tagName; @@ -104,7 +105,10 @@ function render(tagName, attributes) { } else while(children.length) { const child = children.shift(); - if (!child) continue; + if (child === undefined || child === null) { + result += child; + continue; + } if (isCollection(child)) { for (let i = child.length - 1; i >= 0; i--) { @@ -116,7 +120,7 @@ function render(tagName, attributes) { } } - if (tagName) result += `${tagName}>`; + if (isFragment) result += `${tagName}>`; } // Read about the fun world of javascript strings diff --git a/test/stringjsx.js b/test/stringjsx.js index 33c5f44..3643089 100644 --- a/test/stringjsx.js +++ b/test/stringjsx.js @@ -269,6 +269,14 @@ describe('stringjsx', () => { ); }); + it('also supports fragments w/ undefined', () => { + expect( + h(h.Fragment, null,
foo
, bar,foo
bar{-882}, {942}
+-882, 942
{true} love
+true love