Doctors have been told they can withhold life-saving treatment from a terminally-ill nine-month-old baby if his condition deteriorates.
Ruth Winston-Jones wanted continued treatment for Luke
Luke Winston-Jones, who has heart and breathing problems, should not be resuscitated by mechanical ventilation, the High Court in London ruled.
But, after a last-minute concession by the hospitals, he will still have the chance to receive cardiac massage.
His mother, Ruth Winston-Jones, from Anglesey, called it a partial victory.
She said Luke had now been given "a fighting chance".
The judge, Dame Elizabeth Butler-Sloss, president of the High Court Family Division, made her ruling after a two-day hearing.
She had been asked by the Royal Liverpool Children's NHS Trust and North West Wales NHS Trust to declare what treatment was required and what should be withheld, in the absence of agreement between doctors and his mother.
She said she hoped both parties would now work together to ensure Luke's life would be peaceful.
"It is important that everyone in this case, both hospitals and particularly the mother and her family, should turn over a new leaf and move forward," she said.
"It is the duty of the mother for the sake of Luke to reduce areas of conflict to a minimum and listen to what is proposed by those who have a great deal of medical and nursing experience."
Luke has the rare Edwards Syndrome and has never left hospital, but could now be taken home for the first time.
Doctors had said that, although they would provide "bag and mask" resuscitation if Luke became acutely ill, it would not be in his best interests to administer more "aggressive" treatment.
But Ms Winston-Jones, 35, had not wanted such treatment to be ruled out in advance.
Luke in hospital with a doll sent by the Duchess of York
As she left court, she said: "Thank you to everyone for their belief in me."
Her solicitor added: "All Ruth wanted was for Luke to be given a fighting chance and today he has been given that."
In a joint statement, both hospital trusts said: "This is an extremely difficult case in which both trusts have considered very carefully the need to seek the court's ruling on the future treatment of Luke.
"The hospitals have a duty to place the patient's best interests first but they also do their utmost to recognize the wishes of the family wherever possible.
"Following today's ruling, staff at both hospitals will continue to provide the care and support that Luke needs to keep him comfortable, safe and in as little distress as possible.
"We would request that staff at the two hospitals are now given the space and time to do this."
Luke was born by emergency caesarean at a Bangor hospital, Ysbyty Gwynedd, on 30 January.
Due to his serious cardiac defects, including two holes in the heart, he was moved to Alder Hey children's hospital in Liverpool.
He has suffered cardio-respiratory arrests and required resuscitation.
Luke's condition severely affects most organs of the body and few babies survive beyond a year.