Skip to content

Commit

Permalink
fix(parser): ignore standalone attributes at the end of doc
Browse files Browse the repository at this point in the history
the draft document contains the attributes, but they
are filtered out when processing it.

Fixes bytesparadise#605 - Error when parsing doc containing only an attribute

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon committed Jun 14, 2020
1 parent 90b4f2d commit 8678385
Show file tree
Hide file tree
Showing 5 changed files with 3,587 additions and 3,648 deletions.
11 changes: 6 additions & 5 deletions pkg/parser/document_processing_filter_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
// - all document attribute declaration/substitution/reset
// - empty preambles
// - single line comments and comment blocks
// - standalone attributes
func filter(elements []interface{}, matchers ...filterMatcher) []interface{} {
result := make([]interface{}, 0, len(elements))
elements:
Expand Down Expand Up @@ -70,7 +71,7 @@ elements:
}

// AllMatchers all the matchers needed to remove the unneeded blocks/elements from the final document
var allMatchers = []filterMatcher{emptyPreambleMatcher, documentAttributeMatcher, singleLineCommentMatcher, commentBlockMatcher}
var allMatchers = []filterMatcher{emptyPreambleMatcher, attributeMatcher, singleLineCommentMatcher, commentBlockMatcher}

// filterMatcher returns true if the given element is to be filtered out
type filterMatcher func(element interface{}) bool
Expand All @@ -85,11 +86,11 @@ var emptyPreambleMatcher filterMatcher = func(element interface{}) bool {
return result
}

// documentAttributeMatcher filters the element if it is a AttributeDeclaration,
// a AttributeSubstitution or a AttributeReset
var documentAttributeMatcher filterMatcher = func(element interface{}) bool {
// attributeMatcher filters the element if it is a AttributeDeclaration,
// a AttributeSubstitution, a AttributeReset or a standalone Attribute
var attributeMatcher filterMatcher = func(element interface{}) bool {
switch element.(type) {
case types.AttributeDeclaration, types.AttributeSubstitution, types.AttributeReset:
case types.AttributeDeclaration, types.AttributeSubstitution, types.AttributeReset, types.Attributes:
return true
default:
return false
Expand Down
2 changes: 1 addition & 1 deletion pkg/parser/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var _ = Describe("documents", func() {

Context("draft document", func() {

It("empty docunment", func() {
It("empty document", func() {
source := ``
expected := types.DraftDocument{
Blocks: []interface{}{},
Expand Down
Loading

0 comments on commit 8678385

Please sign in to comment.