matches->mLastMatch = bestmatch;
if (lastmatch) {      hasassignment = lastmatch->mAssignments.GetAssignmentFor(mContentVar, &value); NS_ASSERTION(hasassignment, "no content assignment"); if (! hasassignment) return NS_ERROR_UNEXPECTED;  nsIContent* parent = VALUE_TO_ICONTENT(value);  PRInt32 membervar = lastmatch->mRule->GetMemberVariable();  hasassignment = lastmatch->mAssignments.GetAssignmentFor(membervar, &value); NS_ASSERTION(hasassignment, "no member assignment"); if (! hasassignment) return NS_ERROR_UNEXPECTED;  nsIRDFResource* member = VALUE_TO_IRDFRESOURCE(value);  RemoveMember(parent, member, PR_TRUE); }   hasassignment = bestmatch->mAssignments.GetAssignmentFor(mContentVar, &value);  NS_ASSERTION(hasassignment, "no content assignment"); if (! hasassignment) continue;  content = VALUE_TO_ICONTENT(value);       PRBool contentsGenerated = PR_TRUE; nsCOMPtr<nsIXULContent> xulcontent = do_QueryInterface(content); if (xulcontent) xulcontent->GetLazyState(nsIXULContent::eContainerContentsBuilt, contentsGenerated);  if (contentsGenerated) { nsCOMPtr<nsIContent> tmpl; bestmatch->mRule->GetContent(getter_AddRefs(tmpl));  BuildContentFromTemplate(tmpl, content, content, PR_TRUE, VALUE_TO_IRDFRESOURCE(key->mMemberValue), PR_TRUE, bestmatch, nsnull, nsnull);   matches->SetLastMatch(bestmatch); } else {   matches->Remove(bestmatch); }   SetContainerAttrs(content, bestmatch);
nsXULTemplateBuilder::FireNewlyMatchedRules(const nsClusterKeySet& aNewKeys){    // Iterate through newly added keys to determine which rules fired.    //    // XXXwaterson Unfortunately, this could also lead to retractions;    // e.g., (contaner ?a ^empty false) could become "unmatched". How    // to track those?    nsClusterKeySet::ConstIterator last = aNewKeys.Last();    for (nsClusterKeySet::ConstIterator key = aNewKeys.First(); key != last; ++key) {        nsConflictSet::MatchCluster* matches =            mConflictSet.GetMatchesForClusterKey(*key);        NS_ASSERTION(matches != nsnull, "no matched rules for new key");        if (! matches)            continue;        nsTemplateMatch* bestmatch =            mConflictSet.GetMatchWithHighestPriority(matches);        NS_ASSERTION(bestmatch != nsnull, "no matches in match set");        if (! bestmatch)            continue;        // If the new "bestmatch" is different from the last match,        // then we need to yank some content out and rebuild it.        const nsTemplateMatch* lastmatch = matches->mLastMatch;        if (bestmatch != lastmatch) {            ReplaceMatch(VALUE_TO_IRDFRESOURCE(key->mMemberValue), lastmatch, bestmatch);            // Remember the best match as the new "last" match            matches->mLastMatch = bestmatch;        }    }    return NS_OK;}