refactor(jsx): reduce code size and improve maintainability (#2956)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* refactor(jsx): Refactored an utility method to reduce code size and improve maintainability * refactor: tweaks shared namespace string for svg and math for reduced size * fix: degration of c0fade3
authored by Taku Amano · committed by GitHub · parent 32ba5ba · Browse files · 5 files·+26 added−100 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+26 added−100 removed
- tests:+9 added−66 removed
- other:+17 added−34 removed
Provenance
- Commit
ad04fbf- Tree
0ebd134- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
32ba5ba
5 files changed+26 added−100 removed
modified:src/jsx/dom/jsx-dev-runtime.ts-4 removed
2 unmodified lines34567789111213101112134 unmodified lines2 unmodified lines * This module provides the `hono/jsx/dom` dev runtime. */
import type { JSXNode, Props } from '../base'import { normalizeIntrinsicElementProps } from '../utils'import { newJSXNode } from './utils'
export const jsxDEV = (tag: string | Function, props: Props, key?: string): JSXNode => { if (typeof tag === 'string') { normalizeIntrinsicElementProps(props) } return newJSXNode({ tag, props, key,4 unmodified linesmodified:src/jsx/dom/render.ts+8 added-7 removed
3 unmodified lines4 import type { Context as JSXContext } from '../context'5 import { globalContexts as globalJSXContexts, useContext } from '../context'6 import type { EffectData } from '../hooks'7 import { STASH_EFFECT } from '../hooks'8-import { styleObjectForEach } from '../utils'8+import { normalizeIntrinsicElementKey, styleObjectForEach } from '../utils'9 import { createContext } from './context' // import dom-specific versions10 import { newJSXNode } from './utils'11 12 const HONO_PORTAL_ELEMENT = '_hp'3 unmodified lines16 DoubleClick: 'DblClick',17 } as const18 19 const nameSpaceMap: Record<string, string> = {20- svg: 'http://www.w3.org/2000/svg',21- math: 'http://www.w3.org/1998/Math/MathML',20+ svg: '2000/svg',21+ math: '1998/Math/MathML',22 } as const23 24 const skipProps: Set<string> = new Set(['children'])25 93 unmodified lines119 attributes: Props,120 oldAttributes?: Props121 ): void => {122 attributes ||= {}123- for (const [key, value] of Object.entries(attributes)) {123+ for (let [key, value] of Object.entries(attributes)) {124 if (!skipProps.has(key) && (!oldAttributes || oldAttributes[key] !== value)) {125+ key = normalizeIntrinsicElementKey(key)126 const eventSpec = getEventSpec(key)127 if (eventSpec) {128 if (oldAttributes) {129 container.removeEventListener(eventSpec[0], oldAttributes[key], eventSpec[1])61 unmodified lines191 }192 }193 }194 if (oldAttributes) {194- for (const [key, value] of Object.entries(oldAttributes)) {195+ for (let [key, value] of Object.entries(oldAttributes)) {196 if (!skipProps.has(key) && !(key in attributes)) {197+ key = normalizeIntrinsicElementKey(key)198 const eventSpec = getEventSpec(key)199 if (eventSpec) {200 container.removeEventListener(eventSpec[0], value, eventSpec[1])201 } else if (key === 'ref') {308 unmodified lines510 ;(node as NodeObject)[DOM_STASH] = [0, []]511 } else {512 const ns = nameSpaceMap[(node as JSXNode).tag as string]513 if (ns) {512- ;(node as NodeObject).n = ns514 nameSpaceContext ||= createContext('')515 ;(node as JSXNode).props.children = [516 {517 tag: nameSpaceContext.Provider,518 props: {518- value: ns,519+ value: ((node as NodeObject).n = `http://www.w3.org/${ns}`),520 children: (node as JSXNode).props.children,521 },522 },523 // eslint-disable-next-line @typescript-eslint/no-explicit-any115 unmodified linesmodified:src/jsx/base.ts+3 added-7 removed
5 unmodified lines6 import type {7 JSX as HonoJSX,8 IntrinsicElements as IntrinsicElementsDefined,9 } from './intrinsic-elements'10-import { normalizeIntrinsicElementProps, styleObjectForEach } from './utils'10+import { normalizeIntrinsicElementKey, styleObjectForEach } from './utils'11 12 // eslint-disable-next-line @typescript-eslint/no-explicit-any13 export type Props = Record<string, any>14 export type FC<P = Props> = {136 unmodified lines151 let { children } = this152 153 buffer[0] += `<${tag}`154 155- const propsKeys = Object.keys(props || {})156-157- for (let i = 0, len = propsKeys.length; i < len; i++) {158- const key = propsKeys[i]159- const v = props[key]155+ for (let [key, v] of Object.entries(props)) {156+ key = normalizeIntrinsicElementKey(key)157 if (key === 'children') {158 // skip children159 } else if (key === 'style' && typeof v === 'object') {160 // object to style strings118 unmodified lines279 ): JSXNode => {280 if (typeof tag === 'function') {281 return new JSXFunctionNode(tag, props, children)282 } else {286- normalizeIntrinsicElementProps(props)283 return new JSXNode(tag, props, children)284 }285 }286 74 unmodified linesmodified:src/jsx/utils.test.ts+9 added-66 removed
112345678910111213141534567891011171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667681213141587 unmodified linesimport { normalizeIntrinsicElementProps, styleObjectForEach } from './utils'import { normalizeIntrinsicElementKey, styleObjectForEach } from './utils'
describe('normalizeIntrinsicElementProps', () => { it('should convert className to class', () => { const props: Record<string, unknown> = { className: 'test-class', id: 'test-id', }
normalizeIntrinsicElementProps(props)
expect(props).toEqual({ class: 'test-class', id: 'test-id', })describe('normalizeIntrinsicElementKey', () => { test.each` key | expected ${'className'} | ${'class'} ${'htmlFor'} | ${'for'} ${'href'} | ${'href'} `('should convert $key to $expected', ({ key, expected }) => { expect(normalizeIntrinsicElementKey(key)).toBe(expected) })
it('should convert htmlFor to for', () => { const props: Record<string, unknown> = { htmlFor: 'test-for', name: 'test-name', }
normalizeIntrinsicElementProps(props)
expect(props).toEqual({ for: 'test-for', name: 'test-name', }) })
it('should convert multiple attribute aliases', () => { const props: Record<string, unknown> = { className: 'test-class', htmlFor: 'test-for', type: 'text', }
normalizeIntrinsicElementProps(props)
expect(props).toEqual({ class: 'test-class', for: 'test-for', type: 'text', }) })
it('should not modify props without className or htmlFor', () => { const props: Record<string, unknown> = { id: 'test-id', name: 'test-name', }
normalizeIntrinsicElementProps(props)
expect(props).toEqual({ id: 'test-id', name: 'test-name', }) })
it('should handle empty props', () => { const props: Record<string, unknown> = {}
normalizeIntrinsicElementProps(props)
expect(props).toEqual({}) })})
describe('styleObjectForEach', () => { describe('Should output the number as it is, when a number type is passed', () => {87 unmodified linesmodified:src/jsx/utils.ts+6 added-16 removed
123456789101112131415161234567891020 unmodified lines/** * Normalizes intrinsic element properties by converting JSX element properties * to their corresponding HTML attributes. * * @param props - JSX element properties. */export const normalizeIntrinsicElementProps = (props: Record<string, unknown>): void => { if (props && 'className' in props) { props['class'] = props['className'] delete props['className'] } if (props && 'htmlFor' in props) { props['for'] = props['htmlFor'] delete props['htmlFor'] }}const normalizeElementKeyMap = new Map([ ['className', 'class'], ['htmlFor', 'for'],])export const normalizeIntrinsicElementKey = (key: string): string => normalizeElementKeyMap.get(key) || key
export const styleObjectForEach = ( style: Record<string, string | number>, fn: (key: string, value: string | null) => void20 unmodified lines