Bug 420882 - r=sayrer, a=schrep - do some validation of geo lat/long

This commit is contained in:
mkaply@us.ibm.com 2008-03-19 12:52:54 -07:00
Родитель 01474a2051
Коммит b911805c55
2 изменённых файлов: 132 добавлений и 7 удалений

Просмотреть файл

@ -524,7 +524,10 @@ var Microformats = {
result = Microformats.parser.HTMLGetter(node, parentnode);
break;
case "float":
result = parseFloat(Microformats.parser.textGetter(node, parentnode));
var asText = Microformats.parser.textGetter(node, parentnode);
if (!isNaN(asText)) {
result = parseFloat(asText);
}
break;
case "custom":
result = prop.customGetter(node, parentnode);
@ -1572,11 +1575,20 @@ function geo(node, validate) {
}
}
geo.prototype.toString = function() {
if (this.latitude && this.longitude) {
if (this.latitude != undefined) {
if (!isFinite(this.latitude) || (this.latitude > 360) || (this.latitude < -360)) {
return;
}
}
if (this.longitude != undefined) {
if (!isFinite(this.longitude) || (this.longitude > 360) || (this.longitude < -360)) {
return;
}
}
if ((this.latitude != undefined) && (this.longitude != undefined)) {
var s;
if ((this.node.localName.toLowerCase() != "abbr") && (this.node.localName.toLowerCase() != "html:abbr")) {
s = Microformats.parser.textGetter(this.node);
} else {
if ((this.node.localName.toLowerCase() == "abbr") || (this.node.localName.toLowerCase() == "html:abbr")) {
s = this.node.textContent;
}
@ -1626,7 +1638,9 @@ var geo_definition = {
if (value.match(';')) {
latlong = value.split(';');
if (latlong[0]) {
return parseFloat(latlong[0]);
if (!isNaN(latlong[0])) {
return parseFloat(latlong[0]);
}
}
}
}
@ -1641,11 +1655,32 @@ var geo_definition = {
if (value.match(';')) {
latlong = value.split(';');
if (latlong[1]) {
return parseFloat(latlong[1]);
if (!isNaN(latlong[1])) {
return parseFloat(latlong[1]);
}
}
}
}
}
},
validate: function(node) {
var latitude = Microformats.parser.getMicroformatProperty(node, "geo", "latitude");
var longitude = Microformats.parser.getMicroformatProperty(node, "geo", "longitude");
if (latitude != undefined) {
if (!isFinite(latitude) || (latitude > 360) || (latitude < -360)) {
throw("Invalid latitude");
}
} else {
throw("No latitude specified");
}
if (longitude != undefined) {
if (!isFinite(longitude) || (longitude > 360) || (longitude < -360)) {
throw("Invalid longitude");
}
} else {
throw("No longitude specified");
}
return true;
}
};

Просмотреть файл

@ -49,6 +49,27 @@
<abbr class="geo" id="02-geo-vevent-02" title="30.2622;-97.7399">Convention Center</abbr>
</span>
<h3>Illegal geos</h3>
<ul>
<li><span class="geo" id="ill_geo1"><span class="latitude">abc</span>,<span class="longitude">def</span></span></li>
<li><span class="geo" id="ill_geo2"><span class="latitude">12.s2</span>,<span class="longitude">1d.23</span></span></li>
<li><span class="geo" id="ill_geo3"><span class="latitude">999.99</span>,<span class="longitude">999</span></span></li>
<li><span class="geo" id="ill_geo4"><span class="latitude">-181</span>,<span class="longitude">-361</span></span></li>
<li><span class="geo" id="ill_geo5">abc;def</span></li>
<li><span class="geo" id="ill_geo6">12.s2;1d.23</span></li>
<li><span class="geo" id="ill_geo7">999.99;999</span></li>
<li><span class="geo" id="ill_geo8">-181;-361</span></li>
<li><span class="geo" id="ill_geo9">-18;;-31</span></li>
<li><ABBR title="+23.70000;+90.30000" class="extra_geo">Dhaka, Bangladesh</ABBR></li>
<li><span class="geo" id="zero_geo">0;0</span></li>
</ul>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
@ -101,8 +122,77 @@ function test_geo() {
Geo = new geo(document.getElementById("02-geo-vevent-02"));
is(Geo.toString(), "Convention Center", "02-geo-vevent-02");
try {
Geo = new geo(document.getElementById("ill_geo1"), true);
ok(0, "ill_geo1 - should have been caught as invalid geo");
} catch (ex) {
ok(1, "ill_geo1 - caught invalid geo");
}
try {
Geo = new geo(document.getElementById("ill_geo2"), true);
ok(0, "ill_geo2 - should have been caught as invalid geo");
} catch (ex) {
ok(1, "ill_geo2 - caught invalid geo");
}
try {
Geo = new geo(document.getElementById("ill_geo3"), true);
ok(0, "ill_geo3 - should have been caught as invalid geo");
} catch (ex) {
ok(1, "ill_geo3 - caught invalid geo");
}
try {
Geo = new geo(document.getElementById("ill_geo4"), true);
ok(0, "ill_geo4 - should have been caught as invalid geo");
} catch (ex) {
ok(1, "ill_geo4 - caught invalid geo");
}
try {
Geo = new geo(document.getElementById("ill_geo5"), true);
ok(0, "ill_geo5 - should have been caught as invalid geo");
} catch (ex) {
ok(1, "ill_geo5 - caught invalid geo");
}
try {
Geo = new geo(document.getElementById("ill_geo6"), true);
ok(0, "ill_geo6 - should have been caught as invalid geo");
} catch (ex) {
ok(1, "ill_geo6 - caught invalid geo");
}
try {
Geo = new geo(document.getElementById("ill_geo7"), true);
ok(0, "ill_geo7 - should have been caught as invalid geo");
} catch (ex) {
ok(1, "ill_geo7 - caught invalid geo");
}
try {
Geo = new geo(document.getElementById("ill_geo8"), true);
ok(0, "ill_geo8 - should have been caught as invalid geo");
} catch (ex) {
ok(1, "ill_geo8 - caught invalid geo");
}
try {
Geo = new geo(document.getElementById("ill_geo9"), true);
ok(0, "ill_geo9 - should have been caught as invalid geo");
} catch (ex) {
ok(1, "ill_geo9 - caught invalid geo");
}
try {
Geo = new geo(document.getElementById("zero_geo"), true);
ok(1, "zero_geo - creation succeeded");
} catch (ex) {
ok(0, "zero_geo - creation failed");
}
try {
Geo = new geo(document.getElementById("extra_geo"), true);
ok(1, "extra_geo - creation succeeded");
} catch (ex) {
ok(0, "extra_geo - creation failed");
}
}
</script>
</pre>
</body>