1- // @ts -check
2-
3- 'use strict' ;
4-
5- const htm = require ( 'htm' ) ; // linemod-replace-with: import htm from 'htm';
6-
7- const escape = require ( 'stringify-entities' ) ; // linemod-replace-with: import escape from 'stringify-entities';
1+ import htm from 'htm' ;
2+ import { stringifyEntities } from 'stringify-entities' ;
83
94// *** REACT BORROWED ***
105const ATTRIBUTE_NAME_START_CHAR =
@@ -129,7 +124,7 @@ const omittedCloseTags = {
129124 * @param {ElementProps } props
130125 * @returns {Generator<string> }
131126 */
132- const _renderProps = function * ( props ) {
127+ function * _renderProps ( props ) {
133128 // *** REACT BORROWED https://github.com/facebook/react/blob/779a472b0901b2d28e382f3850b2ad09a555b014/packages/react-dom/src/server/DOMMarkupOperations.js#L48-L72 ***
134129 for ( const propKey in props ) {
135130 if ( ! Object . prototype . hasOwnProperty . call ( props , propKey ) ) {
@@ -151,7 +146,7 @@ const _renderProps = function * (props) {
151146 } else if ( propValue === '' ) {
152147 yield ` ${ propKey } =""` ;
153148 } else if ( typeof propValue === 'string' ) {
154- yield ` ${ propKey } ="${ escape ( propValue , { useNamedReferences : true } ) } "` ;
149+ yield ` ${ propKey } ="${ stringifyEntities ( propValue , { escapeOnly : true , useNamedReferences : true } ) } "` ;
155150 } else if ( typeof propValue === 'number' ) {
156151 yield ` ${ propKey } ="${ propValue } "` ;
157152 } else {
@@ -160,15 +155,15 @@ const _renderProps = function * (props) {
160155 }
161156 }
162157 // *** END REACT BORROWED ***
163- } ;
158+ }
164159
165160/**
166161 * @yields {string}
167162 * @template {ElementProps} Props
168163 * @param {StringRenderableElement<Props> } item
169164 * @returns {AsyncIterableIterator<string> }
170165 */
171- const _renderStringItem = async function * ( item ) {
166+ async function * _renderStringItem ( item ) {
172167 const { children, props, type } = item ;
173168
174169 const tag = type . toLowerCase ( ) ;
@@ -186,15 +181,15 @@ const _renderStringItem = async function * (item) {
186181 yield * _render ( children ) ;
187182 yield `</${ tag } >` ;
188183 }
189- } ;
184+ }
190185
191186/**
192187 * @yields {string}
193188 * @template {ElementProps} Props
194189 * @param {BasicRenderableElement<Props> } item
195190 * @returns {AsyncIterableIterator<string> }
196191 */
197- const _renderElement = async function * ( item ) {
192+ async function * _renderElement ( item ) {
198193 const { children, props, skipStringEscape, type } = item ;
199194
200195 if ( type === undefined ) {
@@ -215,29 +210,29 @@ const _renderElement = async function * (item) {
215210 } else {
216211 throw new TypeError ( `Invalid element type: ${ typeof type } ` ) ;
217212 }
218- } ;
213+ }
219214
220215/**
221216 * @yields {string}
222217 * @param {IterableIteratorMaybeAsync<RenderableElement> } iterator
223218 * @returns {AsyncIterableIterator<string> }
224219 */
225- const _renderIterable = async function * ( iterator ) {
220+ async function * _renderIterable ( iterator ) {
226221 for await ( const item of iterator ) {
227222 yield * _render ( item ) ;
228223 }
229- } ;
224+ }
230225
231226/**
232227 * @yields {string}
233228 * @param {RenderableElement|IterableIteratorMaybeAsync<RenderableElement> } item
234229 * @returns {AsyncIterableIterator<string> }
235230 */
236- const _render = async function * ( item ) {
231+ async function * _render ( item ) {
237232 if ( item === undefined || item === null ) {
238233 yield '' ;
239234 } else if ( typeof item === 'string' ) {
240- yield escape ( item , { useNamedReferences : true } ) ;
235+ yield stringifyEntities ( item , { escapeOnly : true , useNamedReferences : true } ) ;
241236 } else if ( typeof item === 'number' ) {
242237 yield item + '' ;
243238 } else {
@@ -249,14 +244,14 @@ const _render = async function * (item) {
249244 throw new TypeError ( `Invalid render item type: ${ typeof item } ` ) ;
250245 }
251246 }
252- } ;
247+ }
253248
254249/**
255250 * @yields {string}
256251 * @param {HtmlMethodResult } item
257252 * @returns {AsyncIterableIterator<string> }
258253 */
259- const render = async function * ( item ) { // linemod-prefix-with: export
254+ export async function * render ( item ) {
260255 if ( item === undefined ) throw new TypeError ( 'Expected an argument' ) ;
261256 if ( ! item ) throw new TypeError ( `Expected a non-falsy argument, got: ${ item } ` ) ;
262257 if ( Array . isArray ( item ) ) {
@@ -268,25 +263,27 @@ const render = async function * (item) { // linemod-prefix-with: export
268263 } else {
269264 throw new TypeError ( `Expected a string or an object, got: ${ typeof item } ` ) ;
270265 }
271- } ;
266+ }
272267
273268/**
274269 * @param {IterableIteratorMaybeAsync<string> } generator
275270 * @returns {Promise<string> }
276271 */
277- const generatorToString = async ( generator ) => { // linemod-prefix-with: export
272+ export async function generatorToString ( generator ) {
278273 let result = '' ;
279274 for await ( const item of generator ) {
280275 result += item ;
281276 }
282277 return result ;
283- } ;
278+ }
284279
285280/**
286281 * @param {HtmlMethodResult } item
287282 * @returns {Promise<string> }
288283 */
289- const renderToString = async ( item ) => generatorToString ( render ( item ) ) ; // linemod-prefix-with: export
284+ export async function renderToString ( item ) {
285+ return generatorToString ( render ( item ) ) ;
286+ }
290287
291288/**
292289 * @template {ElementProps} T
@@ -295,9 +292,9 @@ const renderToString = async (item) => generatorToString(render(item)); // linem
295292 * @param {...RenderableElement } children
296293 * @returns {BasicRenderableElement<T> }
297294 */
298- const h = ( type , props , ...children ) => { // linemod-prefix-with: export
295+ export function h ( type , props , ...children ) {
299296 return { type, props : props || { } , children } ;
300- } ;
297+ }
301298
302299/** @type {(strings: TemplateStringsArray, ...values: Array<ElementPropsValue|ElementProps|RenderableElementFunction<any>|RenderableElement|RenderableElement[]>) => unknown } */
303300const _internalHtml =
@@ -308,7 +305,7 @@ const _internalHtml =
308305 * @param {unknown } result
309306 * @returns {BasicRenderableElement<ElementProps>|string }
310307 */
311- const _checkHtmlResult = ( result ) => {
308+ function _checkHtmlResult ( result ) {
312309 if ( typeof result === 'number' ) {
313310 return result + '' ;
314311 } else if ( ! result ) {
@@ -332,14 +329,14 @@ const _checkHtmlResult = (result) => {
332329 } else {
333330 throw new TypeError ( `Resolved to invalid value type: ${ typeof result } ` ) ;
334331 }
335- } ;
332+ }
336333
337334/**
338335 * @param {TemplateStringsArray } strings
339336 * @param {...ElementPropsValue|ElementProps|RenderableElementFunction<any>|RenderableElement|RenderableElement[] } values
340337 * @returns {HtmlMethodResult }
341338 */
342- const html = ( strings , ...values ) => { // linemod-prefix-with: export
339+ export function html ( strings , ...values ) {
343340 const result = _internalHtml ( strings , ...values ) ;
344341
345342 if ( ! Array . isArray ( result ) ) return _checkHtmlResult ( result ) ;
@@ -348,24 +345,15 @@ const html = (strings, ...values) => { // linemod-prefix-with: export
348345 const unknownArray = result ;
349346
350347 return unknownArray . map ( item => _checkHtmlResult ( item ) ) ;
351- } ;
348+ }
352349
353350/**
354351 * @param {TemplateStringsArray|string } strings
355352 * @param {...(string|number) } values
356353 * @returns {BasicRenderableElement<{}> }
357354 */
358- const rawHtml = ( strings , ...values ) => { // linemod-prefix-with: export
355+ export function rawHtml ( strings , ...values ) {
359356 /** @type {RenderableElementFunction<{}> } */
360357 const type = ( ) => typeof strings === 'string' ? strings : String . raw ( strings , ...values ) ;
361358 return { type, props : { } , children : [ ] , skipStringEscape : true } ;
362- } ;
363-
364- module . exports = { // linemod-remove
365- generatorToString, // linemod-remove
366- html, // linemod-remove
367- h, // linemod-remove
368- rawHtml, // linemod-remove
369- render, // linemod-remove
370- renderToString, // linemod-remove
371- } ; // linemod-remove
359+ }
0 commit comments