r8455 introduced good changes to Class.js that gave us useful instanceof results. This change also made is so that people using the Class function to extend from non-OL constructors were unwittingly modifying their prototypes (by giving them an initialize property whose value was undefined). By ensuring that the parent prototype has an initialize property already, we don't add anything unexpected. r=crschmidt (closes #1987)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@9051 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2009-03-15 01:31:11 +00:00
parent 8df8106a51
commit 49d6463f4e
4 changed files with 28 additions and 11 deletions

View File

@@ -40,15 +40,21 @@ OpenLayers.Class = function() {
if(typeof arguments[i] == "function") {
// make the class passed as the first argument the superclass
if(i == 0 && len > 1) {
// replace the initialize method with an empty function,
// because we do not want to create a real instance here
initialize = arguments[i].prototype.initialize;
arguments[i].prototype.initialize = function() {};
// the line below makes sure that the new class has a
// superclass
extended = new arguments[i];
// restore the original initialize method
arguments[i].prototype.initialize = initialize;
if(typeof initialize === "function") {
// replace the initialize method with an empty function,
// because we do not want to create a real instance here
arguments[i].prototype.initialize = function() {};
// the line below makes sure that the new class has a
// superclass
extended = new arguments[i];
// restore the original initialize method
arguments[i].prototype.initialize = initialize;
} else {
// if someone uses Class to mix objects, we don't want to
// mess with the prototype
extended = new arguments[i];
}
}
// get the prototype of the superclass
parent = arguments[i].prototype;

View File

@@ -558,6 +558,12 @@ OpenLayers.Format.GML = OpenLayers.Class(OpenLayers.Format.XML, {
this.regExes.trimSpace, "");
attributes[name] = value;
}
} else {
// If child has no childNodes (grandchildren),
// set an attribute with null value.
// e.g. <prefix:fieldname/> becomes
// {fieldname: null}
attributes[child.nodeName.split(":").pop()] = null;
}
}
}

View File

@@ -51,7 +51,7 @@
}
function test_Class_inheritance(t) {
t.plan(4);
t.plan(5);
var BaseClass = OpenLayers.Class({
prop: "base",
@@ -81,6 +81,10 @@
t.eq(child["CLASS_NAME"],
"ChildClass",
"new object is an instance of the child class");
var F = OpenLayers.Class(Object, {});
t.ok(!("initialize" in Object.prototype), "no messing with non OL prototypes");
}
function test_Class_multiple_inheritance(t) {

View File

@@ -153,11 +153,12 @@
t.eq(data[0].geometry.components.length, 2, "rings length correct");
}
function test_Format_GML_read_attributes(t) {
t.plan(2);
t.plan(3);
var parser = new OpenLayers.Format.GML();
data = parser.read(test_content[0]);
t.eq(data[0].attributes['NAME'], "WY", "Simple Attribute data is read correctly.");
t.eq(data[0].attributes['LONGNAME'], "Wyoming", "Attribute data is read from CDATA node correctly.");
t.ok(data[0].attributes['EMPTYATTR'] === null, "Attribute set to null for empty element.");
}
function test_Format_GML_read_envelope_geom(t) {
t.plan(13);
@@ -338,7 +339,7 @@
t.xml_eq(output, expect, "[xy true] Bounds correctly written as gml:Box");
}
var test_content = ['<?xml version="1.0" encoding="utf-8" ?>\n<ogr:FeatureCollection\n xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n xsi:schemaLocation="http://ogr.maptools.org/ testoutput.xsd"\n xmlns:ogr="http://ogr.maptools.org/"\n xmlns:gml="http://www.opengis.net/gml">\n <gml:boundedBy>\n <gml:Box>\n <gml:coord><gml:X>-1254041.389711702</gml:X><gml:Y>250906.9515983529</gml:Y></gml:coord>\n <gml:coord><gml:X>-634517.1199908922</gml:X><gml:Y>762236.2940800377</gml:Y></gml:coord>\n </gml:Box>\n </gml:boundedBy> \n <gml:featureMember>\n <ogr:states fid="F0">\n <ogr:geometryProperty><gml:Polygon><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>-634517.11999089224,691849.77929356066,0 -653761.64509297756,471181.53429472551,0 -673343.60852865304,250906.9515983529,0 -1088825.734430399,299284.85108220269,0 -1254041.3897117018,324729.27754874947,0 -1235750.4212498858,434167.33911316615,0 -1190777.7803201093,704392.96327195223,0 -1181607.835811228,762236.29408003774,0 -634517.11999089224,691849.77929356066,0</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>\n <ogr:NAME>WY</ogr:NAME>\n <ogr:LONGNAME><![CDATA[Wyoming]]></ogr:LONGNAME>\n </ogr:states>\n </gml:featureMember>\n</ogr:FeatureCollection>\n',
var test_content = ['<?xml version="1.0" encoding="utf-8" ?>\n<ogr:FeatureCollection\n xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n xsi:schemaLocation="http://ogr.maptools.org/ testoutput.xsd"\n xmlns:ogr="http://ogr.maptools.org/"\n xmlns:gml="http://www.opengis.net/gml">\n <gml:boundedBy>\n <gml:Box>\n <gml:coord><gml:X>-1254041.389711702</gml:X><gml:Y>250906.9515983529</gml:Y></gml:coord>\n <gml:coord><gml:X>-634517.1199908922</gml:X><gml:Y>762236.2940800377</gml:Y></gml:coord>\n </gml:Box>\n </gml:boundedBy> \n <gml:featureMember>\n <ogr:states fid="F0">\n <ogr:geometryProperty><gml:Polygon><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>-634517.11999089224,691849.77929356066,0 -653761.64509297756,471181.53429472551,0 -673343.60852865304,250906.9515983529,0 -1088825.734430399,299284.85108220269,0 -1254041.3897117018,324729.27754874947,0 -1235750.4212498858,434167.33911316615,0 -1190777.7803201093,704392.96327195223,0 -1181607.835811228,762236.29408003774,0 -634517.11999089224,691849.77929356066,0</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>\n <ogr:NAME>WY</ogr:NAME>\n <ogr:EMPTYATTR/><ogr:LONGNAME><![CDATA[Wyoming]]></ogr:LONGNAME>\n </ogr:states>\n </gml:featureMember>\n</ogr:FeatureCollection>\n',
'<wfs:FeatureCollection' +
' xmlns:fs="http://example.com/featureserver"' +
' xmlns:wfs="http://www.opengis.net/wfs"' +