Index: trunk/data_nodist/amenity-in-amenity.osm
===================================================================
--- trunk/data_nodist/amenity-in-amenity.osm	(revision 8833)
+++ trunk/data_nodist/amenity-in-amenity.osm	(revision 8833)
@@ -0,0 +1,38 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<osm version='0.6' upload='true' generator='JOSM'>
+  <node id='-3879' action='modify' visible='true' lat='-22.69072960433' lon='-48.5607904595' />
+  <node id='-3877' action='modify' visible='true' lat='-22.69078106744' lon='-48.56082153115' />
+  <node id='-3875' action='modify' visible='true' lat='-22.6907376271' lon='-48.56085907164' />
+  <node id='-3871' action='modify' visible='true' lat='-22.69070289438' lon='-48.56081834975' />
+  <node id='-3864' visible='true' lat='-22.69073023197' lon='-48.56082133585'>
+    <tag k='amenity' v='hospital' />
+  </node>
+  <node id='-3863' visible='true' lat='-22.69071631095' lon='-48.56081028236' />
+  <node id='-3862' visible='true' lat='-22.69074204865' lon='-48.56081115962' />
+  <node id='-3861' visible='true' lat='-22.69071809155' lon='-48.56082765213' />
+  <node id='-3860' visible='true' lat='-22.69074528609' lon='-48.56083782836' />
+  <way id='-3880' action='modify' visible='true'>
+    <nd ref='-3877' />
+    <nd ref='-3879' />
+    <nd ref='-3871' />
+  </way>
+  <way id='-3876' action='modify' visible='true'>
+    <nd ref='-3871' />
+    <nd ref='-3875' />
+    <nd ref='-3877' />
+  </way>
+  <way id='123' version='3' visible='true'>
+    <nd ref='-3861' />
+    <nd ref='-3860' />
+    <nd ref='-3862' />
+    <nd ref='-3863' />
+    <nd ref='-3861' />
+    <tag k='amenity' v='hospital' />
+  </way>
+  <relation id='123' version='3' action='modify' visible='true'>
+    <member type='way' ref='-3876' role='outer' />
+    <member type='way' ref='-3880' role='outer' />
+    <tag k='amenity' v='hospital' />
+    <tag k='type' v='multipolygon' />
+  </relation>
+</osm>
Index: trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Selector.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Selector.java	(revision 8832)
+++ trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Selector.java	(revision 8833)
@@ -256,5 +256,5 @@
             @Override
             public void visit(Way w) {
-                if (e.child == null && left.matches(new Environment(w))) {
+                if (e.child == null && left.matches(new Environment(w).withParent(e.osm))) {
                     if (e.osm instanceof Way && Geometry.PolygonIntersection.CROSSING.equals(
                             Geometry.polygonIntersection(w.getNodes(), ((Way) e.osm).getNodes()))) {
@@ -273,5 +273,5 @@
             @Override
             public void visit(Node n) {
-                if (e.child == null && left.matches(new Environment(n))) {
+                if (e.child == null && left.matches(new Environment(n).withParent(e.osm))) {
                     if (e.osm instanceof Way && Geometry.nodeInsidePolygon(n, ((Way) e.osm).getNodes())
                             || e.osm instanceof Relation && (
@@ -284,5 +284,5 @@
             @Override
             public void visit(Way w) {
-                if (e.child == null && left.matches(new Environment(w))) {
+                if (e.child == null && left.matches(new Environment(w).withParent(e.osm))) {
                     if (e.osm instanceof Way && Geometry.PolygonIntersection.FIRST_INSIDE_SECOND.equals(
                             Geometry.polygonIntersection(w.getNodes(), ((Way) e.osm).getNodes()))
Index: trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/ChildOrParentSelectorTest.groovy
===================================================================
--- trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/ChildOrParentSelectorTest.groovy	(revision 8832)
+++ trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/ChildOrParentSelectorTest.groovy	(revision 8833)
@@ -2,7 +2,7 @@
 package org.openstreetmap.josm.gui.mappaint.mapcss
 
-import org.openstreetmap.josm.gui.mappaint.MultiCascade;
-
-import static org.junit.Assert.*
+import org.openstreetmap.josm.data.osm.OsmPrimitiveType
+import org.openstreetmap.josm.gui.mappaint.MultiCascade
+import org.openstreetmap.josm.io.OsmReader
 
 import java.util.logging.Logger
@@ -182,3 +182,11 @@
         assert selector.matches(e)
     }
+
+    @Test
+    public void testContains() throws Exception {
+        def ds = OsmReader.parseDataSet(new FileInputStream("data_nodist/amenity-in-amenity.osm"), null)
+        def css = parse("node[tag(\"amenity\") = parent_tag(\"amenity\")] ∈ *[amenity] {}")
+        assert css.matches(new Environment(ds.getPrimitiveById(123, OsmPrimitiveType.WAY)))
+        assert css.matches(new Environment(ds.getPrimitiveById(123, OsmPrimitiveType.RELATION)))
+    }
 }
