Skip to content

Commit 88cfe33

Browse files
committed
feat: add async support for getAttribute and enhance processOperators with async handling
1 parent 682450a commit 88cfe33

File tree

3 files changed

+340
-55
lines changed

3 files changed

+340
-55
lines changed

src/getAttribute.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { processOperators } from "./operators";
1+
import { processOperators, processOperatorsAsync } from "./operators";
22

33
// Store a reference to the original getAttribute function
44
const originalGetAttribute = Element.prototype.getAttribute;
@@ -41,10 +41,29 @@ function getAttribute(element, name) {
4141
return processOperators(element, value);
4242
}
4343

44+
/**
45+
* Asynchronously gets an attribute and processes its value for operators.
46+
* @param {Element} element - The element from which to get the attribute.
47+
* @param {string} name - The attribute name.
48+
* @returns {Promise<string|object>} - A promise that resolves to the processed attribute value.
49+
*/
50+
async function getAttributeAsync(element, name) {
51+
if (!(element instanceof Element)) {
52+
throw new Error("First argument must be an Element");
53+
}
54+
let value = originalGetAttribute.call(element, name);
55+
return await processOperatorsAsync(element, value);
56+
}
57+
4458
// Override the getAttribute method on Element prototype
4559
Element.prototype.getAttribute = function (name) {
4660
return getAttribute(this, name); // Use the custom getAttribute function
4761
};
4862

63+
// Add getAttributeAsync to the Element prototype
64+
Element.prototype.getAttributeAsync = function (name) {
65+
return getAttributeAsync(this, name);
66+
};
67+
4968
// Export the custom getAttribute function
50-
export { getAttribute };
69+
export { getAttribute, getAttributeAsync };

0 commit comments

Comments
 (0)