One of the greatest ironies of President Donald Trump’s headline-sucking tweets attacking a cable TV news host is that on the very same day, Congress was voting on a bill that could give the president a powerful weapon in his ongoing battle with sanctuary cities.

It’s a battle he is losing right now. A federal court in California recently ruled that the only leverage Trump has against sanctuary cities — attempting to withhold money and hope the communities cave — is unconstitutional.

On Thursday, about eight hours after Republicans roundly denounced Trump for reducing a media personality to a facelift, House Republicans tried to fix that legal wrinkle for the president.

They voted to make clear his administration has the power to withhold certain law enforcement grants as punishment for sanctuary cities protecting certain people who are in the country illegally.

“It’s a simple principle that if you’re going to receive taxpayer dollars from the federal government to keep people safe that you’ve got to follow the law and keep them safe,” said Rep. Bob Goodlatte, R-Va., chairman of the House’s judiciary committee.

The House passed it almost entirely along party lines. If this bill passes the Republican Senate, Congress will have given the president a legal tool no president has had before to try to force sanctuary cities to hand over those in the country illegally.

Notice we still said “to try to.” Beyond withholding money and hoping the cities cave, the president doesn’t have much leverage to force these communities to hand over people in the country illegally. And a federal court just ruled that method unconstitutional (which is why Congress is stepping in).

Sanctuary cities have become the most high-profile method of resistance to the Trump administration. Dozens of cities and counties have said they won’t oblige every federal government request to turn over immigrants in the country illegally (though almost all make exceptions for those charged with violent crimes).

Most of these communities are run by Democrats, fully aware that cracking down on those in the country illegally is central to Trump’s identity as a politician. California is even considering becoming the first sanctuary state, which would be a giant middle finger to the Trump administration.

The Justice Department recently tried to withhold an estimated $1 billion worth of law enforcement grants from two sanctuary communities in California. But a U.S. District Court judge ruled in April that the federal government cannot legally withhold those federal grants without writing into the grant that this can be taken away for being a sanctuary city.

The bill the House of Representatives passed on Thursday attempts to specify that when it comes to these specific grants, the president can withhold them from a city specifically because it’s a sanctuary city.