Hypertext: Fix alignment tags adding unwanted newlines (#9548)

This commit is contained in:
Pierre-Yves Rollo 2020-03-26 19:56:35 +01:00 committed by GitHub
parent a0998754a7
commit 49a558574f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 10 deletions

@ -167,6 +167,7 @@ ParsedText::ParsedText(const wchar_t *text)
m_element = NULL; m_element = NULL;
m_paragraph = NULL; m_paragraph = NULL;
m_end_paragraph_reason = ER_NONE;
parse(text); parse(text);
} }
@ -191,7 +192,7 @@ void ParsedText::parse(const wchar_t *text)
cursor++; cursor++;
// If text has begun, don't skip empty line // If text has begun, don't skip empty line
if (m_paragraph) { if (m_paragraph) {
endParagraph(); endParagraph(ER_NEWLINE);
enterElement(ELEMENT_SEPARATOR); enterElement(ELEMENT_SEPARATOR);
} }
escape = false; escape = false;
@ -201,7 +202,7 @@ void ParsedText::parse(const wchar_t *text)
if (c == L'\n') { // Unix breaks if (c == L'\n') { // Unix breaks
// If text has begun, don't skip empty line // If text has begun, don't skip empty line
if (m_paragraph) { if (m_paragraph) {
endParagraph(); endParagraph(ER_NEWLINE);
enterElement(ELEMENT_SEPARATOR); enterElement(ELEMENT_SEPARATOR);
} }
escape = false; escape = false;
@ -232,7 +233,7 @@ void ParsedText::parse(const wchar_t *text)
pushChar(c); pushChar(c);
} }
endParagraph(); endParagraph(ER_NONE);
} }
void ParsedText::endElement() void ParsedText::endElement()
@ -240,11 +241,20 @@ void ParsedText::endElement()
m_element = NULL; m_element = NULL;
} }
void ParsedText::endParagraph() void ParsedText::endParagraph(EndReason reason)
{ {
if (!m_paragraph) if (!m_paragraph)
return; return;
EndReason previous = m_end_paragraph_reason;
m_end_paragraph_reason = reason;
if (m_empty_paragraph && (reason == ER_TAG ||
(reason == ER_NEWLINE && previous == ER_TAG))) {
// Ignore last empty paragraph
m_paragraph = nullptr;
m_paragraphs.pop_back();
return;
}
endElement(); endElement();
m_paragraph = NULL; m_paragraph = NULL;
} }
@ -255,6 +265,7 @@ void ParsedText::enterParagraph()
m_paragraphs.emplace_back(); m_paragraphs.emplace_back();
m_paragraph = &m_paragraphs.back(); m_paragraph = &m_paragraphs.back();
m_paragraph->setStyle(m_style); m_paragraph->setStyle(m_style);
m_empty_paragraph = true;
} }
} }
@ -274,11 +285,15 @@ void ParsedText::enterElement(ElementType type)
void ParsedText::pushChar(wchar_t c) void ParsedText::pushChar(wchar_t c)
{ {
// New word if needed // New word if needed
if (c == L' ' || c == L'\t') if (c == L' ' || c == L'\t') {
if (!m_empty_paragraph)
enterElement(ELEMENT_SEPARATOR); enterElement(ELEMENT_SEPARATOR);
else else
return;
} else {
m_empty_paragraph = false;
enterElement(ELEMENT_TEXT); enterElement(ELEMENT_TEXT);
}
m_element->text += c; m_element->text += c;
} }
@ -571,7 +586,7 @@ u32 ParsedText::parseTag(const wchar_t *text, u32 cursor)
} else { } else {
openTag(name, attrs)->style = m_paragraphtags[name]; openTag(name, attrs)->style = m_paragraphtags[name];
} }
endParagraph(); endParagraph(ER_TAG);
} else } else
return 0; // Unknown tag return 0; // Unknown tag

@ -134,11 +134,13 @@ public:
Tag m_root_tag; Tag m_root_tag;
protected: protected:
typedef enum { ER_NONE, ER_TAG, ER_NEWLINE } EndReason;
// Parser functions // Parser functions
void enterElement(ElementType type); void enterElement(ElementType type);
void endElement(); void endElement();
void enterParagraph(); void enterParagraph();
void endParagraph(); void endParagraph(EndReason reason);
void pushChar(wchar_t c); void pushChar(wchar_t c);
ParsedText::Tag *newTag(const std::string &name, const AttrsList &attrs); ParsedText::Tag *newTag(const std::string &name, const AttrsList &attrs);
ParsedText::Tag *openTag(const std::string &name, const AttrsList &attrs); ParsedText::Tag *openTag(const std::string &name, const AttrsList &attrs);
@ -160,6 +162,8 @@ protected:
StyleList m_style; StyleList m_style;
Element *m_element; Element *m_element;
Paragraph *m_paragraph; Paragraph *m_paragraph;
bool m_empty_paragraph;
EndReason m_end_paragraph_reason;
}; };
class TextDrawer class TextDrawer