LibMarkdown: Don't parse lines starting with a space as heading

This fixes a bug where lines starting with a space would get parsed as
"level 0" headings - it would not find a "#" and therefore never
increase the level counter (starting at zero), which then would cause
the check for "space after #" pass (again, there is no "#").

Eventually we would get funny results like this:

    <h0>[n-1 spaces]oops!</h0>

Also ASSERT(level > 0) in the Heading constructor.
This commit is contained in:
Linus Groh 2020-10-26 17:44:43 +00:00 committed by Andreas Kling
parent 63dcd59fa5
commit 9735879318
Notes: sideshowbarker 2024-07-19 01:42:10 +09:00
2 changed files with 4 additions and 2 deletions

View File

@ -67,11 +67,12 @@ OwnPtr<Heading> Heading::parse(Vector<StringView>::ConstIterator& lines)
const StringView& line = *lines;
size_t level;
for (level = 0; level < line.length(); level++)
for (level = 0; level < line.length(); level++) {
if (line[level] != '#')
break;
}
if (level >= line.length() || line[level] != ' ')
if (!level || level >= line.length() || line[level] != ' ')
return nullptr;
StringView title_view = line.substring_view(level + 1, line.length() - level - 1);

View File

@ -40,6 +40,7 @@ public:
: m_text(move(text))
, m_level(level)
{
ASSERT(m_level > 0);
}
virtual ~Heading() override { }