Pages: 1
Print
Author Topic: Deleting an element  (Read 184 times)
C4
Member

Posts: 12


« on: October 23, 2017, 04:51:36 PM »

Hi,

I am using XMetaL Author Essential 11

I am trying to delete an element and it is a child of another element. I am using this code but when it finds the last instance of this element, it deletes the parent element as well. How do I stop this from happening?

<MACRO name="RemoveAutovalue" key="Ctrl+Shift+A" lang="JScript"><![CDATA[
var rng=ActiveDocument.Range;
rng.MoveToElement("autovalue",true);
rng.Select();
if (Selection.CanRemoveContainerTags) {
Selection.RemoveContainerTags();
}
]]></MACRO>

Thanks
Logged
mag3737
XMetaL Evangelist
Administrator
Member

Posts: 116

I even use XMetaL to write my business letters.


« Reply #1 on: October 23, 2017, 05:27:35 PM »

I think we might need a little more context here. For example:

- What is the parent of the autovalue element?
- Is there any other markup structure involved here?
- Where might the user's cursor/highlight be located when this operation is performed?

I tried out your code to remove the tags from <b> elements within paragraphs (changing "autovalue" to "b"), and the code worked fine if the cursor was placed anywhere before the first <b> and then executed repeatedly. Each execution deleted the next set of <b> tags it found. The <p> was never removed.
« Last Edit: October 23, 2017, 05:29:19 PM by mag3737 » Logged

Tom Magliery
JustSystems Canada, Inc.
mag3737
XMetaL Evangelist
Administrator
Member

Posts: 116

I even use XMetaL to write my business letters.


« Reply #2 on: October 23, 2017, 06:01:49 PM »

Ok, I think the answer is that before you do the RemoveContainerTags() you need to verify that after doing MoveToElement() your range/selection is actually located inside the element whose tags you want to remove. Otherwise, if it so happens that the tags of the resulting location can also be validly removed, they will be.

Try this:

Code:
var rng=ActiveDocument.Range;
rng.MoveToElement("autovalue", true);
rng.Select();
if ((Selection.CanRemoveContainerTags) && (Selection.ContainerName == "autovalue")) {
Selection.RemoveContainerTags();
}

As a side note, you might consider doing the rng.Select() after the tags are removed, instead of before. This should work just as well, with the bonus(?) effect that the cursor will not move unless some tags actually do get removed (i.e. if the code performs a no-op, the user's cursor will remain where it was):

Code:
var rng=ActiveDocument.Range;
rng.MoveToElement("autovalue", true);
if ((rng.CanRemoveContainerTags) && (rng.ContainerName == "autovalue")) {
rng.RemoveContainerTags();
rng.Select();
}

Logged

Tom Magliery
JustSystems Canada, Inc.
C4
Member

Posts: 12


« Reply #3 on: October 23, 2017, 06:32:02 PM »

Thanks - that works.
Logged
Pages: 1
Print
Jump to: