Home Forums General XMetaL Discussion Inserting element example b(bold) i(Italic) in run time Reply To: Inserting element example b(bold) i(Italic) in run time

Derek Read

Reply to: Inserting element example b(bold) i(Italic) in run time

You might use one of the following APIs:
  Selection.ToggleInline()
  Selection.InsertElement()
  Range.InsertElement()
  DOMNode_object.insertBefore()

Here are a few simple examples written in JScript:

[code]//XMetaL Script Language JScript:
//If selection is not inside “bold” wrap it in “bold” if allowed by DTD.
//If selection is inside “bold” and selection is entire element content then remove the containing element
//or split it (depending on selection and DTD).
Selection.ToggleInline(“bold”);[/code]

[code]//XMetaL Script Language JScript:
//Insert the element named “bold” at the current selection.
//If not an insertion point the current selection will be replaced, if legal according to the DTD.
Selection.InsertElement(“bold”);[/code]

[code]//XMetaL Script Language JScript:
//Insert the element named “bold” after the current selection.
//Range is often preferable over Selection as manipulating a Range does not usually move the user's Selection.
var rng = ActiveDocument.Range;
rng.Collapse(sqCollapseEnd);
rng.InsertElement(“bold”);[/code]

[code]// XMetaL Script Language JScript:
// Insert a new Sect1 element (node) before the first Sect1 in the document.
//Paste this example in a “Journalist” DTD document.

// Get the top level node
var topElem = ActiveDocument.documentElement;
if (topElem) {
// Create the new Sect1 node
var newSect1 = ActiveDocument.createElement(“Sect1”);
// Visit each child node of the top-level node
// until the desired node (if it exists) is found
var nextChild=topElem.firstChild;
while (nextChild && nextChild.nodeName!=”Sect1″) {
nextChild = nextChild.nextSibling;
}
// If the desired node was found, insert the new
// node before it. if (nextChild){
var firstSect1 = nextChild;
topElem.insertBefore(newSect1,firstSect1);
}[/code]

Depending on your exact needs you may need to add additional logic to those scripts.

Many APIs let you insert elements. One of the following (or others I have missed) might be more suitable depending on your exact need:

  DOMNode_object.appendChild()
  DOMNode_object.insertBefore()
  Selection.Surround()
  Selection.SurroundNS()
  Selection.SplitContainer()
  Selection.InsertElementNS()
  Selection.InsertWithTemplate()
  Selection.InsertWithTemplateNS()
  Selection.InsertWithRequired()
  Selection.InsertWithRequiredNS()
  Selection.InsertCaption()
  Selection.InsertImage()
  Selection.InsertTable()
  Selection.InsertCALSTable()
  Selection.InsertRowsAbove()
  Selection.InsertRowsBelow()
  Selection.InsertColumnsLeft()
  Selection.InsertColumnsRight()
  Selection.TypingSplit()
  Selection.SplitToElementType()
  Selection.SplitToElementTypeNS()
  Range.Surround()
  Range.SurroundNS()
  Range.SplitContainer()
  Range.InsertElementNS()
  Range.InsertWithTemplate()
  Range.InsertWithTemplateNS()
  Range.InsertWithRequired()
  Range.InsertWithRequiredNS()
  Range.InsertCaption()
  Range.InsertImage()
  Range.InsertTable()
  Range.InsertCALSTable()
  Range.InsertRowsAbove()
  Range.InsertRowsBelow()
  Range.InsertColumnsLeft()
  Range.InsertColumnsRight()
  Range.TypingSplit()
  Range.SplitToElementType()
  Range.SplitToElementTypeNS()

Additional useful APIs (that work with or support those listed above):

  Document_object.createElement()
  Document_object.createElementNS()
  Document_object.createDocumentFragment()
  Selection.CanInsert
  Selection.CanInsertList
  Selection.CanSplitContainer
  Selection.CanSurround
  Range.CanInsert
  Range.CanInsertList
  Range.CanSplitContainer
  Range.CanSurround
  Many Selection and Range methods that allow you to move the Selection and Range
  Many properties and methods that allow you to obtain a Selection or Range element name, attribute value, attribute name, etc.

Reply

Products
Downloads
Support