示例#1
0
NS_IMETHODIMP
HTMLOptionsCollection::SetOption(uint32_t aIndex,
                                 nsIDOMHTMLOptionElement* aOption)
{
  if (!mSelect) {
    return NS_OK;
  }

  // if the new option is null, just remove this option.  Note that it's safe
  // to pass a too-large aIndex in here.
  if (!aOption) {
    mSelect->Remove(aIndex);

    // We're done.
    return NS_OK;
  }

  nsresult rv = NS_OK;

  uint32_t index = uint32_t(aIndex);

  // Now we're going to be setting an option in our collection
  if (index > mElements.Length()) {
    // Fill our array with blank options up to (but not including, since we're
    // about to change it) aIndex, for compat with other browsers.
    rv = SetLength(index);
    NS_ENSURE_SUCCESS(rv, rv);
  }

  NS_ASSERTION(index <= mElements.Length(), "SetLength lied");
  
  nsCOMPtr<nsIDOMNode> ret;
  if (index == mElements.Length()) {
    nsCOMPtr<nsIDOMNode> node = do_QueryInterface(aOption);
    rv = mSelect->AppendChild(node, getter_AddRefs(ret));
  } else {
    // Find the option they're talking about and replace it
    // hold a strong reference to follow COM rules.
    nsRefPtr<HTMLOptionElement> refChild = ItemAsOption(index);
    NS_ENSURE_TRUE(refChild, NS_ERROR_UNEXPECTED);

    nsCOMPtr<nsINode> parent = refChild->GetParent();
    if (parent) {
      nsCOMPtr<nsINode> node = do_QueryInterface(aOption);
      ErrorResult res;
      parent->ReplaceChild(*node, *refChild, res);
      rv = res.ErrorCode();
    }
  }

  return rv;
}
void
HTMLOptionsCollection::IndexedSetter(uint32_t aIndex,
                                     HTMLOptionElement* aOption,
                                     ErrorResult& aError)
{
  if (!mSelect) {
    return;
  }

  // if the new option is null, just remove this option.  Note that it's safe
  // to pass a too-large aIndex in here.
  if (!aOption) {
    mSelect->Remove(aIndex);

    // We're done.
    return;
  }

  // Now we're going to be setting an option in our collection
  if (aIndex > mElements.Length()) {
    // Fill our array with blank options up to (but not including, since we're
    // about to change it) aIndex, for compat with other browsers.
    nsresult rv = SetLength(aIndex);
    if (NS_WARN_IF(NS_FAILED(rv))) {
      aError.Throw(rv);
      return;
    }
  }

  NS_ASSERTION(aIndex <= mElements.Length(), "SetLength lied");
  
  if (aIndex == mElements.Length()) {
    mSelect->AppendChild(*aOption, aError);
    return;
  }

  // Find the option they're talking about and replace it
  // hold a strong reference to follow COM rules.
  RefPtr<HTMLOptionElement> refChild = ItemAsOption(aIndex);
  if (!refChild) {
    aError.Throw(NS_ERROR_UNEXPECTED);
    return;
  }

  nsCOMPtr<nsINode> parent = refChild->GetParent();
  if (!parent) {
    return;
  }

  parent->ReplaceChild(*aOption, *refChild, aError);
}
示例#3
0
Element*
HTMLOptionsCollection::GetElementAt(uint32_t aIndex)
{
  return ItemAsOption(aIndex);
}