botframework-webchat 4.15.3-main.20220706.ecf53f4 → 4.15.3-main.20220720.8c4b995
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.
- package/dist/webchat-es5.js +1 -1
- package/dist/webchat-minimal.js +1 -1
- package/dist/webchat.js +1 -1
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/private/closest.d.ts +2 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/private/closest.d.ts.map +1 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/private/closest.js +25 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/private/findDOMNodeOwner.d.ts +3 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/private/findDOMNodeOwner.d.ts.map +1 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/private/findDOMNodeOwner.js +32 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/private/useAdaptiveCardModEffect.d.ts +13 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/private/useAdaptiveCardModEffect.d.ts.map +1 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/private/useAdaptiveCardModEffect.js +132 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/private/useLazyRef.d.ts +3 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/private/useLazyRef.d.ts.map +1 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/private/useLazyRef.js +21 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/private/usePrevious.d.ts +2 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/private/usePrevious.d.ts.map +1 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/private/usePrevious.js +18 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/private/useValueRef.d.ts +3 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/private/useValueRef.d.ts.map +1 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/private/useValueRef.js +15 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/useActionSetShouldNotBeMenuBarModEffect.d.ts +16 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/useActionSetShouldNotBeMenuBarModEffect.d.ts.map +1 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/useActionSetShouldNotBeMenuBarModEffect.js +45 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/useActionShouldBePushButtonModEffect.d.ts +15 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/useActionShouldBePushButtonModEffect.d.ts.map +1 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/useActionShouldBePushButtonModEffect.js +93 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/useActiveElementModEffect.d.ts +6 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/useActiveElementModEffect.d.ts.map +1 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/useActiveElementModEffect.js +44 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/useDisabledModEffect.d.ts +9 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/useDisabledModEffect.d.ts.map +1 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/useDisabledModEffect.js +50 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/usePersistValuesModEffect.d.ts +6 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/usePersistValuesModEffect.d.ts.map +1 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardHacks/usePersistValuesModEffect.js +103 -0
- package/lib/adaptiveCards/Attachment/AdaptiveCardRenderer.d.ts.map +1 -1
- package/lib/adaptiveCards/Attachment/AdaptiveCardRenderer.js +97 -557
- package/lib/adaptiveCards/Attachment/private/renderAdaptiveCard.d.ts +15 -0
- package/lib/adaptiveCards/Attachment/private/renderAdaptiveCard.d.ts.map +1 -0
- package/lib/adaptiveCards/Attachment/private/renderAdaptiveCard.js +79 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/addEventListenerWithUndo.d.ts +6 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/addEventListenerWithUndo.d.ts.map +1 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/addEventListenerWithUndo.js +25 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/bunchUndos.d.ts +3 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/bunchUndos.d.ts.map +1 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/bunchUndos.js +23 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/durableAddClassWithUndo.d.ts +8 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/durableAddClassWithUndo.d.ts.map +1 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/durableAddClassWithUndo.js +38 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/durableDisableInputElementAccessiblyWithUndo.d.ts +22 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/durableDisableInputElementAccessiblyWithUndo.d.ts.map +1 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/durableDisableInputElementAccessiblyWithUndo.js +96 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/private/addClass.d.ts +5 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/private/addClass.d.ts.map +1 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/private/addClass.js +19 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/private/getAttributeOrFalse.d.ts +7 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/private/getAttributeOrFalse.d.ts.map +1 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/private/getAttributeOrFalse.js +16 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/private/noOp.d.ts +3 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/private/noOp.d.ts.map +1 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/private/noOp.js +14 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/private/setOrRemoveAttributeIfFalse.d.ts +9 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/private/setOrRemoveAttributeIfFalse.d.ts.map +1 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/private/setOrRemoveAttributeIfFalse.js +22 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/setOrRemoveAttributeIfFalseWithUndo.d.ts +12 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/setOrRemoveAttributeIfFalseWithUndo.d.ts.map +1 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/setOrRemoveAttributeIfFalseWithUndo.js +41 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/types/UndoFunction.d.ts +3 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/types/UndoFunction.d.ts.map +1 -0
- package/lib/adaptiveCards/DOMManipulationWithUndo/types/UndoFunction.js +2 -0
- package/lib/addVersion.js +1 -1
- package/lib/createFullStyleSet.d.ts +2 -2
- package/package.json +7 -7
- package/src/adaptiveCards/Attachment/AdaptiveCardHacks/private/closest.ts +17 -0
- package/src/adaptiveCards/Attachment/AdaptiveCardHacks/private/findDOMNodeOwner.ts +25 -0
- package/src/adaptiveCards/Attachment/AdaptiveCardHacks/private/useAdaptiveCardModEffect.ts +93 -0
- package/src/adaptiveCards/Attachment/AdaptiveCardHacks/private/useLazyRef.ts +15 -0
- package/src/adaptiveCards/Attachment/AdaptiveCardHacks/private/usePrevious.ts +12 -0
- package/src/adaptiveCards/Attachment/AdaptiveCardHacks/private/useValueRef.ts +11 -0
- package/src/adaptiveCards/Attachment/AdaptiveCardHacks/useActionSetShouldNotBeMenuBarModEffect.ts +39 -0
- package/src/adaptiveCards/Attachment/AdaptiveCardHacks/useActionShouldBePushButtonModEffect.ts +105 -0
- package/src/adaptiveCards/Attachment/AdaptiveCardHacks/useActiveElementModEffect.ts +35 -0
- package/src/adaptiveCards/Attachment/AdaptiveCardHacks/useDisabledModEffect.ts +45 -0
- package/src/adaptiveCards/Attachment/AdaptiveCardHacks/usePersistValuesModEffect.ts +110 -0
- package/src/adaptiveCards/Attachment/AdaptiveCardRenderer.tsx +83 -582
- package/src/adaptiveCards/Attachment/private/renderAdaptiveCard.ts +75 -0
- package/src/adaptiveCards/DOMManipulationWithUndo/addEventListenerWithUndo.ts +21 -0
- package/src/adaptiveCards/DOMManipulationWithUndo/bunchUndos.tsx +12 -0
- package/src/adaptiveCards/DOMManipulationWithUndo/durableAddClassWithUndo.ts +28 -0
- package/src/adaptiveCards/DOMManipulationWithUndo/durableDisableInputElementAccessiblyWithUndo.ts +84 -0
- package/src/adaptiveCards/DOMManipulationWithUndo/private/addClass.tsx +13 -0
- package/src/adaptiveCards/DOMManipulationWithUndo/private/getAttributeOrFalse.ts +8 -0
- package/src/adaptiveCards/DOMManipulationWithUndo/private/noOp.ts +5 -0
- package/src/adaptiveCards/DOMManipulationWithUndo/private/setOrRemoveAttributeIfFalse.ts +18 -0
- package/src/adaptiveCards/DOMManipulationWithUndo/setOrRemoveAttributeIfFalseWithUndo.ts +34 -0
- package/src/adaptiveCards/DOMManipulationWithUndo/types/UndoFunction.ts +3 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AdaptiveCard,
|
|
3
|
+
GlobalSettings as GlobalSettingsType,
|
|
4
|
+
HostConfig as HostConfigType,
|
|
5
|
+
IMarkdownProcessingResult
|
|
6
|
+
} from 'adaptivecards';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Returns `true`, if the object is a plain object and not a class, otherwise, `false`.
|
|
10
|
+
*/
|
|
11
|
+
function isPlainObject(obj) {
|
|
12
|
+
return Object.getPrototypeOf(obj) === Object.prototype;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export default function renderAdaptiveCard(
|
|
16
|
+
adaptiveCard: AdaptiveCard,
|
|
17
|
+
{
|
|
18
|
+
adaptiveCardsHostConfig,
|
|
19
|
+
adaptiveCardsPackage: { GlobalSettings, HostConfig },
|
|
20
|
+
renderMarkdownAsHTML,
|
|
21
|
+
setTabIndexAtCardRoot
|
|
22
|
+
}: {
|
|
23
|
+
adaptiveCardsHostConfig?: HostConfigType;
|
|
24
|
+
adaptiveCardsPackage: {
|
|
25
|
+
GlobalSettings: typeof GlobalSettingsType;
|
|
26
|
+
HostConfig: typeof HostConfigType;
|
|
27
|
+
};
|
|
28
|
+
renderMarkdownAsHTML?: (markdown: string) => string;
|
|
29
|
+
setTabIndexAtCardRoot: boolean;
|
|
30
|
+
}
|
|
31
|
+
): { element: HTMLElement } | { errors: Error[] } {
|
|
32
|
+
// Currently, the only way to set the Markdown engine is to set it thru static member of AdaptiveCard class
|
|
33
|
+
|
|
34
|
+
// TODO: [P3] Checks if we could make the "renderMarkdownAsHTML" per card
|
|
35
|
+
// This could be limitations from Adaptive Cards package (not supported as of 1.2.5)
|
|
36
|
+
// Because there could be timing difference between .parse and .render, we could be using wrong Markdown engine
|
|
37
|
+
|
|
38
|
+
// "onProcessMarkdown" is a static function but we are trying to scope it to the current object instead.
|
|
39
|
+
// eslint-disable-next-line dot-notation
|
|
40
|
+
adaptiveCard.constructor['onProcessMarkdown'] = (text: string, result: IMarkdownProcessingResult) => {
|
|
41
|
+
if (renderMarkdownAsHTML) {
|
|
42
|
+
result.outputHtml = renderMarkdownAsHTML(text);
|
|
43
|
+
result.didProcess = true;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
if (adaptiveCardsHostConfig) {
|
|
48
|
+
adaptiveCard.hostConfig = isPlainObject(adaptiveCardsHostConfig)
|
|
49
|
+
? new HostConfig(adaptiveCardsHostConfig)
|
|
50
|
+
: adaptiveCardsHostConfig;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// For accessibility issue #1340, `tabindex="0"` must not be set for the root container if it is not interactive.
|
|
54
|
+
GlobalSettings.setTabIndexAtCardRoot = setTabIndexAtCardRoot;
|
|
55
|
+
|
|
56
|
+
const { validationEvents } = adaptiveCard.validateProperties();
|
|
57
|
+
|
|
58
|
+
if (validationEvents.length) {
|
|
59
|
+
return { errors: validationEvents.reduce((items, { message }) => [...items, new Error(message)], [] as Error[]) };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let element: HTMLElement | undefined;
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
element = adaptiveCard.render();
|
|
66
|
+
} catch (error) {
|
|
67
|
+
return { errors: [error] };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (!element) {
|
|
71
|
+
return { errors: [new Error('Adaptive Card rendered as empty element')] };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return { element };
|
|
75
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import noOp from './private/noOp';
|
|
2
|
+
|
|
3
|
+
import type { UndoFunction } from './types/UndoFunction';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Listens to event. Returns a function, when called, will stop listening.
|
|
7
|
+
*/
|
|
8
|
+
export default function addEventListenerWithUndo(
|
|
9
|
+
element: HTMLElement | undefined,
|
|
10
|
+
name: string,
|
|
11
|
+
handler: EventListener,
|
|
12
|
+
options?: AddEventListenerOptions | boolean
|
|
13
|
+
): UndoFunction {
|
|
14
|
+
if (!element) {
|
|
15
|
+
return noOp;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
element.addEventListener(name, handler, options);
|
|
19
|
+
|
|
20
|
+
return () => element.removeEventListener(name, handler, options);
|
|
21
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { UndoFunction } from './types/UndoFunction';
|
|
2
|
+
|
|
3
|
+
export default function bunchUndos(fns: UndoFunction[]): UndoFunction {
|
|
4
|
+
let called: boolean;
|
|
5
|
+
|
|
6
|
+
return () => {
|
|
7
|
+
if (!called) {
|
|
8
|
+
called = true;
|
|
9
|
+
[...fns].reverse().forEach(fn => fn?.());
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { addClass } from './private/addClass';
|
|
2
|
+
import noOp from './private/noOp';
|
|
3
|
+
|
|
4
|
+
import type { UndoFunction } from './types/UndoFunction';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Adds a class to the `HTMLElement` and re-add on mutations.
|
|
8
|
+
*
|
|
9
|
+
* @returns {function} A function, when called, will restore to previous state.
|
|
10
|
+
*/
|
|
11
|
+
export default function durableAddClassWithUndo(element: HTMLElement | undefined, className: string): UndoFunction {
|
|
12
|
+
if (element) {
|
|
13
|
+
addClass(element, className);
|
|
14
|
+
|
|
15
|
+
// After we add the class, keep observing the element to make sure the class is not removed.
|
|
16
|
+
const observer = new MutationObserver(() => addClass(element, className));
|
|
17
|
+
|
|
18
|
+
observer.observe(element, { attributes: true, attributeFilter: ['class'] });
|
|
19
|
+
|
|
20
|
+
return () => {
|
|
21
|
+
element.classList.remove(className);
|
|
22
|
+
|
|
23
|
+
observer.disconnect();
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return noOp;
|
|
28
|
+
}
|
package/src/adaptiveCards/DOMManipulationWithUndo/durableDisableInputElementAccessiblyWithUndo.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import addEventListenerWithUndo from './addEventListenerWithUndo';
|
|
2
|
+
import bunchUndos from './bunchUndos';
|
|
3
|
+
import noOp from './private/noOp';
|
|
4
|
+
import setOrRemoveAttributeIfFalseWithUndo from './setOrRemoveAttributeIfFalseWithUndo';
|
|
5
|
+
|
|
6
|
+
import type { UndoFunction } from './types/UndoFunction';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* An event handler for disabling event bubbling and propagation.
|
|
10
|
+
*/
|
|
11
|
+
function disabledHandler(event: Event): void {
|
|
12
|
+
event.preventDefault();
|
|
13
|
+
event.stopImmediatePropagation();
|
|
14
|
+
event.stopPropagation();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function disable(element: HTMLElement, undoStack: UndoFunction[]): void {
|
|
18
|
+
const tag = element.nodeName.toLowerCase();
|
|
19
|
+
|
|
20
|
+
/* eslint-disable-next-line default-case */
|
|
21
|
+
switch (tag) {
|
|
22
|
+
case 'button':
|
|
23
|
+
case 'input':
|
|
24
|
+
case 'select':
|
|
25
|
+
case 'textarea':
|
|
26
|
+
undoStack.push(
|
|
27
|
+
// "click" handler in capture phase to make sure we can block as much "click" event listeners as possible.
|
|
28
|
+
addEventListenerWithUndo(element, 'click', disabledHandler, { capture: true }),
|
|
29
|
+
setOrRemoveAttributeIfFalseWithUndo(element, 'aria-disabled', 'true'),
|
|
30
|
+
setOrRemoveAttributeIfFalseWithUndo(element, 'tabindex', '-1')
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
if (tag === 'input' || tag === 'textarea') {
|
|
34
|
+
undoStack.push(setOrRemoveAttributeIfFalseWithUndo(element, 'readonly', 'readonly'));
|
|
35
|
+
} else if (tag === 'select') {
|
|
36
|
+
undoStack.push(
|
|
37
|
+
...Array.from(element.querySelectorAll('option') as NodeListOf<HTMLOptionElement>).map(option =>
|
|
38
|
+
setOrRemoveAttributeIfFalseWithUndo(option, 'disabled', 'disabled')
|
|
39
|
+
)
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Disables an input element in accessible fashion with undo function.
|
|
49
|
+
*
|
|
50
|
+
* This is designed for accessibility and mimick the behavior of `disabled` attribute in accessible form:
|
|
51
|
+
*
|
|
52
|
+
* - Take away from focus ring;
|
|
53
|
+
* - If currently focused, do not move focus;
|
|
54
|
+
* - Mark content as readonly.
|
|
55
|
+
*
|
|
56
|
+
* Thus, it should not impact hyperlinks or other contents which are not affected by `disabled` attribute.
|
|
57
|
+
*
|
|
58
|
+
* For simplicity, currently, we did not disable element with `contenteditable` attribute.
|
|
59
|
+
*
|
|
60
|
+
* We only disable these elements: `<button>`, `<input>`, `<select>`, `<textarea>`.
|
|
61
|
+
*
|
|
62
|
+
* We need durability as Adaptive Cards occasionally reset `tabindex="0"`.
|
|
63
|
+
*
|
|
64
|
+
* @returns {function} A function, when called, will restore to previous state.
|
|
65
|
+
*/
|
|
66
|
+
export default function durableDisableInputElementAccessiblyWithUndo(element: HTMLElement | undefined): UndoFunction {
|
|
67
|
+
if (!element) {
|
|
68
|
+
return noOp;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const undoStack: UndoFunction[] = [];
|
|
72
|
+
|
|
73
|
+
const apply = () => disable(element, undoStack);
|
|
74
|
+
|
|
75
|
+
apply();
|
|
76
|
+
|
|
77
|
+
const observer = new MutationObserver(apply);
|
|
78
|
+
|
|
79
|
+
observer.observe(element, { attributeFilter: ['tabindex'] });
|
|
80
|
+
|
|
81
|
+
undoStack.push(() => observer.disconnect());
|
|
82
|
+
|
|
83
|
+
return bunchUndos(undoStack);
|
|
84
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adds a class to the `HTMLElement`. Returns `true` if the class is added, otherwise, `undefined`.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export function addClass(element: HTMLElement, className: string): true | undefined {
|
|
6
|
+
const { classList } = element;
|
|
7
|
+
|
|
8
|
+
if (!classList.contains(className)) {
|
|
9
|
+
classList.add(className);
|
|
10
|
+
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gets the value of an attribute from an element.
|
|
3
|
+
*
|
|
4
|
+
* @returns {false | string} The value of the attribute. `false` if the attribute was not set.
|
|
5
|
+
*/
|
|
6
|
+
export default function getAttributeOrFalse(element: HTMLElement, qualifiedName: string): false | string {
|
|
7
|
+
return !!element && element.hasAttribute(qualifiedName) && (element.getAttribute(qualifiedName) || '');
|
|
8
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sets or removes an attribute from an element.
|
|
3
|
+
*
|
|
4
|
+
* @param {HTMLElement} element - The element to set or remove attribute from.
|
|
5
|
+
* @param {string} qualifiedName - The name of the attribute.
|
|
6
|
+
* @param {false | string} value - The value of the attribute. When passing `false`, remove the attribute.
|
|
7
|
+
*/
|
|
8
|
+
export default function setOrRemoveAttributeIfFalse(
|
|
9
|
+
element: HTMLElement | undefined,
|
|
10
|
+
qualifiedName: string,
|
|
11
|
+
value: false | string
|
|
12
|
+
): void {
|
|
13
|
+
if (value === false) {
|
|
14
|
+
element?.removeAttribute(qualifiedName);
|
|
15
|
+
} else {
|
|
16
|
+
element?.setAttribute(qualifiedName, value);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import getAttributeOrFalse from './private/getAttributeOrFalse';
|
|
2
|
+
import noOp from './private/noOp';
|
|
3
|
+
import setOrRemoveAttributeIfFalse from './private/setOrRemoveAttributeIfFalse';
|
|
4
|
+
|
|
5
|
+
import type { UndoFunction } from './types/UndoFunction';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Sets or removes an attribute from an element with an undo function.
|
|
9
|
+
*
|
|
10
|
+
* @param {HTMLElement} element - The element to set or remove attribute from.
|
|
11
|
+
* @param {string} qualifiedName - The name of the attribute.
|
|
12
|
+
* @param {false | string} value - The value of the attribute. When passing `false`, remove the attribute.
|
|
13
|
+
*
|
|
14
|
+
* @returns {() => void} An undo function, when called, will undo all manipulations by restoring values recorded at the time of the function call.
|
|
15
|
+
*/
|
|
16
|
+
export default function setOrRemoveAttributeIfFalseWithUndo(
|
|
17
|
+
element: HTMLElement | undefined,
|
|
18
|
+
qualifiedName: string,
|
|
19
|
+
value: false | string
|
|
20
|
+
): UndoFunction {
|
|
21
|
+
if (!element) {
|
|
22
|
+
return noOp;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const prevValue = getAttributeOrFalse(element, qualifiedName);
|
|
26
|
+
|
|
27
|
+
if (prevValue === value) {
|
|
28
|
+
return noOp;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
setOrRemoveAttributeIfFalse(element, qualifiedName, value);
|
|
32
|
+
|
|
33
|
+
return () => setOrRemoveAttributeIfFalse(element, qualifiedName, prevValue);
|
|
34
|
+
}
|