Skip to content

Commit 49a5585

Browse files
authoredMar 26, 2020
Hypertext: Fix alignment tags adding unwanted newlines (#9548)
1 parent a099875 commit 49a5585

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed
 

Diff for: ‎src/gui/guiHyperText.cpp

+24-9
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ ParsedText::ParsedText(const wchar_t *text)
167167

168168
m_element = NULL;
169169
m_paragraph = NULL;
170+
m_end_paragraph_reason = ER_NONE;
170171

171172
parse(text);
172173
}
@@ -191,7 +192,7 @@ void ParsedText::parse(const wchar_t *text)
191192
cursor++;
192193
// If text has begun, don't skip empty line
193194
if (m_paragraph) {
194-
endParagraph();
195+
endParagraph(ER_NEWLINE);
195196
enterElement(ELEMENT_SEPARATOR);
196197
}
197198
escape = false;
@@ -201,7 +202,7 @@ void ParsedText::parse(const wchar_t *text)
201202
if (c == L'\n') { // Unix breaks
202203
// If text has begun, don't skip empty line
203204
if (m_paragraph) {
204-
endParagraph();
205+
endParagraph(ER_NEWLINE);
205206
enterElement(ELEMENT_SEPARATOR);
206207
}
207208
escape = false;
@@ -232,19 +233,28 @@ void ParsedText::parse(const wchar_t *text)
232233
pushChar(c);
233234
}
234235

235-
endParagraph();
236+
endParagraph(ER_NONE);
236237
}
237238

238239
void ParsedText::endElement()
239240
{
240241
m_element = NULL;
241242
}
242243

243-
void ParsedText::endParagraph()
244+
void ParsedText::endParagraph(EndReason reason)
244245
{
245246
if (!m_paragraph)
246247
return;
247248

249+
EndReason previous = m_end_paragraph_reason;
250+
m_end_paragraph_reason = reason;
251+
if (m_empty_paragraph && (reason == ER_TAG ||
252+
(reason == ER_NEWLINE && previous == ER_TAG))) {
253+
// Ignore last empty paragraph
254+
m_paragraph = nullptr;
255+
m_paragraphs.pop_back();
256+
return;
257+
}
248258
endElement();
249259
m_paragraph = NULL;
250260
}
@@ -255,6 +265,7 @@ void ParsedText::enterParagraph()
255265
m_paragraphs.emplace_back();
256266
m_paragraph = &m_paragraphs.back();
257267
m_paragraph->setStyle(m_style);
268+
m_empty_paragraph = true;
258269
}
259270
}
260271

@@ -274,11 +285,15 @@ void ParsedText::enterElement(ElementType type)
274285
void ParsedText::pushChar(wchar_t c)
275286
{
276287
// New word if needed
277-
if (c == L' ' || c == L'\t')
278-
enterElement(ELEMENT_SEPARATOR);
279-
else
288+
if (c == L' ' || c == L'\t') {
289+
if (!m_empty_paragraph)
290+
enterElement(ELEMENT_SEPARATOR);
291+
else
292+
return;
293+
} else {
294+
m_empty_paragraph = false;
280295
enterElement(ELEMENT_TEXT);
281-
296+
}
282297
m_element->text += c;
283298
}
284299

@@ -571,7 +586,7 @@ u32 ParsedText::parseTag(const wchar_t *text, u32 cursor)
571586
} else {
572587
openTag(name, attrs)->style = m_paragraphtags[name];
573588
}
574-
endParagraph();
589+
endParagraph(ER_TAG);
575590

576591
} else
577592
return 0; // Unknown tag

Diff for: ‎src/gui/guiHyperText.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,13 @@ class ParsedText
134134
Tag m_root_tag;
135135

136136
protected:
137+
typedef enum { ER_NONE, ER_TAG, ER_NEWLINE } EndReason;
138+
137139
// Parser functions
138140
void enterElement(ElementType type);
139141
void endElement();
140142
void enterParagraph();
141-
void endParagraph();
143+
void endParagraph(EndReason reason);
142144
void pushChar(wchar_t c);
143145
ParsedText::Tag *newTag(const std::string &name, const AttrsList &attrs);
144146
ParsedText::Tag *openTag(const std::string &name, const AttrsList &attrs);
@@ -160,6 +162,8 @@ class ParsedText
160162
StyleList m_style;
161163
Element *m_element;
162164
Paragraph *m_paragraph;
165+
bool m_empty_paragraph;
166+
EndReason m_end_paragraph_reason;
163167
};
164168

165169
class TextDrawer

0 commit comments

Comments
 (0)
Please sign in to comment.