Home Forums General XMetaL Discussion How Do I Detect if a Node is Still in the Document? Reply To: How Do I Detect if a Node is Still in the Document?

Derek Read

Reply to: How Do I Detect if a Node is Still in the Document?

You don't have to use the key-savvy APIs, but the key allows you to track different changes.

Here's something that might help you understand the feature. The script example for ChangedNodesbyKey in the Programmers Guide doesn't mention the first API I use here (but you need it unless you are using just ChangedNodes instead of ChangedNodesByKey).

1. Open a document.

2. Run the following script. This creates a key:
[code]//XMetaL Script Language JScript:

3. Run the following script. You should be told that no changes have been made.
[code]//XMetaL Script Language JScript:
var mykey = “key1”;
if (ActiveDocument.ViewType > 1) {
var nds = ActiveDocument.ChangedNodesbyKey(mykey);
var l = nds.length;
var changenode = null;
var name = null;
var f = 0;

if (l==0){
ActiveDocument.Host.Alert(“No nodes have been changed since the document was opened or the last time nodes were cleared.”);
else {
for (var i=0; i<=l;i++){
name = changenode.nodeName;
+ “nnNewNode:tt” + ActiveDocument.GetNodeStatebyKey(mykey, “NewNode”, changenode)
+ “nContentInserted:tt” + ActiveDocument.GetNodeStatebyKey(mykey, “ContentInserted”, changenode)
+ “nContentDeleted:tt” + ActiveDocument.GetNodeStatebyKey(mykey, “ContentDeleted”, changenode)
+ “nAttributesChanged:tt” + ActiveDocument.GetNodeStatebyKey(mykey, “AttributesChanged”, changenode));
if (f==nds.length){

4. Make one of the following changes to the document: Type some text, insert an element, delete an element, change an attribute.

5. Run the script from step 3 again. This time it will report details on what changed.

6. Repeat steps 4 and 5 until you figure out how this stuff works (it can be complex).

7. Add other macros that make (separate) calls to the following and test various combinations:
  a) Application.RemoveChangedNodeKey(mykey);
  b) ActiveDocument.ClearAllChangedStatesbyKey(mykey);
  c) ActiveDocument.ClearNodeChangedStates(mykey, changenode, true);
  d) ActiveDocument.ClearNodeChangedStatesbyKey(mykey,changenode,false);

If you don't want to deal with keys (I would recommend that unless there is a real need to keep track of separate types of changes for some reason) then you can just the non-key version of the same APIs. In this case you don't need step 2:

[code]// XMetaL Script Language JScript:
if (ActiveDocument.ViewType > 1) {
var chgdNodes = ActiveDocument.ChangedNodes;
var msg = “Changed nodes: ” + chgdNodes.length;
for (i=0;i changenode=chgdNodes.item(i);
msg += “n—————————————-n”;
msg += changenode.nodeName;
msg += “nNewNode:tt” + ActiveDocument.GetNodeState(“NewNode”, changenode);
msg += “nContentInserted:tt” + ActiveDocument.GetNodeState(“ContentInserted”, changenode);
msg += “nContentDeleted:tt” + ActiveDocument.GetNodeState(“ContentDeleted”, changenode);
msg += “nAttributesChanged:tt” + ActiveDocument.GetNodeState(“AttributesChanged”, changenode);

Step 7 will be different as well as you will need to use the non-key version of those APIs.