/* Copyright 2010 Google Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include #include #include #include #include #include "ascii.h" #include "error.h" #include "nokogiri_gumbo.h" #include "macros.h" #include "parser.h" #include "string_buffer.h" #include "util.h" #include "vector.h" // Prints a formatted message to a StringBuffer. This automatically resizes the // StringBuffer as necessary to fit the message. Returns the number of bytes // written. static int PRINTF(2) print_message ( GumboStringBuffer* output, const char* format, ... ) { va_list args; int remaining_capacity = output->capacity - output->length; va_start(args, format); int bytes_written = vsnprintf ( output->data + output->length, remaining_capacity, format, args ); va_end(args); #if _MSC_VER && _MSC_VER < 1900 if (bytes_written == -1) { // vsnprintf returns -1 on older MSVC++ if there's not enough capacity, // instead of returning the number of bytes that would've been written had // there been enough. In this case, we'll double the buffer size and hope // it fits when we retry (letting it fail and returning 0 if it doesn't), // since there's no way to smartly resize the buffer. gumbo_string_buffer_reserve(output->capacity * 2, output); va_start(args, format); int result = vsnprintf ( output->data + output->length, remaining_capacity, format, args ); va_end(args); return result == -1 ? 0 : result; } #else // -1 in standard C99 indicates an encoding error. Return 0 and do nothing. if (bytes_written == -1) { return 0; } #endif if (bytes_written >= remaining_capacity) { gumbo_string_buffer_reserve(output->capacity + bytes_written, output); remaining_capacity = output->capacity - output->length; va_start(args, format); bytes_written = vsnprintf ( output->data + output->length, remaining_capacity, format, args ); va_end(args); } output->length += bytes_written; return bytes_written; } static void print_tag_stack ( const GumboParserError* error, GumboStringBuffer* output ) { print_message(output, " Currently open tags: "); for (unsigned int i = 0; i < error->tag_stack.length; ++i) { if (i) { print_message(output, ", "); } GumboTag tag = (GumboTag)(intptr_t) error->tag_stack.data[i]; print_message(output, "%s", gumbo_normalized_tagname(tag)); } gumbo_string_buffer_append_codepoint('.', output); } static void handle_tokenizer_error ( const GumboError* error, GumboStringBuffer* output ) { switch (error->type) { case GUMBO_ERR_ABRUPT_CLOSING_OF_EMPTY_COMMENT: print_message(output, "Empty comment abruptly closed by '%s', use '-->'.", error->v.tokenizer.state == GUMBO_LEX_COMMENT_START? ">" : "->"); break; case GUMBO_ERR_ABRUPT_DOCTYPE_PUBLIC_IDENTIFIER: print_message ( output, "DOCTYPE public identifier missing closing %s.", error->v.tokenizer.state == GUMBO_LEX_DOCTYPE_PUBLIC_ID_DOUBLE_QUOTED? "quotation mark (\")" : "apostrophe (')" ); break; case GUMBO_ERR_ABRUPT_DOCTYPE_SYSTEM_IDENTIFIER: print_message ( output, "DOCTYPE system identifier missing closing %s.", error->v.tokenizer.state == GUMBO_LEX_DOCTYPE_SYSTEM_ID_DOUBLE_QUOTED? "quotation mark (\")" : "apostrophe (')" ); break; case GUMBO_ERR_ABSENCE_OF_DIGITS_IN_NUMERIC_CHARACTER_REFERENCE: print_message ( output, "Numeric character reference '%.*s' does not contain any %sdigits.", (int)error->original_text.length, error->original_text.data, error->v.tokenizer.state == GUMBO_LEX_HEXADECIMAL_CHARACTER_REFERENCE_START? "hexadecimal " : "" ); break; case GUMBO_ERR_CDATA_IN_HTML_CONTENT: print_message(output, "CDATA section outside foreign (SVG or MathML) content."); break; case GUMBO_ERR_CHARACTER_REFERENCE_OUTSIDE_UNICODE_RANGE: print_message ( output, "Numeric character reference '%.*s' references a code point that is outside the valid Unicode range.", (int)error->original_text.length, error->original_text.data ); break; case GUMBO_ERR_CONTROL_CHARACTER_IN_INPUT_STREAM: print_message ( output, "Input contains prohibited control code point U+%04X.", error->v.tokenizer.codepoint ); break; case GUMBO_ERR_CONTROL_CHARACTER_REFERENCE: print_message ( output, "Numeric character reference '%.*s' references prohibited control code point U+%04X.", (int)error->original_text.length, error->original_text.data, error->v.tokenizer.codepoint ); break; case GUMBO_ERR_END_TAG_WITH_ATTRIBUTES: print_message(output, "End tag contains attributes."); break; case GUMBO_ERR_DUPLICATE_ATTRIBUTE: print_message(output, "Tag contains multiple attributes with the same name."); break; case GUMBO_ERR_END_TAG_WITH_TRAILING_SOLIDUS: print_message(output, "End tag ends with '/>', use '>'."); break; case GUMBO_ERR_EOF_BEFORE_TAG_NAME: print_message(output, "End of input where a tag name is expected."); break; case GUMBO_ERR_EOF_IN_CDATA: print_message(output, "End of input in CDATA section."); break; case GUMBO_ERR_EOF_IN_COMMENT: print_message(output, "End of input in comment."); break; case GUMBO_ERR_EOF_IN_DOCTYPE: print_message(output, "End of input in DOCTYPE."); break; case GUMBO_ERR_EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT: print_message(output, "End of input in text that resembles an HTML comment inside script element content."); break; case GUMBO_ERR_EOF_IN_TAG: print_message(output, "End of input in tag."); break; case GUMBO_ERR_INCORRECTLY_CLOSED_COMMENT: print_message(output, "Comment closed incorrectly by '--!>', use '-->'."); break; case GUMBO_ERR_INCORRECTLY_OPENED_COMMENT: print_message(output, "Comment, DOCTYPE, or CDATA opened incorrectly, use '