Skip to content

Commit 4fed235

Browse files
committed
move numeric prop key utils to separate file
1 parent 4091b3e commit 4fed235

File tree

17 files changed

+109
-145
lines changed

17 files changed

+109
-145
lines changed

packages/qwik/src/core/client/dom-container.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ import {
3232
QLocaleAttr,
3333
} from '../shared/utils/markers';
3434
import { isPromise } from '../shared/utils/promises';
35-
import { getPropId, isSlotProp, type NumericPropKey } from '../shared/utils/prop';
35+
import { isSlotProp } from '../shared/utils/prop';
36+
import { getPropId, type NumericPropKey } from '../shared/utils/numeric-prop-key';
3637
import { qDev } from '../shared/utils/qdev';
3738
import {
3839
convertScopedStyleIdsToArray,

packages/qwik/src/core/client/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import type { QRL } from '../shared/qrl/qrl.public';
44
import type { Container } from '../shared/types';
5-
import type { NumericPropKey } from '../shared/utils/prop';
5+
import type { NumericPropKey } from '../shared/utils/numeric-prop-key';
66
import type { VNodeJournal } from './vnode';
77

88
export type ClientAttrValue = unknown | null;

packages/qwik/src/core/client/util-mapArray.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { assertTrue } from '../shared/error/assert';
2-
import type { NumericPropKey } from '../shared/utils/prop';
2+
import type { NumericPropKey } from '../shared/utils/numeric-prop-key';
33

44
export const mapApp_findIndx = <T>(
55
array: (T | null)[],

packages/qwik/src/core/client/vnode-diff.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,13 @@ import { getNewElementNamespaceData } from './vnode-namespace';
8888
import { EffectProperty, isSignal, SubscriptionData } from '../signal/signal';
8989
import type { Signal } from '../signal/signal.public';
9090
import { executeComponent } from '../shared/component-execution';
91+
import { getSlotName, isEventProp, isHandlerProp, isQProp, isSlotProp } from '../shared/utils/prop';
9192
import {
9293
StaticPropId,
9394
getPropId,
9495
getPropName,
95-
getSlotName,
96-
isEventProp,
97-
isHandlerProp,
98-
isQProp,
99-
isSlotProp,
10096
type NumericPropKey,
101-
} from '../shared/utils/prop';
97+
} from '../shared/utils/numeric-prop-key';
10298
import { escapeHTML } from '../shared/utils/character-escaping';
10399
import { clearAllEffects } from '../signal/signal-cleanup';
104100
import { serializeAttribute } from '../shared/utils/styles';

packages/qwik/src/core/client/vnode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ import {
170170
} from './vnode-namespace';
171171
import { mergeMaps } from '../shared/utils/maps';
172172
import { _EFFECT_BACK_REF } from '../signal/flags';
173-
import { getPropId, getPropName, type NumericPropKey } from '../shared/utils/prop';
173+
import { getPropId, getPropName, type NumericPropKey } from '../shared/utils/numeric-prop-key';
174174

175175
//////////////////////////////////////////////////////////////////////////////////////////////////////
176176

packages/qwik/src/core/shared/jsx/jsx-runtime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { WrappedSignal, WrappedSignalFlags } from '../../signal/signal';
1212
import type { DevJSX, FunctionComponent, JSXNode, JSXNodeInternal } from './types/jsx-node';
1313
import type { QwikJSX } from './types/jsx-qwik';
1414
import type { JSXChildren } from './types/jsx-qwik-attributes';
15-
import { getPropId, getPropName, type NumericPropKey } from '../utils/prop';
15+
import { getPropId, getPropName, type NumericPropKey } from '../utils/numeric-prop-key';
1616

1717
export type Props = Record<string, unknown>;
1818
export type PropsProxy = { [_VAR_PROPS]: Props; [_CONST_PROPS]: Props | null };

packages/qwik/src/core/shared/jsx/types/jsx-node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { NumericPropKey } from '../../utils/prop';
1+
import type { NumericPropKey } from '../../utils/numeric-prop-key';
22
import type { JSXChildren } from './jsx-qwik-attributes';
33

44
/**
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import {
2+
createEventName,
3+
isHtmlAttributeAnEventName,
4+
isJsxPropertyAnEventName,
5+
parseEventNameFromIndex,
6+
} from './event-names';
7+
import {
8+
ELEMENT_ID,
9+
ELEMENT_KEY,
10+
ELEMENT_PROPS,
11+
HANDLER_PREFIX,
12+
NON_SERIALIZABLE_MARKER_PREFIX,
13+
Q_PREFIX,
14+
dangerouslySetInnerHTML,
15+
refAttr,
16+
OnRenderProp,
17+
} from './markers';
18+
import type { KnownEventNames } from '@qwik.dev/core';
19+
20+
const propNameToId = new Map<string | symbol, NumericPropKey>();
21+
const idToPropName: (string | symbol)[] = [];
22+
export type NumericPropKey = number & { __brand__: 'NumericPropKey' };
23+
24+
const colonOnLength = ':on'.length;
25+
26+
export const enum NumericPropKeyFlags {
27+
EVENT = 1,
28+
Q_PREFIX = 2,
29+
HANDLER_PREFIX = 4,
30+
SLOT = 8,
31+
}
32+
33+
export const NumericFlagsShift = 4;
34+
35+
export const getPropId = (name: string | symbol): NumericPropKey => {
36+
let id = propNameToId.get(name);
37+
if (id != null) {
38+
return id;
39+
}
40+
id = (idToPropName.length << NumericFlagsShift) as NumericPropKey;
41+
if (typeof name === 'string') {
42+
if (isJsxPropertyAnEventName(name)) {
43+
name = normalizeEvent(name);
44+
(id as number) |= NumericPropKeyFlags.EVENT;
45+
} else if (isHtmlAttributeAnEventName(name)) {
46+
(id as number) |= NumericPropKeyFlags.EVENT;
47+
} else if (name.startsWith(Q_PREFIX)) {
48+
(id as number) |= NumericPropKeyFlags.Q_PREFIX;
49+
} else if (name.startsWith(HANDLER_PREFIX)) {
50+
(id as number) |= NumericPropKeyFlags.HANDLER_PREFIX;
51+
}
52+
53+
if (!name.startsWith(Q_PREFIX) && !name.startsWith(NON_SERIALIZABLE_MARKER_PREFIX)) {
54+
(id as number) |= NumericPropKeyFlags.SLOT;
55+
}
56+
}
57+
idToPropName.push(name);
58+
propNameToId.set(name, id);
59+
return id;
60+
};
61+
62+
export const StaticPropId = {
63+
// ELEMENT_KEY should be always first, because of `getKey` in vnode_diff.ts
64+
ELEMENT_KEY: getPropId(ELEMENT_KEY),
65+
ELEMENT_ID: getPropId(ELEMENT_ID),
66+
ELEMENT_PROPS: getPropId(ELEMENT_PROPS),
67+
REF: getPropId(refAttr),
68+
INNERHTML: getPropId(dangerouslySetInnerHTML),
69+
VALUE: getPropId('value'),
70+
ON_RENDER: getPropId(OnRenderProp),
71+
CLASS: getPropId('class'),
72+
CLASS_NAME: getPropId('classname'),
73+
};
74+
75+
export const getPropName = <T extends string>(id: NumericPropKey): T => {
76+
return idToPropName[id >> NumericFlagsShift] as T;
77+
};
78+
79+
function normalizeEvent(name: string): string {
80+
const index = name.indexOf(':on');
81+
const scope = (name.substring(0, index) || undefined) as 'window' | 'document' | undefined;
82+
const eventName = parseEventNameFromIndex(name, index + colonOnLength);
83+
name = createEventName(eventName, scope) as KnownEventNames;
84+
return name;
85+
}

packages/qwik/src/core/shared/utils/prop.ts

Lines changed: 7 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -7,94 +7,16 @@ import {
77
type PropsProxy,
88
} from '../jsx/jsx-runtime';
99
import type { JSXNodeInternal } from '../jsx/types/jsx-node';
10-
import type { KnownEventNames } from '../jsx/types/jsx-qwik-events';
1110
import type { Container, HostElement } from '../types';
1211
import { _CONST_PROPS, _VAR_PROPS } from './constants';
12+
import { QDefaultSlot } from './markers';
1313
import {
14-
createEventName,
15-
parseEventNameFromIndex,
16-
isJsxPropertyAnEventName,
17-
isHtmlAttributeAnEventName,
18-
} from './event-names';
19-
import {
20-
ELEMENT_ID,
21-
ELEMENT_KEY,
22-
ELEMENT_PROPS,
23-
HANDLER_PREFIX,
24-
NON_SERIALIZABLE_MARKER_PREFIX,
25-
OnRenderProp,
26-
QDefaultSlot,
27-
Q_PREFIX,
28-
dangerouslySetInnerHTML,
29-
refAttr,
30-
} from './markers';
31-
32-
const propNameToId = new Map<string | symbol, NumericPropKey>();
33-
const idToPropName: (string | symbol)[] = [];
34-
export type NumericPropKey = number & { __brand__: 'NumericPropKey' };
35-
36-
const colonOnLength = ':on'.length;
37-
38-
export const enum NumericPropKeyFlags {
39-
EVENT = 1,
40-
Q_PREFIX = 2,
41-
HANDLER_PREFIX = 4,
42-
SLOT = 8,
43-
}
44-
45-
export const NumericFlagsShift = 4;
46-
47-
export const getPropId = (name: string | symbol): NumericPropKey => {
48-
let id = propNameToId.get(name);
49-
if (id != null) {
50-
return id;
51-
}
52-
id = (idToPropName.length << NumericFlagsShift) as NumericPropKey;
53-
if (typeof name === 'string') {
54-
if (isJsxPropertyAnEventName(name)) {
55-
name = normalizeEvent(name);
56-
(id as number) |= NumericPropKeyFlags.EVENT;
57-
} else if (isHtmlAttributeAnEventName(name)) {
58-
(id as number) |= NumericPropKeyFlags.EVENT;
59-
} else if (name.startsWith(Q_PREFIX)) {
60-
(id as number) |= NumericPropKeyFlags.Q_PREFIX;
61-
} else if (name.startsWith(HANDLER_PREFIX)) {
62-
(id as number) |= NumericPropKeyFlags.HANDLER_PREFIX;
63-
}
64-
65-
if (!name.startsWith(Q_PREFIX) && !name.startsWith(NON_SERIALIZABLE_MARKER_PREFIX)) {
66-
(id as number) |= NumericPropKeyFlags.SLOT;
67-
}
68-
}
69-
idToPropName.push(name);
70-
propNameToId.set(name, id);
71-
return id;
72-
};
73-
74-
export const StaticPropId = {
75-
// ELEMENT_KEY should be always first, because of `getKey` in vnode_diff.ts
76-
ELEMENT_KEY: getPropId(ELEMENT_KEY),
77-
ELEMENT_ID: getPropId(ELEMENT_ID),
78-
ELEMENT_PROPS: getPropId(ELEMENT_PROPS),
79-
REF: getPropId(refAttr),
80-
INNERHTML: getPropId(dangerouslySetInnerHTML),
81-
VALUE: getPropId('value'),
82-
ON_RENDER: getPropId(OnRenderProp),
83-
CLASS: getPropId('class'),
84-
CLASS_NAME: getPropId('classname'),
85-
};
86-
87-
export const getPropName = <T extends string>(id: NumericPropKey): T => {
88-
return idToPropName[id >> NumericFlagsShift] as T;
89-
};
90-
91-
function normalizeEvent(name: string): string {
92-
const index = name.indexOf(':on');
93-
const scope = (name.substring(0, index) || undefined) as 'window' | 'document' | undefined;
94-
const eventName = parseEventNameFromIndex(name, index + colonOnLength);
95-
name = createEventName(eventName, scope) as KnownEventNames;
96-
return name;
97-
}
14+
NumericFlagsShift,
15+
NumericPropKeyFlags,
16+
getPropId,
17+
getPropName,
18+
type NumericPropKey,
19+
} from './numeric-prop-key';
9820

9921
function getFlags(id: number) {
10022
return ((1 << NumericFlagsShift) - 1) & (id >> 0);
@@ -154,8 +76,3 @@ export const _restProps = (props: PropsProxy, omit: string[], target: Props = {}
15476

15577
return createPropsProxy(varPropsTarget, constPropsTarget);
15678
};
157-
158-
export const __testing__ = {
159-
propNameToId,
160-
idToPropName,
161-
};

packages/qwik/src/core/shared/utils/prop.unit.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)