From 4143d8ab2189430904af47a2d0e07efeec300b5d Mon Sep 17 00:00:00 2001 From: Johan Lindell Date: Tue, 27 Sep 2016 12:08:24 +0200 Subject: [PATCH 1/4] Rewrite of getOptionsFromElement --- src/help/getOptionsFromElement.js | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/help/getOptionsFromElement.js b/src/help/getOptionsFromElement.js index f24e187d..d1c8b84e 100644 --- a/src/help/getOptionsFromElement.js +++ b/src/help/getOptionsFromElement.js @@ -1,25 +1,22 @@ 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]; + + 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; From 89be5f81dc507215502c8d677603870d2ea5452b Mon Sep 17 00:00:00 2001 From: Johan Lindell Date: Wed, 28 Sep 2016 09:15:08 +0200 Subject: [PATCH 2/4] Now uses dash to convert camel case properties in HTML elements --- src/help/getOptionsFromElement.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/help/getOptionsFromElement.js b/src/help/getOptionsFromElement.js index d1c8b84e..403405f9 100644 --- a/src/help/getOptionsFromElement.js +++ b/src/help/getOptionsFromElement.js @@ -8,10 +8,13 @@ function getOptionsFromElement(element){ var name = attributes[i].name; var value = attributes[i].value; - var match = name.match(/(jsbarcode|data)-([A-Za-z0-9]+)/i); + 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; } } From 812459352380bc50f4b07f3f357a4cdb353414d0 Mon Sep 17 00:00:00 2001 From: Johan Lindell Date: Wed, 28 Sep 2016 09:23:17 +0200 Subject: [PATCH 3/4] Updated initTest --- test/browser/initTest.html | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) 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"> From 6393368004c9ff36506f3edd47ef724600d8c1ee Mon Sep 17 00:00:00 2001 From: Johan Lindell Date: Wed, 28 Sep 2016 14:05:27 +0200 Subject: [PATCH 4/4] Updated readme to reflect changes to html properties --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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"> ````