nsresult txBufferingHandler::characters(const nsSubstring& aData, PRBool aDOE) { NS_ENSURE_TRUE(mBuffer, NS_ERROR_OUT_OF_MEMORY); mCanAddAttribute = PR_FALSE; txOutputTransaction::txTransactionType type = aDOE ? txOutputTransaction::eCharacterNoOETransaction : txOutputTransaction::eCharacterTransaction; txOutputTransaction* transaction = mBuffer->getLastTransaction(); if (transaction && transaction->mType == type) { mBuffer->mStringValue.Append(aData); static_cast<txCharacterTransaction*>(transaction)->mLength += aData.Length(); return NS_OK; } transaction = new txCharacterTransaction(type, aData.Length()); NS_ENSURE_TRUE(transaction, NS_ERROR_OUT_OF_MEMORY); mBuffer->mStringValue.Append(aData); return mBuffer->addTransaction(transaction); }
bool nsCoreUtils::IsWhitespaceString(const nsSubstring& aString) { nsSubstring::const_char_iterator iterBegin, iterEnd; aString.BeginReading(iterBegin); aString.EndReading(iterEnd); while (iterBegin != iterEnd && IsWhitespace(*iterBegin)) ++iterBegin; return iterBegin == iterEnd; }
/** * Check for a modifier flag of the following form: * "flag=string" * "flag!=string" * @param aFlag The flag to compare. * @param aData The tokenized data to check; this is lowercased * before being passed in. * @param aValue The value that is expected. * @param aResult If this is "ok" when passed in, this is left alone. * Otherwise if the flag is found it is set to eBad or eOK. * @return Whether the flag was handled. */ static bool CheckStringFlag(const nsSubstring& aFlag, const nsSubstring& aData, const nsSubstring& aValue, TriState& aResult) { if (aData.Length() < aFlag.Length() + 1) { return false; } if (!StringBeginsWith(aData, aFlag)) { return false; } bool comparison = true; if (aData[aFlag.Length()] != '=') { if (aData[aFlag.Length()] == '!' && aData.Length() >= aFlag.Length() + 2 && aData[aFlag.Length() + 1] == '=') { comparison = false; } else { return false; } } if (aResult != eOK) { nsDependentSubstring testdata = Substring(aData, aFlag.Length() + (comparison ? 1 : 2)); if (testdata.Equals(aValue)) { aResult = comparison ? eOK : eBad; } else { aResult = comparison ? eBad : eOK; } } return true; }
void mozInlineSpellWordUtil::NormalizeWord(nsSubstring& aWord) { nsAutoString result; ::NormalizeWord(aWord, 0, aWord.Length(), result); aWord = result; }
nsresult txExprParser::createExprInternal(const nsSubstring& aExpression, uint32_t aSubStringPos, txIParseContext* aContext, Expr** aExpr) { NS_ENSURE_ARG_POINTER(aExpr); *aExpr = nullptr; txExprLexer lexer; nsresult rv = lexer.parse(aExpression); if (NS_FAILED(rv)) { nsASingleFragmentString::const_char_iterator start; aExpression.BeginReading(start); aContext->SetErrorOffset(lexer.mPosition - start + aSubStringPos); return rv; } nsAutoPtr<Expr> expr; rv = createExpr(lexer, aContext, getter_Transfers(expr)); if (NS_SUCCEEDED(rv) && lexer.peek()->mType != Token::END) { rv = NS_ERROR_XPATH_BINARY_EXPECTED; } if (NS_FAILED(rv)) { nsASingleFragmentString::const_char_iterator start; aExpression.BeginReading(start); aContext->SetErrorOffset(lexer.peek()->mStart - start + aSubStringPos); return rv; } txXPathOptimizer optimizer; Expr* newExpr = nullptr; rv = optimizer.optimize(expr, &newExpr); NS_ENSURE_SUCCESS(rv, rv); *aExpr = newExpr ? newExpr : expr.forget(); return NS_OK; }
nsresult txUnknownHandler::startElement(nsIAtom* aPrefix, const nsSubstring& aLocalName, const int32_t aNsID) { if (!mFlushed) { // Make sure that mEs->mResultHandler == this is true, otherwise we'll // leak mEs->mResultHandler in createHandlerAndFlush. NS_ASSERTION(mEs->mResultHandler == this, "We're leaking mEs->mResultHandler."); bool htmlRoot = aNsID == kNameSpaceID_None && !aPrefix && aLocalName.Equals(NS_LITERAL_STRING("html"), txCaseInsensitiveStringComparator()); nsresult rv = createHandlerAndFlush(htmlRoot, aLocalName, aNsID); NS_ENSURE_SUCCESS(rv, rv); } return mEs->mResultHandler->startElement(aPrefix, aLocalName, aNsID); }
// This is to fix characters that the spellchecker may not like static void NormalizeWord(const nsSubstring& aInput, int32_t aPos, int32_t aLen, nsAString& aOutput) { aOutput.Truncate(); for (int32_t i = 0; i < aLen; i++) { PRUnichar ch = aInput.CharAt(i + aPos); // remove ignorable characters from the word if (IsIgnorableCharacter(ch)) continue; // the spellchecker doesn't handle curly apostrophes in all languages if (ch == 0x2019) { // RIGHT SINGLE QUOTATION MARK ch = '\''; } aOutput.Append(ch); } }
/** * Check for a modifier flag of the following forms: * "flag" (same as "true") * "flag=yes|true|1" * "flag="no|false|0" * @param aFlag The flag to compare. * @param aData The tokenized data to check; this is lowercased * before being passed in. * @param aResult If the flag is found, the value is assigned here. * @return Whether the flag was handled. */ static bool CheckFlag(const nsSubstring& aFlag, const nsSubstring& aData, bool& aResult) { if (!StringBeginsWith(aData, aFlag)) { return false; } if (aFlag.Length() == aData.Length()) { // the data is simply "flag", which is the same as "flag=yes" aResult = true; return true; } if (aData.CharAt(aFlag.Length()) != '=') { // the data is "flag2=", which is not anything we care about return false; } if (aData.Length() == aFlag.Length() + 1) { aResult = false; return true; } switch (aData.CharAt(aFlag.Length() + 1)) { case '1': case 't': //true case 'y': //yes aResult = true; return true; case '0': case 'f': //false case 'n': //no aResult = false; return true; } return false; }
nsresult txUnknownHandler::startElement(nsIAtom* aPrefix, const nsSubstring& aLocalName, const PRInt32 aNsID) { // Make sure that mEs->mResultHandler == this is true, otherwise we'll // leak mEs->mResultHandler in createHandlerAndFlush and we may crash // later on trying to delete this handler again. NS_ASSERTION(mEs->mResultHandler == this, "We're leaking mEs->mResultHandler."); PRBool htmlRoot = aNsID == kNameSpaceID_None && !aPrefix && aLocalName.Equals(NS_LITERAL_STRING("html"), txCaseInsensitiveStringComparator()); nsresult rv = createHandlerAndFlush(htmlRoot, aLocalName, aNsID); NS_ENSURE_SUCCESS(rv, rv); rv = mEs->mResultHandler->startElement(aPrefix, aLocalName, aNsID); delete this; return rv; }
/** * This method gets called when a tag needs to be sent out * * @update gess 3/25/98 * @param * @return result status */ nsresult CViewSourceHTML::WriteTag(PRInt32 aTagType,const nsSubstring & aText,PRInt32 attrCount,PRBool aTagInError) { nsresult result=NS_OK; // adjust line number to what it will be after we finish writing this tag // XXXbz life here sucks. We can't use the GetNewlineCount on the token, // because Text tokens in <style>, <script>, etc lie through their teeth. // On the other hand, the parser messes up newline counting in some token // types (bug 137315). So our line numbers will disagree with the parser's // in some cases... // XXXbenjamn Shouldn't we be paying attention to the aCountLines BuildModel // parameter here? mLineNumber += aText.CountChar(PRUnichar('\n')); nsTokenAllocator* theAllocator=mTokenizer->GetTokenAllocator(); NS_ASSERTION(0!=theAllocator,"Error: no allocator"); if(0==theAllocator) return NS_ERROR_FAILURE; // Highlight all parts of all erroneous tags. if (mSyntaxHighlight && aTagInError) { CStartToken* theTagToken= static_cast<CStartToken*> (theAllocator->CreateTokenOfType(eToken_start, eHTMLTag_span, NS_LITERAL_STRING("SPAN"))); NS_ENSURE_TRUE(theTagToken, NS_ERROR_OUT_OF_MEMORY); mErrorNode.Init(theTagToken, theAllocator); AddAttrToNode(mErrorNode, theAllocator, NS_LITERAL_STRING("class"), NS_LITERAL_STRING("error")); mSink->OpenContainer(mErrorNode); IF_FREE(theTagToken, theAllocator); #ifdef DUMP_TO_FILE if (gDumpFile) { fprintf(gDumpFile, "<span class=\"error\">"); } #endif } if (kBeforeText[aTagType][0] != 0) { NS_ConvertASCIItoUTF16 beforeText(kBeforeText[aTagType]); mITextToken.SetIndirectString(beforeText); nsCParserNode theNode(&mITextToken, 0/*stack token*/); mSink->AddLeaf(theNode); } #ifdef DUMP_TO_FILE if (gDumpFile && kDumpFileBeforeText[aTagType][0]) fprintf(gDumpFile, kDumpFileBeforeText[aTagType]); #endif // DUMP_TO_FILE if (mSyntaxHighlight && aTagType != kText) { CStartToken* theTagToken= static_cast<CStartToken*> (theAllocator->CreateTokenOfType(eToken_start, eHTMLTag_span, NS_LITERAL_STRING("SPAN"))); NS_ENSURE_TRUE(theTagToken, NS_ERROR_OUT_OF_MEMORY); mStartNode.Init(theTagToken, theAllocator); AddAttrToNode(mStartNode, theAllocator, NS_LITERAL_STRING("class"), NS_ConvertASCIItoUTF16(kElementClasses[aTagType])); mSink->OpenContainer(mStartNode); //emit <starttag>... IF_FREE(theTagToken, theAllocator); #ifdef DUMP_TO_FILE if (gDumpFile) { fprintf(gDumpFile, "<span class=\""); fprintf(gDumpFile, kElementClasses[aTagType]); fprintf(gDumpFile, "\">"); } #endif // DUMP_TO_FILE } STOP_TIMER(); mITextToken.SetIndirectString(aText); //now emit the tag name... nsCParserNode theNode(&mITextToken, 0/*stack token*/); mSink->AddLeaf(theNode); #ifdef DUMP_TO_FILE if (gDumpFile) { fputs(NS_ConvertUTF16toUTF8(aText).get(), gDumpFile); } #endif // DUMP_TO_FILE if (mSyntaxHighlight && aTagType != kText) { mStartNode.ReleaseAll(); mSink->CloseContainer(eHTMLTag_span); //emit </endtag>... #ifdef DUMP_TO_FILE if (gDumpFile) fprintf(gDumpFile, "</span>"); #endif //DUMP_TO_FILE } if(attrCount){ result=WriteAttributes(aText, theAllocator, attrCount, aTagInError); } // Tokens are set in error if their ending > is not there, so don't output // the after-text if (!aTagInError && kAfterText[aTagType][0] != 0) { NS_ConvertASCIItoUTF16 afterText(kAfterText[aTagType]); mITextToken.SetIndirectString(afterText); nsCParserNode theNode(&mITextToken, 0/*stack token*/); mSink->AddLeaf(theNode); } #ifdef DUMP_TO_FILE if (!aTagInError && gDumpFile && kDumpFileAfterText[aTagType][0]) fprintf(gDumpFile, kDumpFileAfterText[aTagType]); #endif // DUMP_TO_FILE if (mSyntaxHighlight && aTagInError) { mErrorNode.ReleaseAll(); mSink->CloseContainer(eHTMLTag_span); //emit </endtag>... #ifdef DUMP_TO_FILE if (gDumpFile) fprintf(gDumpFile, "</span>"); #endif //DUMP_TO_FILE } START_TIMER(); return result; }
/** * Creates an Attribute Value Template using the given value * This should move to XSLProcessor class */ nsresult txExprParser::createAVT(const nsSubstring& aAttrValue, txIParseContext* aContext, Expr** aResult) { *aResult = nullptr; nsresult rv = NS_OK; nsAutoPtr<Expr> expr; FunctionCall* concat = nullptr; nsAutoString literalString; bool inExpr = false; nsSubstring::const_char_iterator iter, start, end, avtStart; aAttrValue.BeginReading(iter); aAttrValue.EndReading(end); avtStart = iter; while (iter != end) { // Every iteration through this loop parses either a literal section // or an expression start = iter; nsAutoPtr<Expr> newExpr; if (!inExpr) { // Parse literal section literalString.Truncate(); while (iter != end) { PRUnichar q = *iter; if (q == '{' || q == '}') { // Store what we've found so far and set a new |start| to // skip the (first) brace literalString.Append(Substring(start, iter)); start = ++iter; // Unless another brace follows we've found the start of // an expression (in case of '{') or an unbalanced brace // (in case of '}') if (iter == end || *iter != q) { if (q == '}') { aContext->SetErrorOffset(iter - avtStart); return NS_ERROR_XPATH_UNBALANCED_CURLY_BRACE; } inExpr = true; break; } // We found a second brace, let that be part of the next // literal section being parsed and continue looping } ++iter; } if (start == iter && literalString.IsEmpty()) { // Restart the loop since we didn't create an expression continue; } newExpr = new txLiteralExpr(literalString + Substring(start, iter)); } else { // Parse expressions, iter is already past the initial '{' when // we get here. while (iter != end) { if (*iter == '}') { rv = createExprInternal(Substring(start, iter), start - avtStart, aContext, getter_Transfers(newExpr)); NS_ENSURE_SUCCESS(rv, rv); inExpr = false; ++iter; // skip closing '}' break; } else if (*iter == '\'' || *iter == '"') { PRUnichar q = *iter; while (++iter != end && *iter != q) {} /* do nothing */ if (iter == end) { break; } } ++iter; } if (inExpr) { aContext->SetErrorOffset(start - avtStart); return NS_ERROR_XPATH_UNBALANCED_CURLY_BRACE; } } // Add expression, create a concat() call if necessary if (!expr) { expr = newExpr; } else { if (!concat) { concat = new txCoreFunctionCall(txCoreFunctionCall::CONCAT); NS_ENSURE_TRUE(concat, NS_ERROR_OUT_OF_MEMORY); rv = concat->addParam(expr.forget()); expr = concat; NS_ENSURE_SUCCESS(rv, rv); } rv = concat->addParam(newExpr.forget()); NS_ENSURE_SUCCESS(rv, rv); } } if (inExpr) { aContext->SetErrorOffset(iter - avtStart); return NS_ERROR_XPATH_UNBALANCED_CURLY_BRACE; } if (!expr) { expr = new txLiteralExpr(EmptyString()); } *aResult = expr.forget(); return NS_OK; }