diff --git a/lib/OpenLayers/Format/KML.js b/lib/OpenLayers/Format/KML.js
index b98d18f912..e8ccba5060 100644
--- a/lib/OpenLayers/Format/KML.js
+++ b/lib/OpenLayers/Format/KML.js
@@ -885,12 +885,18 @@ OpenLayers.Format.KML = OpenLayers.Class(OpenLayers.Format.XML, {
child = children[i];
if(child.nodeType == 1) {
grandchildren = child.childNodes;
- if(grandchildren.length == 1 || grandchildren.length == 3) {
+ if(grandchildren.length >= 1 || grandchildren.length <= 3) {
var grandchild;
switch (grandchildren.length) {
case 1:
grandchild = grandchildren[0];
break;
+ case 2:
+ var c1 = grandchildren[0];
+ var c2 = grandchildren[1];
+ grandchild = c1.nodeType == 3 || c1.nodeType == 4 ?
+ c1 : c2;
+ break;
case 3:
default:
grandchild = grandchildren[1];
diff --git a/tests/Format/KML.html b/tests/Format/KML.html
index c09724b3f5..8f294e4202 100644
--- a/tests/Format/KML.html
+++ b/tests/Format/KML.html
@@ -37,7 +37,7 @@
function test_Format_KML_readCdataAttributes_20(t) {
t.plan(2);
- var cdata = '#rel1.0 17.266666, 48.283333';
+ var cdata = ' #rel1.0 17.266666, 48.283333';
var features = (new OpenLayers.Format.KML()).read(cdata);
t.eq(features[0].attributes.description, "Full of text.", "Description attribute in cdata read correctly");
t.eq(features[0].attributes.name, "Pezinok", "title attribute in cdata read correctly");