Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: escape shorthand tags if needed #51

Merged
merged 1 commit into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions emitterc.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ func yaml_emitter_emit_document_start(emitter *yaml_emitter_t, event *yaml_event
if !yaml_emitter_write_tag_handle(emitter, tag_directive.handle) {
return false
}
if !yaml_emitter_write_tag_content(emitter, tag_directive.prefix, true) {
if !yaml_emitter_write_tag_content(emitter, tag_directive.prefix, true, true) {
return false
}
if !yaml_emitter_write_indent(emitter) {
Expand Down Expand Up @@ -1080,7 +1080,7 @@ func yaml_emitter_process_tag(emitter *yaml_emitter_t) bool {
return false
}
if len(emitter.tag_data.suffix) > 0 {
if !yaml_emitter_write_tag_content(emitter, emitter.tag_data.suffix, false) {
if !yaml_emitter_write_tag_content(emitter, emitter.tag_data.suffix, need_brackets, false) {
return false
}
}
Expand All @@ -1094,7 +1094,7 @@ func yaml_emitter_process_tag(emitter *yaml_emitter_t) bool {
if !yaml_emitter_write_indicator(emitter, []byte("!<"), true, false, false) {
return false
}
if !yaml_emitter_write_tag_content(emitter, emitter.tag_data.suffix, false) {
if !yaml_emitter_write_tag_content(emitter, emitter.tag_data.suffix, false, false) {
return false
}
if !yaml_emitter_write_indicator(emitter, []byte{'>'}, false, false, false) {
Expand Down Expand Up @@ -1562,7 +1562,7 @@ func yaml_emitter_write_tag_handle(emitter *yaml_emitter_t, value []byte) bool {
return true
}

func yaml_emitter_write_tag_content(emitter *yaml_emitter_t, value []byte, need_whitespace bool) bool {
func yaml_emitter_write_tag_content(emitter *yaml_emitter_t, value []byte, allow_uri, need_whitespace bool) bool {
if need_whitespace && !emitter.whitespace {
if !put(emitter, ' ') {
return false
Expand All @@ -1571,8 +1571,10 @@ func yaml_emitter_write_tag_content(emitter *yaml_emitter_t, value []byte, need_
for i := 0; i < len(value); {
var must_write bool
switch value[i] {
case ';', '/', '?', ':', '@', '&', '=', '+', '$', ',', '_', '.', '~', '*', '\'', '(', ')', '[', ']':
case ';', '/', '?', ':', '@', '&', '=', '+', '$', '_', '.', '~', '*', '\'', '(', ')':
must_write = true
case '{', '}', '[', ']', ',':
must_write = allow_uri
default:
must_write = is_alpha(value, i)
}
Expand Down
15 changes: 15 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,21 @@ var marshalTests = []struct {
},
"!<!!>\n",
},
// https://github.com/go-faster/yaml/issues/50
{
yaml.Node{
Kind: yaml.ScalarNode,
Tag: "[",
},
"!<%5B>\n",
},
{
yaml.Node{
Kind: yaml.ScalarNode,
Tag: "]",
},
"!<%5D>\n",
},

// Enforced tagging with shorthand notation (issue #616).
{
Expand Down