Browse Source

feat: treat "checked" attribute as boolean

Ahmad Kholid 3 years ago
parent
commit
2f56fd46f6
1 changed files with 13 additions and 3 deletions
  1. 13 3
      src/content/blocks-handler.js

+ 13 - 3
src/content/blocks-handler.js

@@ -175,12 +175,22 @@ export function elementScroll(block) {
 
 export function attributeValue(block) {
   return new Promise((resolve) => {
-    const result = [];
+    let result = [];
+    const { attributeName, multiple } = block.data;
+    const isCheckboxOrRadio = (element) => {
+      if (element.tagName !== 'INPUT') return false;
+
+      return ['checkbox', 'radio'].includes(element.getAttribute('type'));
+    };
 
     handleElement(block, (element) => {
-      const value = element.getAttribute(block.data.attributeName);
+      const value =
+        attributeName === 'checked' && isCheckboxOrRadio(element)
+          ? element.checked
+          : element.getAttribute(attributeName);
 
-      result.push(value);
+      if (multiple) result.push(value);
+      else result = value;
     });
 
     resolve(result);