diff --git a/README.md b/README.md index aeb91a51..3683ecae 100644 --- a/README.md +++ b/README.md @@ -97,8 +97,8 @@ Use any `jsbarcode-*` or `data-*` as attributes where `*` is any option. + jsbarcode-text-margin="0" + jsbarcode-font-options="bold"> ```` diff --git a/src/help/getOptionsFromElement.js b/src/help/getOptionsFromElement.js index f24e187d..403405f9 100644 --- a/src/help/getOptionsFromElement.js +++ b/src/help/getOptionsFromElement.js @@ -1,25 +1,25 @@ import optionsFromStrings from "./optionsFromStrings.js"; -import defaults from "../options/defaults.js"; function getOptionsFromElement(element){ var options = {}; - for(var property in defaults){ - if(defaults.hasOwnProperty(property)){ - // jsbarcode-* - if(element.hasAttribute("jsbarcode-" + property.toLowerCase())){ - options[property] = element.getAttribute("jsbarcode-" + property.toLowerCase()); - } - - // data-* - if(element.hasAttribute("data-" + property.toLowerCase())){ - options[property] = element.getAttribute("data-" + property.toLowerCase()); - } + + var attributes = element.attributes; + for(var i = 0; i < attributes.length; i++){ + var name = attributes[i].name; + var value = attributes[i].value; + + var match = name.match(/(jsbarcode|data)-([A-Za-z0-9-]+)/i); + if(match){ + var property = match[2]; + + // Transforms foo-bar to fooBar + property = property.replace(/-[a-zA-Z]/, (val) => val[1].toUpperCase()); + + options[property] = value; } } - options["value"] = element.getAttribute("jsbarcode-value") || element.getAttribute("data-value"); - -// Since all atributes are string they need to be converted to integers + // Since all atributes are string they need to be converted to integers options = optionsFromStrings(options); return options; diff --git a/test/browser/initTest.html b/test/browser/initTest.html index 13d007da..fd5b51d5 100644 --- a/test/browser/initTest.html +++ b/test/browser/initTest.html @@ -24,6 +24,8 @@ JsBarcode("#barcode1").init(); $("#barcode2").JsBarcode().init(); JsBarcode("#barcode3").init(); + + JsBarcode(".barcode4").init(); // Should not give errors } @@ -31,8 +33,9 @@ + jsbarcode-text-margin="0" + jsbarcode-flat="true" + jsbarcode-line-color="#e00"> @@ -48,8 +51,8 @@ + jsbarcode-text-margin="0" + jsbarcode-line-color="#e00"> @@ -65,8 +68,8 @@ + jsbarcode-text-margin="0" + jsbarcode-line-color="#e00">