Skip to content

Convert to Blocks: HTML Attributes are not copied to the DOM Node within callbacks of custom transforms #8648

@AndiDittrich

Description

@AndiDittrich

Dear Gutenberg Team,

the "Convert to Blocks" feature (content parser) seems to ignore element attributes on raw DOM Nodes.
By converting "RAW" blocks by custom transform rules, no attributes are available within the isMatch(node) callback even if they are defined in the legacy html sources.


Example Code to reproduce the issue

Plugin

wp.blocks.registerBlockType('test/myblock', {

    // block element transformation/converting
    transforms: {
        from: [
        {
            type: 'raw',
            priority: 4,
            isMatch: function isMatch(node) {
                // ALL kind of attributes/classes are MISSING
                console.log("classes: ", node.classList.length);
                console.log("attributes: ", node.attributes.length);
                return false;
            },
            transform: function transform(node) {
                // some custom transform rule
                return wp.blocks.createBlock('core/codeblock', { content: node.textContent });
            }
        }
        ]
    }
})

Editor Content

<pre class="EnlighterJSRAW" data-enlighter-language="php">
define('ENLIGHTER_GUTENBERG_PLUGIN_PATH', dirname(__FILE__));
define('ENLIGHTER_GUTENBERG_PLUGIN_URL', plugins_url('/enlighter-gutenberg/'));
</pre>

<div class="echo">lalala</div>

Current Behaviour

Attributes are ignored by the parser and not copied to the node passed to the transform callbacks isMatch(node) / transform(node`)

Console Output
gutenberg_transform_attribute_bug


Expected Behaviour

ALL attributes should be copied to the node (and become available in isMatch(node) + transform(node) to apply custom transform rules.

This is absolutely required to convert legacy addons to blocks (lot of them are based on custom classes)

best regards, Andi

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs Technical FeedbackNeeds testing from a developer perspective.[Type] BugAn existing feature does not function as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions