diff --git a/grammar/asciidoc-block.pegjs b/grammar/asciidoc-block.pegjs index ac54f2e..bfbbcf5 100644 --- a/grammar/asciidoc-block.pegjs +++ b/grammar/asciidoc-block.pegjs @@ -95,14 +95,17 @@ function processBlockMetadata (cacheKey = offset(), posattrs, contentAttributeNa let posIdx = 0 for (const name of posattrs) { const posKey = `$${++posIdx}` - if (name == null) continue - if (!(posKey in attributes)) continue - // Q: should existing named attribute be allowed to take precedence? (this has never been the case) - if (name in attributes) names.splice(names.indexOf(name), 1) - names.splice(names.indexOf(posKey), 0, name) - const valueObject = attributes[name] = attributes[posKey] - // NOTE remap value as deferred function to avoid having to resolve again for positional attribute - if (valueObject.constructor === Function) attributes[posKey] = () => attributes[name] + if (name == null || !names.includes(posKey)) continue + if (names.includes(name)) { + if (names.indexOf(posKey) < names.indexOf(name)) continue + } else { + // insert synthetic named attribute ahead of positional attribute + names.splice(names.indexOf(posKey), 0, name) + } + // assign positional attribute to named attribute, overwriting explicit named attribute if it exists + attributes[name] = attributes[posKey] + // remap positional attribute to resolved value of implicit named attribute (which will be resolved first) + attributes[posKey] = () => attributes[name] } } const promote = {} diff --git a/test/tests/block/image/implicit-named-attribute-override-input.adoc b/test/tests/block/image/implicit-named-attribute-override-input.adoc new file mode 100644 index 0000000..7b401da --- /dev/null +++ b/test/tests/block/image/implicit-named-attribute-override-input.adoc @@ -0,0 +1,2 @@ +[alt=Player Status] +image::player-status.png[Player Status Window,600,800] diff --git a/test/tests/block/image/implicit-named-attribute-override-output.json b/test/tests/block/image/implicit-named-attribute-override-output.json new file mode 100644 index 0000000..651e17d --- /dev/null +++ b/test/tests/block/image/implicit-named-attribute-override-output.json @@ -0,0 +1,25 @@ +{ + "name": "document", + "type": "block", + "blocks": [ + { + "name": "image", + "type": "block", + "form": "macro", + "target": "player-status.png", + "location": [{ "line": 2, "col": 1 }, { "line": 2, "col": 54 }], + "metadata": { + "attributes": { + "alt": "Player Status Window", + "$1": "Player Status Window", + "$2": "600", + "$3": "800", + "width": "600", + "height": "800" + }, + "location": [{ "line": 1, "col": 1 }, { "line": 1, "col": 19 }] + } + } + ], + "location": [{ "line": 1, "col": 1 }, { "line": 2, "col": 54 }] +}