@schukai/monster 4.148.2 → 4.148.4

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 (29) hide show
  1. package/package.json +1 -1
  2. package/source/components/datatable/filter/date-presets.mjs +13 -0
  3. package/source/components/datatable/filter/date-range.mjs +12 -0
  4. package/source/components/datatable/filter/date-time.mjs +14 -1
  5. package/source/components/datatable/filter/date.mjs +14 -1
  6. package/source/components/datatable/filter/input.mjs +14 -1
  7. package/source/components/datatable/filter/range.mjs +11 -0
  8. package/source/components/datatable/filter/text-operator.mjs +19 -0
  9. package/source/components/datatable/filter/time.mjs +14 -1
  10. package/source/components/form/buy-box.mjs +2 -0
  11. package/source/components/form/cart-control.mjs +2 -0
  12. package/source/components/form/select.mjs +104 -16
  13. package/source/components/state/state.mjs +2 -0
  14. package/source/data/extend.mjs +7 -1
  15. package/source/dom/customcontrol.mjs +28 -23
  16. package/source/dom/customelement.mjs +105 -39
  17. package/source/dom/util/extract-keys.mjs +2 -1
  18. package/source/dom/util/init-options-from-attributes.mjs +1 -1
  19. package/source/dom/util/set-option-from-attribute.mjs +14 -3
  20. package/test/cases/components/datatable/filter-form-value.mjs +98 -0
  21. package/test/cases/components/form/non-value-form-association.mjs +53 -0
  22. package/test/cases/components/form/select.mjs +275 -0
  23. package/test/cases/data/extend.mjs +27 -1
  24. package/test/cases/dom/customcontrol.mjs +188 -6
  25. package/test/cases/dom/customelement-initfromscripthost.mjs +62 -0
  26. package/test/cases/dom/customelement.mjs +123 -2
  27. package/test/cases/dom/util/extract-keys.mjs +14 -0
  28. package/test/cases/dom/util/init-options-from-attributes.mjs +18 -0
  29. package/test/cases/dom/util/set-option-from-attribute.mjs +151 -0
@@ -117,6 +117,7 @@ const updateCloneDataSymbol = Symbol("@schukai/monster/dom/@@updateCloneData");
117
117
  * @type {symbol}
118
118
  */
119
119
  const scriptHostElementSymbol = Symbol("scriptHostElement");
120
+ const optionDefaultsSymbol = Symbol("optionDefaults");
120
121
  const managedShadowRootSymbol = Symbol("managedShadowRoot");
121
122
  const visibilityStateSymbol = Symbol("visibilityState");
122
123
  let hostVisibilityStyleSheet = null;
@@ -215,7 +216,11 @@ class CustomElement extends HTMLElement {
215
216
 
216
217
  this[attributeObserverSymbol] = {};
217
218
 
218
- const options = initOptionsFromAttributes(this, extend({}, this.defaults));
219
+ this[optionDefaultsSymbol] = this.defaults;
220
+ const options = initOptionsFromAttributes(
221
+ this,
222
+ extend({}, this[optionDefaultsSymbol]),
223
+ );
219
224
  if (!isObject(options)) {
220
225
  throw new Error(
221
226
  `The options are not defined correctly in the ${this.getTag()} element.`,
@@ -819,6 +824,9 @@ class CustomElement extends HTMLElement {
819
824
  this,
820
825
  attrName,
821
826
  this[internalSymbol].getSubject()["options"],
827
+ {},
828
+ "data-monster-option-",
829
+ this[optionDefaultsSymbol],
822
830
  );
823
831
  }
824
832
 
@@ -888,21 +896,22 @@ function callControlCallback(callBackFunctionName, ...args) {
888
896
  return;
889
897
  }
890
898
 
891
- if (this[scriptHostElementSymbol].length === 0) {
892
- const targetId = this.getAttribute(ATTRIBUTE_SCRIPT_HOST);
893
- if (!targetId) {
894
- return;
895
- }
896
-
897
- const list = targetId.split(",");
898
- for (const id of list) {
899
- const host = findElementWithIdUpwards(this, id.trim());
900
- if (!(host instanceof HTMLElement)) {
901
- continue;
902
- }
899
+ const targetId = this.getAttribute(ATTRIBUTE_SCRIPT_HOST);
900
+ if (!targetId) {
901
+ return;
902
+ }
903
903
 
904
- this[scriptHostElementSymbol].push(host);
904
+ // The attribute and DOM ancestry can change while the control remains alive.
905
+ // Resolve the small ordered host list against the current context for every call.
906
+ this[scriptHostElementSymbol].length = 0;
907
+ const list = targetId.split(",");
908
+ for (const id of list) {
909
+ const host = findElementWithIdUpwards(this, id.trim());
910
+ if (!(host instanceof HTMLElement)) {
911
+ continue;
905
912
  }
913
+
914
+ this[scriptHostElementSymbol].push(host);
906
915
  }
907
916
 
908
917
  for (const host of this[scriptHostElementSymbol]) {
@@ -1091,48 +1100,105 @@ function containChildNode(node) {
1091
1100
  */
1092
1101
  function initOptionObserver() {
1093
1102
  const self = this;
1103
+ const disabledQuery =
1104
+ "button, command, fieldset, keygen, optgroup, option, select, textarea, input, [data-monster-objectlink]";
1094
1105
 
1095
1106
  self[internalSymbol].lastDisabledValue = undefined;
1107
+ self[internalSymbol].parentDisabledElements = new Set();
1096
1108
 
1097
- const syncDisabledState = () => {
1098
- const flag = self.getOption("disabled", false);
1109
+ const getDisabledTargets = () => {
1110
+ const targets = new Set();
1111
+ const shadowRoot = getManagedShadowRoot.call(self);
1099
1112
 
1100
- if (flag === self[internalSymbol].lastDisabledValue) {
1101
- return;
1113
+ try {
1114
+ for (const element of self.querySelectorAll(disabledQuery)) {
1115
+ targets.add(element);
1116
+ }
1117
+ if (shadowRoot instanceof ShadowRoot) {
1118
+ for (const element of shadowRoot.querySelectorAll(disabledQuery)) {
1119
+ targets.add(element);
1120
+ }
1121
+ }
1122
+ for (const element of getSlottedElements.call(self, disabledQuery)) {
1123
+ targets.add(element);
1124
+ }
1125
+ } catch (e) {}
1126
+
1127
+ return targets;
1128
+ };
1129
+
1130
+ const disconnectDisabledObservers = () => {
1131
+ self[internalSymbol].disabledLightObserver?.disconnect();
1132
+ self[internalSymbol].disabledShadowObserver?.disconnect();
1133
+ delete self[internalSymbol].disabledLightObserver;
1134
+ delete self[internalSymbol].disabledShadowObserver;
1135
+ };
1136
+
1137
+ const observeDisabledTargets = () => {
1138
+ const callback = () => syncDisabledState(true);
1139
+ if (!self[internalSymbol].disabledLightObserver) {
1140
+ self[internalSymbol].disabledLightObserver = new MutationObserver(
1141
+ callback,
1142
+ );
1143
+ self[internalSymbol].disabledLightObserver.observe(self, {
1144
+ attributes: true,
1145
+ attributeFilter: [ATTRIBUTE_DISABLED, "slot"],
1146
+ childList: true,
1147
+ subtree: true,
1148
+ });
1102
1149
  }
1103
1150
 
1104
1151
  const shadowRoot = getManagedShadowRoot.call(self);
1105
- if (!(shadowRoot instanceof ShadowRoot) && !self.childNodes.length) {
1152
+ if (
1153
+ shadowRoot instanceof ShadowRoot &&
1154
+ !self[internalSymbol].disabledShadowObserver
1155
+ ) {
1156
+ self[internalSymbol].disabledShadowObserver = new MutationObserver(
1157
+ callback,
1158
+ );
1159
+ self[internalSymbol].disabledShadowObserver.observe(shadowRoot, {
1160
+ attributes: true,
1161
+ attributeFilter: [ATTRIBUTE_DISABLED, "slot"],
1162
+ childList: true,
1163
+ subtree: true,
1164
+ });
1165
+ }
1166
+ };
1167
+
1168
+ const syncDisabledState = (force = false) => {
1169
+ const flag =
1170
+ self.getOption("disabled", false) === true ||
1171
+ self[internalSymbol].formDisabledValue === true;
1172
+
1173
+ if (force !== true && flag === self[internalSymbol].lastDisabledValue) {
1106
1174
  return;
1107
1175
  }
1108
1176
 
1109
1177
  self[internalSymbol].lastDisabledValue = flag;
1178
+ const ownedElements = self[internalSymbol].parentDisabledElements;
1110
1179
 
1111
- const query =
1112
- "button, command, fieldset, keygen, optgroup, option, select, textarea, input, [data-monster-objectlink]";
1113
-
1114
- let elements = [];
1115
- if (shadowRoot instanceof ShadowRoot) {
1116
- elements = shadowRoot.querySelectorAll(query);
1180
+ if (flag !== true) {
1181
+ disconnectDisabledObservers();
1182
+ for (const element of ownedElements) {
1183
+ element.removeAttribute(ATTRIBUTE_DISABLED);
1184
+ }
1185
+ ownedElements.clear();
1186
+ return;
1117
1187
  }
1118
1188
 
1119
- let nodeList;
1120
- try {
1121
- const baseElements =
1122
- elements.length > 0 ? elements : self.querySelectorAll(query);
1123
- nodeList = new Set([
1124
- ...baseElements,
1125
- ...getSlottedElements.call(self, query),
1126
- ]);
1127
- } catch (e) {
1128
- nodeList = elements;
1189
+ observeDisabledTargets();
1190
+ const targets = getDisabledTargets();
1191
+ for (const element of [...ownedElements]) {
1192
+ if (!targets.has(element)) {
1193
+ element.removeAttribute(ATTRIBUTE_DISABLED);
1194
+ ownedElements.delete(element);
1195
+ }
1129
1196
  }
1130
1197
 
1131
- for (const element of [...nodeList]) {
1132
- if (flag === true) {
1198
+ for (const element of targets) {
1199
+ if (!element.hasAttribute(ATTRIBUTE_DISABLED)) {
1133
1200
  element.setAttribute(ATTRIBUTE_DISABLED, "");
1134
- } else {
1135
- element.removeAttribute(ATTRIBUTE_DISABLED);
1201
+ ownedElements.add(element);
1136
1202
  }
1137
1203
  }
1138
1204
  };
@@ -55,7 +55,7 @@ function extractKeys(
55
55
  currentKebabKeyPrefix,
56
56
  currentValuePrefix,
57
57
  ) {
58
- for (const key in currentObj) {
58
+ for (const key of Object.keys(currentObj)) {
59
59
  const compactSegment = normalizeKeySegment(key);
60
60
  const kebabSegment = toKebabCase(key);
61
61
 
@@ -73,6 +73,7 @@ function extractKeys(
73
73
  const newValuePrefix = currentValuePrefix
74
74
  ? currentValuePrefix + valueSeparator + key
75
75
  : key;
76
+ appendKeys(newCompactKeyPrefix, newKebabKeyPrefix, newValuePrefix);
76
77
  helper(
77
78
  currentObj[key],
78
79
  newCompactKeyPrefix,
@@ -84,7 +84,7 @@ function initOptionsFromAttributes(
84
84
  if (element.hasAttribute(name)) {
85
85
  let value = element.getAttribute(name);
86
86
  if (
87
- mapping.hasOwnProperty(optionName) &&
87
+ Object.prototype.hasOwnProperty.call(mapping, optionName) &&
88
88
  isFunction(mapping[optionName])
89
89
  ) {
90
90
  value = mapping[optionName](value);
@@ -24,6 +24,7 @@ import {
24
24
  } from "../../types/is.mjs";
25
25
  import { attributeObserverSymbol } from "../customelement.mjs";
26
26
  import { extractKeys } from "./extract-keys.mjs";
27
+ import { clone } from "../../util/clone.mjs";
27
28
 
28
29
  export { setOptionFromAttribute };
29
30
 
@@ -56,6 +57,7 @@ export { setOptionFromAttribute };
56
57
  * @param {Object} options - The options object to be initialized.
57
58
  * @param {Object} mapping - A mapping between the attribute value and the property value.
58
59
  * @param {string} prefix - The prefix of the attributes to be considered.
60
+ * @param {Object} defaults - The default option schema used for coercion and removal.
59
61
  * @return {Object} - The initialized options object.
60
62
  * @this HTMLElement - The context of the DOM element.
61
63
  */
@@ -65,12 +67,13 @@ function setOptionFromAttribute(
65
67
  options,
66
68
  mapping = {},
67
69
  prefix = "data-monster-option-",
70
+ defaults = options,
68
71
  ) {
69
72
  if (!(element instanceof HTMLElement)) return options;
70
- if (!element.hasAttributes()) return options;
71
73
 
72
74
  const keyMap = extractKeys(options);
73
75
  const finder = new Pathfinder(options);
76
+ const defaultFinder = new Pathfinder(defaults);
74
77
 
75
78
  // check if the attribute name is a valid option.
76
79
  // the mapping between the attribute is simple. The dash is replaced by a dot.
@@ -79,15 +82,23 @@ function setOptionFromAttribute(
79
82
  if (!finder.exists(optionName)) return;
80
83
 
81
84
  if (!element.hasAttribute(name)) {
85
+ if (defaultFinder.exists(optionName)) {
86
+ finder.setVia(optionName, clone(defaultFinder.getVia(optionName)));
87
+ }
82
88
  return options;
83
89
  }
84
90
 
85
91
  let value = element.getAttribute(name);
86
- if (mapping.hasOwnProperty(optionName) && isFunction(mapping[optionName])) {
92
+ if (
93
+ Object.prototype.hasOwnProperty.call(mapping, optionName) &&
94
+ isFunction(mapping[optionName])
95
+ ) {
87
96
  value = mapping[optionName](value);
88
97
  }
89
98
 
90
- let optionValue = finder.getVia(optionName);
99
+ let optionValue = defaultFinder.exists(optionName)
100
+ ? defaultFinder.getVia(optionName)
101
+ : finder.getVia(optionName);
91
102
  if (optionValue === null || optionValue === undefined) {
92
103
  optionValue = value;
93
104
  }
@@ -0,0 +1,98 @@
1
+ 'use strict';
2
+
3
+ import { expect } from 'chai';
4
+ import { getDocument } from '../../../../source/dom/util.mjs';
5
+ import { initJSDOM } from '../../../util/jsdom.mjs';
6
+
7
+ const cases = [
8
+ { tag: 'monster-filter-date', value: '2026-07-16' },
9
+ { tag: 'monster-filter-date-time', value: '2026-07-16T12:30' },
10
+ { tag: 'monster-filter-time', value: '12:30' },
11
+ { tag: 'monster-filter-input', value: 'search term' },
12
+ { tag: 'monster-filter-text-operator', value: ':search term', userValue: 'search term' },
13
+ { tag: 'monster-filter-date-presets', preset: true },
14
+ { tag: 'monster-filter-date-range', value: '2026-07-01-2026-07-16' },
15
+ { tag: 'monster-filter-range', value: '10-20' },
16
+ ];
17
+
18
+ describe('datatable filter form values issue #502', function () {
19
+ let document;
20
+
21
+ before(async function () {
22
+ await initJSDOM({});
23
+ await import('element-internals-polyfill');
24
+ await Promise.all([
25
+ import('../../../../source/components/datatable/filter/date.mjs'),
26
+ import('../../../../source/components/datatable/filter/date-time.mjs'),
27
+ import('../../../../source/components/datatable/filter/time.mjs'),
28
+ import('../../../../source/components/datatable/filter/input.mjs'),
29
+ import('../../../../source/components/datatable/filter/text-operator.mjs'),
30
+ import('../../../../source/components/datatable/filter/date-presets.mjs'),
31
+ import('../../../../source/components/datatable/filter/date-range.mjs'),
32
+ import('../../../../source/components/datatable/filter/range.mjs'),
33
+ ]);
34
+ document = getDocument();
35
+ });
36
+
37
+ afterEach(function () {
38
+ document.getElementById('mocks').innerHTML = '';
39
+ });
40
+
41
+ for (const definition of cases) {
42
+ it(`${definition.tag} should submit programmatic and empty values`, async function () {
43
+ const form = document.createElement('form');
44
+ const control = document.createElement(definition.tag);
45
+ control.setAttribute('name', 'filter');
46
+ form.appendChild(control);
47
+ document.getElementById('mocks').appendChild(form);
48
+
49
+ expect(new window.FormData(form).get('filter')).to.equal('');
50
+
51
+ const value = definition.preset
52
+ ? control.shadowRoot.querySelector('select').options[1].value
53
+ : definition.value;
54
+ control.value = value;
55
+
56
+ expect(new window.FormData(form).get('filter')).to.equal(control.value);
57
+
58
+ control.setAttribute('value', value);
59
+ control.value = '';
60
+ control.formResetCallback();
61
+ expect(new window.FormData(form).get('filter')).to.equal(control.value);
62
+
63
+ control.formStateRestoreCallback(value, 'restore');
64
+ expect(new window.FormData(form).get('filter')).to.equal(control.value);
65
+
66
+ control.remove();
67
+ form.appendChild(control);
68
+ expect(new window.FormData(form).get('filter')).to.equal(control.value);
69
+
70
+ control.setAttribute('disabled', '');
71
+ await new Promise((resolve) => setTimeout(resolve, 20));
72
+ if (!window.navigator.userAgent.includes('jsdom')) {
73
+ expect(new window.FormData(form).get('filter')).to.equal(null);
74
+ }
75
+ });
76
+
77
+ it(`${definition.tag} should submit user-driven values`, function () {
78
+ const form = document.createElement('form');
79
+ const control = document.createElement(definition.tag);
80
+ control.setAttribute('name', 'filter');
81
+ form.appendChild(control);
82
+ document.getElementById('mocks').appendChild(form);
83
+
84
+ const input = definition.tag === 'monster-filter-text-operator'
85
+ ? control.shadowRoot.querySelector('[data-monster-role=query]')
86
+ : definition.preset
87
+ ? control.shadowRoot.querySelector('select')
88
+ : control.shadowRoot.querySelector('[data-monster-role=query], [data-monster-role=input]');
89
+ input.value = definition.preset
90
+ ? input.options[1].value
91
+ : definition.userValue ?? definition.value;
92
+ input.dispatchEvent(new window.Event('input', { bubbles: true, composed: true }));
93
+ input.dispatchEvent(new window.Event('change', { bubbles: true, composed: true }));
94
+
95
+ expect(new window.FormData(form).get('filter')).to.equal(control.value);
96
+ });
97
+ }
98
+ });
@@ -0,0 +1,53 @@
1
+ 'use strict';
2
+
3
+ import { expect } from 'chai';
4
+ import { initJSDOM } from '../../../util/jsdom.mjs';
5
+
6
+ describe('non-value component form association issue #503', function () {
7
+ let CustomControl;
8
+ let components;
9
+
10
+ before(async function () {
11
+ await initJSDOM({});
12
+ await import('element-internals-polyfill');
13
+ ({ CustomControl } = await import('../../../../source/dom/customcontrol.mjs'));
14
+ const [{ State }, { CartControl }, { BuyBox }] = await Promise.all([
15
+ import('../../../../source/components/state/state.mjs'),
16
+ import('../../../../source/components/form/cart-control.mjs'),
17
+ import('../../../../source/components/form/buy-box.mjs'),
18
+ ]);
19
+ components = [State, CartControl, BuyBox];
20
+ });
21
+
22
+ afterEach(function () {
23
+ document.getElementById('mocks').innerHTML = '';
24
+ });
25
+
26
+ it('should opt non-value components out of form association', function () {
27
+ for (const Component of components) {
28
+ expect(Component.formAssociated, Component.getTag()).to.equal(false);
29
+ }
30
+ });
31
+
32
+ it('should preserve the public CustomControl inheritance', function () {
33
+ for (const Component of components) {
34
+ const control = document.createElement(Component.getTag());
35
+ expect(control, Component.getTag()).to.be.instanceof(CustomControl);
36
+ }
37
+ });
38
+
39
+ it('should not contribute values or throw during form reset', function () {
40
+ for (const Component of components) {
41
+ const form = document.createElement('form');
42
+ const control = document.createElement(Component.getTag());
43
+ control.setAttribute('name', Component.getTag());
44
+ form.appendChild(control);
45
+ document.getElementById('mocks').appendChild(form);
46
+
47
+ expect(() => form.reset()).not.to.throw();
48
+ expect(new window.FormData(form).get(Component.getTag())).to.equal(null);
49
+
50
+ form.remove();
51
+ }
52
+ });
53
+ });