(
owner: Owner,
tags: ReadonlyArray<string> | undefined,
decorate: (
builder: any,
...args: ReadonlyArray<any>
) => ElementBuilder<any, any, any, any>
)
| 1123 | * @internal |
| 1124 | */ |
| 1125 | export const makePart = ( |
| 1126 | owner: Owner, |
| 1127 | tags: ReadonlyArray<string> | undefined, |
| 1128 | decorate: ( |
| 1129 | builder: any, |
| 1130 | ...args: ReadonlyArray<any> |
| 1131 | ) => ElementBuilder<any, any, any, any> |
| 1132 | ): Part<any, any, any> => |
| 1133 | (target: PartTarget<any>, ...args: ReadonlyArray<any>): any => { |
| 1134 | const builder: unknown = target |
| 1135 | if (!(builder instanceof ElementBuilder)) { |
| 1136 | throw new Error( |
| 1137 | owner.path + ": a part takes an element builder, such as h.button" |
| 1138 | ) |
| 1139 | } |
| 1140 | if (tags !== undefined && !tags.includes(builder.tag)) { |
| 1141 | throw new Error( |
| 1142 | owner.path + ": a part for <" + tags.join("> or <") + "> was given <" + |
| 1143 | builder.tag + ">" |
| 1144 | ) |
| 1145 | } |
| 1146 | const blank: ElementBuilder<any, any, any, any> = Object.assign( |
| 1147 | Object.create(Object.getPrototypeOf(builder)), |
| 1148 | blankFields, |
| 1149 | { tag: builder.tag, removals: [] } |
| 1150 | ) |
| 1151 | const patch = decorate(blank, ...args) |
| 1152 | if (!(patch instanceof ElementBuilder)) { |
| 1153 | throw new Error(owner.path + ": a part returns the builder it was given") |
| 1154 | } |
| 1155 | if (patch.elementKey !== undefined) { |
| 1156 | throw new Error(owner.path + ": a part can't set an element's key") |
| 1157 | } |
| 1158 | if (patch.owned.length > 0) { |
| 1159 | throw new Error(owner.path + ": a part can't apply another part") |
| 1160 | } |
| 1161 | const attrs: Record<string, string> = { ...builder.elementAttrs } |
| 1162 | let tokens: Record<string, string> | undefined |
| 1163 | for (const name of patch.removals ?? []) { |
| 1164 | // Token lists are additive: a part can omit its own tokens, not the consumer's. |
| 1165 | if (!tokenListAttributes.has(name)) delete attrs[name] |
| 1166 | } |
| 1167 | for (const name in patch.elementAttrs) { |
| 1168 | if (tokenListAttributes.has(name)) { |
| 1169 | ;(tokens ??= {})[name] = patch.elementAttrs[name]! |
| 1170 | } else attrs[name] = patch.elementAttrs[name]! |
| 1171 | } |
| 1172 | const layer: Owned = { |
| 1173 | owner, |
| 1174 | handlers: patch.handlers, |
| 1175 | eventOptions: patch.eventOptions, |
| 1176 | refKey: patch.refKey, |
| 1177 | mount: patch.mount, |
| 1178 | tokens: tokens ?? noFields |
| 1179 | } |
| 1180 | const out = withFields(builder, { |
| 1181 | elementAttrs: attrs, |
| 1182 | elementProps: patch.elementProps === noFields |
no test coverage detected