The two cases questioned a longstanding precedent that upheld such laws, which opponents say violate free speech rights.