diff --git a/README.md b/README.md index 246e5635..5410c0da 100644 --- a/README.md +++ b/README.md @@ -279,17 +279,6 @@ moment of adding them. { "foo,bar": ["baz"], "[object Object]": ["baz", "baz"] } ``` -Also, reading of properties on implicit block mapping keys is not supported yet. -So, the following YAML document cannot be loaded. - -``` yaml -&anchor foo: - foo: bar - *anchor: duplicate key - baz: bat - *anchor: duplicate key -``` - js-yaml for enterprise ---------------------- diff --git a/lib/js-yaml/loader.js b/lib/js-yaml/loader.js index ef01386b..d5bd1b45 100644 --- a/lib/js-yaml/loader.js +++ b/lib/js-yaml/loader.js @@ -991,6 +991,12 @@ function readBlockMapping(state, nodeIndent, flowIndent) { detected = false, ch; + if (state.blockMappingEntry < state.position) { + state.anchor = _anchor = null; + state.tag = _tag = null; + state.position = state.blockMappingEntry; + } + if (state.anchor !== null) { state.anchorMap[state.anchor] = _result; } @@ -1292,7 +1298,8 @@ function composeNode(state, parentIndent, nodeContext, allowToSeek, allowCompact typeQuantity, type, flowIndent, - blockIndent; + blockIndent, + ch; if (state.listener !== null) { state.listener('open', state); @@ -1321,6 +1328,7 @@ function composeNode(state, parentIndent, nodeContext, allowToSeek, allowCompact } } + state.blockMappingEntry = state.position; if (indentStatus === 1) { while (readTagProperty(state) || readAnchorProperty(state)) { if (skipSeparationSpace(state, true, -1)) { @@ -1334,8 +1342,12 @@ function composeNode(state, parentIndent, nodeContext, allowToSeek, allowCompact } else if (state.lineIndent < parentIndent) { indentStatus = -1; } + state.blockMappingEntry = state.position; } else { - allowBlockCollections = false; + ch = state.input.charCodeAt(state.position); + if (ch === 0x7C/* | */ || ch === 0x3E/* > */) { + allowBlockCollections = false; + } } } } @@ -1356,7 +1368,7 @@ function composeNode(state, parentIndent, nodeContext, allowToSeek, allowCompact if (indentStatus === 1) { if (allowBlockCollections && (readBlockSequence(state, blockIndent) || - readBlockMapping(state, blockIndent, flowIndent)) || + readBlockMapping(state, state.blockMappingEntry - state.lineStart, flowIndent)) || readFlowCollection(state, flowIndent)) { hasContent = true; } else { diff --git a/test/samples-common/anchor-mapping-key.js b/test/samples-common/anchor-mapping-key.js new file mode 100644 index 00000000..82cc0576 --- /dev/null +++ b/test/samples-common/anchor-mapping-key.js @@ -0,0 +1,7 @@ +'use strict'; + +module.exports = { + foo:{ + foo:'bar' + } +}; diff --git a/test/samples-common/anchor-mapping-key.yml b/test/samples-common/anchor-mapping-key.yml new file mode 100644 index 00000000..b22fa8e0 --- /dev/null +++ b/test/samples-common/anchor-mapping-key.yml @@ -0,0 +1,2 @@ +&anchor foo: + *anchor : bar \ No newline at end of file diff --git a/test/samples-common/tag-mapping-key.js b/test/samples-common/tag-mapping-key.js new file mode 100644 index 00000000..38ebd93f --- /dev/null +++ b/test/samples-common/tag-mapping-key.js @@ -0,0 +1,5 @@ +'use strict'; + +module.exports = { + TRUE: 1 +}; diff --git a/test/samples-common/tag-mapping-key.yml b/test/samples-common/tag-mapping-key.yml new file mode 100644 index 00000000..a3479a44 --- /dev/null +++ b/test/samples-common/tag-mapping-key.yml @@ -0,0 +1 @@ +!!str TRUE: 1 \ No newline at end of file diff --git a/test/samples-load-errors/duplicate-anchor-key.yml b/test/samples-load-errors/duplicate-anchor-key.yml new file mode 100644 index 00000000..7d146ebb --- /dev/null +++ b/test/samples-load-errors/duplicate-anchor-key.yml @@ -0,0 +1,3 @@ +&anchor foo: + foo: bar, + *anchor: duplicate key \ No newline at end of file