Funny, you seem pretty clear on it. Yes, if you have recognized sovereignty over an area and are repelling a recognized invader, you will be less likely to be condemned than would be an aggressive invader
But it isn't clear, other than in recognition, that the cases are so different. ISIS draws its support from the northern Sunni population in Iraq. The fact that most Iraqi or Syrian Sunnis in its territory don't support them isn't relevant- most Gazans do not like or support Hamas (and please don't try and claim that Hamas is a 'political party').
Also, I think it's a lot worse to completely level a city while defending yourself than destroying a small portion of it while invading. But just my opinion here.