Pages: 1
Print
Author Topic: How to loop through current selection  (Read 2184 times)
NandoC
Member

Posts: 4


« on: August 28, 2014, 08:01:12 AM »

Hi I just started doing some work with Xmetal 7.0 and I am wondering how i can loop through the current selection and insert an element to each of the nodes that have been selected.

example:
<car>ford</car><car>toyota</car> has been highlighted

And let's say we want to underline what was selected to

<car><u>ford</u></car><car><u>toyota</u></car>

Thanks
Logged
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 2621



WWW
« Reply #1 on: August 28, 2014, 11:32:54 AM »

Here's one possibility among many.

//XMetaL Script Language JScript:
var rng = ActiveDocument.Range;
var targetElem = "car";
var insertElem = "u";
while(rng.MoveToElement(targetElem)) {
   rng.SelectContainerContents();
   rng.Surround(insertElem);
}

Depending on your actual needs, the full content of your documents, and the starting position for the Selection, that code may either be a good or bad way to solve your issue. Without knowing the full details I cannot say. I suspect that you will find it needs to at least be extended to cover cases that you have not anticipated.
Logged
NandoC
Member

Posts: 4


« Reply #2 on: September 09, 2014, 03:05:27 PM »

Thanks Derek, that worked.
Logged
Pages: 1
Print
Jump to: