Skip to content

Fix #413 parse entities positioning #414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

unintended
Copy link
Contributor

Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context.

Fixes #413

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

@JrooTJunior
Copy link
Member

LGTM

Can you also make backport to dev-3.x branch with adding corresponding test that prove you changes?

And that is something strange. When i check entity length after adding MarkdownV2 i didn't see the need to use utf-16-le and then i remove it. So as i an see in your description i was wrong... Thanks

@JrooTJunior JrooTJunior added the bug Something is wrong with the framework label Sep 4, 2020
@unintended
Copy link
Contributor Author

I updated the code: added tests, refactored encoding to methods, but didn't manage to put surrogates methods inside _unparse_entities. Maybe someone will try to make make it one day :)
Also tried to port to dev-3.x, but with no luck yet (couldn't port the tests properly becoase of different nature of api classes) and I don't have much time to do it, I'm sorry.

@JrooTJunior
Copy link
Member

in dev-3.x you can add test-case to parameterized test of decorations:

@pytest.mark.parametrize(
"decorator,text,entities,result",
[
[html_decoration, "test", None, "test"],
[html_decoration, "test", [], "test"],
[
html_decoration,
"test1 test2 test3 test4 test5 test6 test7",
[
MessageEntity(type="bold", offset=6, length=29),
MessageEntity(type="underline", offset=12, length=5),
MessageEntity(type="italic", offset=24, length=5),
],
"test1 <b>test2 <u>test3</u> test4 <i>test5</i> test6</b> test7",
],
[
html_decoration,
"test1 test2 test3 test4 test5",
[
MessageEntity(type="bold", offset=6, length=17),
MessageEntity(type="underline", offset=12, length=5),
],
"test1 <b>test2 <u>test3</u> test4</b> test5",
],
[
html_decoration,
"test1 test2 test3 test4",
[
MessageEntity(type="bold", offset=6, length=11),
MessageEntity(type="underline", offset=12, length=5),
],
"test1 <b>test2 <u>test3</u></b> test4",
],
[
html_decoration,
"test1 test2 test3",
[MessageEntity(type="bold", offset=6, length=5)],
"test1 <b>test2</b> test3",
],
[
html_decoration,
"test1 test2",
[MessageEntity(type="bold", offset=0, length=5)],
"<b>test1</b> test2",
],
[
html_decoration,
"strike bold",
[
MessageEntity(type="strikethrough", offset=0, length=6),
MessageEntity(type="bold", offset=7, length=4),
],
"<s>strike</s> <b>bold</b>",
],
[
html_decoration,
"test",
[
MessageEntity(type="strikethrough", offset=0, length=5),
MessageEntity(type="bold", offset=0, length=5),
],
"<s><b>test</b></s>",
],
[
html_decoration,
"strikeboldunder",
[
MessageEntity(type="strikethrough", offset=0, length=15),
MessageEntity(type="bold", offset=6, length=9),
MessageEntity(type="underline", offset=10, length=5),
],
"<s>strike<b>bold<u>under</u></b></s>",
],
[
html_decoration,
"@username",
[
MessageEntity(type="mention", offset=0, length=9),
MessageEntity(type="bold", offset=0, length=9),
],
"<b>@username</b>",
],
[
html_decoration,
"/command",
[
MessageEntity(type="bot_command", offset=0, length=8),
MessageEntity(type="bold", offset=0, length=8),
],
"<b>/command</b>",
],
[
html_decoration,
"+1-212-555-0123",
[
MessageEntity(type="phone_number", offset=0, length=15),
MessageEntity(type="bold", offset=0, length=15),
],
"<b>+1-212-555-0123</b>",
],
[
html_decoration,
"test te👍🏿st test",
[MessageEntity(type="bold", offset=5, length=6, url=None, user=None)],
"test <b>te👍🏿st</b> test",
],
[
html_decoration,
"👋🏾 Hi!",
[MessageEntity(type="bold", offset=0, length=8, url=None, user=None)],
"<b>👋🏾 Hi!</b>",
],
],
)
def test_unparse(
self,
decorator: TextDecoration,
text: str,
entities: Optional[List[MessageEntity]],
result: str,
):
assert decorator.unparse(text, entities) == result

and at the end of list add item:

[
    markdown_decoration,
    "🚀 i'm bold and italic and still bold",
    [
        MessageEntity(offset=3, length=34, type="bold"),
        MessageEntity(offset=12, length=10, type="italic"),
    ],
    "🚀 *i'm bold _and italic_\r and still bold*"
]

@JrooTJunior JrooTJunior merged commit 17eb8a5 into aiogram:dev-2.x Sep 4, 2020
@JrooTJunior
Copy link
Member

Thanks for contribution 😉

uburuntu pushed a commit to uburuntu/aiogram that referenced this pull request Oct 4, 2020
* fix entity positioning in parse_entities() aiogram#413
* add tests and small fixes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is wrong with the framework
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Broken text length and entity positions for a message that contain text with emojis
2 participants