Sunday, January 8, 2012

Text Nodes

Text nodes appear in the element tree when a container element has multiple children some of which are containers for text and some of which are just a text string.

Consider the case of an element, p, that allows text (<TEXT>) and other elements as described in the EDD:

I have made a small modification to oil.xml to illustrate this point. The word "every" is italicized by wrapping it in the element i. Before this change, the p element had no children. Now it has three. The first and the last of these are text nodes as illustrated below.

The function getElementName() uses the element property ElementDef to find the definition object and then the name property to get the name string.

function getElementName(elem) {
    var elemDef, name = null;

    elemDef = elem.ElementDef;
    if (elemDef.ObjectValid()) {
        name = elemDef.Name;
    return name;

If an element definition is NULL, that element is a text node. Armed with this knowledge, the displayAttrs() function used in a previous post can be improved.

//writes element name and list of attributes to the console
function  displayAttrs(elem) {
    var i, name, aName, attrs;
    name = getElementName(elem);
    if (name != null) {
        attrs = elem.GetAttributes();
        for (i = 0; i < attrs.len; i++) {
            aName = attrs[i].name;
            Console( "     " + aName);      
    } else {
        Console("Selected element is a text node");

Here a text node is selected:

The output when displayAttrs() is called on the selected text node is shown here:


  1. Hi!

    Thanks for the awesome blog, your articles are a great ExtendScript information source.

    I'm working on a script which would change the content of TEXT -nodes and just can't figure out how to do it. I've been trying to select/delete/add -text within a TEXT -node and it just doesn't work - I must be doing something wrong.

    My approach has been that the element is a child of another element, let's say "elem" so I've been trying to select the text like

    var childelem = elem.FirstChildElement
    var tRange = childelem.TextRange

    ... and work on the text - and no luck...

    1. Hello,

      I have not had a chance to create an example but my quick thought is that when working with elements, you need to use the data structure ElementRange rather than TextRange. Text ranges are applicable only when working with paragraphs.

    2. Hi, thanks for the reply.

      I couldn't figure out how to edit the element text content in any smart way, so I finally avoided the problem by creating a similar new element with new content next to the old one (ElementDef.WrapElement()) and then deleting the deprecated one. It's not beautiful but it works :)

      Editing text node contents should be basic stuff (I think?) so it was a big surprise how difficult it was.

    3. It should be possible to add directly to the text node if you have the right insertion point.

      Did you get an error code when you tried your add?

    4. No error codes, it just didn't add anything with the AddText when I tried to add to the beginning TextLoc of the element. Also it seemed impossible to select the existing text content within the element.

      I tried at least the following:

      var eTr = elem.TextRange
      doc.TextSelection = eTr

      Which selected the entire element (not just the text content)


      var eTr = elem.FirstChildElement.TextRange (I figured this would be the -element, at least the same selection produced "null", when I alerted the ElementDef.Name)

      doc.TextSelection = eTr

      Which didn't select anything