Exemple #1
0
const nsAString&
nsCParserStartNode::GetValueAt(PRUint32 anIndex) const 
{
  if (PRInt32(anIndex) < mAttributes.GetSize()) {
    CAttributeToken* attr = 
      static_cast<CAttributeToken*>(mAttributes.ObjectAt(anIndex));
    if (attr) {
      return attr->GetValue();
    }
  }
  return EmptyString();
}
Exemple #2
0
void nsCParserStartNode::GetSource(nsString& aString) const
{
  aString.Assign(PRUnichar('<'));
  const PRUnichar* theTagName = 
    nsHTMLTags::GetStringValue(nsHTMLTag(mToken->GetTypeID()));
  if (theTagName) {
    aString.Append(theTagName);
  }
  PRInt32 index;
  PRInt32 size = mAttributes.GetSize();
  for (index = 0 ; index < size; ++index) {
    CAttributeToken *theToken = 
      static_cast<CAttributeToken*>(mAttributes.ObjectAt(index));
    if (theToken) {
      theToken->AppendSourceTo(aString);
      aString.Append(PRUnichar(' ')); //this will get removed...
    }
  }
  aString.Append(PRUnichar('>'));
}
/**
 *  This method gets called when a tag needs to write it's attributes
 *
 *  @update  gess 3/25/98
 *  @param
 *  @return  result status
 */
nsresult CViewSourceHTML::WriteAttributes(const nsAString& tagName, 
                                          nsTokenAllocator* allocator, 
                                          PRInt32 attrCount, PRBool aOwnerInError) {
  nsresult result=NS_OK;

  if(attrCount){ //go collect the attributes...
    int attr = 0;
    for(attr = 0; attr < attrCount; ++attr){
      CToken* theToken = mTokenizer->PeekToken();
      if(theToken)  {
        eHTMLTokenTypes theType = eHTMLTokenTypes(theToken->GetTokenType());
        if(eToken_attribute == theType){
          mTokenizer->PopToken(); //pop it for real...
          mTokenNode.AddAttribute(theToken);  //and add it to the node.

          CAttributeToken* theAttrToken = (CAttributeToken*)theToken;
          const nsSubstring& theKey = theAttrToken->GetKey();

          // The attribute is only in error if its owner is NOT in error.
          const PRBool attributeInError =
            !aOwnerInError && theAttrToken->IsInError();

          result = WriteTag(kAttributeName,theKey,0,attributeInError);
          const nsSubstring& theValue = theAttrToken->GetValue();

          if(!theValue.IsEmpty() || theAttrToken->mHasEqualWithoutValue){
            if (IsUrlAttribute(tagName, theKey, theValue)) {
              WriteHrefAttribute(allocator, theValue);
            } else {
              WriteTag(kAttributeValue,theValue,0,attributeInError);
            }
          }
        }
      }
      else return kEOF;
    }
  }

  return result;
}
void CViewSourceHTML::AddAttrToNode(nsCParserStartNode& aNode,
                                    nsTokenAllocator* aAllocator,
                                    const nsAString& aAttrName,
                                    const nsAString& aAttrValue)
{
  NS_PRECONDITION(aAllocator, "Must have a token allocator!");

  CAttributeToken* theAttr =
    (CAttributeToken*) aAllocator->CreateTokenOfType(eToken_attribute,
                                                     eHTMLTag_unknown,
                                                     aAttrValue);
  if (!theAttr) {
    NS_ERROR("Failed to allocate attribute token");
    return;
  }

  theAttr->SetKey(aAttrName);
  aNode.AddAttribute(theAttr);

  // Parser nodes assume that they are being handed a ref when AddAttribute is
  // called, unlike Init() and construction, when they actually addref the
  // incoming token.  Do NOT release here unless this setup changes.
}
/**
 * This method is called just after we've consumed a start or end
 * tag, and we now have to consume its attributes.
 * 
 * @param   aChar is the last char read
 * @param   aToken is the start or end tag that "owns" these attributes.
 * @param   aScanner represents our input source
 * @return  Error result.
 */
nsresult
nsHTMLTokenizer::ConsumeAttributes(PRUnichar aChar,
                                   CToken* aToken,
                                   nsScanner& aScanner)
{
  bool done = false;
  nsresult result = NS_OK;
  int16_t theAttrCount = 0;

  nsTokenAllocator* theAllocator = this->GetTokenAllocator();

  while (!done && result == NS_OK) {
    CAttributeToken* theToken =
      static_cast<CAttributeToken*>
                 (theAllocator->CreateTokenOfType(eToken_attribute,
                                                     eHTMLTag_unknown));
    if (MOZ_LIKELY(theToken != nullptr)) {
      // Tell the new token to finish consuming text...
      result = theToken->Consume(aChar, aScanner, mFlags);

      if (NS_SUCCEEDED(result)) {
        ++theAttrCount;
        AddToken((CToken*&)theToken, result, &mTokenDeque, theAllocator);
      } else {
        IF_FREE(theToken, mTokenAllocator);
        // Bad attribute returns shouldn't propagate out.
        if (NS_ERROR_HTMLPARSER_BADATTRIBUTE == result) {
          result = NS_OK;
        }
      }
    }
    else {
      result = NS_ERROR_OUT_OF_MEMORY;
    }

#ifdef DEBUG
    if (NS_SUCCEEDED(result)) {
      int32_t newline = 0;
      aScanner.SkipWhitespace(newline);
      NS_ASSERTION(newline == 0,
          "CAttribute::Consume() failed to collect all the newlines!");
    }
#endif
    if (NS_SUCCEEDED(result)) {
      result = aScanner.Peek(aChar);
      if (NS_SUCCEEDED(result)) {
        if (aChar == kGreaterThan) { // You just ate the '>'
          aScanner.GetChar(aChar); // Skip the '>'
          done = true;
        } else if (aChar == kLessThan) {
          aToken->SetInError(true);
          done = true;
        }
      }
    }
  }

  if (NS_FAILED(result)) {
    aToken->SetInError(true);

    if (!aScanner.IsIncremental()) {
      result = NS_OK;
    }
  }

  aToken->SetAttributeCount(theAttrCount);
  return result;
}