mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:57:35 +00:00
LibMarkdown: Handle broken link markup better
Let's output *something* instead of crashing on a failed assertion.
This commit is contained in:
parent
279cf9294a
commit
b8aab5fdc3
1 changed files with 20 additions and 7 deletions
|
@ -24,6 +24,7 @@
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <AK/ScopeGuard.h>
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
#include <LibMarkdown/Text.h>
|
#include <LibMarkdown/Text.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -158,12 +159,14 @@ bool Text::parse(const StringView& str)
|
||||||
int first_span_in_the_current_link = -1;
|
int first_span_in_the_current_link = -1;
|
||||||
|
|
||||||
auto append_span_if_needed = [&](size_t offset) {
|
auto append_span_if_needed = [&](size_t offset) {
|
||||||
|
ASSERT(current_span_start <= offset);
|
||||||
if (current_span_start != offset) {
|
if (current_span_start != offset) {
|
||||||
Span span {
|
Span span {
|
||||||
unescape(str.substring_view(current_span_start, offset - current_span_start)),
|
unescape(str.substring_view(current_span_start, offset - current_span_start)),
|
||||||
current_style
|
current_style
|
||||||
};
|
};
|
||||||
m_spans.append(move(span));
|
m_spans.append(move(span));
|
||||||
|
current_span_start = offset;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -199,31 +202,41 @@ bool Text::parse(const StringView& str)
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case '[':
|
case '[':
|
||||||
ASSERT(first_span_in_the_current_link == -1);
|
if (first_span_in_the_current_link != -1)
|
||||||
|
dbg() << "Dropping the outer link";
|
||||||
first_span_in_the_current_link = m_spans.size();
|
first_span_in_the_current_link = m_spans.size();
|
||||||
break;
|
break;
|
||||||
case ']': {
|
case ']': {
|
||||||
ASSERT(first_span_in_the_current_link != -1);
|
if (first_span_in_the_current_link == -1) {
|
||||||
ASSERT(offset + 2 < str.length());
|
dbg() << "Unmatched ]";
|
||||||
offset++;
|
continue;
|
||||||
ASSERT(str[offset] == '(');
|
}
|
||||||
offset++;
|
ScopeGuard guard = [&] {
|
||||||
|
first_span_in_the_current_link = -1;
|
||||||
|
};
|
||||||
|
if (offset + 2 >= str.length() || str[offset + 1] != '(')
|
||||||
|
continue;
|
||||||
|
offset += 2;
|
||||||
size_t start_of_href = offset;
|
size_t start_of_href = offset;
|
||||||
|
|
||||||
do
|
do
|
||||||
offset++;
|
offset++;
|
||||||
while (offset < str.length() && str[offset] != ')');
|
while (offset < str.length() && str[offset] != ')');
|
||||||
|
if (offset == str.length())
|
||||||
|
offset--;
|
||||||
|
|
||||||
const StringView href = str.substring_view(start_of_href, offset - start_of_href);
|
const StringView href = str.substring_view(start_of_href, offset - start_of_href);
|
||||||
for (size_t i = first_span_in_the_current_link; i < m_spans.size(); i++)
|
for (size_t i = first_span_in_the_current_link; i < m_spans.size(); i++)
|
||||||
m_spans[i].style.href = href;
|
m_spans[i].style.href = href;
|
||||||
first_span_in_the_current_link = -1;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We've processed the character as a special, so the next offset will
|
||||||
|
// start after it. Note that explicit continue statements skip over this
|
||||||
|
// line, effectively treating the character as not special.
|
||||||
current_span_start = offset + 1;
|
current_span_start = offset + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue