Skip to content

Avoid ValueError on Unicode-numeric start/colspan attributes#269

Open
santhreal wants to merge 1 commit into
matthewwithanm:developfrom
santhreal:fix/int-attr-unicode-numeric-valueerror
Open

Avoid ValueError on Unicode-numeric start/colspan attributes#269
santhreal wants to merge 1 commit into
matthewwithanm:developfrom
santhreal:fix/int-attr-unicode-numeric-valueerror

Conversation

@santhreal

Copy link
Copy Markdown

markdownify('<ol start="²"><li>a</li></ol>') raises ValueError: invalid literal for int() with base 10: '²'. The same happens for a colspan attribute, e.g. <td colspan="²"> or <td colspan="②">.

The list-start guard uses str.isnumeric() and the colspan guards use str.isdigit(). Both return True for Unicode numeric characters (superscripts, fractions, CJK numerals, roman numerals, circled digits) that the following int() then rejects, so the whole conversion crashes on valid HTML where a browser would just fall back to the default.

str.isdecimal() is True for exactly the characters int() accepts (verified across the full Unicode range), so switching the guards to it removes the crash while keeping every currently-working value identical, including the existing "foo" / "-1" / "1.5" / "undefined" fallbacks.

Repro:

from markdownify import markdownify as md
md('<ol start="²"><li>a</li></ol>')                       # ValueError
md('<table><tr><td colspan="②">x</td></tr></table>')       # ValueError

Extended test_ol and test_table with the Unicode-attribute cases.

`<ol start="²">` and `<td colspan="②">` raised ValueError from the public
markdownify() API. The guards used str.isnumeric()/str.isdigit(), which
accept Unicode numerics (superscripts, fractions, CJK, roman, circled
digits) that int() then rejects. Use str.isdecimal(), which accepts exactly
what int() parses, so these values fall back to the default (start=1,
colspan=1) as browsers do, matching the existing behavior for "foo"/"1.5".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant