SHANGHAI - Roger Federer survived a huge scare Thursday to reach the Shanghai Masters quarter-finals and guarantee a record 300th week as world number one as Novak Djokovic and Andy Murray showed ominous form.The Swiss 17-time Grand Slam champion came into the match against compatriot Stanislas Wawrinka with an enviable 11-1 head-to-head record but struggled to impose himself and was forced to dig deep before winning 4-6, 7-6 (7/4), 6-0.In a gripping contest lasting nearly two hours, Wawrinka missed a golden chance when he failed to make the most of a break point opportunity at 4-4 in the second set that, if taken, would have left him serving for the match."I consider myself a little lucky. I never stopped believing, pushed till the end, then got off to a good start in the third set. Basically that was it," said Federer, bidding to win a record 22nd Masters crown."It was a tough match. He was the better player for basically two sets. It's great finding a way out that way, that's for sure."Federer, surrounded this week by black-clad security heavies after a bizarre pre-tournament death threat, said reaching the jaw-dropping 300-week mark, which he will hit when the new rankings are out on Monday, was an "incredible milestone"."So here I am at 300 weeks. It's pretty incredible. Probably one of my biggest accomplishments. I'm very proud of that record, no doubt about it.""It's a great reward for me," added Federer, who returned to the number one spot following this year's Wimbledon triumph.The previous record was held by American Pete Sampras, who spent a total of 286 weeks as world number one.Wawrinka, who teamed up with his illustrious compatriot to win Olympics doubles gold in 2008, broke Federer in the seventh game of the match to lead 4-3 and sealed the first set under lights at Qi Zhong Stadium.Out-of-sorts Federer, 31, continued to struggle in the second set as the pair swapped breaks of serve and was in desperate trouble when Wawrinka earned the break point that, if taken, would have given him a 5-4 lead over his Davis Cup team-mate.But the Swiss great dodged that bullet and stayed strong in a nerve-jangling tie-break to level the match, forcing a change of momentum and crushing his dejected 27-year-old opponent 6-0 in the decider.Earlier, Serbian second seed Djokovic, looking to wrestle the world number one ranking from Federer, barely broke sweat against Spain's Feliciano Lopez, wrapping up a straightforward 6-3, 6-3 victory in front of enthusiastic fans.In-form Djokovic, fresh from his third China Open title in Beijing on Sunday, struck 12 aces and took advantage of some below-par serving by the left-handed Lopez, breaking him three times."I'm not very well known around the Tour for big serving," said Djokovic, 25. "But so far in this tournament, and also in Beijing, it has been working very well for me."Defending champion Murray, 25, proved far too strong for Ukraine's Alexandr Dolgopolov, serving consistently well and seizing on five out of six break point opportunities to win 6-2, 6-2 in just 57 minutes.It was the first match of the tournament for the Scot, who had a walkover into the third round. The top eight seeds received a first round bye. Fifth seed Jo-Wilfried Tsonga beat Marcos Baghdatis of Cyprus 6-2, 7-6 (7/2) and will next play Czech fourth seed Tomas Berdych, who fended off a barrage of 17 aces from American Sam Querrey to win 6-2, 6-7 (3/7), 6-4.Radek Stepanek of the Czech Republic upset eighth seed John Isner of the United States and will play Murray while 10th seed Marin Cilic of Croatia beat Spain's Fernando Verdasco to set up a clash with Federer.German veteran Tommy Haas, 34, who reached a career high of number two, beat Serbian sixth seed Janko Tipsarevic 6-2, 6-1 and will provide a tough test for Djokovic in the last eight.