Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@abdnh
Copy link
Collaborator

@abdnh abdnh commented Jun 27, 2024

Closes #3189

The source of the issue appears to be encode_attribute(). Commenting out extract_av_tags() doesn't seem to make a difference, so I guess no change is required there.

@abdnh
Copy link
Collaborator Author

abdnh commented Jun 27, 2024

This error is printed with deeply nested clozes:

Traceback (most recent call last):
  File "D:\dev\anki\repo\qt\aqt\main.py", line 276, in on_focus_changed
    gui_hooks.focus_did_change(new, old)
  File "D:\dev\anki\repo\out/qt\_aqt\hooks.py", line 3460, in __call__
    hook(new, old)
  File "D:\dev\anki\repo\qt\aqt\main.py", line 832, in on_focus_did_change
    self.reviewer.refresh_if_needed()
  File "D:\dev\anki\repo\qt\aqt\reviewer.py", line 204, in refresh_if_needed
    self._redraw_current_card()
  File "D:\dev\anki\repo\qt\aqt\reviewer.py", line 237, in _redraw_current_card
    self._showQuestion()
  File "D:\dev\anki\repo\qt\aqt\reviewer.py", line 373, in _showQuestion
    q = c.question()
  File "D:\dev\anki\repo\pylib\anki\cards.py", line 139, in question
    return self.render_output(reload, browser).question_and_style()
  File "D:\dev\anki\repo\pylib\anki\cards.py", line 155, in render_output
    anki.template.TemplateRenderContext.from_existing_card(
  File "D:\dev\anki\repo\pylib\anki\template.py", line 225, in render
    partial = self._partially_render()
  File "D:\dev\anki\repo\pylib\anki\template.py", line 272, in _partially_render
    out = self._col._backend.render_existing_card(
  File "D:\dev\anki\repo\out/pylib\anki\_backend_generated.py", line 1449, in render_existing_card
    output.ParseFromString(raw_bytes)
google.protobuf.message.DecodeError: Error parsing message

I think the issue can still occur with less than 11 levels but not sure whether there's a better solution.

@dae
Copy link
Member

dae commented Jun 28, 2024

I had an alternative idea:

diff --git a/rslib/src/cloze.rs b/rslib/src/cloze.rs
index 32b2c4921..4c78fbad3 100644
--- a/rslib/src/cloze.rs
+++ b/rslib/src/cloze.rs
@@ -161,11 +161,15 @@ fn parse_text_with_clozes(text: &str) -> Vec<TextOrCloze<'_>> {
     let mut output = vec![];
     for token in tokenize(text) {
         match token {
-            Token::OpenCloze(ordinal) => open_clozes.push(ExtractedCloze {
-                ordinal,
-                nodes: Vec::with_capacity(1), // common case
-                hint: None,
-            }),
+            Token::OpenCloze(ordinal) => {
+                if open_clozes.len() < 3 {
+                    open_clozes.push(ExtractedCloze {
+                        ordinal,
+                        nodes: Vec::with_capacity(1), // common case
+                        hint: None,
+                    })
+                }
+            }
             Token::Text(mut text) => {
                 if let Some(cloze) = open_clozes.last_mut() {
                     // extract hint if found

Because the limiting is done earlier and is at a lower limit, I'm hoping this will address the protobuf error you're getting (presumably we're serialising a deeply-nested list). It also makes it more obvious when the limit has been exceeded (by breaking cloze rendering), instead of only failing to encode entities. WDYT?

@dae
Copy link
Member

dae commented Jul 10, 2024

Thanks Abdo!

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.

Nested clozes cause exponentially growing html

2 participants