Skip to content
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

class org.asciidoctor.jruby.ast.impl.CellImpl cannot be cast to class org.asciidoctor.ast.StructuralNode #1096

Closed
rahmanusta opened this issue Jun 28, 2022 · 5 comments · Fixed by #1099
Labels

Comments

@rahmanusta
Copy link

Hello,

I'm using the findBy API, and I'm getting the following error.

java.lang.ClassCastException: class org.asciidoctor.jruby.ast.impl.CellImpl cannot be cast to class org.asciidoctor.ast.StructuralNode (org.asciidoctor.jruby.ast.impl.CellImpl and org.asciidoctor.ast.StructuralNode are in unnamed module of loader 'app')
	at com.kodedu.service.extension.CacheAppendRemoverProcessor.process(CacheAppendRemoverProcessor.java:23)
	at org.asciidoctor.jruby.extension.processorproxies.TreeprocessorProxy.process(TreeprocessorProxy.java:95)
	at org.asciidoctor.jruby.extension.processorproxies.TreeprocessorProxy$INVOKER$i$1$0$process.call(TreeprocessorProxy$INVOKER$i$1$0$process.gen)
	at org.jruby.internal.runtime.methods.JavaMethod$JavaMethodOneOrN.call(JavaMethod.java:967)
	at org.jruby.RubyMethod.call(RubyMethod.java:119)

The processor that used below:

public class CacheAppendRemoverProcessor extends Treeprocessor {
    private final Map<Object, Object> SELECTOR = Map.of("traverse_documents", "true");

    @Override
    public Document process(Document document) {
        List<StructuralNode> nodes = document.findBy(SELECTOR);
        for (StructuralNode node : nodes) {
            if (node instanceof BlockImpl block) {
                if ("image".equals(block.getContext())) {
                    String target = (String) block.getAttribute("target");
                    if (Objects.nonNull(target)) {
                        String[] split = target.split("\\?cache");
                        if (split.length == 2) {
                            block.setAttribute("target", split[0], true);
                        }
                    }
                }
            }
        }
        return document;
    }

}

Is it a bug or do I miss something?

Thank you

@abelsromero
Copy link
Member

It seems there's a table in the doc that needs to be treated. Can you share a document to reproduce please?

@rahmanusta
Copy link
Author

I'm able to reproduce it when a doc have table. Example below.

Test.adoc

== Test


[width="100%",options="header,footer"]
|====================
| cell | cell | cell 
| cell | cell | cell 
| cell | cell | cell 
|====================

@abelsromero
Copy link
Member

abelsromero commented Jun 29, 2022

I see the issue is findBy supposed to return List<StructuralNode>, but it also returns the table cells whose class does not extend from that.
For now to be safe, you could cast the return to List<ContentNode> instead and treat each node accordingly.

@robertpanzer
Copy link
Member

I wonder why we have made Cell a ContentNode instead of a StructuralNode.
In Asciidoctor Ruby a Cell is an AbstractBlock just like a "normal" Block or a Table.
I'll play with making it a subclass of a StructuralNode.

robertpanzer added a commit to robertpanzer/asciidoctorj that referenced this issue Jul 23, 2022
robertpanzer added a commit to robertpanzer/asciidoctorj that referenced this issue Jul 24, 2022
robertpanzer added a commit that referenced this issue Jul 26, 2022
Fixes #1096. Make Cell extend StructuralNode instead of ContentNode.
robertpanzer added a commit that referenced this issue Jul 26, 2022
…uralnode

v2.5.x: Fixes #1096. Make Cell extend StructuralNode instead of ContentNode.
@robertpanzer
Copy link
Member

@rahmanusta I just published AsciidoctorJ 2.5.5 that should fix this bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants