Home › Forums › General XMetaL Discussion › How Does Selection.CanDelete Work? › Reply To: How Does Selection.CanDelete Work?
Reply to: How Does Selection.CanDelete Work?
August 5, 2011 at 6:09 pmThe determining factor is basically this: If an author could press the Delete key to remove something then this API should return true.
Confusion might be due to you wondering “why can the user delete something that is required?”
Required elements can be deleted in almost all cases and there is good reason for this. I'll try to explain why that is necessary in an XML editor (even one like XMetaL that has a “Rules Checking” feature, which most other XML editors do not have).
I think you can think of it like this:
If your document requires an element and that element is not yet present you must be able to insert it. It think this much is obvious. Note that just before insertion of the element the document is invalid (and the Validation Log will indicate this). If the product did not allow you to remove a required element you just inserted then Undo would not function and you might be stuck. In all cases it is possible to delete everything in a document up to an including the root element and then start over with a different root element (that might be the most relevant key point).
I will try to explain with some examples.
Example 1:
DTD:
In this case there are two possible minimally valid documents:
DocA:
DocB:
If an author starts with DocB (having created it themselves or from a template or opened from elsewhere) and then wishes to create DocA, XMetaL allows them to remove the
Example 2:
DTD:
In this case
So, say you start with the following:
But you actually wanted this:
You need to be able to remove the required element
In other words, your actual interaction with XMetaL might be this:
1. Start with
The document is invalid at this point and pressing F9 (or attempting a save) will indicate that in the Validation Log.
2. Insert
3. Oops, you didn't want to insert
The document is invalid at this point and pressing F9 (or attempting a save) will indicate that in the Validation Log.
4. Insert
The document is valid at this point.
Example 3:
DTD:
In this case these are both valid options:
DocA:
DocB:
In both cases XMetaL allows you to remove all children of
However, there is also a special case. With DocA XMetaL will not allow you to remove
1. If the doc is empty the element list shows all elements (because any are valid).
2. If the doc is
3. If the doc is
4. If the doc is
5. If the doc is
6. If the doc is
More APIs:
If the standard “Rules Checking” functionality does not guide your authors enough you can help them further by providing document templates (Template subfolder in the XMetaL install path) and templates for individual elements (in the CTM file). For restricting what they can remove you can use additional APIs and your own scripting logic. APIs related to this functionality include (all are on the Selection object):
ReadOnly
ReadOnlyContainer
WritePermittedContainer
NonRemovableContainer
CanDelete takes the properties for any elements affected by those APIs into account as well. You do need to be careful if you try to stop people from removing things using these APIs (for all the reasons described above at least), so you will need to be very aware of your document model and possibly your authors' desired workflows as well.