Skip to content
Myr

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

5 files+26 added−100 removed

modified:src/jsx/dom/jsx-dev-runtime.ts-4 removed
modified:src/jsx/dom/render.ts+8 added-7 removed
3 unmodified lines
4import type { Context as JSXContext } from '../context'5import { globalContexts as globalJSXContexts, useContext } from '../context'6import type { EffectData } from '../hooks'7import { STASH_EFFECT } from '../hooks'8import { styleObjectForEach } from '../utils'8import { normalizeIntrinsicElementKey, styleObjectForEach } from '../utils'9import { createContext } from './context' // import dom-specific versions10import { newJSXNode } from './utils'1112const HONO_PORTAL_ELEMENT = '_hp'
3 unmodified lines
16 DoubleClick: 'DblClick',17} as const1819const 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 const2324const skipProps: Set<string> = new Set(['children'])25
93 unmodified lines
119 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 lines
191 }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 lines
510 ;(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-any
115 unmodified lines
modified:src/jsx/base.ts+3 added-7 removed
5 unmodified lines
6import type {7 JSX as HonoJSX,8 IntrinsicElements as IntrinsicElementsDefined,9} from './intrinsic-elements'10import { normalizeIntrinsicElementProps, styleObjectForEach } from './utils'10import { normalizeIntrinsicElementKey, styleObjectForEach } from './utils'1112// eslint-disable-next-line @typescript-eslint/no-explicit-any13export type Props = Record<string, any>14export type FC<P = Props> = {
136 unmodified lines
151 let { children } = this152153 buffer[0] += `<${tag}`154155 const propsKeys = Object.keys(props || {})156157 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 strings
118 unmodified lines
279): 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 lines
modified:src/jsx/utils.test.ts+9 added-66 removed
modified:src/jsx/utils.ts+6 added-16 removed