ElementAttribute.js [plain text]
function(element, attributeName) {
"use strict";
let lowercaseAttributeName = attributeName.toLowerCase();
let tagName = element.tagName.toUpperCase();
if (lowercaseAttributeName === "selected" || lowercaseAttributeName === "checked") {
switch (tagName) {
case "OPTION":
return element.selected ? "true" : null;
case "INPUT":
if (element.type === "checkbox" || element.type === "radio")
return element.checked ? "true" : null;
break;
}
}
function getAttribute(element, attributeName) {
let attr = element.getAttributeNode(attributeName);
return (attr && attr.specified) ? attr.value : null;
}
if ((tagName === "IMG" && lowercaseAttributeName === "src") || (tagName === "A" && lowercaseAttributeName === "href")) {
let value = getAttribute(element, lowercaseAttributeName);
return value ? element[lowercaseAttributeName] : value;
}
function isBooleanAttribute(element, attributeName) {
const booleanElements = {
audio: ["autoplay", "controls", "loop", "muted"],
button: ["autofocus", "disabled", "formnovalidate"],
details: ["open"],
dialog: ["open"],
fieldset: ["disabled"],
form: ["novalidate"],
iframe: ["allowfullscreen"],
img: ["ismap"],
input: [
"autofocus",
"checked",
"disabled",
"formnovalidate",
"multiple",
"readonly",
"required",
],
keygen: ["autofocus", "disabled"],
menuitem: ["checked", "default", "disabled"],
ol: ["reversed"],
optgroup: ["disabled"],
option: ["disabled", "selected"],
script: ["async", "defer"],
select: ["autofocus", "disabled", "multiple", "required"],
textarea: ["autofocus", "disabled", "readonly", "required"],
track: ["default"],
video: ["autoplay", "controls", "loop", "muted"],
};
if (attributeName == "hidden" || attributeName == "itemscope")
return true;
if (!booleanElements.hasOwnProperty(element.localName))
return false;
return booleanElements[element.localName].includes(attributeName);
}
const propertyNameAliases = {"class": "className", "readonly": "readOnly"};
let propertyName = propertyNameAliases[attributeName] || attributeName;
if (isBooleanAttribute(element, lowercaseAttributeName)) {
let value = !(getAttribute(element, attributeName) === null) || element[propertyName];
return value ? 'true' : null;
}
let property;
try {
property = element[propertyName];
} catch (e) { }
if (property == null || typeof property == "object") {
let value = getAttribute(element, attributeName);
return value != null ? value.toString() : null;
}
return property != null ? property.toString() : null;
}