From aed59fa3f96cd46ebfc19fb4a51b61fc7001bec8 Mon Sep 17 00:00:00 2001 From: dota17 Date: Sun, 28 Jun 2020 12:06:22 +0800 Subject: [PATCH 1/4] support the reading of properties on implicit block mapping keys --- lib/js-yaml/loader.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/js-yaml/loader.js b/lib/js-yaml/loader.js index ef01386b..447b0f37 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 { From 1e0edabd16db8c28132000230234dcabaf2a0d7c Mon Sep 17 00:00:00 2001 From: dota17 Date: Sun, 28 Jun 2020 16:08:59 +0800 Subject: [PATCH 2/4] add test cases --- test/samples-common/anchor-mapping-key.js | 7 +++++++ test/samples-common/anchor-mapping-key.yml | 2 ++ test/samples-common/tag-mapping-key.js | 5 +++++ test/samples-common/tag-mapping-key.yml | 1 + test/samples-load-errors/duplicate-anchor-key.yml | 3 +++ 5 files changed, 18 insertions(+) create mode 100644 test/samples-common/anchor-mapping-key.js create mode 100644 test/samples-common/anchor-mapping-key.yml create mode 100644 test/samples-common/tag-mapping-key.js create mode 100644 test/samples-common/tag-mapping-key.yml create mode 100644 test/samples-load-errors/duplicate-anchor-key.yml diff --git a/test/samples-common/anchor-mapping-key.js b/test/samples-common/anchor-mapping-key.js new file mode 100644 index 00000000..8a1c716a --- /dev/null +++ b/test/samples-common/anchor-mapping-key.js @@ -0,0 +1,7 @@ +'use strict' + +module.exports = { + foo:{ + foo:'bar' + } +} \ No newline at end of file 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..86e98b24 --- /dev/null +++ b/test/samples-common/tag-mapping-key.js @@ -0,0 +1,5 @@ +'use strict' + +module.exports = { + TRUE: 1 +} \ No newline at end of file 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 From 57cb33be8deb94de12e9681020326822fc182331 Mon Sep 17 00:00:00 2001 From: dota17 Date: Sun, 28 Jun 2020 16:11:01 +0800 Subject: [PATCH 3/4] modified README.md --- README.md | 11 ----------- 1 file changed, 11 deletions(-) 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 ---------------------- From 2143508a3e79341d03d872b2d892795561b96c01 Mon Sep 17 00:00:00 2001 From: dota17 Date: Sun, 28 Jun 2020 17:11:37 +0800 Subject: [PATCH 4/4] eslint fix --- lib/js-yaml/loader.js | 6 +++--- test/samples-common/anchor-mapping-key.js | 10 +++++----- test/samples-common/tag-mapping-key.js | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/js-yaml/loader.js b/lib/js-yaml/loader.js index 447b0f37..d5bd1b45 100644 --- a/lib/js-yaml/loader.js +++ b/lib/js-yaml/loader.js @@ -991,7 +991,7 @@ function readBlockMapping(state, nodeIndent, flowIndent) { detected = false, ch; - if(state.blockMappingEntry < state.position){ + if (state.blockMappingEntry < state.position) { state.anchor = _anchor = null; state.tag = _tag = null; state.position = state.blockMappingEntry; @@ -1345,8 +1345,8 @@ function composeNode(state, parentIndent, nodeContext, allowToSeek, allowCompact state.blockMappingEntry = state.position; } else { ch = state.input.charCodeAt(state.position); - if(ch === 0x7C/* | */ || ch === 0x3E/* > */){ - allowBlockCollections = false + if (ch === 0x7C/* | */ || ch === 0x3E/* > */) { + allowBlockCollections = false; } } } diff --git a/test/samples-common/anchor-mapping-key.js b/test/samples-common/anchor-mapping-key.js index 8a1c716a..82cc0576 100644 --- a/test/samples-common/anchor-mapping-key.js +++ b/test/samples-common/anchor-mapping-key.js @@ -1,7 +1,7 @@ -'use strict' +'use strict'; module.exports = { - foo:{ - foo:'bar' - } -} \ No newline at end of file + foo:{ + foo:'bar' + } +}; diff --git a/test/samples-common/tag-mapping-key.js b/test/samples-common/tag-mapping-key.js index 86e98b24..38ebd93f 100644 --- a/test/samples-common/tag-mapping-key.js +++ b/test/samples-common/tag-mapping-key.js @@ -1,5 +1,5 @@ -'use strict' +'use strict'; module.exports = { - TRUE: 1 -} \ No newline at end of file + TRUE: 1 +};