Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ba39198
Report unclosed script/style/title tail as text at EOF
oalders Jul 27, 2026
647bd3b
Fix marked section state at end of document
pjcj Jul 28, 2026
d75539f
Recognise TEMP as a marked section keyword
pjcj Jul 28, 2026
0c4de42
Close marked sections with unknown keywords
pjcj Jul 28, 2026
2d6aa8e
Document marked section status keywords
pjcj Jul 28, 2026
38d14a2
Clear eof flag when a handler stops the parse
pjcj Jul 28, 2026
b5c1922
Take only the encoding from an unclosed title
pjcj Jul 28, 2026
62dff0f
Report no empty text event at ]]>
pjcj Jul 28, 2026
7122447
Close literal elements at end tags with attributes
pjcj Jul 28, 2026
0f64acf
Emit the implicit end when nothing is buffered
pjcj Jul 28, 2026
3bf2b15
Flush the pending end tag before ignore cleanup
pjcj Jul 28, 2026
46865c6
Guard the parser object during the eof flush
pjcj Jul 28, 2026
29bfa3c
Emit the implicit end for every literal element
pjcj Jul 28, 2026
8a85ddc
Simplify the literal branch of the EOF flush
pjcj Jul 28, 2026
7cb7215
Assert the full event list for NUL literal cases
pjcj Jul 28, 2026
0ff5d9e
Defer the literal end inside marked sections
pjcj Jul 28, 2026
119894b
Extract the synthetic end report into a helper
pjcj Jul 30, 2026
739cc67
Keep the start tag's case in the implicit end
pjcj Jul 30, 2026
ded6c21
Seek only the terminator in a marked section tail
pjcj Jul 30, 2026
836b6bc
Say the encoding scan reads only a title's text
pjcj Jul 30, 2026
103c0a6
Let the title text scan set only the encoding
pjcj Jul 30, 2026
dde8b67
Document the implicit end of literal elements
pjcj Aug 4, 2026
dbefb57
Condense the Changes entries
pjcj Aug 4, 2026
4928db3
Reset the relaxed comment-end state at end of document
oalders Sep 1, 2026
99b5449
Copy the literal element name when cloning a parser
oalders Sep 1, 2026
116521c
Guard the literal element name copy at runtime
oalders Sep 1, 2026
dab4520
Clarify the title encoding scan and the marked-section end
oalders Sep 1, 2026
0af567b
Fix a wording slip in the marked-section end comment
oalders Sep 1, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
Change history for HTML-Parser

{{$NEXT}}
- Report the remaining bytes of an unclosed literal element (script,
style, title, xmp, iframe, textarea) at the end of a document as
the element's raw text, as browsers do, instead of re-parsing them
as markup. Previously a close tag broken by an embedded NUL, or a
plainly unclosed element, could expose the following tags as
elements.
- Fix the implicit end event of an unclosed literal element. Report
it after the element's text, for xmp, iframe and textarea too
(plaintext has no end tag and gets none), with the start tag's
name as written under case_sensitive, and not at all inside an
element that ignore_elements is skipping.
- Close a literal element at an end tag that carries attributes or a
slash, for example </script foo=bar>, as browsers do. Enable
strict_end for the old behaviour.
- Fix marked sections:
- Recognise TEMP as a status keyword and close a section whose
keywords are all unrecognised, instead of leaving the ]]> in
the document text.
- Stop reporting a zero-length text event at the ]]> of an
INCLUDE section.
- Clear marked section state at EOF, so that a later document is
no longer parsed as if the section were still open.
- Clear the eof flag when a handler stops the parse from inside
eof(), so that the next document parsed by the same object is no
longer discarded.
- Reset the relaxed comment-end state at end of document, so that a
document with unterminated markup no longer changes how comments
in a later document parsed by the same object are terminated.
- Avoid a crash when a handler called during eof() drops the last
reference to the parser.
- Recover only the character encoding from a <meta> inside a title
that is never closed, and set no other header from it. See
ENCODING DECLARATIONS in HTML::HeadParser.
- Copy the open literal element's name when cloning a parser across
ithreads, so that a clone made while such an element is open no
longer reports its implicit end with an empty name.

3.85 2026-05-19
- Replace deprecated uvuni_to_utf8() with uvchr_to_utf8() (GH#50)
Expand Down
4 changes: 4 additions & 0 deletions Parser.xs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ dup_pstate(pTHX_ PSTATE *pstate, CLONE_PARAMS *params)
pstate2->eof = pstate->eof;

pstate2->literal_mode = pstate->literal_mode;
Copy(pstate->literal_mode_name, pstate2->literal_mode_name,
sizeof(pstate->literal_mode_name), char);
pstate2->is_cdata = pstate->is_cdata;
pstate2->no_dash_dash_comment_end = pstate->no_dash_dash_comment_end;
pstate2->pending_end_tag = pstate->pending_end_tag;
Expand Down Expand Up @@ -329,13 +331,15 @@ eof(self)
PREINIT:
PSTATE* p_state = get_pstate_hv(aTHX_ self);
PPCODE:
(void)sv_2mortal(SvREFCNT_inc(SvRV(self)));
if (p_state->parsing)
p_state->eof = 1;
else {
p_state->parsing = 1;
parse(aTHX_ p_state, 0, self); /* flush */
SPAGAIN;
p_state->parsing = 0;
p_state->eof = 0;
}
PUSHs(self);

Expand Down
147 changes: 119 additions & 28 deletions hparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ static const char * const argname[] = {
((p_state)->xml_mode || (p_state)->empty_element_tags)

static void flush_pending_text(PSTATE* p_state, SV* self);
static void report_synthetic_end(PSTATE* p_state, char *name, SV* self);

/*
* Parser functions.
Expand Down Expand Up @@ -182,12 +183,9 @@ report_event(PSTATE* p_state,
#endif

if (p_state->pending_end_tag && event != E_TEXT && event != E_COMMENT) {
token_pos_t t;
char dummy;
t.beg = p_state->pending_end_tag;
t.end = p_state->pending_end_tag + strlen(p_state->pending_end_tag);
char *name = p_state->pending_end_tag;
p_state->pending_end_tag = 0;
report_event(p_state, E_END, &dummy, &dummy, 0, &t, 1, self);
report_synthetic_end(p_state, name, self);
SPAGAIN;
}

Expand Down Expand Up @@ -656,6 +654,17 @@ report_event(PSTATE* p_state,
}


static void
report_synthetic_end(PSTATE* p_state, char *name, SV* self)
{
token_pos_t t;
char dummy;
t.beg = name;
t.end = name + strlen(name);
report_event(p_state, E_END, &dummy, &dummy, 0, &t, 1, self);
}


EXTERN SV*
argspec_compile(SV* src, PSTATE* p_state)
{
Expand Down Expand Up @@ -968,7 +977,8 @@ marked_section_update(PSTATE* p_state)
STRLEN len;
char *token_str = SvPV(*svp, len);
enum marked_section_t token;
if (strEQ(token_str, "include"))
if (strEQ(token_str, "include") ||
strEQ(token_str, "temp"))
token = MS_INCLUDE;
else if (strEQ(token_str, "rcdata"))
token = MS_RCDATA;
Expand All @@ -984,6 +994,10 @@ marked_section_update(PSTATE* p_state)
}
}
}
/* Treat unrecognised keywords as INCLUDE in the same way that
* <![[ does. */
if (p_state->ms == MS_NONE && av_len(ms_stack) >= 0)
p_state->ms = MS_INCLUDE;
}
/* printf("MS %d\n", p_state->ms); */
p_state->is_cdata = (p_state->ms == MS_CDATA);
Expand Down Expand Up @@ -1398,6 +1412,19 @@ parse_start(PSTATE* p_state, char *beg, char *end, U32 utf8, SV* self)
if (!--len) {
/* found it */
p_state->literal_mode = literal_mode_elem[i].str;
/* tag_len equals this element's name length, so it
* always fits; guard at runtime too, in case a
* longer literal element name is ever added, so a
* release build (NDEBUG drops the assert) cannot
* overflow the fixed buffer. */
assert(tag_len <
(int)sizeof(p_state->literal_mode_name));
if (tag_len >=
(int)sizeof(p_state->literal_mode_name))
croak("literal element name too long");
Copy(tokens[0].beg, p_state->literal_mode_name,
tag_len, char);
p_state->literal_mode_name[tag_len] = '\0';
p_state->is_cdata = literal_mode_elem[i].is_cdata;
/* printf("Found %s\n", p_state->literal_mode); */
goto END_OF_LITERAL_SEARCH;
Expand Down Expand Up @@ -1524,6 +1551,20 @@ parse_null(PSTATE* p_state, char *beg, char *end, U32 utf8, SV* self)
#include "pfunc.h" /* declares the parsefunc[] */
#endif /* USE_PFUNC */

static void
report_literal_end(PSTATE* p_state, SV* self)
{
if (p_state->is_cdata) {
/* effectively make it an empty element */
report_synthetic_end(p_state, p_state->literal_mode_name, self);
}
else {
p_state->pending_end_tag = p_state->literal_mode_name;
}
p_state->literal_mode = 0;
p_state->is_cdata = 0;
}

static char*
parse_buf(pTHX_ PSTATE* p_state, char *beg, char *end, U32 utf8, SV* self)
{
Expand Down Expand Up @@ -1565,13 +1606,21 @@ parse_buf(pTHX_ PSTATE* p_state, char *beg, char *end, U32 utf8, SV* self)
if (!*l && (strNE(p_state->literal_mode, "plaintext") || p_state->closing_plaintext)) {
/* matched it all */
token_pos_t end_token;
char *gt = end;
end_token.beg = end_text + 2;
end_token.end = s;

while (isHSPACE(*s))
s++;
if (*s == '>') {
s++;
if (p_state->strict_end) {
while (isHSPACE(*s))
s++;
if (*s == '>')
gt = s;
}
else if (isHSPACE(*s) || *s == '>' || *s == '/') {
gt = skip_until_gt(s, end);
}
if (gt < end) {
s = gt + 1;
if (t != end_text)
report_event(p_state, E_TEXT, t, end_text, utf8,
0, 0, self);
Expand Down Expand Up @@ -1622,8 +1671,9 @@ parse_buf(pTHX_ PSTATE* p_state, char *beg, char *end, U32 utf8, SV* self)
s++;
if (*s == '>') {
s++;
report_event(p_state, E_TEXT, t, end_text, utf8,
0, 0, self);
if (t != end_text)
report_event(p_state, E_TEXT, t, end_text, utf8,
0, 0, self);
report_event(p_state, E_NONE, end_text, s, utf8,
0, 0, self);
t = s;
Expand Down Expand Up @@ -1723,7 +1773,7 @@ parse(pTHX_
/* eof */
char empty[1];
if (p_state->buf && SvOK(p_state->buf)) {
/* flush it */
/* flush s..end, the buffered text not yet parsed */
s = SvPV(p_state->buf, len);
end = s + len;
utf8 = SvUTF8(p_state->buf);
Expand All @@ -1739,22 +1789,43 @@ parse(pTHX_
/* rest is considered text */
break;
}
if (strEQ(p_state->literal_mode, "script") ||
strEQ(p_state->literal_mode, "style"))
{
/* effectively make it an empty element */
token_pos_t t;
char dummy;
t.beg = p_state->literal_mode;
t.end = p_state->literal_mode + strlen(p_state->literal_mode);
report_event(p_state, E_END, &dummy, &dummy, 0, &t, 1, self);
}
else {
p_state->pending_end_tag = p_state->literal_mode;
/* Unclosed script/style/title at end of document. A browser

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[general] Important — pre-existing EOF asymmetry across literal-mode elements. This fix gives script/style an implicit close event and title a pending_end_tag close at EOF, but xmp/iframe/textarea/plaintext hit the early break just above and fall through to the generic "rest is text" path with no synthesized end event — the element is left open when literal_mode/is_cdata are zeroed during state reset (verified: <textarea>foo at EOF produces a start but no matching end). This is not a regression from this PR (those four elements were already safe from the tail-re-parse bug, and the Changes entry correctly lists only script/style/title), but the resulting asymmetry is worth an explicit maintainer decision: either accept it as documented behaviour or extend end-event synthesis to the other four in a follow-up.

* keeps the remaining bytes as the element's raw text and
* closes it implicitly. It does not re-parse them looking
* for markup. Re-parsing here would expose tags that follow a
* close tag broken by (e.g.) an embedded NUL, diverging from
* every browser. */
#ifdef MARKED_SECTION
/* Only a marked section terminator in these bytes is
* structure rather than literal text, so seek it instead
* of re-parsing the remainder for markup. */
if (p_state->ms_stack && av_len(p_state->ms_stack) >= 0) {
char *t = s;
while (t + 3 <= end &&
!(t[0] == ']' && t[1] == ']' && t[2] == '>'))
t++;
if (t + 3 <= end) {
/* the implicit end follows the element's text */
if (t != s)
report_event(p_state, E_TEXT, s, t, utf8,
0, 0, self);
/* Report the end directly rather than via
* report_literal_end: a marked section can force
* is_cdata on even for title, and the ]]> below is
* closed by the nesting-aware parse_buf, so the
* deferred-end path report_literal_end takes for a
* non-cdata element must not run here. */
report_synthetic_end(p_state,
p_state->literal_mode_name,
self);
p_state->literal_mode = 0;
p_state->is_cdata = 0;
s = parse_buf(aTHX_ p_state, t, end, utf8, self);
continue;
}
}
p_state->literal_mode = 0;
s = parse_buf(aTHX_ p_state, s, end, utf8, self);
continue;
#endif
break;
}

if (!p_state->strict_comment && !p_state->no_dash_dash_comment_end && *s == '<') {
Expand Down Expand Up @@ -1786,9 +1857,22 @@ parse(pTHX_
SvREFCNT_dec(p_state->buf);
p_state->buf = 0;
}
if (p_state->literal_mode &&
strNE(p_state->literal_mode, "plaintext"))
{
/* the unclosed element still ends, even with nothing buffered */
report_literal_end(p_state, self);
}
if (p_state->pend_text && SvOK(p_state->pend_text))
flush_pending_text(p_state, self);

if (p_state->pending_end_tag) {
/* flush while the ignore state below still applies */
char *name = p_state->pending_end_tag;
p_state->pending_end_tag = 0;
report_synthetic_end(p_state, name, self);
}

if (p_state->ignoring_element) {
/* document not balanced */
SvREFCNT_dec(p_state->ignoring_element);
Expand All @@ -1804,6 +1888,13 @@ parse(pTHX_
p_state->start_document = 0;
p_state->literal_mode = 0;
p_state->is_cdata = 0;
p_state->pending_end_tag = 0;
p_state->no_dash_dash_comment_end = 0;
#ifdef MARKED_SECTION
if (p_state->ms_stack)
av_clear(p_state->ms_stack);
marked_section_update(p_state);
#endif
return;
}

Expand Down
1 change: 1 addition & 0 deletions hparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ struct p_state {

/* special parsing modes */
char* literal_mode;
char literal_mode_name[10]; /* the start tag's name as written */
bool is_cdata;
bool no_dash_dash_comment_end;
char *pending_end_tag;
Expand Down
Loading