Changeset 8206 in josm for trunk


Ignore:
Timestamp:
2015-04-17T23:06:34+02:00 (5 years ago)
Author:
simon04
Message:

fix #10299 - MapCSS index for last element of object

Negative index numbers count from last to first, so >[index=-1] matches the last one.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/Environment.java

    r7509 r8206  
    3838     */
    3939    public Integer index = null;
     40
     41    /**
     42     * count of nodes in parent way or members in parent relation. Must be != null in LINK context.
     43     */
     44    public Integer count = null;
    4045
    4146    /**
     
    6974        this.source = other.source;
    7075        this.index = other.index;
     76        this.count = other.count;
    7177        this.context = other.getContext();
    7278    }
     
    99105     * @param parent the matching parent object
    100106     * @param index index of node in parent way or member in parent relation
     107     * @param count count of nodes in parent way or members in parent relation
    101108     * @return A clone of this environment, with the specified parent, index, and context set to {@link Context#LINK}
    102109     * @since 6175
     
    104111     * @see #index
    105112     */
    106     public Environment withParentAndIndexAndLinkContext(OsmPrimitive parent, int index) {
     113    public Environment withParentAndIndexAndLinkContext(OsmPrimitive parent, int index, int count) {
    107114        Environment e = new Environment(this);
    108115        e.parent = parent;
    109116        e.index = index;
     117        e.count = count;
    110118        e.context = Context.LINK;
    111119        return e;
     
    128136     * @param child the matching child object
    129137     * @param index index of node in parent way or member in parent relation
     138     * @param count count of nodes in parent way or members in parent relation
    130139     * @return A clone of this environment, with the specified child, index, and context set to {@code Context#LINK}
    131140     * @since 6175
     
    133142     * @see #index
    134143     */
    135     public Environment withChildAndIndexAndLinkContext(OsmPrimitive child, int index) {
     144    public Environment withChildAndIndexAndLinkContext(OsmPrimitive child, int index, int count) {
    136145        Environment e = new Environment(this);
    137146        e.child = child;
    138147        e.index = index;
     148        e.count = count;
    139149        e.context = Context.LINK;
    140150        return e;
     
    144154     * Creates a clone of this environment, with the specified index.
    145155     * @param index index of node in parent way or member in parent relation
     156     * @param count count of nodes in parent way or members in parent relation
    146157     * @return A clone of this environment, with the specified index
    147158     * @see #index
    148159     */
    149     public Environment withIndex(int index) {
     160    public Environment withIndex(int index, int count) {
    150161        Environment e = new Environment(this);
    151162        e.index = index;
     163        e.count = count;
    152164        return e;
    153165    }
     
    214226        child = null;
    215227        index = null;
     228        count = null;
    216229    }
    217230
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Condition.java

    r7447 r8206  
    271271        public boolean applies(Environment env) {
    272272            if (env.index == null) return false;
    273             return op.eval(Integer.toString(env.index + 1), index);
     273            if (index.startsWith("-")) {
     274                return env.count != null && op.eval(Integer.toString(env.index - env.count), index);
     275            } else {
     276                return op.eval(Integer.toString(env.index + 1), index);
     277            }
    274278        }
    275279    }
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Selector.java

    r8086 r8206  
    150150                    Node n = w.getNode(i);
    151151                    if (n.equals(e.osm)) {
    152                         if (link.matches(e.withParentAndIndexAndLinkContext(w, i))) {
     152                        if (link.matches(e.withParentAndIndexAndLinkContext(w, i, w.getNodesCount()))) {
    153153                            e.parent = w;
    154154                            e.index = i;
     155                            e.count = w.getNodesCount();
    155156                            return;
    156157                        }
     
    172173                    RelationMember m = r.getMember(i);
    173174                    if (m.getMember().equals(e.osm)) {
    174                         if (link.matches(e.withParentAndIndexAndLinkContext(r, i))) {
     175                        if (link.matches(e.withParentAndIndexAndLinkContext(r, i, r.getMembersCount()))) {
    175176                            e.parent = r;
    176177                            e.index = i;
     178                            e.count = r.getMembersCount();
    177179                            return;
    178180                        }
     
    329331                                e.child = n;
    330332                                e.index = i;
     333                                e.count = w.getNodesCount();
    331334                                e.parent = w;
    332335                                return true;
     
    346349                        Node n = wayNodes.get(i);
    347350                        if (left.matches(e.withPrimitive(n))) {
    348                             if (link.matches(e.withChildAndIndexAndLinkContext(n, i))) {
     351                            if (link.matches(e.withChildAndIndexAndLinkContext(n, i, wayNodes.size()))) {
     352                                e = e.withChildAndIndexAndLinkContext(n, i, wayNodes.size());
    349353                                e.child = n;
    350354                                e.index = i;
     355                                e.count = wayNodes.size();
    351356                                return true;
    352357                            }
     
    359364                        OsmPrimitive member = members.get(i).getMember();
    360365                        if (left.matches(e.withPrimitive(member))) {
    361                             if (link.matches(e.withChildAndIndexAndLinkContext(member, i))) {
     366                            if (link.matches(e.withChildAndIndexAndLinkContext(member, i, members.size()))) {
    362367                                e.child = member;
    363368                                e.index = i;
     369                                e.count = members.size();
    364370                                return true;
    365371                            }
  • trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/KeyConditionTest.groovy

    r7081 r8206  
    7777        r.addMember(new RelationMember("my_role", n))
    7878
    79         Environment e = new Environment().withPrimitive(n).withParent(r).withIndex(0).withLinkContext()
     79        Environment e = new Environment().withPrimitive(n).withParent(r).withIndex(0, r.membersCount).withLinkContext()
    8080
    8181        Condition cond = Condition.createKeyCondition("my_role", false, null, Context.LINK)
     
    9292        r.addMember(new RelationMember("my_role", n))
    9393
    94         Environment e = new Environment().withPrimitive(n).withParent(r).withIndex(0).withLinkContext()
     94        Environment e = new Environment().withPrimitive(n).withParent(r).withIndex(0, r.membersCount).withLinkContext()
    9595
    9696        Condition cond = Condition.createKeyCondition("another_role", false, null, Context.LINK)
  • trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/KeyValueConditionTest.groovy

    r7081 r8206  
    6363        r.addMember(new RelationMember("my_role", n))
    6464
    65         Environment e = new Environment().withPrimitive(n).withParent(r).withLinkContext().withIndex(0)
     65        Environment e = new Environment().withPrimitive(n).withParent(r).withLinkContext().withIndex(0, r.membersCount)
    6666
    6767        Condition cond = new Condition.RoleCondition("my_role", Op.EQ)
     
    7878        r.addMember(new RelationMember("my_role", n))
    7979
    80         Environment e = new Environment().withPrimitive(n).withParent(r).withIndex(0).withLinkContext()
     80        Environment e = new Environment().withPrimitive(n).withParent(r).withIndex(0, r.membersCount).withLinkContext()
    8181
    8282        Condition cond = Condition.createKeyValueCondition("role", "my_role", Op.NEQ, Context.LINK, false)
Note: See TracChangeset for help on using the changeset viewer.