diff options
author | Bjørn Erik Pedersen <[email protected]> | 2017-07-03 09:00:17 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2017-07-03 09:01:54 +0200 |
commit | e10e51a00827b9fdc1bee51439fef05afc529831 (patch) | |
tree | f92a4f28edae84ae15da9ac58218f4e7dee36aa7 | |
parent | 34c566773a1364077e1397daece85b22948dc721 (diff) | |
download | hugo-e10e51a00827b9fdc1bee51439fef05afc529831.tar.gz hugo-e10e51a00827b9fdc1bee51439fef05afc529831.zip |
parser: Fix handling of JSON front matter with escaped quotes
Fixes #3661
-rw-r--r-- | parser/page.go | 9 | ||||
-rw-r--r-- | parser/parse_frontmatter_test.go | 7 |
2 files changed, 14 insertions, 2 deletions
diff --git a/parser/page.go b/parser/page.go index bacc9754b..9d633e29d 100644 --- a/parser/page.go +++ b/parser/page.go @@ -305,6 +305,7 @@ func extractFrontMatterDelims(r *bufio.Reader, left, right []byte) (fm []byte, e level int sameDelim = bytes.Equal(left, right) inQuote bool + escaped bool ) // Frontmatter must start with a delimiter. To check it first, // pre-reads beginning delimiter length - 1 bytes from Reader @@ -333,7 +334,12 @@ func extractFrontMatterDelims(r *bufio.Reader, left, right []byte) (fm []byte, e switch c { case '"': - inQuote = !inQuote + if !escaped { + inQuote = !inQuote + } + escaped = false + case '\\': + escaped = true case left[len(left)-1]: if sameDelim { // YAML, TOML case if bytes.HasSuffix(buf.Bytes(), left) && (buf.Len() == len(left) || buf.Bytes()[buf.Len()-len(left)-1] == '\n') { @@ -396,6 +402,7 @@ func extractFrontMatterDelims(r *bufio.Reader, left, right []byte) (fm []byte, e return buf.Bytes(), nil } + } } diff --git a/parser/parse_frontmatter_test.go b/parser/parse_frontmatter_test.go index 08a1cc42c..e2722f788 100644 --- a/parser/parse_frontmatter_test.go +++ b/parser/parse_frontmatter_test.go @@ -299,6 +299,11 @@ func TestExtractFrontMatterDelim(t *testing.T) { // Issue #3511 {`{ "title": "{" }`, `{ "title": "{" }`, noErrExpected}, {`{ "title": "{}" }`, `{ "title": "{}" }`, noErrExpected}, + // Issue #3661 + {`{ "title": "\"" }`, `{ "title": "\"" }`, noErrExpected}, + {`{ "title": "\"{", "other": "\"{}" }`, `{ "title": "\"{", "other": "\"{}" }`, noErrExpected}, + {`{ "title": "\"Foo\"" }`, `{ "title": "\"Foo\"" }`, noErrExpected}, + {`{ "title": "\"Foo\"\"" }`, `{ "title": "\"Foo\"\"" }`, noErrExpected}, } for i, test := range tests { @@ -310,7 +315,7 @@ func TestExtractFrontMatterDelim(t *testing.T) { } if !bytes.Equal(fm, []byte(test.extracted)) { t.Logf("\n%q\n", string(test.frontmatter)) - t.Errorf("[%d] Frontmatter did not match:\nexp: %q\ngot: %q", i, string(test.extracted), fm) + t.Errorf("[%d] Frontmatter did not match:\nexp: %q\ngot: %q", i, string(test.extracted), fm) } } } |