XMLList.prototype.setChild = function()

in asdoc/XMLList.js [1007:1120]


XMLList.prototype.setChild = function(elementName, elements) {
  var /** @type {Object} */ r;
  var /** @type {number} */ idx = org.apache.royale.utils.Language.uint(elementName);
  if (idx + '' == elementName + '') {
    if (this.XMLList__targetObject) {
      if (org.apache.royale.utils.Language.is(this.XMLList__targetObject, XMLList))
        r = this.targetObject.XMLList_resolveValue();
      else
        r = this.XMLList__targetObject;
      if (r == null)
        return null;
    } else {
      r = null;
    }
    if (idx >= this._xmlArray.length) {
      var /** @type {XML} */ xmlR = r;
      if (!r && org.apache.royale.utils.Language.is(r, XMLList)) {
        if (!r.XMLList_isSingle())
          return null;
        xmlR = r[0];
      }
      if (xmlR && xmlR.nodeKind() != 'element')
        return null;
      var /** @type {XML} */ y = this.XMLList_xmlFromProperty(xmlR);
      idx = (this._xmlArray.length) >>> 0;
      if (y.nodeKind() != 'attribute') {
        if (xmlR) {
          if (idx > 0) {
            var /** @type {number} */ j = 0;
            while (j < xmlR.getChildrenArray().length - 1 && xmlR.getChildrenArray()[j] !== this._xmlArray[idx - 1]) {
              j++;
            }
          } else {
            j = (xmlR.getChildrenArray().length - 1) >>> 0;
          }
          xmlR.insertChildAfter(xmlR.getChildrenArray()[j], y);
        }
        if (org.apache.royale.utils.Language.is(elements, XML))
          y.setName(elements.name());
        else if (org.apache.royale.utils.Language.is(elements, XMLList))
          y.setName(new QName(elements.XMLList__targetProperty));
      }
      this.XMLList_Append(y);
    }
    if (!(org.apache.royale.utils.Language.is(elements, XML) || org.apache.royale.utils.Language.is(elements, XMLList)) || (org.apache.royale.utils.Language.is(elements, XML) && (elements.nodeKind() == 'text' || elements.nodeKind() == 'attribute'))) {
      elements = elements + '';
    }
    if (this._xmlArray[idx].nodeKind() == 'attribute') {
      var /** @type {QName} */ z = this._xmlArray[idx].name();
      this._xmlArray[idx].parent().setAttribute(z, elements);
      var /** @type {XMLList} */ attr = this._xmlArray[idx].parent().attribute(z);
      this._xmlArray[idx] = attr[0];
      this.XMLList_addIndex((idx) >> 0);
    } else if (org.apache.royale.utils.Language.is(elements, XMLList)) {
      var /** @type {XMLList} */ c = new XMLList(elements);
      var /** @type {XML} */ parent = this._xmlArray[idx].parent();
      if (parent) {
        var /** @type {number} */ q = (parent.getChildrenArray().indexOf(this._xmlArray[idx])) >> 0;
        parent.replaceChildAt(q, c);
        for (j = 0; j < c._xmlArray.child('length') - 1; j++) {
          c.setChild(j, parent.getChildrenArray()[q + j]);
        }
      }
      if (c.length() == 0) {
        this._xmlArray.splice(idx, 1);
        for (j = idx; j < this.length() - 1; j++) {
          this.XMLList_addIndex((j) >> 0);
        }
      } else {
        var /** @type {number} */ l = (this._xmlArray.length) >>> 0;
        var /** @type {number} */ cl = (c.length() - 1) >>> 0;
        for (j = (l - 1) >>> 0; j > idx; j--) {
          this._xmlArray[j + cl] = this._xmlArray[j];
          this.XMLList_addIndex((j + cl) >> 0);
        }
        for (j = 0; j < cl; j++) {
          this._xmlArray[idx + j] = c[j];
          this.XMLList_addIndex((idx + j) >> 0);
        }
      }
    } else if (org.apache.royale.utils.Language.is(elements, XML) || this._xmlArray[idx].nodeKind() == 'text' || this._xmlArray[idx].nodeKind() == 'comment' || this._xmlArray[idx].nodeKind() == 'processing-instruction') {
      parent = this._xmlArray[idx].parent();
      if (parent) {
        q = (parent.getChildrenArray().indexOf(this._xmlArray[idx])) >> 0;
        parent.replaceChildAt(q, elements);
        elements = parent.getChildrenArray()[q];
      }
      if (typeof(elements) == 'string') {
        var /** @type {XML} */ t = new XML();
        t.setParent(this);
        t.setValue(elements + '');
        this._xmlArray[idx] = t;
      } else {
        this._xmlArray[idx] = elements;
      }
      this.XMLList_addIndex((idx) >> 0);
    } else {
      this._xmlArray[idx].setChild('*', elements);
    }
  } else {
    if (this._xmlArray.length <= 1) {
      if (this.XMLList_isEmpty()) {
        r = this.XMLList_resolveValue();
        if (r == null || r.length() != 1)
          return null;
        this.XMLList_Append(r);
      }
      this._xmlArray[0].setChild(elementName, elements);
    } else {
      throw new TypeError('Error #1089: Assignment to lists with more than one item is not supported.');
    }
  }
  return elements;
};