Skip to content

Commit

Permalink
Numeric value: Use lang-attribute of the input element itself or any …
Browse files Browse the repository at this point in the history
…of its parents.
  • Loading branch information
noniq committed Dec 21, 2015
1 parent 8bede8b commit c19bfeb
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## Version 1.1.0
## Numeric Value
* The rules for deciding if “german” parsing rules should be used have changed: Instead of only looking at the `lang` attribute of the `<html>` element, now the first `lang` attribute found on the input element itself or any of its parent elements is considered. (This means, that the `lang` attribute of `<html>` will only be used if there are no intermediate elements with different `lang` attributes.)

## Version 1.0.0
### Conditional Visibility
* Breaking: Option `skipAnimations` has been removed. To force re-evaluation of visibilities without animation (e.g. after the DOM has been modified), use `$(...).trigger("setVisibilities")` instead (this is already done automatically on `pageready`.)
Expand Down
6 changes: 6 additions & 0 deletions features/numeric_value.feature
Expand Up @@ -35,6 +35,12 @@ Feature: Numeric value of input elements
| € 5 | 0 |
| | 0 |

Scenario: Locale specified on the input element directly
Given the HTML element has a lang attribute set to "de"
And the element "textfield" has a lang attribute set to "en"
When I fill in "textfield" with "1,234.56"
Then the numeric value of "textfield" should be "1234.56"

Scenario: Reading value from non-input elements
Then the numeric value of "non_input_element_english" should be "1234.56"
Given the HTML element has a lang attribute set to "de"
Expand Down
4 changes: 4 additions & 0 deletions features/step_definitions/numeric_value_steps.rb
Expand Up @@ -2,6 +2,10 @@
page.execute_script("$('html').attr('lang', '#{lang}')")
end

Given %r{the element "([^"]*)" has a lang attribute set to "([^"]*)"$} do |id, lang|
page.execute_script("$('##{id}').attr('lang', '#{lang}')")
end

Then %r{^the numeric value of "([^"]*)" should be "([^"]*)"$} do |input, value|
page.evaluate_script("$('##{input}').numericValue()").should be_within(0.0001).of value.to_f
end
Expand Down
10 changes: 6 additions & 4 deletions lib/assets/javascripts/da-js/numeric_value.js.coffee
@@ -1,22 +1,24 @@
# Like val(), but return the value parsed as float (or 0, if the value is not numeric). Also works for non-input elements (using the element's text content as value).
#
# Thousands delimiters (",") are handled correctly.
# German thousands delimiters (".") and separators (",") are handled correctly if the lang attribute of the HTML element is set to "de".
# German thousands delimiters (".") and separators (",") are handled correctly if the lang attribute of the input element (or any of its parent elements) is set to "de".
#
# Options:
# ignoreCurrencySign: If true, values with leading currency signs (€, $) are considered numeric and parsed correctly.
# nullOnError: If true, null is returned (instead of 0) if the value is not numeric.

$.fn.numericValue = (options = {}) ->

delocalize = (val) ->
if $("html").attr("lang") == "de"
delocalize = (val, locale) ->
if locale == "de"
val.replace(".", "").replace(",", ".")
else
val.replace(",", "")

$this = $(this)
val = delocalize(if $this.is(":input") then $this.val() else $this.text())
locale = $this.closest("[lang]").attr("lang")
val = if $this.is(":input") then $this.val() else $this.text()
val = delocalize(val, locale)
val = val.replace(/^\s*[€$]\s*/, "") if options.ignoreCurrencySign
if $.isNumeric(val)
parseFloat(val)
Expand Down
2 changes: 1 addition & 1 deletion lib/da-js/version.rb
@@ -1,5 +1,5 @@
module Da
module Js
VERSION = "1.0.0"
VERSION = "1.1.0"
end
end

0 comments on commit c19bfeb

Please sign in to comment.