tiny-essentials 1.16.0 → 1.16.1

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.
@@ -4218,6 +4218,494 @@ class TinyHtml {
4218
4218
 
4219
4219
  //////////////////////////////////////////////////////////////////////
4220
4220
 
4221
+ /**
4222
+ * Stores camelCase to kebab-case CSS property aliases.
4223
+ *
4224
+ * Used to normalize property names when interacting with `element.style` or `getComputedStyle`.
4225
+ *
4226
+ * ⚠️ This object should not be modified directly. Use `TinyHtml.cssPropAliases` instead to ensure reverse mappings stay in sync.
4227
+ *
4228
+ * Example of how to add a new alias:
4229
+ *
4230
+ * ```js
4231
+ * TinyHtml.cssPropAliases.tinyPudding = 'tiny-pudding';
4232
+ * ```
4233
+ *
4234
+ * This will automatically update `TinyHtml.cssPropRevAliases['tiny-pudding']` with `'tinyPudding'`.
4235
+ *
4236
+ * @type {Record<string | symbol, string>}
4237
+ */
4238
+ static #cssPropAliases = {
4239
+ alignContent: 'align-content',
4240
+ alignItems: 'align-items',
4241
+ alignSelf: 'align-self',
4242
+ animationDelay: 'animation-delay',
4243
+ animationDirection: 'animation-direction',
4244
+ animationDuration: 'animation-duration',
4245
+ animationFillMode: 'animation-fill-mode',
4246
+ animationIterationCount: 'animation-iteration-count',
4247
+ animationName: 'animation-name',
4248
+ animationPlayState: 'animation-play-state',
4249
+ animationTimingFunction: 'animation-timing-function',
4250
+ backfaceVisibility: 'backface-visibility',
4251
+ backgroundAttachment: 'background-attachment',
4252
+ backgroundBlendMode: 'background-blend-mode',
4253
+ backgroundClip: 'background-clip',
4254
+ backgroundColor: 'background-color',
4255
+ backgroundImage: 'background-image',
4256
+ backgroundOrigin: 'background-origin',
4257
+ backgroundPosition: 'background-position',
4258
+ backgroundRepeat: 'background-repeat',
4259
+ backgroundSize: 'background-size',
4260
+ borderBottom: 'border-bottom',
4261
+ borderBottomColor: 'border-bottom-color',
4262
+ borderBottomLeftRadius: 'border-bottom-left-radius',
4263
+ borderBottomRightRadius: 'border-bottom-right-radius',
4264
+ borderBottomStyle: 'border-bottom-style',
4265
+ borderBottomWidth: 'border-bottom-width',
4266
+ borderCollapse: 'border-collapse',
4267
+ borderColor: 'border-color',
4268
+ borderImage: 'border-image',
4269
+ borderImageOutset: 'border-image-outset',
4270
+ borderImageRepeat: 'border-image-repeat',
4271
+ borderImageSlice: 'border-image-slice',
4272
+ borderImageSource: 'border-image-source',
4273
+ borderImageWidth: 'border-image-width',
4274
+ borderLeft: 'border-left',
4275
+ borderLeftColor: 'border-left-color',
4276
+ borderLeftStyle: 'border-left-style',
4277
+ borderLeftWidth: 'border-left-width',
4278
+ borderRadius: 'border-radius',
4279
+ borderRight: 'border-right',
4280
+ borderRightColor: 'border-right-color',
4281
+ borderRightStyle: 'border-right-style',
4282
+ borderRightWidth: 'border-right-width',
4283
+ borderSpacing: 'border-spacing',
4284
+ borderStyle: 'border-style',
4285
+ borderTop: 'border-top',
4286
+ borderTopColor: 'border-top-color',
4287
+ borderTopLeftRadius: 'border-top-left-radius',
4288
+ borderTopRightRadius: 'border-top-right-radius',
4289
+ borderTopStyle: 'border-top-style',
4290
+ borderTopWidth: 'border-top-width',
4291
+ borderWidth: 'border-width',
4292
+ boxDecorationBreak: 'box-decoration-break',
4293
+ boxShadow: 'box-shadow',
4294
+ boxSizing: 'box-sizing',
4295
+ breakAfter: 'break-after',
4296
+ breakBefore: 'break-before',
4297
+ breakInside: 'break-inside',
4298
+ captionSide: 'caption-side',
4299
+ caretColor: 'caret-color',
4300
+ clipPath: 'clip-path',
4301
+ columnCount: 'column-count',
4302
+ columnFill: 'column-fill',
4303
+ columnGap: 'column-gap',
4304
+ columnRule: 'column-rule',
4305
+ columnRuleColor: 'column-rule-color',
4306
+ columnRuleStyle: 'column-rule-style',
4307
+ columnRuleWidth: 'column-rule-width',
4308
+ columnSpan: 'column-span',
4309
+ columnWidth: 'column-width',
4310
+ counterIncrement: 'counter-increment',
4311
+ counterReset: 'counter-reset',
4312
+ emptyCells: 'empty-cells',
4313
+ flexBasis: 'flex-basis',
4314
+ flexDirection: 'flex-direction',
4315
+ flexFlow: 'flex-flow',
4316
+ flexGrow: 'flex-grow',
4317
+ flexShrink: 'flex-shrink',
4318
+ flexWrap: 'flex-wrap',
4319
+ fontFamily: 'font-family',
4320
+ fontFeatureSettings: 'font-feature-settings',
4321
+ fontKerning: 'font-kerning',
4322
+ fontLanguageOverride: 'font-language-override',
4323
+ fontSize: 'font-size',
4324
+ fontSizeAdjust: 'font-size-adjust',
4325
+ fontStretch: 'font-stretch',
4326
+ fontStyle: 'font-style',
4327
+ fontSynthesis: 'font-synthesis',
4328
+ fontVariant: 'font-variant',
4329
+ fontVariantAlternates: 'font-variant-alternates',
4330
+ fontVariantCaps: 'font-variant-caps',
4331
+ fontVariantEastAsian: 'font-variant-east-asian',
4332
+ fontVariantLigatures: 'font-variant-ligatures',
4333
+ fontVariantNumeric: 'font-variant-numeric',
4334
+ fontVariantPosition: 'font-variant-position',
4335
+ fontWeight: 'font-weight',
4336
+ gridArea: 'grid-area',
4337
+ gridAutoColumns: 'grid-auto-columns',
4338
+ gridAutoFlow: 'grid-auto-flow',
4339
+ gridAutoRows: 'grid-auto-rows',
4340
+ gridColumn: 'grid-column',
4341
+ gridColumnEnd: 'grid-column-end',
4342
+ gridColumnGap: 'grid-column-gap',
4343
+ gridColumnStart: 'grid-column-start',
4344
+ gridGap: 'grid-gap',
4345
+ gridRow: 'grid-row',
4346
+ gridRowEnd: 'grid-row-end',
4347
+ gridRowGap: 'grid-row-gap',
4348
+ gridRowStart: 'grid-row-start',
4349
+ gridTemplate: 'grid-template',
4350
+ gridTemplateAreas: 'grid-template-areas',
4351
+ gridTemplateColumns: 'grid-template-columns',
4352
+ gridTemplateRows: 'grid-template-rows',
4353
+ imageRendering: 'image-rendering',
4354
+ justifyContent: 'justify-content',
4355
+ letterSpacing: 'letter-spacing',
4356
+ lineBreak: 'line-break',
4357
+ lineHeight: 'line-height',
4358
+ listStyle: 'list-style',
4359
+ listStyleImage: 'list-style-image',
4360
+ listStylePosition: 'list-style-position',
4361
+ listStyleType: 'list-style-type',
4362
+ marginBottom: 'margin-bottom',
4363
+ marginLeft: 'margin-left',
4364
+ marginRight: 'margin-right',
4365
+ marginTop: 'margin-top',
4366
+ maskClip: 'mask-clip',
4367
+ maskComposite: 'mask-composite',
4368
+ maskImage: 'mask-image',
4369
+ maskMode: 'mask-mode',
4370
+ maskOrigin: 'mask-origin',
4371
+ maskPosition: 'mask-position',
4372
+ maskRepeat: 'mask-repeat',
4373
+ maskSize: 'mask-size',
4374
+ maskType: 'mask-type',
4375
+ maxHeight: 'max-height',
4376
+ maxWidth: 'max-width',
4377
+ minHeight: 'min-height',
4378
+ minWidth: 'min-width',
4379
+ mixBlendMode: 'mix-blend-mode',
4380
+ objectFit: 'object-fit',
4381
+ objectPosition: 'object-position',
4382
+ offsetAnchor: 'offset-anchor',
4383
+ offsetDistance: 'offset-distance',
4384
+ offsetPath: 'offset-path',
4385
+ offsetRotate: 'offset-rotate',
4386
+ outlineColor: 'outline-color',
4387
+ outlineOffset: 'outline-offset',
4388
+ outlineStyle: 'outline-style',
4389
+ outlineWidth: 'outline-width',
4390
+ overflowAnchor: 'overflow-anchor',
4391
+ overflowWrap: 'overflow-wrap',
4392
+ overflowX: 'overflow-x',
4393
+ overflowY: 'overflow-y',
4394
+ paddingBottom: 'padding-bottom',
4395
+ paddingLeft: 'padding-left',
4396
+ paddingRight: 'padding-right',
4397
+ paddingTop: 'padding-top',
4398
+ pageBreakAfter: 'page-break-after',
4399
+ pageBreakBefore: 'page-break-before',
4400
+ pageBreakInside: 'page-break-inside',
4401
+ perspectiveOrigin: 'perspective-origin',
4402
+ placeContent: 'place-content',
4403
+ placeItems: 'place-items',
4404
+ placeSelf: 'place-self',
4405
+ pointerEvents: 'pointer-events',
4406
+ rowGap: 'row-gap',
4407
+ scrollBehavior: 'scroll-behavior',
4408
+ scrollMargin: 'scroll-margin',
4409
+ scrollMarginBlock: 'scroll-margin-block',
4410
+ scrollMarginBlockEnd: 'scroll-margin-block-end',
4411
+ scrollMarginBlockStart: 'scroll-margin-block-start',
4412
+ scrollMarginBottom: 'scroll-margin-bottom',
4413
+ scrollMarginInline: 'scroll-margin-inline',
4414
+ scrollMarginInlineEnd: 'scroll-margin-inline-end',
4415
+ scrollMarginInlineStart: 'scroll-margin-inline-start',
4416
+ scrollMarginLeft: 'scroll-margin-left',
4417
+ scrollMarginRight: 'scroll-margin-right',
4418
+ scrollMarginTop: 'scroll-margin-top',
4419
+ scrollPadding: 'scroll-padding',
4420
+ scrollPaddingBlock: 'scroll-padding-block',
4421
+ scrollPaddingBlockEnd: 'scroll-padding-block-end',
4422
+ scrollPaddingBlockStart: 'scroll-padding-block-start',
4423
+ scrollPaddingBottom: 'scroll-padding-bottom',
4424
+ scrollPaddingInline: 'scroll-padding-inline',
4425
+ scrollPaddingInlineEnd: 'scroll-padding-inline-end',
4426
+ scrollPaddingInlineStart: 'scroll-padding-inline-start',
4427
+ scrollPaddingLeft: 'scroll-padding-left',
4428
+ scrollPaddingRight: 'scroll-padding-right',
4429
+ scrollPaddingTop: 'scroll-padding-top',
4430
+ scrollSnapAlign: 'scroll-snap-align',
4431
+ scrollSnapStop: 'scroll-snap-stop',
4432
+ scrollSnapType: 'scroll-snap-type',
4433
+ shapeImageThreshold: 'shape-image-threshold',
4434
+ shapeMargin: 'shape-margin',
4435
+ shapeOutside: 'shape-outside',
4436
+ tabSize: 'tab-size',
4437
+ tableLayout: 'table-layout',
4438
+ textAlign: 'text-align',
4439
+ textAlignLast: 'text-align-last',
4440
+ textCombineUpright: 'text-combine-upright',
4441
+ textDecoration: 'text-decoration',
4442
+ textDecorationColor: 'text-decoration-color',
4443
+ textDecorationLine: 'text-decoration-line',
4444
+ textDecorationStyle: 'text-decoration-style',
4445
+ textIndent: 'text-indent',
4446
+ textJustify: 'text-justify',
4447
+ textOrientation: 'text-orientation',
4448
+ textOverflow: 'text-overflow',
4449
+ textShadow: 'text-shadow',
4450
+ textTransform: 'text-transform',
4451
+ transformBox: 'transform-box',
4452
+ transformOrigin: 'transform-origin',
4453
+ transformStyle: 'transform-style',
4454
+ transitionDelay: 'transition-delay',
4455
+ transitionDuration: 'transition-duration',
4456
+ transitionProperty: 'transition-property',
4457
+ transitionTimingFunction: 'transition-timing-function',
4458
+ unicodeBidi: 'unicode-bidi',
4459
+ userSelect: 'user-select',
4460
+ verticalAlign: 'vertical-align',
4461
+ whiteSpace: 'white-space',
4462
+ willChange: 'will-change',
4463
+ wordBreak: 'word-break',
4464
+ wordSpacing: 'word-spacing',
4465
+ wordWrap: 'word-wrap',
4466
+ writingMode: 'writing-mode',
4467
+ zIndex: 'z-index',
4468
+ WebkitTransform: '-webkit-transform',
4469
+ WebkitTransition: '-webkit-transition',
4470
+ WebkitBoxShadow: '-webkit-box-shadow',
4471
+ MozBoxShadow: '-moz-box-shadow',
4472
+ MozTransform: '-moz-transform',
4473
+ MozTransition: '-moz-transition',
4474
+ msTransform: '-ms-transform',
4475
+ msTransition: '-ms-transition',
4476
+ };
4477
+
4478
+ /** @type {Record<string | symbol, string>} */
4479
+ static cssPropAliases = new Proxy(TinyHtml.#cssPropAliases, {
4480
+ set(target, camelCaseKey, kebabValue) {
4481
+ target[camelCaseKey] = kebabValue;
4482
+ // @ts-ignore
4483
+ TinyHtml.cssPropRevAliases[kebabValue] = camelCaseKey;
4484
+ return true;
4485
+ },
4486
+ });
4487
+
4488
+ /** @type {Record<string | symbol, string>} */
4489
+ static cssPropRevAliases = Object.fromEntries(
4490
+ Object.entries(TinyHtml.#cssPropAliases).map(([camel, kebab]) => [kebab, camel]),
4491
+ );
4492
+
4493
+ /**
4494
+ * Converts a camelCase string to kebab-case
4495
+ * @param {string} str
4496
+ * @returns {string}
4497
+ */
4498
+ static toStyleKc(str) {
4499
+ if (typeof TinyHtml.cssPropAliases[str] === 'string') return TinyHtml.cssPropAliases[str];
4500
+ return str;
4501
+ }
4502
+
4503
+ /**
4504
+ * Converts a kebab-case string to camelCase
4505
+ * @param {string} str
4506
+ * @returns {string}
4507
+ */
4508
+ static toStyleCc(str) {
4509
+ if (typeof TinyHtml.cssPropRevAliases[str] === 'string') return TinyHtml.cssPropRevAliases[str];
4510
+ return str;
4511
+ }
4512
+
4513
+ /**
4514
+ * Sets one or more CSS inline style properties on the given element(s).
4515
+ *
4516
+ * - If `prop` is a string, the `value` will be applied to that property.
4517
+ * - If `prop` is an object, each key-value pair will be applied as a CSS property and value.
4518
+ *
4519
+ * @param {TinyHtmlElement|TinyHtmlElement[]} el - The element to inspect.
4520
+ * @param {string|Object} prop - The property name or an object with key-value pairs
4521
+ * @param {string|null} [value=null] - The value to set (if `prop` is a string)
4522
+ */
4523
+ static setStyle(el, prop, value = null) {
4524
+ TinyHtml._preHtmlElems(el, 'setStyle').forEach((elem) => {
4525
+ if (typeof prop === 'object') {
4526
+ for (const [k, v] of Object.entries(prop)) {
4527
+ elem.style.setProperty(
4528
+ TinyHtml.toStyleKc(k),
4529
+ typeof v === 'string' ? v : typeof v === 'number' ? `${v}px` : String(v),
4530
+ );
4531
+ }
4532
+ } else elem.style.setProperty(TinyHtml.toStyleKc(prop), value);
4533
+ });
4534
+ }
4535
+
4536
+ /**
4537
+ * Sets one or more CSS inline style properties on the given element(s).
4538
+ *
4539
+ * - If `prop` is a string, the `value` will be applied to that property.
4540
+ * - If `prop` is an object, each key-value pair will be applied as a CSS property and value.
4541
+ *
4542
+ * @param {string|Object} prop - The property name or an object with key-value pairs
4543
+ * @param {string|null} [value=null] - The value to set (if `prop` is a string)
4544
+ */
4545
+ setStyle(prop, value) {
4546
+ return TinyHtml.setStyle(this, prop, value);
4547
+ }
4548
+
4549
+ /**
4550
+ * Gets the value of a specific inline style property.
4551
+ *
4552
+ * Returns only the value set directly via the `style` attribute.
4553
+ *
4554
+ * @param {TinyHtmlElement|TinyHtmlElement[]} el - A single element to inspect.
4555
+ * @param {string} prop - The style property name to retrieve.
4556
+ * @returns {string} The style value of the specified property.
4557
+ */
4558
+ static getStyle(el, prop) {
4559
+ return TinyHtml._preHtmlElem(el, 'getStyle').style.getPropertyValue(TinyHtml.toStyleKc(prop));
4560
+ }
4561
+
4562
+ /**
4563
+ * Gets the value of a specific inline style property.
4564
+ *
4565
+ * Returns only the value set directly via the `style` attribute.
4566
+ *
4567
+ * @param {string} prop - The style property name to retrieve.
4568
+ * @returns {string} The style value of the specified property.
4569
+ */
4570
+ getStyle(prop) {
4571
+ return TinyHtml.getStyle(this, prop);
4572
+ }
4573
+
4574
+ /**
4575
+ * Gets all inline styles defined directly on the element (`style` attribute).
4576
+ *
4577
+ * Returns an object with all property-value pairs in kebab-case format.
4578
+ *
4579
+ * @param {TinyHtmlElement|TinyHtmlElement[]} el - A single element to inspect.
4580
+ * @param {Object} [settings={}] - Optional configuration settings.
4581
+ * @param {boolean} [settings.camelCase=false] - If `true`, the property names will be converted to camelCase.
4582
+ * @param {boolean} [settings.rawAttr=false] - If `true`, reads the style string from the `style` attribute instead of using the style object.
4583
+ * @returns {Record<string, string>} All inline styles as an object.
4584
+ *
4585
+ * @throws {TypeError} If `camelCase` or `rawAttr` is not a boolean.
4586
+ */
4587
+ static style(el, { camelCase = false, rawAttr = false } = {}) {
4588
+ if (typeof camelCase !== 'boolean')
4589
+ throw new TypeError(`"camelCase" must be a boolean. Received: ${typeof camelCase}`);
4590
+ if (typeof rawAttr !== 'boolean')
4591
+ throw new TypeError(`"rawAttr" must be a boolean. Received: ${typeof rawAttr}`);
4592
+
4593
+ const elem = TinyHtml._preHtmlElem(el, 'style');
4594
+ /** @type {Record<string, string>} */
4595
+ const result = {};
4596
+
4597
+ if (rawAttr) {
4598
+ const raw = elem.getAttribute('style') || '';
4599
+ const entries = raw.split(';');
4600
+ for (const entry of entries) {
4601
+ const [rawProp, rawVal] = entry.split(':');
4602
+ if (!rawProp || !rawVal) continue;
4603
+
4604
+ const prop = rawProp.trim();
4605
+ const value = rawVal.trim();
4606
+ result[camelCase ? TinyHtml.toStyleCc(prop) : prop] = value;
4607
+ }
4608
+ } else {
4609
+ const styles = elem.style;
4610
+ for (let i = 0; i < styles.length; i++) {
4611
+ const prop = styles[i]; // Already in kebab-case
4612
+ const value = styles.getPropertyValue(prop);
4613
+ result[camelCase ? TinyHtml.toStyleCc(prop) : prop] = value;
4614
+ }
4615
+ }
4616
+
4617
+ return result;
4618
+ }
4619
+
4620
+ /**
4621
+ * Gets all inline styles defined directly on the element (`style` attribute).
4622
+ *
4623
+ * Returns an object with all property-value pairs in kebab-case format.
4624
+ *
4625
+ * @param {Object} [settings={}] - Optional configuration settings.
4626
+ * @param {boolean} [settings.camelCase=false] - If `true`, the property names will be converted to camelCase.
4627
+ * @param {boolean} [settings.rawAttr=false] - If `true`, reads the style string from the `style` attribute instead of using the style object.
4628
+ * @returns {Record<string, string>} All inline styles as an object.
4629
+ */
4630
+ style(settings) {
4631
+ return TinyHtml.style(this, settings);
4632
+ }
4633
+
4634
+ /**
4635
+ * Removes one or more inline CSS properties from the given element(s).
4636
+ *
4637
+ * @param {TinyHtmlElement|TinyHtmlElement[]} el - A single element or an array of elements.
4638
+ * @param {string|string[]} prop - A property name or an array of property names to remove.
4639
+ */
4640
+ static removeStyle(el, prop) {
4641
+ TinyHtml._preHtmlElems(el, 'removeStyle').forEach((elem) => {
4642
+ if (Array.isArray(prop)) {
4643
+ for (const p of prop) {
4644
+ elem.style.removeProperty(TinyHtml.toStyleKc(p));
4645
+ }
4646
+ } else elem.style.removeProperty(TinyHtml.toStyleKc(prop));
4647
+ });
4648
+ }
4649
+
4650
+ /**
4651
+ * Removes one or more inline CSS properties from the given element(s).
4652
+ *
4653
+ * @param {string|string[]} prop - A property name or an array of property names to remove.
4654
+ */
4655
+ removeStyle(prop) {
4656
+ return TinyHtml.removeStyle(this, prop);
4657
+ }
4658
+
4659
+ /**
4660
+ * Toggles a CSS property value between two given values.
4661
+ *
4662
+ * The current computed value is compared to `val1`. If it matches, the property is set to `val2`. Otherwise, it is set to `val1`.
4663
+ *
4664
+ * @param {TinyHtmlElement|TinyHtmlElement[]} el - A single element or an array of elements.
4665
+ * @param {string} prop - The CSS property to toggle.
4666
+ * @param {string} val1 - The first value (used as "current" check).
4667
+ * @param {string} val2 - The second value (used as the "alternative").
4668
+ */
4669
+ static toggleStyle(el, prop, val1, val2) {
4670
+ TinyHtml._preHtmlElems(el, 'toggleStyle').forEach((elem) => {
4671
+ const current = TinyHtml.getStyle(elem, prop).trim();
4672
+ const newVal = current === TinyHtml.toStyleKc(val1) ? val2 : val1;
4673
+ TinyHtml.setStyle(elem, prop, newVal);
4674
+ });
4675
+ }
4676
+
4677
+ /**
4678
+ * Toggles a CSS property value between two given values.
4679
+ *
4680
+ * The current computed value is compared to `val1`. If it matches, the property is set to `val2`. Otherwise, it is set to `val1`.
4681
+ *
4682
+ * @param {string} prop - The CSS property to toggle.
4683
+ * @param {string} val1 - The first value (used as "current" check).
4684
+ * @param {string} val2 - The second value (used as the "alternative").
4685
+ */
4686
+ toggleStyle(prop, val1, val2) {
4687
+ return TinyHtml.toggleStyle(this, prop, val1, val2);
4688
+ }
4689
+
4690
+ /**
4691
+ * Removes all inline styles (`style` attribute) from the given element(s).
4692
+ *
4693
+ * @param {TinyElement|TinyElement[]} el - A single element or an array of elements.
4694
+ */
4695
+ static clearStyle(el) {
4696
+ TinyHtml._preElems(el, 'clearStyle').forEach((elem) => elem.removeAttribute('style'));
4697
+ }
4698
+
4699
+ /**
4700
+ * Removes all inline styles (`style` attribute) from the given element(s).
4701
+ *
4702
+ */
4703
+ clearStyle() {
4704
+ return TinyHtml.clearStyle(this);
4705
+ }
4706
+
4707
+ //////////////////////////////////////////////////////////////////////
4708
+
4221
4709
  /**
4222
4710
  * Focus the element.
4223
4711
  *