@bquery/bquery 1.5.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (380) hide show
  1. package/README.md +193 -23
  2. package/dist/a11y/announce.d.ts +43 -0
  3. package/dist/a11y/announce.d.ts.map +1 -0
  4. package/dist/a11y/audit.d.ts +42 -0
  5. package/dist/a11y/audit.d.ts.map +1 -0
  6. package/dist/a11y/index.d.ts +53 -0
  7. package/dist/a11y/index.d.ts.map +1 -0
  8. package/dist/a11y/media-preferences.d.ts +77 -0
  9. package/dist/a11y/media-preferences.d.ts.map +1 -0
  10. package/dist/a11y/roving-tab-index.d.ts +38 -0
  11. package/dist/a11y/roving-tab-index.d.ts.map +1 -0
  12. package/dist/a11y/skip-link.d.ts +37 -0
  13. package/dist/a11y/skip-link.d.ts.map +1 -0
  14. package/dist/a11y/trap-focus.d.ts +49 -0
  15. package/dist/a11y/trap-focus.d.ts.map +1 -0
  16. package/dist/a11y/types.d.ts +152 -0
  17. package/dist/a11y/types.d.ts.map +1 -0
  18. package/dist/a11y-C5QOVvRn.js +421 -0
  19. package/dist/a11y-C5QOVvRn.js.map +1 -0
  20. package/dist/a11y.es.mjs +14 -0
  21. package/dist/component/component.d.ts +13 -5
  22. package/dist/component/component.d.ts.map +1 -1
  23. package/dist/component/html.d.ts +40 -3
  24. package/dist/component/html.d.ts.map +1 -1
  25. package/dist/component/index.d.ts +3 -2
  26. package/dist/component/index.d.ts.map +1 -1
  27. package/dist/component/library.d.ts.map +1 -1
  28. package/dist/component/scope.d.ts +138 -0
  29. package/dist/component/scope.d.ts.map +1 -0
  30. package/dist/component/types.d.ts +184 -17
  31. package/dist/component/types.d.ts.map +1 -1
  32. package/dist/component-CuuTijA6.js +684 -0
  33. package/dist/component-CuuTijA6.js.map +1 -0
  34. package/dist/component.es.mjs +10 -6
  35. package/dist/{config-DRmZZno3.js → config-BW35FKuA.js} +4 -4
  36. package/dist/config-BW35FKuA.js.map +1 -0
  37. package/dist/constraints-3lV9yyBw.js +100 -0
  38. package/dist/constraints-3lV9yyBw.js.map +1 -0
  39. package/dist/core/collection.d.ts +48 -0
  40. package/dist/core/collection.d.ts.map +1 -1
  41. package/dist/core/element.d.ts +92 -0
  42. package/dist/core/element.d.ts.map +1 -1
  43. package/dist/core/env.d.ts +18 -0
  44. package/dist/core/env.d.ts.map +1 -0
  45. package/dist/core/index.d.ts +1 -0
  46. package/dist/core/index.d.ts.map +1 -1
  47. package/dist/core/shared.d.ts +8 -0
  48. package/dist/core/shared.d.ts.map +1 -1
  49. package/dist/core/utils/index.d.ts +52 -41
  50. package/dist/core/utils/index.d.ts.map +1 -1
  51. package/dist/core-Cjl7GUu8.js +717 -0
  52. package/dist/core-Cjl7GUu8.js.map +1 -0
  53. package/dist/{core-DPdbItcq.js → core-DnlyjbF2.js} +1 -1
  54. package/dist/{core-DPdbItcq.js.map → core-DnlyjbF2.js.map} +1 -1
  55. package/dist/core.es.mjs +45 -44
  56. package/dist/custom-directives-7wAShnnd.js +9 -0
  57. package/dist/custom-directives-7wAShnnd.js.map +1 -0
  58. package/dist/devtools/devtools.d.ts +212 -0
  59. package/dist/devtools/devtools.d.ts.map +1 -0
  60. package/dist/devtools/index.d.ts +20 -0
  61. package/dist/devtools/index.d.ts.map +1 -0
  62. package/dist/devtools/types.d.ts +69 -0
  63. package/dist/devtools/types.d.ts.map +1 -0
  64. package/dist/devtools-D2fQLhDN.js +122 -0
  65. package/dist/devtools-D2fQLhDN.js.map +1 -0
  66. package/dist/devtools.es.mjs +19 -0
  67. package/dist/dnd/draggable.d.ts +51 -0
  68. package/dist/dnd/draggable.d.ts.map +1 -0
  69. package/dist/dnd/droppable.d.ts +38 -0
  70. package/dist/dnd/droppable.d.ts.map +1 -0
  71. package/dist/dnd/index.d.ts +47 -0
  72. package/dist/dnd/index.d.ts.map +1 -0
  73. package/dist/dnd/sortable.d.ts +43 -0
  74. package/dist/dnd/sortable.d.ts.map +1 -0
  75. package/dist/dnd/types.d.ts +250 -0
  76. package/dist/dnd/types.d.ts.map +1 -0
  77. package/dist/dnd-B8EgyzaI.js +244 -0
  78. package/dist/dnd-B8EgyzaI.js.map +1 -0
  79. package/dist/dnd.es.mjs +6 -0
  80. package/dist/env-NeVmr4Gf.js +19 -0
  81. package/dist/env-NeVmr4Gf.js.map +1 -0
  82. package/dist/forms/create-form.d.ts +49 -0
  83. package/dist/forms/create-form.d.ts.map +1 -0
  84. package/dist/forms/index.d.ts +39 -0
  85. package/dist/forms/index.d.ts.map +1 -0
  86. package/dist/forms/types.d.ts +139 -0
  87. package/dist/forms/types.d.ts.map +1 -0
  88. package/dist/forms/validators.d.ts +179 -0
  89. package/dist/forms/validators.d.ts.map +1 -0
  90. package/dist/forms-C3yovgH9.js +141 -0
  91. package/dist/forms-C3yovgH9.js.map +1 -0
  92. package/dist/forms.es.mjs +14 -0
  93. package/dist/full.d.ts +37 -9
  94. package/dist/full.d.ts.map +1 -1
  95. package/dist/full.es.mjs +186 -91
  96. package/dist/full.iife.js +47 -31
  97. package/dist/full.iife.js.map +1 -1
  98. package/dist/full.umd.js +47 -31
  99. package/dist/full.umd.js.map +1 -1
  100. package/dist/i18n/formatting.d.ts +40 -0
  101. package/dist/i18n/formatting.d.ts.map +1 -0
  102. package/dist/i18n/i18n.d.ts +48 -0
  103. package/dist/i18n/i18n.d.ts.map +1 -0
  104. package/dist/i18n/index.d.ts +57 -0
  105. package/dist/i18n/index.d.ts.map +1 -0
  106. package/dist/i18n/translate.d.ts +83 -0
  107. package/dist/i18n/translate.d.ts.map +1 -0
  108. package/dist/i18n/types.d.ts +156 -0
  109. package/dist/i18n/types.d.ts.map +1 -0
  110. package/dist/i18n-BnnhTFOS.js +89 -0
  111. package/dist/i18n-BnnhTFOS.js.map +1 -0
  112. package/dist/i18n.es.mjs +6 -0
  113. package/dist/index.d.ts +11 -0
  114. package/dist/index.d.ts.map +1 -1
  115. package/dist/index.es.mjs +233 -138
  116. package/dist/media/battery.d.ts +35 -0
  117. package/dist/media/battery.d.ts.map +1 -0
  118. package/dist/media/breakpoints.d.ts +51 -0
  119. package/dist/media/breakpoints.d.ts.map +1 -0
  120. package/dist/media/clipboard.d.ts +30 -0
  121. package/dist/media/clipboard.d.ts.map +1 -0
  122. package/dist/media/device-sensors.d.ts +54 -0
  123. package/dist/media/device-sensors.d.ts.map +1 -0
  124. package/dist/media/geolocation.d.ts +38 -0
  125. package/dist/media/geolocation.d.ts.map +1 -0
  126. package/dist/media/index.d.ts +42 -0
  127. package/dist/media/index.d.ts.map +1 -0
  128. package/dist/media/media-query.d.ts +36 -0
  129. package/dist/media/media-query.d.ts.map +1 -0
  130. package/dist/media/network.d.ts +35 -0
  131. package/dist/media/network.d.ts.map +1 -0
  132. package/dist/media/types.d.ts +173 -0
  133. package/dist/media/types.d.ts.map +1 -0
  134. package/dist/media/viewport.d.ts +32 -0
  135. package/dist/media/viewport.d.ts.map +1 -0
  136. package/dist/media-Di2Ta22s.js +340 -0
  137. package/dist/media-Di2Ta22s.js.map +1 -0
  138. package/dist/media.es.mjs +12 -0
  139. package/dist/motion/index.d.ts +7 -3
  140. package/dist/motion/index.d.ts.map +1 -1
  141. package/dist/motion/morph.d.ts +27 -0
  142. package/dist/motion/morph.d.ts.map +1 -0
  143. package/dist/motion/parallax.d.ts +30 -0
  144. package/dist/motion/parallax.d.ts.map +1 -0
  145. package/dist/motion/reduced-motion.d.ts +36 -3
  146. package/dist/motion/reduced-motion.d.ts.map +1 -1
  147. package/dist/motion/types.d.ts +58 -0
  148. package/dist/motion/types.d.ts.map +1 -1
  149. package/dist/motion/typewriter.d.ts +31 -0
  150. package/dist/motion/typewriter.d.ts.map +1 -0
  151. package/dist/motion-qPj_TYGv.js +530 -0
  152. package/dist/motion-qPj_TYGv.js.map +1 -0
  153. package/dist/motion.es.mjs +27 -23
  154. package/dist/mount-SM07RUa6.js +403 -0
  155. package/dist/mount-SM07RUa6.js.map +1 -0
  156. package/dist/{object-qGpWr6-J.js → object-BCk-1c8T.js} +5 -4
  157. package/dist/{object-qGpWr6-J.js.map → object-BCk-1c8T.js.map} +1 -1
  158. package/dist/{platform-B7JhGBc7.js → platform-CPbCprb6.js} +3 -3
  159. package/dist/platform-CPbCprb6.js.map +1 -0
  160. package/dist/platform.es.mjs +2 -2
  161. package/dist/plugin/index.d.ts +22 -0
  162. package/dist/plugin/index.d.ts.map +1 -0
  163. package/dist/plugin/registry.d.ts +108 -0
  164. package/dist/plugin/registry.d.ts.map +1 -0
  165. package/dist/plugin/types.d.ts +110 -0
  166. package/dist/plugin/types.d.ts.map +1 -0
  167. package/dist/plugin-cPoOHFLY.js +64 -0
  168. package/dist/plugin-cPoOHFLY.js.map +1 -0
  169. package/dist/plugin.es.mjs +9 -0
  170. package/dist/reactive/computed.d.ts +7 -0
  171. package/dist/reactive/computed.d.ts.map +1 -1
  172. package/dist/reactive-Cfv0RK6x.js +233 -0
  173. package/dist/reactive-Cfv0RK6x.js.map +1 -0
  174. package/dist/reactive.es.mjs +18 -17
  175. package/dist/registry-CWf368tT.js +26 -0
  176. package/dist/registry-CWf368tT.js.map +1 -0
  177. package/dist/router/bq-link.d.ts +112 -0
  178. package/dist/router/bq-link.d.ts.map +1 -0
  179. package/dist/router/constraints.d.ts +9 -0
  180. package/dist/router/constraints.d.ts.map +1 -0
  181. package/dist/router/index.d.ts +14 -6
  182. package/dist/router/index.d.ts.map +1 -1
  183. package/dist/router/match.d.ts +0 -1
  184. package/dist/router/match.d.ts.map +1 -1
  185. package/dist/router/path-pattern.d.ts +14 -0
  186. package/dist/router/path-pattern.d.ts.map +1 -0
  187. package/dist/router/query.d.ts.map +1 -1
  188. package/dist/router/router.d.ts +3 -1
  189. package/dist/router/router.d.ts.map +1 -1
  190. package/dist/router/types.d.ts +48 -4
  191. package/dist/router/types.d.ts.map +1 -1
  192. package/dist/router/use-route.d.ts +50 -0
  193. package/dist/router/use-route.d.ts.map +1 -0
  194. package/dist/router/utils.d.ts +3 -0
  195. package/dist/router/utils.d.ts.map +1 -1
  196. package/dist/router-BrthaP_z.js +473 -0
  197. package/dist/router-BrthaP_z.js.map +1 -0
  198. package/dist/router.es.mjs +13 -10
  199. package/dist/{sanitize-jyJ2ryE2.js → sanitize-B1V4JswB.js} +95 -83
  200. package/dist/sanitize-B1V4JswB.js.map +1 -0
  201. package/dist/security/index.d.ts +2 -0
  202. package/dist/security/index.d.ts.map +1 -1
  203. package/dist/security/sanitize.d.ts +4 -1
  204. package/dist/security/sanitize.d.ts.map +1 -1
  205. package/dist/security/trusted-html.d.ts +53 -0
  206. package/dist/security/trusted-html.d.ts.map +1 -0
  207. package/dist/security.es.mjs +10 -9
  208. package/dist/ssr/hydrate.d.ts +65 -0
  209. package/dist/ssr/hydrate.d.ts.map +1 -0
  210. package/dist/ssr/index.d.ts +59 -0
  211. package/dist/ssr/index.d.ts.map +1 -0
  212. package/dist/ssr/render.d.ts +62 -0
  213. package/dist/ssr/render.d.ts.map +1 -0
  214. package/dist/ssr/serialize.d.ts +118 -0
  215. package/dist/ssr/serialize.d.ts.map +1 -0
  216. package/dist/ssr/types.d.ts +70 -0
  217. package/dist/ssr/types.d.ts.map +1 -0
  218. package/dist/ssr-B2qd_WBB.js +248 -0
  219. package/dist/ssr-B2qd_WBB.js.map +1 -0
  220. package/dist/ssr.es.mjs +9 -0
  221. package/dist/store/create-store.d.ts.map +1 -1
  222. package/dist/store/define-store.d.ts +1 -1
  223. package/dist/store/define-store.d.ts.map +1 -1
  224. package/dist/store/index.d.ts +1 -1
  225. package/dist/store/index.d.ts.map +1 -1
  226. package/dist/store/mapping.d.ts +1 -1
  227. package/dist/store/mapping.d.ts.map +1 -1
  228. package/dist/store/persisted.d.ts +38 -4
  229. package/dist/store/persisted.d.ts.map +1 -1
  230. package/dist/store/types.d.ts +140 -3
  231. package/dist/store/types.d.ts.map +1 -1
  232. package/dist/store/utils.d.ts +2 -2
  233. package/dist/store/utils.d.ts.map +1 -1
  234. package/dist/store/watch.d.ts +1 -1
  235. package/dist/store/watch.d.ts.map +1 -1
  236. package/dist/store-DWpyH6p5.js +338 -0
  237. package/dist/store-DWpyH6p5.js.map +1 -0
  238. package/dist/store.es.mjs +11 -10
  239. package/dist/storybook/index.d.ts +37 -0
  240. package/dist/storybook/index.d.ts.map +1 -0
  241. package/dist/storybook.es.mjs +151 -0
  242. package/dist/storybook.es.mjs.map +1 -0
  243. package/dist/testing/index.d.ts +23 -0
  244. package/dist/testing/index.d.ts.map +1 -0
  245. package/dist/testing/testing.d.ts +156 -0
  246. package/dist/testing/testing.d.ts.map +1 -0
  247. package/dist/testing/types.d.ts +134 -0
  248. package/dist/testing/types.d.ts.map +1 -0
  249. package/dist/testing-CsqjNUyy.js +224 -0
  250. package/dist/testing-CsqjNUyy.js.map +1 -0
  251. package/dist/testing.es.mjs +9 -0
  252. package/dist/type-guards-Do9DWgNp.js +44 -0
  253. package/dist/type-guards-Do9DWgNp.js.map +1 -0
  254. package/dist/untrack-DJVQQ2WM.js +33 -0
  255. package/dist/untrack-DJVQQ2WM.js.map +1 -0
  256. package/dist/view/custom-directives.d.ts +20 -0
  257. package/dist/view/custom-directives.d.ts.map +1 -0
  258. package/dist/view/evaluate.d.ts.map +1 -1
  259. package/dist/view/process.d.ts.map +1 -1
  260. package/dist/view.es.mjs +11 -10
  261. package/package.json +52 -11
  262. package/src/a11y/announce.ts +131 -0
  263. package/src/a11y/audit.ts +314 -0
  264. package/src/a11y/index.ts +68 -0
  265. package/src/a11y/media-preferences.ts +255 -0
  266. package/src/a11y/roving-tab-index.ts +164 -0
  267. package/src/a11y/skip-link.ts +255 -0
  268. package/src/a11y/trap-focus.ts +184 -0
  269. package/src/a11y/types.ts +183 -0
  270. package/src/component/component.ts +345 -65
  271. package/src/component/html.ts +153 -53
  272. package/src/component/index.ts +12 -2
  273. package/src/component/library.ts +66 -28
  274. package/src/component/scope.ts +212 -0
  275. package/src/component/types.ts +238 -19
  276. package/src/core/collection.ts +707 -628
  277. package/src/core/element.ts +981 -774
  278. package/src/core/env.ts +60 -0
  279. package/src/core/index.ts +49 -48
  280. package/src/core/shared.ts +62 -13
  281. package/src/core/utils/index.ts +148 -83
  282. package/src/devtools/devtools.ts +410 -0
  283. package/src/devtools/index.ts +48 -0
  284. package/src/devtools/types.ts +104 -0
  285. package/src/dnd/draggable.ts +296 -0
  286. package/src/dnd/droppable.ts +228 -0
  287. package/src/dnd/index.ts +62 -0
  288. package/src/dnd/sortable.ts +307 -0
  289. package/src/dnd/types.ts +293 -0
  290. package/src/forms/create-form.ts +278 -0
  291. package/src/forms/index.ts +65 -0
  292. package/src/forms/types.ts +154 -0
  293. package/src/forms/validators.ts +265 -0
  294. package/src/full.ts +260 -3
  295. package/src/i18n/formatting.ts +67 -0
  296. package/src/i18n/i18n.ts +200 -0
  297. package/src/i18n/index.ts +67 -0
  298. package/src/i18n/translate.ts +182 -0
  299. package/src/i18n/types.ts +171 -0
  300. package/src/index.ts +108 -36
  301. package/src/media/battery.ts +116 -0
  302. package/src/media/breakpoints.ts +131 -0
  303. package/src/media/clipboard.ts +80 -0
  304. package/src/media/device-sensors.ts +158 -0
  305. package/src/media/geolocation.ts +119 -0
  306. package/src/media/index.ts +76 -0
  307. package/src/media/media-query.ts +92 -0
  308. package/src/media/network.ts +115 -0
  309. package/src/media/types.ts +177 -0
  310. package/src/media/viewport.ts +84 -0
  311. package/src/motion/index.ts +57 -48
  312. package/src/motion/morph.ts +151 -0
  313. package/src/motion/parallax.ts +120 -0
  314. package/src/motion/reduced-motion.ts +66 -17
  315. package/src/motion/transition.ts +97 -97
  316. package/src/motion/types.ts +63 -0
  317. package/src/motion/typewriter.ts +164 -0
  318. package/src/platform/announcer.ts +208 -208
  319. package/src/platform/config.ts +163 -163
  320. package/src/platform/cookies.ts +165 -165
  321. package/src/platform/index.ts +39 -39
  322. package/src/platform/meta.ts +168 -168
  323. package/src/plugin/index.ts +37 -0
  324. package/src/plugin/registry.ts +269 -0
  325. package/src/plugin/types.ts +137 -0
  326. package/src/reactive/async-data.ts +486 -486
  327. package/src/reactive/computed.ts +130 -92
  328. package/src/reactive/index.ts +37 -37
  329. package/src/reactive/signal.ts +29 -29
  330. package/src/router/bq-link.ts +279 -0
  331. package/src/router/constraints.ts +201 -0
  332. package/src/router/index.ts +49 -41
  333. package/src/router/match.ts +312 -106
  334. package/src/router/path-pattern.ts +52 -0
  335. package/src/router/query.ts +38 -35
  336. package/src/router/router.ts +402 -211
  337. package/src/router/types.ts +139 -93
  338. package/src/router/use-route.ts +68 -0
  339. package/src/router/utils.ts +157 -116
  340. package/src/security/constants.ts +211 -211
  341. package/src/security/index.ts +12 -10
  342. package/src/security/sanitize.ts +6 -2
  343. package/src/security/trusted-html.ts +71 -0
  344. package/src/ssr/hydrate.ts +82 -0
  345. package/src/ssr/index.ts +70 -0
  346. package/src/ssr/render.ts +508 -0
  347. package/src/ssr/serialize.ts +296 -0
  348. package/src/ssr/types.ts +81 -0
  349. package/src/store/create-store.ts +467 -329
  350. package/src/store/define-store.ts +2 -1
  351. package/src/store/index.ts +27 -22
  352. package/src/store/mapping.ts +2 -1
  353. package/src/store/persisted.ts +249 -61
  354. package/src/store/types.ts +247 -94
  355. package/src/store/utils.ts +135 -141
  356. package/src/store/watch.ts +2 -1
  357. package/src/storybook/index.ts +480 -0
  358. package/src/testing/index.ts +42 -0
  359. package/src/testing/testing.ts +593 -0
  360. package/src/testing/types.ts +170 -0
  361. package/src/view/custom-directives.ts +30 -0
  362. package/src/view/evaluate.ts +292 -290
  363. package/src/view/process.ts +108 -92
  364. package/dist/component-CY5MVoYN.js +0 -531
  365. package/dist/component-CY5MVoYN.js.map +0 -1
  366. package/dist/config-DRmZZno3.js.map +0 -1
  367. package/dist/core-CK2Mfpf4.js +0 -648
  368. package/dist/core-CK2Mfpf4.js.map +0 -1
  369. package/dist/motion-C5DRdPnO.js +0 -415
  370. package/dist/motion-C5DRdPnO.js.map +0 -1
  371. package/dist/platform-B7JhGBc7.js.map +0 -1
  372. package/dist/reactive-BDya-ia8.js +0 -253
  373. package/dist/reactive-BDya-ia8.js.map +0 -1
  374. package/dist/router-CijiICxt.js +0 -188
  375. package/dist/router-CijiICxt.js.map +0 -1
  376. package/dist/sanitize-jyJ2ryE2.js.map +0 -1
  377. package/dist/store-CPK9E62U.js +0 -262
  378. package/dist/store-CPK9E62U.js.map +0 -1
  379. package/dist/view-Cdi0g-qo.js +0 -396
  380. package/dist/view-Cdi0g-qo.js.map +0 -1
@@ -1,774 +1,981 @@
1
- import { createElementFromHtml, insertContent, setHtml } from './dom';
2
-
3
- /**
4
- * Wrapper for a single DOM element.
5
- * Provides a chainable, jQuery-like API for DOM manipulation.
6
- *
7
- * This class encapsulates a DOM element and provides methods for:
8
- * - Class manipulation (addClass, removeClass, toggleClass)
9
- * - Attribute and property access (attr, prop, data)
10
- * - Content manipulation (text, html, append, prepend)
11
- * - Style manipulation (css)
12
- * - Event handling (on, off, once, trigger)
13
- * - DOM traversal (find, closest, parent, children, siblings)
14
- *
15
- * All mutating methods return `this` for method chaining.
16
- *
17
- * @example
18
- * ```ts
19
- * $('#button')
20
- * .addClass('active')
21
- * .css({ color: 'blue' })
22
- * .on('click', () => console.log('clicked'));
23
- * ```
24
- */
25
- /** Handler signature for delegated events */
26
- type DelegatedHandler = (event: Event, target: Element) => void;
27
-
28
- export class BQueryElement {
29
- /**
30
- * Stores delegated event handlers for cleanup via undelegate().
31
- * Key format: `${event}:${selector}`
32
- * @internal
33
- */
34
- private readonly delegatedHandlers = new Map<string, Map<DelegatedHandler, EventListener>>();
35
-
36
- /**
37
- * Creates a new BQueryElement wrapper.
38
- * @param element - The DOM element to wrap
39
- */
40
- constructor(private readonly element: Element) {}
41
-
42
- /**
43
- * Exposes the raw DOM element when direct access is needed.
44
- * Use sparingly; prefer the wrapper methods for consistency.
45
- */
46
- get raw(): Element {
47
- return this.element;
48
- }
49
-
50
- /**
51
- * Exposes the underlying DOM element.
52
- * Provided for spec compatibility and read-only access.
53
- */
54
- get node(): Element {
55
- return this.element;
56
- }
57
-
58
- /** Add one or more classes. */
59
- addClass(...classNames: string[]): this {
60
- this.element.classList.add(...classNames);
61
- return this;
62
- }
63
-
64
- /** Remove one or more classes. */
65
- removeClass(...classNames: string[]): this {
66
- this.element.classList.remove(...classNames);
67
- return this;
68
- }
69
-
70
- /** Toggle a class by name. */
71
- toggleClass(className: string, force?: boolean): this {
72
- this.element.classList.toggle(className, force);
73
- return this;
74
- }
75
-
76
- /** Get or set an attribute. */
77
- attr(name: string, value?: string): string | this {
78
- if (value === undefined) {
79
- return this.element.getAttribute(name) ?? '';
80
- }
81
- this.element.setAttribute(name, value);
82
- return this;
83
- }
84
-
85
- /** Remove an attribute. */
86
- removeAttr(name: string): this {
87
- this.element.removeAttribute(name);
88
- return this;
89
- }
90
-
91
- /** Toggle an attribute on/off. */
92
- toggleAttr(name: string, force?: boolean): this {
93
- const hasAttr = this.element.hasAttribute(name);
94
- const shouldAdd = force ?? !hasAttr;
95
- if (shouldAdd) {
96
- this.element.setAttribute(name, '');
97
- } else {
98
- this.element.removeAttribute(name);
99
- }
100
- return this;
101
- }
102
-
103
- /** Get or set a property. */
104
- prop<T extends keyof Element>(name: T, value?: Element[T]): Element[T] | this {
105
- if (value === undefined) {
106
- return this.element[name];
107
- }
108
- this.element[name] = value;
109
- return this;
110
- }
111
-
112
- /** Read or write data attributes in camelCase. */
113
- data(name: string, value?: string): string | this {
114
- const key = name.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
115
- if (value === undefined) {
116
- return this.element.getAttribute(`data-${key}`) ?? '';
117
- }
118
- this.element.setAttribute(`data-${key}`, value);
119
- return this;
120
- }
121
-
122
- /** Get or set text content. */
123
- text(value?: string): string | this {
124
- if (value === undefined) {
125
- return this.element.textContent ?? '';
126
- }
127
- this.element.textContent = value;
128
- return this;
129
- }
130
-
131
- /** Set HTML content using a sanitized string. */
132
- /**
133
- * Sets sanitized HTML content on the element.
134
- * Uses the security module to sanitize input and prevent XSS attacks.
135
- *
136
- * @param value - The HTML string to set (will be sanitized)
137
- * @returns The instance for method chaining
138
- *
139
- * @example
140
- * ```ts
141
- * $('#content').html('<strong>Hello</strong>');
142
- * ```
143
- */
144
- html(value: string): this {
145
- setHtml(this.element, value);
146
- return this;
147
- }
148
-
149
- /**
150
- * Sets HTML content without sanitization.
151
- * Use only when you trust the HTML source completely.
152
- *
153
- * @param value - The raw HTML string to set
154
- * @returns The instance for method chaining
155
- *
156
- * @warning This method bypasses XSS protection. Use with caution.
157
- */
158
- htmlUnsafe(value: string): this {
159
- this.element.innerHTML = value;
160
- return this;
161
- }
162
-
163
- /**
164
- * Gets or sets CSS styles on the element.
165
- *
166
- * @param property - A CSS property name or an object of property-value pairs
167
- * @param value - The value when setting a single property
168
- * @returns The computed style value when getting a single property, or the instance for method chaining when setting
169
- *
170
- * @example
171
- * ```ts
172
- * // Get a computed style value
173
- * const color = $('#box').css('color');
174
- *
175
- * // Set a single property
176
- * $('#box').css('color', 'red');
177
- *
178
- * // Set multiple properties
179
- * $('#box').css({ color: 'red', 'font-size': '16px' });
180
- * ```
181
- */
182
- css(property: string): string;
183
- css(property: string, value: string): this;
184
- css(property: Record<string, string>): this;
185
- css(property: string | Record<string, string>, value?: string): string | this {
186
- if (typeof property === 'string') {
187
- if (value !== undefined) {
188
- (this.element as HTMLElement).style.setProperty(property, value);
189
- return this;
190
- }
191
- const view = this.element.ownerDocument?.defaultView;
192
- if (!view || typeof view.getComputedStyle !== 'function') {
193
- return '';
194
- }
195
- return view.getComputedStyle(this.element).getPropertyValue(property);
196
- }
197
-
198
- for (const [key, val] of Object.entries(property)) {
199
- (this.element as HTMLElement).style.setProperty(key, val);
200
- }
201
- return this;
202
- }
203
-
204
- /**
205
- * Appends HTML or elements to the end of the element.
206
- *
207
- * @param content - HTML string or element(s) to append
208
- * @returns The instance for method chaining
209
- */
210
- append(content: string | Element | Element[]): this {
211
- this.insertContent(content, 'beforeend');
212
- return this;
213
- }
214
-
215
- /**
216
- * Prepends HTML or elements to the beginning of the element.
217
- *
218
- * @param content - HTML string or element(s) to prepend
219
- * @returns The instance for method chaining
220
- */
221
- prepend(content: string | Element | Element[]): this {
222
- this.insertContent(content, 'afterbegin');
223
- return this;
224
- }
225
-
226
- /**
227
- * Inserts content before this element.
228
- *
229
- * @param content - HTML string or element(s) to insert
230
- * @returns The instance for method chaining
231
- */
232
- before(content: string | Element | Element[]): this {
233
- this.insertContent(content, 'beforebegin');
234
- return this;
235
- }
236
-
237
- /**
238
- * Inserts content after this element.
239
- *
240
- * @param content - HTML string or element(s) to insert
241
- * @returns The instance for method chaining
242
- */
243
- after(content: string | Element | Element[]): this {
244
- this.insertContent(content, 'afterend');
245
- return this;
246
- }
247
-
248
- /**
249
- * Wraps the element with the specified wrapper element or tag.
250
- *
251
- * @param wrapper - Tag name string or Element to wrap with
252
- * @returns The instance for method chaining
253
- *
254
- * @example
255
- * ```ts
256
- * $('#content').wrap('div'); // Wraps with <div>
257
- * $('#content').wrap(document.createElement('section'));
258
- * ```
259
- */
260
- wrap(wrapper: string | Element): this {
261
- const wrapperEl = typeof wrapper === 'string' ? document.createElement(wrapper) : wrapper;
262
- this.element.parentNode?.insertBefore(wrapperEl, this.element);
263
- wrapperEl.appendChild(this.element);
264
- return this;
265
- }
266
-
267
- /**
268
- * Removes the parent element, keeping this element in its place.
269
- * Essentially the opposite of wrap().
270
- *
271
- * **Important**: This method only moves the current element out of its parent
272
- * before removing the parent. Any sibling elements will be removed along with
273
- * the parent. For unwrapping multiple siblings, use a collection: `$$(siblings).unwrap()`.
274
- *
275
- * @returns The instance for method chaining
276
- *
277
- * @example
278
- * ```ts
279
- * // Before: <div><span id="text">Hello</span></div>
280
- * $('#text').unwrap();
281
- * // After: <span id="text">Hello</span>
282
- * ```
283
- */
284
- unwrap(): this {
285
- const parent = this.element.parentElement;
286
- if (parent && parent.parentNode) {
287
- parent.parentNode.insertBefore(this.element, parent);
288
- parent.remove();
289
- }
290
- return this;
291
- }
292
-
293
- /**
294
- * Replaces this element with new content.
295
- *
296
- * @param content - HTML string (sanitized) or Element to replace with
297
- * @returns A new BQueryElement wrapping the replacement element
298
- *
299
- * @example
300
- * ```ts
301
- * const newEl = $('#old').replaceWith('<div id="new">Replaced</div>');
302
- * ```
303
- */
304
- replaceWith(content: string | Element): BQueryElement {
305
- const newEl = typeof content === 'string' ? createElementFromHtml(content) : content;
306
- this.element.replaceWith(newEl);
307
- return new BQueryElement(newEl);
308
- }
309
-
310
- /**
311
- * Scrolls the element into view with configurable behavior.
312
- *
313
- * @param options - ScrollIntoView options or boolean for legacy behavior
314
- * @returns The instance for method chaining
315
- *
316
- * @example
317
- * ```ts
318
- * $('#section').scrollTo(); // Smooth scroll
319
- * $('#section').scrollTo({ behavior: 'instant', block: 'start' });
320
- * ```
321
- */
322
- scrollTo(options: ScrollIntoViewOptions | boolean = { behavior: 'smooth' }): this {
323
- this.element.scrollIntoView(options);
324
- return this;
325
- }
326
-
327
- /**
328
- * Removes the element from the DOM.
329
- *
330
- * @returns The instance for method chaining (though element is now detached)
331
- */
332
- remove(): this {
333
- this.element.remove();
334
- return this;
335
- }
336
-
337
- /**
338
- * Clears all child nodes from the element.
339
- *
340
- * @returns The instance for method chaining
341
- */
342
- empty(): this {
343
- this.element.innerHTML = '';
344
- return this;
345
- }
346
-
347
- /**
348
- * Clones the element, optionally with all descendants.
349
- *
350
- * @param deep - If true, clone all descendants (default: true)
351
- * @returns A new BQueryElement wrapping the cloned element
352
- */
353
- clone(deep: boolean = true): BQueryElement {
354
- return new BQueryElement(this.element.cloneNode(deep) as Element);
355
- }
356
-
357
- /**
358
- * Finds all descendant elements matching the selector.
359
- *
360
- * @param selector - CSS selector to match
361
- * @returns Array of matching elements
362
- */
363
- find(selector: string): Element[] {
364
- return Array.from(this.element.querySelectorAll(selector));
365
- }
366
-
367
- /**
368
- * Finds the first descendant element matching the selector.
369
- *
370
- * @param selector - CSS selector to match
371
- * @returns The first matching element or null
372
- */
373
- findOne(selector: string): Element | null {
374
- return this.element.querySelector(selector);
375
- }
376
-
377
- /**
378
- * Finds the closest ancestor matching the selector.
379
- *
380
- * @param selector - CSS selector to match
381
- * @returns The matching ancestor or null
382
- */
383
- closest(selector: string): Element | null {
384
- return this.element.closest(selector);
385
- }
386
-
387
- /**
388
- * Gets the parent element.
389
- *
390
- * @returns The parent element or null
391
- */
392
- parent(): Element | null {
393
- return this.element.parentElement;
394
- }
395
-
396
- /**
397
- * Gets all child elements.
398
- *
399
- * @returns Array of child elements
400
- */
401
- children(): Element[] {
402
- return Array.from(this.element.children);
403
- }
404
-
405
- /**
406
- * Gets all sibling elements.
407
- *
408
- * @returns Array of sibling elements (excluding this element)
409
- */
410
- siblings(): Element[] {
411
- const parent = this.element.parentElement;
412
- if (!parent) return [];
413
- return Array.from(parent.children).filter((child) => child !== this.element);
414
- }
415
-
416
- /**
417
- * Gets the next sibling element.
418
- *
419
- * @returns The next sibling element or null
420
- */
421
- next(): Element | null {
422
- return this.element.nextElementSibling;
423
- }
424
-
425
- /**
426
- * Gets the previous sibling element.
427
- *
428
- * @returns The previous sibling element or null
429
- */
430
- prev(): Element | null {
431
- return this.element.previousElementSibling;
432
- }
433
-
434
- /**
435
- * Adds an event listener.
436
- *
437
- * @param event - Event type to listen for
438
- * @param handler - Event handler function
439
- * @returns The instance for method chaining
440
- */
441
- on(event: string, handler: EventListenerOrEventListenerObject): this {
442
- this.element.addEventListener(event, handler);
443
- return this;
444
- }
445
-
446
- /**
447
- * Adds a one-time event listener that removes itself after firing.
448
- *
449
- * @param event - Event type to listen for
450
- * @param handler - Event handler function
451
- * @returns The instance for method chaining
452
- */
453
- once(event: string, handler: EventListener): this {
454
- this.element.addEventListener(event, handler, { once: true });
455
- return this;
456
- }
457
-
458
- /**
459
- * Removes an event listener.
460
- *
461
- * @param event - Event type
462
- * @param handler - The handler to remove
463
- * @returns The instance for method chaining
464
- */
465
- off(event: string, handler: EventListenerOrEventListenerObject): this {
466
- this.element.removeEventListener(event, handler);
467
- return this;
468
- }
469
-
470
- /**
471
- * Triggers a custom event on the element.
472
- *
473
- * @param event - Event type to trigger
474
- * @param detail - Optional detail data to include with the event
475
- * @returns The instance for method chaining
476
- */
477
- trigger(event: string, detail?: unknown): this {
478
- this.element.dispatchEvent(new CustomEvent(event, { detail, bubbles: true, cancelable: true }));
479
- return this;
480
- }
481
-
482
- /**
483
- * Adds a delegated event listener that only triggers for matching descendants.
484
- * More efficient than adding listeners to many elements individually.
485
- *
486
- * Use `undelegate()` to remove the listener later.
487
- *
488
- * @param event - Event type to listen for
489
- * @param selector - CSS selector to match against event targets
490
- * @param handler - Event handler function, receives the matched element as context
491
- * @returns The instance for method chaining
492
- *
493
- * @example
494
- * ```ts
495
- * // Instead of adding listeners to each button:
496
- * const handler = (e, target) => console.log('Clicked:', target.textContent);
497
- * $('#list').delegate('click', '.item', handler);
498
- *
499
- * // Later, remove the delegated listener:
500
- * $('#list').undelegate('click', '.item', handler);
501
- * ```
502
- */
503
- delegate(
504
- event: string,
505
- selector: string,
506
- handler: (event: Event, target: Element) => void
507
- ): this {
508
- const key = `${event}:${selector}`;
509
- const wrapper: EventListener = (e: Event) => {
510
- const target = (e.target as Element).closest(selector);
511
- if (target && this.element.contains(target)) {
512
- handler(e, target);
513
- }
514
- };
515
-
516
- // Store the wrapper so it can be removed later
517
- if (!this.delegatedHandlers.has(key)) {
518
- this.delegatedHandlers.set(key, new Map());
519
- }
520
- this.delegatedHandlers.get(key)!.set(handler, wrapper);
521
-
522
- this.element.addEventListener(event, wrapper);
523
- return this;
524
- }
525
-
526
- /**
527
- * Removes a delegated event listener previously added with `delegate()`.
528
- *
529
- * @param event - Event type that was registered
530
- * @param selector - CSS selector that was used
531
- * @param handler - The original handler function passed to delegate()
532
- * @returns The instance for method chaining
533
- *
534
- * @example
535
- * ```ts
536
- * const handler = (e, target) => console.log('Clicked:', target.textContent);
537
- * $('#list').delegate('click', '.item', handler);
538
- *
539
- * // Remove the delegated listener:
540
- * $('#list').undelegate('click', '.item', handler);
541
- * ```
542
- */
543
- undelegate(
544
- event: string,
545
- selector: string,
546
- handler: (event: Event, target: Element) => void
547
- ): this {
548
- const key = `${event}:${selector}`;
549
- const handlers = this.delegatedHandlers.get(key);
550
-
551
- if (handlers) {
552
- const wrapper = handlers.get(handler);
553
- if (wrapper) {
554
- this.element.removeEventListener(event, wrapper);
555
- handlers.delete(handler);
556
-
557
- // Clean up empty maps
558
- if (handlers.size === 0) {
559
- this.delegatedHandlers.delete(key);
560
- }
561
- }
562
- }
563
-
564
- return this;
565
- }
566
-
567
- /**
568
- * Checks if the element matches a CSS selector.
569
- *
570
- * @param selector - CSS selector to match against
571
- * @returns True if the element matches the selector
572
- */
573
- matches(selector: string): boolean {
574
- return this.element.matches(selector);
575
- }
576
-
577
- /**
578
- * Alias for `matches()`. Checks if the element matches a CSS selector.
579
- *
580
- * @param selector - CSS selector to match against
581
- * @returns True if the element matches the selector
582
- *
583
- * @example
584
- * ```ts
585
- * if ($('#el').is('.active')) {
586
- * console.log('Element is active');
587
- * }
588
- * ```
589
- */
590
- is(selector: string): boolean {
591
- return this.matches(selector);
592
- }
593
-
594
- /**
595
- * Checks if the element has a specific class.
596
- *
597
- * @param className - Class name to check
598
- * @returns True if the element has the class
599
- */
600
- hasClass(className: string): boolean {
601
- return this.element.classList.contains(className);
602
- }
603
-
604
- /**
605
- * Shows the element by removing the hidden attribute and setting display.
606
- *
607
- * @param display - Optional display value (default: '')
608
- * @returns The instance for method chaining
609
- */
610
- show(display: string = ''): this {
611
- this.element.removeAttribute('hidden');
612
- (this.element as HTMLElement).style.display = display;
613
- return this;
614
- }
615
-
616
- /**
617
- * Hides the element by setting display to 'none'.
618
- *
619
- * @returns The instance for method chaining
620
- */
621
- hide(): this {
622
- (this.element as HTMLElement).style.display = 'none';
623
- return this;
624
- }
625
-
626
- /**
627
- * Toggles the visibility of the element.
628
- *
629
- * @param force - Optional force show (true) or hide (false)
630
- * @returns The instance for method chaining
631
- */
632
- toggle(force?: boolean): this {
633
- const isHidden = (this.element as HTMLElement).style.display === 'none';
634
- const shouldShow = force ?? isHidden;
635
- return shouldShow ? this.show() : this.hide();
636
- }
637
-
638
- /**
639
- * Focuses the element.
640
- *
641
- * @returns The instance for method chaining
642
- */
643
- focus(): this {
644
- (this.element as HTMLElement).focus();
645
- return this;
646
- }
647
-
648
- /**
649
- * Blurs (unfocuses) the element.
650
- *
651
- * @returns The instance for method chaining
652
- */
653
- blur(): this {
654
- (this.element as HTMLElement).blur();
655
- return this;
656
- }
657
-
658
- /**
659
- * Gets or sets the value of form elements.
660
- *
661
- * @param newValue - Optional value to set
662
- * @returns The current value when getting, or the instance when setting
663
- */
664
- val(newValue?: string): string | this {
665
- const input = this.element as HTMLInputElement;
666
- if (newValue === undefined) {
667
- return input.value ?? '';
668
- }
669
- input.value = newValue;
670
- return this;
671
- }
672
-
673
- /**
674
- * Serializes form data to a plain object.
675
- * Only works on form elements; returns empty object for non-forms.
676
- *
677
- * @returns Object with form field names as keys and values
678
- *
679
- * @example
680
- * ```ts
681
- * // For a form with <input name="email" value="test@example.com">
682
- * const data = $('#myForm').serialize();
683
- * // { email: 'test@example.com' }
684
- * ```
685
- */
686
- serialize(): Record<string, string | string[]> {
687
- const form = this.element as HTMLFormElement;
688
- if (form.tagName.toLowerCase() !== 'form') {
689
- return {};
690
- }
691
-
692
- const result: Record<string, string | string[]> = {};
693
- const formData = new FormData(form);
694
-
695
- for (const [key, value] of formData.entries()) {
696
- if (typeof value !== 'string') continue; // Skip File objects
697
-
698
- if (key in result) {
699
- // Handle multiple values (e.g., checkboxes)
700
- const existing = result[key];
701
- if (Array.isArray(existing)) {
702
- existing.push(value);
703
- } else {
704
- result[key] = [existing, value];
705
- }
706
- } else {
707
- result[key] = value;
708
- }
709
- }
710
-
711
- return result;
712
- }
713
-
714
- /**
715
- * Serializes form data to a URL-encoded query string.
716
- *
717
- * @returns URL-encoded string suitable for form submission
718
- *
719
- * @example
720
- * ```ts
721
- * const queryString = $('#myForm').serializeString();
722
- * // 'email=test%40example.com&name=John'
723
- * ```
724
- */
725
- serializeString(): string {
726
- const form = this.element as HTMLFormElement;
727
- if (form.tagName.toLowerCase() !== 'form') {
728
- return '';
729
- }
730
-
731
- const formData = new FormData(form);
732
- const params = new URLSearchParams();
733
-
734
- for (const [key, value] of formData.entries()) {
735
- if (typeof value === 'string') {
736
- params.append(key, value);
737
- }
738
- }
739
-
740
- return params.toString();
741
- }
742
-
743
- /**
744
- * Gets the bounding client rectangle of the element.
745
- *
746
- * @returns The element's bounding rectangle
747
- */
748
- rect(): DOMRect {
749
- return this.element.getBoundingClientRect();
750
- }
751
-
752
- /**
753
- * Gets the offset dimensions (width, height, top, left).
754
- *
755
- * @returns Object with offset dimensions
756
- */
757
- offset(): { width: number; height: number; top: number; left: number } {
758
- const el = this.element as HTMLElement;
759
- return {
760
- width: el.offsetWidth,
761
- height: el.offsetHeight,
762
- top: el.offsetTop,
763
- left: el.offsetLeft,
764
- };
765
- }
766
-
767
- /**
768
- * Internal method to insert content at a specified position.
769
- * @internal
770
- */
771
- private insertContent(content: string | Element | Element[], position: InsertPosition) {
772
- insertContent(this.element, content, position);
773
- }
774
- }
1
+ import { createElementFromHtml, insertContent, setHtml } from './dom';
2
+ import { getOuterSize, isHTMLElement } from './shared';
3
+ import { isPrototypePollutionKey } from './utils/object';
4
+
5
+ /**
6
+ * Wrapper for a single DOM element.
7
+ * Provides a chainable, jQuery-like API for DOM manipulation.
8
+ *
9
+ * This class encapsulates a DOM element and provides methods for:
10
+ * - Class manipulation (addClass, removeClass, toggleClass)
11
+ * - Attribute and property access (attr, prop, data)
12
+ * - Content manipulation (text, html, append, prepend)
13
+ * - Style manipulation (css)
14
+ * - Event handling (on, off, once, trigger)
15
+ * - DOM traversal (find, closest, parent, children, siblings)
16
+ *
17
+ * All mutating methods return `this` for method chaining.
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * $('#button')
22
+ * .addClass('active')
23
+ * .css({ color: 'blue' })
24
+ * .on('click', () => console.log('clicked'));
25
+ * ```
26
+ */
27
+ /** Handler signature for delegated events */
28
+ type DelegatedHandler = (event: Event, target: Element) => void;
29
+
30
+ type SerializableFormControl = HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement;
31
+
32
+ const isSerializableFormControl = (element: Element): element is SerializableFormControl => {
33
+ const tagName = element.tagName.toLowerCase();
34
+ return tagName === 'input' || tagName === 'textarea' || tagName === 'select';
35
+ };
36
+
37
+ const collectFormEntries = (form: HTMLFormElement): Array<[string, string]> => {
38
+ const entries: Array<[string, string]> = [];
39
+ const elementCtor = form.ownerDocument.defaultView?.Element ?? Element;
40
+
41
+ for (const control of Array.from(form.elements)) {
42
+ if (!(control instanceof elementCtor) || !isSerializableFormControl(control)) {
43
+ continue;
44
+ }
45
+
46
+ const name = control.name;
47
+ if (!name || control.disabled || isPrototypePollutionKey(name)) {
48
+ continue;
49
+ }
50
+
51
+ if (control.tagName.toLowerCase() === 'input') {
52
+ const input = control as HTMLInputElement;
53
+ const type = input.type.toLowerCase();
54
+
55
+ if (type === 'checkbox' || type === 'radio') {
56
+ if (input.checked) {
57
+ entries.push([name, input.value]);
58
+ }
59
+ continue;
60
+ }
61
+
62
+ if (
63
+ type === 'file' ||
64
+ type === 'submit' ||
65
+ type === 'button' ||
66
+ type === 'reset' ||
67
+ type === 'image'
68
+ ) {
69
+ continue;
70
+ }
71
+
72
+ entries.push([name, input.value]);
73
+ continue;
74
+ }
75
+
76
+ if (control.tagName.toLowerCase() === 'select') {
77
+ const select = control as HTMLSelectElement;
78
+
79
+ if (select.multiple) {
80
+ for (const option of Array.from(select.selectedOptions)) {
81
+ entries.push([name, option.value]);
82
+ }
83
+ } else {
84
+ entries.push([name, select.value]);
85
+ }
86
+ continue;
87
+ }
88
+
89
+ entries.push([name, (control as HTMLTextAreaElement).value]);
90
+ }
91
+
92
+ return entries;
93
+ };
94
+
95
+ const getFormEntries = (form: HTMLFormElement): Array<[string, string]> => {
96
+ if (typeof FormData === 'function') {
97
+ try {
98
+ const entries: Array<[string, string]> = [];
99
+
100
+ for (const [key, value] of new FormData(form).entries()) {
101
+ if (isPrototypePollutionKey(key) || typeof value !== 'string') {
102
+ continue;
103
+ }
104
+ entries.push([key, value]);
105
+ }
106
+
107
+ // Some environments expose FormData(form) but fail to populate entries for
108
+ // successful controls. Fall back to manual collection only in that zero-entry case.
109
+ return entries.length > 0 ? entries : collectFormEntries(form);
110
+ } catch {
111
+ // Fall back to manual collection when FormData is unavailable for this form
112
+ // or the environment does not fully support constructing it.
113
+ }
114
+ }
115
+
116
+ return collectFormEntries(form);
117
+ };
118
+
119
+ export class BQueryElement {
120
+ /**
121
+ * Stores delegated event handlers for cleanup via undelegate().
122
+ * Key format: `${event}:${selector}`
123
+ * @internal
124
+ */
125
+ private readonly delegatedHandlers = new Map<string, Map<DelegatedHandler, EventListener>>();
126
+
127
+ /**
128
+ * Creates a new BQueryElement wrapper.
129
+ * @param element - The DOM element to wrap
130
+ */
131
+ constructor(private readonly element: Element) {}
132
+
133
+ /**
134
+ * Exposes the raw DOM element when direct access is needed.
135
+ * Use sparingly; prefer the wrapper methods for consistency.
136
+ */
137
+ get raw(): Element {
138
+ return this.element;
139
+ }
140
+
141
+ /**
142
+ * Exposes the underlying DOM element.
143
+ * Provided for spec compatibility and read-only access.
144
+ */
145
+ get node(): Element {
146
+ return this.element;
147
+ }
148
+
149
+ /** Add one or more classes. */
150
+ addClass(...classNames: string[]): this {
151
+ this.element.classList.add(...classNames);
152
+ return this;
153
+ }
154
+
155
+ /** Remove one or more classes. */
156
+ removeClass(...classNames: string[]): this {
157
+ this.element.classList.remove(...classNames);
158
+ return this;
159
+ }
160
+
161
+ /** Toggle a class by name. */
162
+ toggleClass(className: string, force?: boolean): this {
163
+ this.element.classList.toggle(className, force);
164
+ return this;
165
+ }
166
+
167
+ /** Get or set an attribute. */
168
+ attr(name: string, value?: string): string | this {
169
+ if (value === undefined) {
170
+ return this.element.getAttribute(name) ?? '';
171
+ }
172
+ this.element.setAttribute(name, value);
173
+ return this;
174
+ }
175
+
176
+ /** Remove an attribute. */
177
+ removeAttr(name: string): this {
178
+ this.element.removeAttribute(name);
179
+ return this;
180
+ }
181
+
182
+ /** Toggle an attribute on/off. */
183
+ toggleAttr(name: string, force?: boolean): this {
184
+ const hasAttr = this.element.hasAttribute(name);
185
+ const shouldAdd = force ?? !hasAttr;
186
+ if (shouldAdd) {
187
+ this.element.setAttribute(name, '');
188
+ } else {
189
+ this.element.removeAttribute(name);
190
+ }
191
+ return this;
192
+ }
193
+
194
+ /** Get or set a property. */
195
+ prop<T extends keyof Element>(name: T, value?: Element[T]): Element[T] | this {
196
+ if (value === undefined) {
197
+ return this.element[name];
198
+ }
199
+ this.element[name] = value;
200
+ return this;
201
+ }
202
+
203
+ /** Read or write data attributes in camelCase. */
204
+ data(name: string, value?: string): string | this {
205
+ const key = name.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
206
+ if (value === undefined) {
207
+ return this.element.getAttribute(`data-${key}`) ?? '';
208
+ }
209
+ this.element.setAttribute(`data-${key}`, value);
210
+ return this;
211
+ }
212
+
213
+ /** Get or set text content. */
214
+ text(value?: string): string | this {
215
+ if (value === undefined) {
216
+ return this.element.textContent ?? '';
217
+ }
218
+ this.element.textContent = value;
219
+ return this;
220
+ }
221
+
222
+ /** Set HTML content using a sanitized string. */
223
+ /**
224
+ * Sets sanitized HTML content on the element.
225
+ * Uses the security module to sanitize input and prevent XSS attacks.
226
+ *
227
+ * @param value - The HTML string to set (will be sanitized)
228
+ * @returns The instance for method chaining
229
+ *
230
+ * @example
231
+ * ```ts
232
+ * $('#content').html('<strong>Hello</strong>');
233
+ * ```
234
+ */
235
+ html(value: string): this {
236
+ setHtml(this.element, value);
237
+ return this;
238
+ }
239
+
240
+ /**
241
+ * Sets HTML content without sanitization.
242
+ * Use only when you trust the HTML source completely.
243
+ *
244
+ * @param value - The raw HTML string to set
245
+ * @returns The instance for method chaining
246
+ *
247
+ * @warning This method bypasses XSS protection. Use with caution.
248
+ */
249
+ htmlUnsafe(value: string): this {
250
+ this.element.innerHTML = value;
251
+ return this;
252
+ }
253
+
254
+ /**
255
+ * Gets or sets CSS styles on the element.
256
+ *
257
+ * @param property - A CSS property name or an object of property-value pairs
258
+ * @param value - The value when setting a single property
259
+ * @returns The computed style value when getting a single property, or the instance for method chaining when setting
260
+ *
261
+ * @example
262
+ * ```ts
263
+ * // Get a computed style value
264
+ * const color = $('#box').css('color');
265
+ *
266
+ * // Set a single property
267
+ * $('#box').css('color', 'red');
268
+ *
269
+ * // Set multiple properties
270
+ * $('#box').css({ color: 'red', 'font-size': '16px' });
271
+ * ```
272
+ */
273
+ css(property: string): string;
274
+ css(property: string, value: string): this;
275
+ css(property: Record<string, string>): this;
276
+ css(property: string | Record<string, string>, value?: string): string | this {
277
+ if (typeof property === 'string') {
278
+ if (value !== undefined) {
279
+ (this.element as HTMLElement).style.setProperty(property, value);
280
+ return this;
281
+ }
282
+ const view = this.element.ownerDocument?.defaultView;
283
+ if (!view || typeof view.getComputedStyle !== 'function') {
284
+ return '';
285
+ }
286
+ return view.getComputedStyle(this.element).getPropertyValue(property);
287
+ }
288
+
289
+ for (const [key, val] of Object.entries(property)) {
290
+ (this.element as HTMLElement).style.setProperty(key, val);
291
+ }
292
+ return this;
293
+ }
294
+
295
+ /**
296
+ * Appends HTML or elements to the end of the element.
297
+ *
298
+ * @param content - HTML string or element(s) to append
299
+ * @returns The instance for method chaining
300
+ */
301
+ append(content: string | Element | Element[]): this {
302
+ this.insertContent(content, 'beforeend');
303
+ return this;
304
+ }
305
+
306
+ /**
307
+ * Prepends HTML or elements to the beginning of the element.
308
+ *
309
+ * @param content - HTML string or element(s) to prepend
310
+ * @returns The instance for method chaining
311
+ */
312
+ prepend(content: string | Element | Element[]): this {
313
+ this.insertContent(content, 'afterbegin');
314
+ return this;
315
+ }
316
+
317
+ /**
318
+ * Inserts content before this element.
319
+ *
320
+ * @param content - HTML string or element(s) to insert
321
+ * @returns The instance for method chaining
322
+ */
323
+ before(content: string | Element | Element[]): this {
324
+ this.insertContent(content, 'beforebegin');
325
+ return this;
326
+ }
327
+
328
+ /**
329
+ * Inserts content after this element.
330
+ *
331
+ * @param content - HTML string or element(s) to insert
332
+ * @returns The instance for method chaining
333
+ */
334
+ after(content: string | Element | Element[]): this {
335
+ this.insertContent(content, 'afterend');
336
+ return this;
337
+ }
338
+
339
+ /**
340
+ * Wraps the element with the specified wrapper element or tag.
341
+ *
342
+ * @param wrapper - Tag name string or Element to wrap with
343
+ * @returns The instance for method chaining
344
+ *
345
+ * @example
346
+ * ```ts
347
+ * $('#content').wrap('div'); // Wraps with <div>
348
+ * $('#content').wrap(document.createElement('section'));
349
+ * ```
350
+ */
351
+ wrap(wrapper: string | Element): this {
352
+ const wrapperEl = typeof wrapper === 'string' ? document.createElement(wrapper) : wrapper;
353
+ this.element.parentNode?.insertBefore(wrapperEl, this.element);
354
+ wrapperEl.appendChild(this.element);
355
+ return this;
356
+ }
357
+
358
+ /**
359
+ * Removes the parent element, keeping this element in its place.
360
+ * Essentially the opposite of wrap().
361
+ *
362
+ * **Important**: This method only moves the current element out of its parent
363
+ * before removing the parent. Any sibling elements will be removed along with
364
+ * the parent. For unwrapping multiple siblings, use a collection: `$$(siblings).unwrap()`.
365
+ *
366
+ * @returns The instance for method chaining
367
+ *
368
+ * @example
369
+ * ```ts
370
+ * // Before: <div><span id="text">Hello</span></div>
371
+ * $('#text').unwrap();
372
+ * // After: <span id="text">Hello</span>
373
+ * ```
374
+ */
375
+ unwrap(): this {
376
+ const parent = this.element.parentElement;
377
+ if (parent && parent.parentNode) {
378
+ parent.parentNode.insertBefore(this.element, parent);
379
+ parent.remove();
380
+ }
381
+ return this;
382
+ }
383
+
384
+ /**
385
+ * Replaces this element with new content.
386
+ *
387
+ * @param content - HTML string (sanitized) or Element to replace with
388
+ * @returns A new BQueryElement wrapping the replacement element
389
+ *
390
+ * @example
391
+ * ```ts
392
+ * const newEl = $('#old').replaceWith('<div id="new">Replaced</div>');
393
+ * ```
394
+ */
395
+ replaceWith(content: string | Element): BQueryElement {
396
+ const newEl = typeof content === 'string' ? createElementFromHtml(content) : content;
397
+ this.element.replaceWith(newEl);
398
+ return new BQueryElement(newEl);
399
+ }
400
+
401
+ /**
402
+ * Removes the element from the DOM while keeping the wrapped node available
403
+ * for later reuse.
404
+ *
405
+ * @returns The instance for method chaining
406
+ *
407
+ * @example
408
+ * ```ts
409
+ * const item = $('#item').detach();
410
+ * document.body.appendChild(item.raw);
411
+ * ```
412
+ */
413
+ detach(): this {
414
+ return this.remove();
415
+ }
416
+
417
+ /**
418
+ * Gets the zero-based index of the element among its element siblings.
419
+ *
420
+ * @returns Index within the parent element, or -1 when detached
421
+ *
422
+ * @example
423
+ * ```ts
424
+ * const index = $('#item').index();
425
+ * ```
426
+ */
427
+ index(): number {
428
+ const parent = this.element.parentElement;
429
+ if (!parent) {
430
+ return -1;
431
+ }
432
+ return Array.from(parent.children).indexOf(this.element);
433
+ }
434
+
435
+ /**
436
+ * Returns all child nodes, including text nodes and comments.
437
+ *
438
+ * @returns Array of child nodes
439
+ *
440
+ * @example
441
+ * ```ts
442
+ * const nodes = $('#content').contents();
443
+ * ```
444
+ */
445
+ contents(): ChildNode[] {
446
+ return Array.from(this.element.childNodes);
447
+ }
448
+
449
+ /**
450
+ * Gets the nearest positioned ancestor used for offset calculations.
451
+ *
452
+ * @returns The offset parent element, or null when unavailable
453
+ *
454
+ * @example
455
+ * ```ts
456
+ * const parent = $('#item').offsetParent();
457
+ * ```
458
+ */
459
+ offsetParent(): Element | null {
460
+ return isHTMLElement(this.element) ? this.element.offsetParent : null;
461
+ }
462
+
463
+ /**
464
+ * Gets the current position relative to the offset parent.
465
+ *
466
+ * @returns Position object with top and left coordinates
467
+ *
468
+ * @example
469
+ * ```ts
470
+ * const { top, left } = $('#item').position();
471
+ * ```
472
+ */
473
+ position(): { top: number; left: number } {
474
+ if (!isHTMLElement(this.element)) {
475
+ return { top: 0, left: 0 };
476
+ }
477
+
478
+ const el = this.element;
479
+ return {
480
+ top: el.offsetTop,
481
+ left: el.offsetLeft,
482
+ };
483
+ }
484
+
485
+ /**
486
+ * Gets the outer width of the element, optionally including margins.
487
+ *
488
+ * @param includeMargin - When true, include horizontal margins
489
+ * @returns Outer width in pixels
490
+ *
491
+ * @example
492
+ * ```ts
493
+ * const width = $('#panel').outerWidth();
494
+ * const widthWithMargin = $('#panel').outerWidth(true);
495
+ * ```
496
+ */
497
+ outerWidth(includeMargin: boolean = false): number {
498
+ return getOuterSize(this.element, 'width', includeMargin);
499
+ }
500
+
501
+ /**
502
+ * Gets the outer height of the element, optionally including margins.
503
+ *
504
+ * @param includeMargin - When true, include vertical margins
505
+ * @returns Outer height in pixels
506
+ *
507
+ * @example
508
+ * ```ts
509
+ * const height = $('#panel').outerHeight();
510
+ * const heightWithMargin = $('#panel').outerHeight(true);
511
+ * ```
512
+ */
513
+ outerHeight(includeMargin: boolean = false): number {
514
+ return getOuterSize(this.element, 'height', includeMargin);
515
+ }
516
+
517
+ /**
518
+ * Scrolls the element into view with configurable behavior.
519
+ *
520
+ * @param options - ScrollIntoView options or boolean for legacy behavior
521
+ * @returns The instance for method chaining
522
+ *
523
+ * @example
524
+ * ```ts
525
+ * $('#section').scrollTo(); // Smooth scroll
526
+ * $('#section').scrollTo({ behavior: 'instant', block: 'start' });
527
+ * ```
528
+ */
529
+ scrollTo(options: ScrollIntoViewOptions | boolean = { behavior: 'smooth' }): this {
530
+ this.element.scrollIntoView(options);
531
+ return this;
532
+ }
533
+
534
+ /**
535
+ * Removes the element from the DOM.
536
+ *
537
+ * @returns The instance for method chaining (though element is now detached)
538
+ */
539
+ remove(): this {
540
+ this.element.remove();
541
+ return this;
542
+ }
543
+
544
+ /**
545
+ * Clears all child nodes from the element.
546
+ *
547
+ * @returns The instance for method chaining
548
+ */
549
+ empty(): this {
550
+ this.element.innerHTML = '';
551
+ return this;
552
+ }
553
+
554
+ /**
555
+ * Clones the element, optionally with all descendants.
556
+ *
557
+ * @param deep - If true, clone all descendants (default: true)
558
+ * @returns A new BQueryElement wrapping the cloned element
559
+ */
560
+ clone(deep: boolean = true): BQueryElement {
561
+ return new BQueryElement(this.element.cloneNode(deep) as Element);
562
+ }
563
+
564
+ /**
565
+ * Finds all descendant elements matching the selector.
566
+ *
567
+ * @param selector - CSS selector to match
568
+ * @returns Array of matching elements
569
+ */
570
+ find(selector: string): Element[] {
571
+ return Array.from(this.element.querySelectorAll(selector));
572
+ }
573
+
574
+ /**
575
+ * Finds the first descendant element matching the selector.
576
+ *
577
+ * @param selector - CSS selector to match
578
+ * @returns The first matching element or null
579
+ */
580
+ findOne(selector: string): Element | null {
581
+ return this.element.querySelector(selector);
582
+ }
583
+
584
+ /**
585
+ * Finds the closest ancestor matching the selector.
586
+ *
587
+ * @param selector - CSS selector to match
588
+ * @returns The matching ancestor or null
589
+ */
590
+ closest(selector: string): Element | null {
591
+ return this.element.closest(selector);
592
+ }
593
+
594
+ /**
595
+ * Gets the parent element.
596
+ *
597
+ * @returns The parent element or null
598
+ */
599
+ parent(): Element | null {
600
+ return this.element.parentElement;
601
+ }
602
+
603
+ /**
604
+ * Gets all child elements.
605
+ *
606
+ * @returns Array of child elements
607
+ */
608
+ children(): Element[] {
609
+ return Array.from(this.element.children);
610
+ }
611
+
612
+ /**
613
+ * Gets all sibling elements.
614
+ *
615
+ * @returns Array of sibling elements (excluding this element)
616
+ */
617
+ siblings(): Element[] {
618
+ const parent = this.element.parentElement;
619
+ if (!parent) return [];
620
+ return Array.from(parent.children).filter((child) => child !== this.element);
621
+ }
622
+
623
+ /**
624
+ * Gets the next sibling element.
625
+ *
626
+ * @returns The next sibling element or null
627
+ */
628
+ next(): Element | null {
629
+ return this.element.nextElementSibling;
630
+ }
631
+
632
+ /**
633
+ * Gets the previous sibling element.
634
+ *
635
+ * @returns The previous sibling element or null
636
+ */
637
+ prev(): Element | null {
638
+ return this.element.previousElementSibling;
639
+ }
640
+
641
+ /**
642
+ * Adds an event listener.
643
+ *
644
+ * @param event - Event type to listen for
645
+ * @param handler - Event handler function
646
+ * @returns The instance for method chaining
647
+ */
648
+ on(event: string, handler: EventListenerOrEventListenerObject): this {
649
+ this.element.addEventListener(event, handler);
650
+ return this;
651
+ }
652
+
653
+ /**
654
+ * Adds a one-time event listener that removes itself after firing.
655
+ *
656
+ * @param event - Event type to listen for
657
+ * @param handler - Event handler function
658
+ * @returns The instance for method chaining
659
+ */
660
+ once(event: string, handler: EventListener): this {
661
+ this.element.addEventListener(event, handler, { once: true });
662
+ return this;
663
+ }
664
+
665
+ /**
666
+ * Removes an event listener.
667
+ *
668
+ * @param event - Event type
669
+ * @param handler - The handler to remove
670
+ * @returns The instance for method chaining
671
+ */
672
+ off(event: string, handler: EventListenerOrEventListenerObject): this {
673
+ this.element.removeEventListener(event, handler);
674
+ return this;
675
+ }
676
+
677
+ /**
678
+ * Triggers a custom event on the element.
679
+ *
680
+ * @param event - Event type to trigger
681
+ * @param detail - Optional detail data to include with the event
682
+ * @returns The instance for method chaining
683
+ */
684
+ trigger(event: string, detail?: unknown): this {
685
+ this.element.dispatchEvent(new CustomEvent(event, { detail, bubbles: true, cancelable: true }));
686
+ return this;
687
+ }
688
+
689
+ /**
690
+ * Adds a delegated event listener that only triggers for matching descendants.
691
+ * More efficient than adding listeners to many elements individually.
692
+ *
693
+ * Use `undelegate()` to remove the listener later.
694
+ *
695
+ * @param event - Event type to listen for
696
+ * @param selector - CSS selector to match against event targets
697
+ * @param handler - Event handler function, receives the matched element as context
698
+ * @returns The instance for method chaining
699
+ *
700
+ * @example
701
+ * ```ts
702
+ * // Instead of adding listeners to each button:
703
+ * const handler = (e, target) => console.log('Clicked:', target.textContent);
704
+ * $('#list').delegate('click', '.item', handler);
705
+ *
706
+ * // Later, remove the delegated listener:
707
+ * $('#list').undelegate('click', '.item', handler);
708
+ * ```
709
+ */
710
+ delegate(
711
+ event: string,
712
+ selector: string,
713
+ handler: (event: Event, target: Element) => void
714
+ ): this {
715
+ const key = `${event}:${selector}`;
716
+ const wrapper: EventListener = (e: Event) => {
717
+ const target = (e.target as Element).closest(selector);
718
+ if (target && this.element.contains(target)) {
719
+ handler(e, target);
720
+ }
721
+ };
722
+
723
+ // Store the wrapper so it can be removed later
724
+ if (!this.delegatedHandlers.has(key)) {
725
+ this.delegatedHandlers.set(key, new Map());
726
+ }
727
+ this.delegatedHandlers.get(key)!.set(handler, wrapper);
728
+
729
+ this.element.addEventListener(event, wrapper);
730
+ return this;
731
+ }
732
+
733
+ /**
734
+ * Removes a delegated event listener previously added with `delegate()`.
735
+ *
736
+ * @param event - Event type that was registered
737
+ * @param selector - CSS selector that was used
738
+ * @param handler - The original handler function passed to delegate()
739
+ * @returns The instance for method chaining
740
+ *
741
+ * @example
742
+ * ```ts
743
+ * const handler = (e, target) => console.log('Clicked:', target.textContent);
744
+ * $('#list').delegate('click', '.item', handler);
745
+ *
746
+ * // Remove the delegated listener:
747
+ * $('#list').undelegate('click', '.item', handler);
748
+ * ```
749
+ */
750
+ undelegate(
751
+ event: string,
752
+ selector: string,
753
+ handler: (event: Event, target: Element) => void
754
+ ): this {
755
+ const key = `${event}:${selector}`;
756
+ const handlers = this.delegatedHandlers.get(key);
757
+
758
+ if (handlers) {
759
+ const wrapper = handlers.get(handler);
760
+ if (wrapper) {
761
+ this.element.removeEventListener(event, wrapper);
762
+ handlers.delete(handler);
763
+
764
+ // Clean up empty maps
765
+ if (handlers.size === 0) {
766
+ this.delegatedHandlers.delete(key);
767
+ }
768
+ }
769
+ }
770
+
771
+ return this;
772
+ }
773
+
774
+ /**
775
+ * Checks if the element matches a CSS selector.
776
+ *
777
+ * @param selector - CSS selector to match against
778
+ * @returns True if the element matches the selector
779
+ */
780
+ matches(selector: string): boolean {
781
+ return this.element.matches(selector);
782
+ }
783
+
784
+ /**
785
+ * Alias for `matches()`. Checks if the element matches a CSS selector.
786
+ *
787
+ * @param selector - CSS selector to match against
788
+ * @returns True if the element matches the selector
789
+ *
790
+ * @example
791
+ * ```ts
792
+ * if ($('#el').is('.active')) {
793
+ * console.log('Element is active');
794
+ * }
795
+ * ```
796
+ */
797
+ is(selector: string): boolean {
798
+ return this.matches(selector);
799
+ }
800
+
801
+ /**
802
+ * Checks if the element has a specific class.
803
+ *
804
+ * @param className - Class name to check
805
+ * @returns True if the element has the class
806
+ */
807
+ hasClass(className: string): boolean {
808
+ return this.element.classList.contains(className);
809
+ }
810
+
811
+ /**
812
+ * Shows the element by removing the hidden attribute and setting display.
813
+ *
814
+ * @param display - Optional display value (default: '')
815
+ * @returns The instance for method chaining
816
+ */
817
+ show(display: string = ''): this {
818
+ this.element.removeAttribute('hidden');
819
+ (this.element as HTMLElement).style.display = display;
820
+ return this;
821
+ }
822
+
823
+ /**
824
+ * Hides the element by setting display to 'none'.
825
+ *
826
+ * @returns The instance for method chaining
827
+ */
828
+ hide(): this {
829
+ (this.element as HTMLElement).style.display = 'none';
830
+ return this;
831
+ }
832
+
833
+ /**
834
+ * Toggles the visibility of the element.
835
+ *
836
+ * @param force - Optional force show (true) or hide (false)
837
+ * @returns The instance for method chaining
838
+ */
839
+ toggle(force?: boolean): this {
840
+ const isHidden = (this.element as HTMLElement).style.display === 'none';
841
+ const shouldShow = force ?? isHidden;
842
+ return shouldShow ? this.show() : this.hide();
843
+ }
844
+
845
+ /**
846
+ * Focuses the element.
847
+ *
848
+ * @returns The instance for method chaining
849
+ */
850
+ focus(): this {
851
+ (this.element as HTMLElement).focus();
852
+ return this;
853
+ }
854
+
855
+ /**
856
+ * Blurs (unfocuses) the element.
857
+ *
858
+ * @returns The instance for method chaining
859
+ */
860
+ blur(): this {
861
+ (this.element as HTMLElement).blur();
862
+ return this;
863
+ }
864
+
865
+ /**
866
+ * Gets or sets the value of form elements.
867
+ *
868
+ * @param newValue - Optional value to set
869
+ * @returns The current value when getting, or the instance when setting
870
+ */
871
+ val(newValue?: string): string | this {
872
+ const input = this.element as HTMLInputElement;
873
+ if (newValue === undefined) {
874
+ return input.value ?? '';
875
+ }
876
+ input.value = newValue;
877
+ return this;
878
+ }
879
+
880
+ /**
881
+ * Serializes form data to a plain object.
882
+ * Only works on form elements; returns empty object for non-forms.
883
+ *
884
+ * For security hardening, the returned object uses a null prototype,
885
+ * so inherited members like `hasOwnProperty` are not available directly.
886
+ * Prefer `Object.keys()` or `Object.prototype.hasOwnProperty.call(...)`
887
+ * when checking for fields on the serialized result.
888
+ *
889
+ * @returns Object with form field names as keys and values
890
+ *
891
+ * @example
892
+ * ```ts
893
+ * // For a form with <input name="email" value="test@example.com">
894
+ * const data = $('#myForm').serialize();
895
+ * // { email: 'test@example.com' }
896
+ * Object.prototype.hasOwnProperty.call(data, 'email'); // true
897
+ * ```
898
+ */
899
+ serialize(): Record<string, string | string[]> {
900
+ const form = this.element as HTMLFormElement;
901
+ if (form.tagName.toLowerCase() !== 'form') {
902
+ return {};
903
+ }
904
+
905
+ const result = Object.create(null) as Record<string, string | string[]>;
906
+
907
+ for (const [key, value] of getFormEntries(form)) {
908
+ if (Object.prototype.hasOwnProperty.call(result, key)) {
909
+ // Handle multiple values (e.g., checkboxes)
910
+ const existing = result[key];
911
+ if (Array.isArray(existing)) {
912
+ existing.push(value);
913
+ } else {
914
+ result[key] = [existing, value];
915
+ }
916
+ } else {
917
+ result[key] = value;
918
+ }
919
+ }
920
+
921
+ return result;
922
+ }
923
+
924
+ /**
925
+ * Serializes form data to a URL-encoded query string.
926
+ *
927
+ * @returns URL-encoded string suitable for form submission
928
+ *
929
+ * @example
930
+ * ```ts
931
+ * const queryString = $('#myForm').serializeString();
932
+ * // 'email=test%40example.com&name=John'
933
+ * ```
934
+ */
935
+ serializeString(): string {
936
+ const form = this.element as HTMLFormElement;
937
+ if (form.tagName.toLowerCase() !== 'form') {
938
+ return '';
939
+ }
940
+
941
+ const params = new URLSearchParams();
942
+
943
+ for (const [key, value] of getFormEntries(form)) {
944
+ params.append(key, value);
945
+ }
946
+
947
+ return params.toString();
948
+ }
949
+
950
+ /**
951
+ * Gets the bounding client rectangle of the element.
952
+ *
953
+ * @returns The element's bounding rectangle
954
+ */
955
+ rect(): DOMRect {
956
+ return this.element.getBoundingClientRect();
957
+ }
958
+
959
+ /**
960
+ * Gets the offset dimensions (width, height, top, left).
961
+ *
962
+ * @returns Object with offset dimensions
963
+ */
964
+ offset(): { width: number; height: number; top: number; left: number } {
965
+ const el = this.element as HTMLElement;
966
+ return {
967
+ width: el.offsetWidth,
968
+ height: el.offsetHeight,
969
+ top: el.offsetTop,
970
+ left: el.offsetLeft,
971
+ };
972
+ }
973
+
974
+ /**
975
+ * Internal method to insert content at a specified position.
976
+ * @internal
977
+ */
978
+ private insertContent(content: string | Element | Element[], position: InsertPosition) {
979
+ insertContent(this.element, content, position);
980
+ }
981
+ }