Permalink
Browse files

Bugfixes, HTML5 form validation

  • Loading branch information...
1 parent ae68f15 commit 8d7fc04cbcd9375a3e82b164afb850390f2dabc7 @netzhuffle committed Feb 20, 2012
Showing with 271 additions and 163 deletions.
  1. +2 −0 .gitignore
  2. +16 −9 src/Term.js
  3. +31 −15 src/TermElement.js
  4. +5 −5 src/index.html
  5. +199 −128 src/mootools.js
  6. +16 −4 src/script.js
  7. +2 −2 src/style.css
View
@@ -0,0 +1,2 @@
+.project
+.settings/*
View
@@ -3,30 +3,29 @@ var Term = new Class({
this._input = this._parse(input);
},
- isError: function() {
- return this._input == null;
+ isValid: function() {
+ return typeOf(this._input) != "null";
},
getHtml: function() {
- if (this.isError()) {
+ if (!this.isValid()) {
return;
}
return "(html)";
},
calculate: function() {
- if (this.isError()) {
+ if (!this.isValid()) {
return;
}
return this._input.calculate(36); // result in base 36
},
_parse: function(input) {
- input = input.toLowerCase();
-
- if (!input.test(/^[-+ 0-9a-z]*$/)) {
+ input = input || "0";
+ if (!input.test(new RegExp("^" + Term.pattern + "$"))) {
return;
}
@@ -56,10 +55,18 @@ var Term = new Class({
element = "";
}
}
- if (hierarchy.length && element) {
- hierarchy[hierarchy.length-1].setSecond(element);
+ if (element) {
+ if (hierarchy.length) {
+ hierarchy[hierarchy.length-1].setSecond(element);
+ } else {
+ return new TermElement(element);
+ }
}
return hierarchy[hierarchy.length-1];
}
+});
+
+Term.extend({
+ pattern: "[-+ 0-9A-Za-z]*"
});
View
@@ -32,7 +32,7 @@ var TermElement = new Class({
},
getOperator: function() {
- return this;
+ return this._operator;
},
setOperator: function(operator) {
this._operator = operator;
@@ -41,44 +41,57 @@ var TermElement = new Class({
},
calculate: function(base) {
- if (!this._first || !this._second) {
+ base = base || 10;
+ var result = this.calculateBigInt();
+ if(!result) {
+ return;
+ }
+
+ return (result && result.negative ? "-" : "") + bigInt2str(result, base);
+ },
+
+ calculateBigInt: function() {
+ if (!this._first || this._operator && !this._second || this._second && !this._operator) {
return;
}
var first = this._first;
- if (first.calculate) {
- first = first.calculate();
- if (!first) {
+ if (typeOf(first.calculateBigInt) == "function") {
+ first = first.calculateBigInt();
+ if(!first) {
return;
}
}
var second = this._second;
- if (second.calculate) {
- second = second.calculate();
- if (!second) {
+ if (second && typeOf(second.calculateBigInt) == "function") {
+ second = second.calculateBigInt();
+ if(!second) {
return;
}
}
var result;
- if (this._operator == "+") {
+ if (!this._operator) {
+ result = this._first;
+ } else if (this._operator == "+") {
result = this._add(first, second);
} else if (this._operator == "-") {
result = this._substract(first, second);
+ } else {
+ return;
}
if (isZero(result)) {
result.negative = false;
}
- if (base) {
- result = (result.negative ? "-" : "") + bigInt2str(result, base);
- }
-
return result;
},
_parse: function(element) {
- if(typeOf(element) == "string") {
+ // bigInt = array
+ if(typeOf(element) == "array" || typeOf(element) == "object" && typeOf(element.calculateBigInt) == "function") {
+ return element;
+ } else if(typeOf(element) == "string") {
// check base
var base = 36;
if (element.length >= 2 && element.charAt(0) == 0 && this.bases[element.charAt(1)]) {
@@ -87,9 +100,12 @@ var TermElement = new Class({
}
// create bigint
element = str2bigInt(element, base);
+
+ return element;
+ } else {
+ return;
}
- return element;
},
_add: function(first, second) {
View
@@ -2,19 +2,19 @@
<title>Ari-TR</title>
<meta charset="utf-8">
-<link type="stylesheet" href="style.css">
+<link rel="stylesheet" href="style.css">
<p>
- 0m?? = Muggelzahl (z.B. 0m10 = A), 0x?? Hexadezimal, 0o?? Oktal, 0b?? Binär.
+ 0m?? = Muggelzahl (z.B. 0m10 = A), 0x?? Hexadezimal, 0o?? Oktal, 0b?? Binär, ?? Zauberzahl.
</p>
<form id="form" action="" method="post">
<p>
- <input id="input" name="input" size="100">
+ <input id="input" name="input" title="Die Rechnung darf Basen-Angaben (0m, 0x, 0o, 0b), Zauberziffern (0-9, a-z, A-Z) sowie Operatoren (+, -) enthalten. Leerzeichen ( ) sind ebenfalls erlaubt und werden ignoriert." size="100">
</p>
- <p id="styledinput"></p>
+ <p id="styledinput">0</p>
<p>
=
- <output id="output" name="output"></output>
+ <output id="output" for="input" name="output">0</output>
</p>
</form>
Oops, something went wrong.

0 comments on commit 8d7fc04

Please sign in to comment.