From 04181677ea2a74f91799dd8a6090f32ec5c73298 Mon Sep 17 00:00:00 2001 From: * <*> Date: Wed, 7 Jun 2023 15:12:51 +0800 Subject: [PATCH] Match-id-376100c85662cffb23b28ed1f31fb28473ed51a5 --- libs/horizon/src/dom/SelectionRangeHandler.ts | 4 ++-- libs/horizon/src/dom/utils/Common.ts | 4 ++-- libs/horizon/src/dom/validators/ValidateProps.ts | 5 ++--- libs/horizon/src/dom/valueHandler/InputValueHandler.ts | 9 ++++----- libs/horizon/src/dom/valueHandler/SelectValueHandler.ts | 7 +++---- .../horizon/src/dom/valueHandler/TextareaValueHandler.ts | 3 +-- libs/horizon/src/event/EventBinding.ts | 2 +- libs/horizon/src/event/FormValueController.ts | 6 +++--- libs/horizon/src/renderer/TreeBuilder.ts | 7 +++---- libs/horizon/src/renderer/hooks/UseImperativeHook.ts | 5 +---- libs/horizon/src/renderer/render/DomComponent.ts | 3 +-- libs/horizon/src/renderer/render/DomText.ts | 2 +- libs/horizon/src/renderer/vnode/VNodeCreator.ts | 3 +-- 13 files changed, 25 insertions(+), 35 deletions(-) diff --git a/libs/horizon/src/dom/SelectionRangeHandler.ts b/libs/horizon/src/dom/SelectionRangeHandler.ts index a8f643c1..f9488f9b 100644 --- a/libs/horizon/src/dom/SelectionRangeHandler.ts +++ b/libs/horizon/src/dom/SelectionRangeHandler.ts @@ -17,7 +17,7 @@ * 处理文本框、输入框中框选范围内的数据 */ -import { getIFrameFocusedDom, isNull, isText } from './utils/Common'; +import { getIFrameFocusedDom, isText } from './utils/Common'; import { isElement } from './utils/Common'; @@ -30,7 +30,7 @@ function setSelectionRange(dom: HTMLInputElement | HTMLTextAreaElement, range) { const { start, end } = range; let realEnd = end; - if (isNull(realEnd)) { + if (realEnd === null || realEnd === undefined) { realEnd = start; } diff --git a/libs/horizon/src/dom/utils/Common.ts b/libs/horizon/src/dom/utils/Common.ts index 001a32f1..dde99489 100644 --- a/libs/horizon/src/dom/utils/Common.ts +++ b/libs/horizon/src/dom/utils/Common.ts @@ -85,6 +85,6 @@ export function shouldAutoFocus(tagName: string, props: Props): boolean { return types.includes(tagName) ? Boolean(props.autoFocus) : false; } -export function isNull(val) { - return val === null || val === undefined; +export function isNotNull(object: any): boolean { + return object !== null && object !== undefined; } diff --git a/libs/horizon/src/dom/validators/ValidateProps.ts b/libs/horizon/src/dom/validators/ValidateProps.ts index 9eabcc09..ed1f0132 100644 --- a/libs/horizon/src/dom/validators/ValidateProps.ts +++ b/libs/horizon/src/dom/validators/ValidateProps.ts @@ -14,7 +14,6 @@ */ import { getPropDetails, PROPERTY_TYPE, PropDetails } from './PropertiesData'; -import { isNull } from '../utils/Common'; const INVALID_EVENT_NAME_REGEX = /^on[^A-Z]/; @@ -74,7 +73,7 @@ export function isInvalidValue( propDetails: PropDetails | null, isNativeTag: boolean ): boolean { - if (isNull(value)) { + if (value === null || value === undefined) { return true; } @@ -105,7 +104,7 @@ export function validateProps(type, props) { } // style属性必须是对象 - if (!isNull(props.style) && typeof props.style !== 'object') { + if (props.style !== null && props.style !== undefined && typeof props.style !== 'object') { throw new Error('style should be a object.'); } diff --git a/libs/horizon/src/dom/valueHandler/InputValueHandler.ts b/libs/horizon/src/dom/valueHandler/InputValueHandler.ts index 749e85b3..16908947 100644 --- a/libs/horizon/src/dom/valueHandler/InputValueHandler.ts +++ b/libs/horizon/src/dom/valueHandler/InputValueHandler.ts @@ -15,7 +15,6 @@ import { updateCommonProp } from '../DOMPropertiesHandler/UpdateCommonProp'; import { Props } from '../utils/Interface'; -import { isNull } from '../utils/Common'; function getInitValue(dom: HTMLInputElement, props: Props) { const { value, defaultValue, checked, defaultChecked } = props; @@ -30,7 +29,7 @@ function getInitValue(dom: HTMLInputElement, props: Props) { export function getInputPropsWithoutValue(dom: HTMLInputElement, props: Props) { // checked属于必填属性,无法置 let { checked } = props; - if (isNull(checked)) { + if (checked === undefined) { checked = getInitValue(dom, props).initChecked; } @@ -46,12 +45,12 @@ export function getInputPropsWithoutValue(dom: HTMLInputElement, props: Props) { export function updateInputValue(dom: HTMLInputElement, props: Props) { const { value, checked } = props; - if (!isNull(value)) { + if (value !== undefined) { // 处理 dom.value 逻辑 if (dom.value !== String(value)) { dom.value = String(value); } - } else if (!isNull(checked)) { + } else if (checked !== undefined) { updateCommonProp(dom, 'checked', checked, true); } } @@ -61,7 +60,7 @@ export function setInitInputValue(dom: HTMLInputElement, props: Props) { const { value, defaultValue } = props; const { initValue, initChecked } = getInitValue(dom, props); - if (!isNull(value) || !isNull(defaultValue)) { + if (value !== undefined || defaultValue !== undefined) { // value 的使用优先级 value 属性 > defaultValue 属性 > 空字符串 const initValueStr = String(initValue); diff --git a/libs/horizon/src/dom/valueHandler/SelectValueHandler.ts b/libs/horizon/src/dom/valueHandler/SelectValueHandler.ts index 86297315..9ba9273f 100644 --- a/libs/horizon/src/dom/valueHandler/SelectValueHandler.ts +++ b/libs/horizon/src/dom/valueHandler/SelectValueHandler.ts @@ -14,7 +14,6 @@ */ import { HorizonSelect, Props } from '../utils/Interface'; -import { isNull } from '../utils/Common'; function updateMultipleValue(options, newValues) { const newValueSet = new Set(); @@ -70,18 +69,18 @@ export function updateSelectValue(dom: HorizonSelect, props: Props, isInit = fal dom._multiple = newMultiple; // 设置了 value 属性 - if (!isNull(value)) { + if (value !== null && value !== undefined) { updateValue(dom.options, value, newMultiple); } else if (oldMultiple !== newMultiple) { // 修改了 multiple 属性 // 切换 multiple 之后,如果设置了 defaultValue 需要重新应用 - if (!isNull(defaultValue)) { + if (defaultValue !== null && defaultValue !== undefined) { updateValue(dom.options, defaultValue, newMultiple); } else { // 恢复到未选定状态 updateValue(dom.options, newMultiple ? [] : '', newMultiple); } - } else if (isInit && !isNull(defaultValue)) { + } else if (isInit && defaultValue !== null && defaultValue !== undefined) { // 设置了 defaultValue 属性 updateValue(dom.options, defaultValue, newMultiple); } diff --git a/libs/horizon/src/dom/valueHandler/TextareaValueHandler.ts b/libs/horizon/src/dom/valueHandler/TextareaValueHandler.ts index e852cd1f..cf91b9c0 100644 --- a/libs/horizon/src/dom/valueHandler/TextareaValueHandler.ts +++ b/libs/horizon/src/dom/valueHandler/TextareaValueHandler.ts @@ -14,13 +14,12 @@ */ import { Props } from '../utils/Interface'; -import { isNull } from '../utils/Common'; // 值的优先级 value > children > defaultValue function getInitValue(props: Props) { const { value } = props; - if (isNull(value)) { + if (value === undefined) { const { defaultValue, children } = props; let initValue = defaultValue; diff --git a/libs/horizon/src/event/EventBinding.ts b/libs/horizon/src/event/EventBinding.ts index 56e1567c..baf965dd 100644 --- a/libs/horizon/src/event/EventBinding.ts +++ b/libs/horizon/src/event/EventBinding.ts @@ -73,7 +73,7 @@ export function lazyDelegateOnRoot(currentRoot: VNode, eventName: string) { const nativeFullName = isCapture ? nativeEvent + 'capture' : nativeEvent; // 事件存储在DOM节点属性,避免多个VNode(root和portal)对应同一个DOM, 造成事件重复监听 - currentRoot.realNode.$EV = currentRoot.realNode.$EV || {}; + currentRoot.realNode.$EV = currentRoot.realNode.$EV ?? {}; const events = currentRoot.realNode.$EV; if (!events[nativeFullName]) { diff --git a/libs/horizon/src/event/FormValueController.ts b/libs/horizon/src/event/FormValueController.ts index 2e296e6d..1b1649d0 100644 --- a/libs/horizon/src/event/FormValueController.ts +++ b/libs/horizon/src/event/FormValueController.ts @@ -14,7 +14,7 @@ */ import { getVNodeProps } from '../dom/DOMInternalKeys'; -import { getDomTag, isNull } from '../dom/utils/Common'; +import { getDomTag, isNotNull } from '../dom/utils/Common'; import { Props } from '../dom/utils/Interface'; import { updateTextareaValue } from '../dom/valueHandler/TextareaValueHandler'; import { updateInputHandlerIfChanged } from '../dom/valueHandler/ValueChangeHandler'; @@ -41,14 +41,14 @@ function controlInputValue(inputDom: HTMLInputElement, props: Props) { const { name, type } = props; // 如果是 radio,找出同一form内,name相同的Radio,更新它们Handler的Value - if (type === 'radio' && !isNull(name)) { + if (type === 'radio' && isNotNull(name)) { const radioList = document.querySelectorAll(`input[type="radio"][name="${name}"]`); for (let i = 0; i < radioList.length; i++) { const radio = radioList[i]; if (radio === inputDom) { continue; } - if (!isNull(radio.form) && !isNull(inputDom.form) && radio.form !== inputDom.form) { + if (isNotNull(radio.form) && isNotNull(inputDom.form) && radio.form !== inputDom.form) { continue; } diff --git a/libs/horizon/src/renderer/TreeBuilder.ts b/libs/horizon/src/renderer/TreeBuilder.ts index cc99e968..ad27609c 100644 --- a/libs/horizon/src/renderer/TreeBuilder.ts +++ b/libs/horizon/src/renderer/TreeBuilder.ts @@ -54,7 +54,6 @@ import { import { getPathArr } from './utils/vNodePath'; import { injectUpdater } from '../external/devtools'; import { popCurrentRoot, pushCurrentRoot } from './RootStack'; -import { isNull } from '../dom/utils/Common'; // 不可恢复错误 let unrecoverableErrorDuringBuild: any = null; @@ -140,7 +139,7 @@ function bubbleVNode(vNode: VNode): void { node = parent; // 更新processing,抛出异常时可以使用 processing = node; - } while (!isNull(node)); + } while (node); // 修改结果 if (getBuildResult() === BuildInComplete) { @@ -181,7 +180,7 @@ function getChildByIndex(vNode: VNode, idx: number) { let node = vNode.child; for (let i = 0; i < idx; i++) { // 场景:当组件被销毁,业务若异步(定时器)调用setState修改状态,可能出现路径错误,此处进行保护。 - if (isNull(node)) { + if (node === null || node === undefined) { return null; } @@ -226,7 +225,7 @@ export function calcStartUpdateVNode(treeRoot: VNode) { const pathIndex = Number(startNodePath[i]); node = getChildByIndex(node, pathIndex)!; // 路径错误时,回退到从根更新 - if (isNull(node)) { + if (node === null) { return treeRoot; } } diff --git a/libs/horizon/src/renderer/hooks/UseImperativeHook.ts b/libs/horizon/src/renderer/hooks/UseImperativeHook.ts index 94d69743..9a39a69b 100644 --- a/libs/horizon/src/renderer/hooks/UseImperativeHook.ts +++ b/libs/horizon/src/renderer/hooks/UseImperativeHook.ts @@ -17,10 +17,7 @@ import { useLayoutEffectImpl } from './UseEffectHook'; import { getHookStage } from './HookStage'; import { throwNotInFuncError } from './BaseHook'; import type { Ref } from './HookType'; - -function isNotNull(object: any): boolean { - return object !== null && object !== undefined; -} +import { isNotNull } from '../../dom/utils/Common'; function effectFunc(func: () => R, ref: Ref | ((any) => any) | null): (() => void) | null { if (typeof ref === 'function') { diff --git a/libs/horizon/src/renderer/render/DomComponent.ts b/libs/horizon/src/renderer/render/DomComponent.ts index 247986ad..77273d96 100644 --- a/libs/horizon/src/renderer/render/DomComponent.ts +++ b/libs/horizon/src/renderer/render/DomComponent.ts @@ -23,7 +23,6 @@ import { markRef } from './BaseComponent'; import { DomComponent, DomPortal, DomText } from '../vnode/VNodeTags'; import { travelVNodeTree } from '../vnode/VNodeUtils'; import { createChildrenByDiff } from '../diff/nodeDiffComparator'; -import { isNull } from '../../dom/utils/Common'; function updateDom(processing: VNode, type: any, newProps: Props) { // 如果oldProps !== newProps,意味着存在更新,并且需要处理其相关的副作用 @@ -55,7 +54,7 @@ export function bubbleRender(processing: VNode) { const type = processing.type; const newProps = processing.props; - if (!processing.isCreated && !isNull(processing.realNode)) { + if (!processing.isCreated && processing.realNode !== null) { // 更新dom属性 updateDom(processing, type, newProps); diff --git a/libs/horizon/src/renderer/render/DomText.ts b/libs/horizon/src/renderer/render/DomText.ts index 2f8cec01..663f15f6 100644 --- a/libs/horizon/src/renderer/render/DomText.ts +++ b/libs/horizon/src/renderer/render/DomText.ts @@ -27,7 +27,7 @@ export function captureRender(): VNode | null { export function bubbleRender(processing: VNode) { const newText = processing.props; - if (!processing.isCreated && !isNull(processing.realNode)) { + if (!processing.isCreated && processing.realNode !== null) { // 更新 const oldText = processing.oldProps; // 如果文本不同,将其标记为更新 diff --git a/libs/horizon/src/renderer/vnode/VNodeCreator.ts b/libs/horizon/src/renderer/vnode/VNodeCreator.ts index 27e08e99..65b777c8 100644 --- a/libs/horizon/src/renderer/vnode/VNodeCreator.ts +++ b/libs/horizon/src/renderer/vnode/VNodeCreator.ts @@ -44,7 +44,6 @@ import { import { VNode } from './VNode'; import { JSXElement, Source } from '../Types'; import { markVNodePath } from '../utils/vNodePath'; -import { isNull } from '../../dom/utils/Common'; const typeLazyMap = { [TYPE_FORWARD_REF]: ForwardRef, @@ -137,7 +136,7 @@ export function createUndeterminedVNode(type, key, props, source: Source | null) vNodeTag = typeMap[type.vtype]; isLazy = type.vtype === TYPE_LAZY; } else { - throw Error(`Component type is invalid, got: ${isNull(type) ? type : componentType}`); + throw Error(`Component type is invalid, got: ${type === null || type === undefined ? type : componentType}`); } const vNode = newVirtualNode(vNodeTag, key, props);