WASHINGTON — U.S. President Donald Trump on Wednesday signed into law legislation backing protesters in Hong Kong despite angry objections from Beijing, at a time when Trump is seeking a deal to end a damaging trade war.

The legislation, approved unanimously by the U.S. Senate and by all but one lawmaker in the House of Representatives last week, requires the State Department to certify, at least annually, that Hong Kong retains enough autonomy to justify favorable U.S. trading terms that have helped it maintain its position as a world financial center. The law also threatens sanctions for human rights violations.

Congress passed a second bill, which Trump also signed, banning the export to the Hong Kong police of crowd-control munitions, such as teargas, pepper spray, rubber bullets and stun guns.

"I signed these bills out of respect for President Xi, China, and the people of Hong Kong. They are being enacted in the hope that Leaders and Representatives of China and Hong Kong will be able to amicably settle their differences leading to long term peace and prosperity for all," Trump said in a statement.

At the heart of the matter is Beijing's promise to allow Hong Kong a "high degree of autonomy" for 50 years when it regained sovereignty over the city in 1997, a pledge that has formed the basis of the territory's special status under U.S. law. Protesters say freedoms have been steadily eroded.

Trump had been vague about whether he would sign or veto the legislation, while trying to strike a deal with China on trade that he has made a top priority ahead of his 2020 re-election bid.

China has denounced the legislation as gross interference in its affairs and a violation of international law.

"This is a severe interference in Hong Kong affairs, which are China’s internal affairs," the Ministry of Foreign Affairs said in a statement. "It is also in serious violation of international law and basic norms governing international relations. The Chinese government and the people firmly oppose such stark hegemonic acts."

Beijing warned that the United States would shoulder the consequences of China's counter measures if it continued to "act arbitrarily" in regards to Hong Kong, according to a foreign ministry statement.

Hong Kong's Beijing-backed government said the legislation sent the wrong signal to demonstrators and "clearly interfered" with the city's internal affairs.

After the Senate passed the legislation, Beijing vowed counter-measures to safeguard its sovereignty and security. Its Foreign Ministry said the United States must immediately stop interfering in Hong Kong and China's other internal affairs, or "the negative consequences will boomerang on itself."

If Trump had opted to use his veto, it could have been overridden by two-thirds votes in both the Senate and the House — easily attainable as measured by the votes in each chamber. The legislation would have automatically become law on Dec. 3 if Trump had opted to do nothing.

Republican Senator Marco Rubio applauded Trump's decision to sign the bill. "The U.S. now has new and meaningful tools to deter further influence and interference from Beijing into Hong Kong’s internal affairs," Rubio said in a statement.

TRADE TALKS

Last week, Trump boasted that he alone had prevented Beijing from crushing the demonstrations with a million soldiers, while adding that he had told Chinese President Xi Jinping that doing so would have "a tremendous negative impact" on trade talks.

Trump prompted questions about his commitment to protecting freedoms in Hong Kong when he referred in August to its mass street protests as "riots" that were a matter for China to deal with.

Trump again referred to "riots" last week, but has also called on China to handle the issue humanely, while warning repeatedly of the impact on trade talks.

Many see the U.S. legislation as symbolic, but the bills' provisions have the potential, if implemented, to upend relations between the United States and Hong Kong and change the territory's status to that of any other Chinese city.

Analysts say any move to end Hong Kong's special treatment could prove self-defeating for the United States, which has benefited from the business-friendly conditions in the territory. If Hong Kong becomes just another Chinese port, companies that rely on the territory's role as a middleman or for trans-shipping would likely take their business elsewhere.

That said, the bills contain strong waivers that would allow the president to block their provisions on national-security and national-interest grounds.

According to the State Department, 85,000 U.S. citizens lived in Hong Kong in 2018 and more than 1,300 U.S. companies operate there, including nearly every major U.S. financial firm.

The territory is a major destination for U.S. legal and accounting services. In 2018 the largest U.S. bilateral trade-in-goods surplus was with Hong Kong at $31.1 billion.

Trade between Hong Kong and the United States was estimated to be worth $67.3 billion in 2018, with the United States running a $33.8 billion surplus — its biggest with any country or territory, according to the Office of the U.S. Trade Representative.