XML.prototype.setChild = function()

in asdoc/XML.js [2595:2665]


XML.prototype.setChild = function(elementName, elements) {
  var /** @type {number} */ i = 0;
  var /** @type {number} */ len = 0;
  //var /** @type {number} */ i = 0;
  //var /** @type {number} */ len = 0;
  if (org.apache.royale.utils.Language.uint(elementName).toString() == elementName)
    throw new TypeError('not permitted');
  var /** @type {string} */ ref = this.getNodeRef();
  if (ref == XML.TEXT || ref == XML.COMMENT || ref == XML.PROCESSING_INSTRUCTION || ref == XML.ATTRIBUTE)
    return this;
  var /** @type {QName} */ n = this.XML_toXMLName(elementName);
  if (n.isAttribute) {
    return this.setAttribute(n, elements + '');
  }
  i = -1;
  var /** @type {boolean} */ wild = n.localName == '*';
  var /** @type {boolean} */ primitiveAssign = !wild && !(org.apache.royale.utils.Language.is(elements, XML) || org.apache.royale.utils.Language.is(elements, XMLList));
  var /** @type {number} */ k = (this.childrenLength() - 1) >> 0;
  while (k > -1) {
    if ((wild || (this._children[k].getNodeRef() == XML.ELEMENT && this._children[k].localName() == n.localName)) && (n.uri == null || (this._children[k].getNodeRef() == XML.ELEMENT && this._children[k]._name.uri == n.uri))) {
      if (i !== -1) {
        this.removeChildAt(i);
      }
      i = k;
    }
    k--;
  }
  if (i == -1) {
    i = this.childrenLength();
    if (primitiveAssign) {
      XML._internal = true;
      var /** @type {XML} */ y = new XML();
      XML._internal = false;
      y._name = XML.getQName(n.localName, n.prefix, n.uri, false);
      y.setParent(this);
      var /** @type {Namespace} */ ns = new Namespace('', n);
      this.replaceChildAt(i, y);
      y.setNamespace(ns);
    }
  }
  if (primitiveAssign) {
    y = this._children[i];
    if (this.XML__notification || (XML.recursiveNotify && this.XML__parent)) {
      len = y.childrenLength();
      var /** @type {string} */ firstChildStr = null;
      for (k = 0; k < len; ++k) {
        var /** @type {XML} */ removed = y._children[k];
        if (!k)
          firstChildStr = removed.toXMLString();
        y.xml$_notify("nodeRemoved", y, removed, null);
      }
    }
    k = (y.childrenLength() - 1) >> 0;
    y.XML__internalSuppressNotify = true;
    while (k > -1) {
      y.removeChildAt(0);
      k--;
    }
    y.XML__internalSuppressNotify = false;
    elements = elements + '';
    if (elements) {
      y.replaceChildAt(0, elements);
      if (this.XML__notification || (XML.recursiveNotify && this.XML__parent)) {
        y.xml$_notify("textSet", y._children[0], elements, new XML(firstChildStr));
      }
    }
  } else {
    this.replaceChildAt(i, elements);
  }
  return elements;
};