diff options
-rw-r--r-- | selector.go | 8 | ||||
-rw-r--r-- | selector_test.go | 31 |
2 files changed, 25 insertions, 14 deletions
diff --git a/selector.go b/selector.go index 5259df2..ff1e7c1 100644 --- a/selector.go +++ b/selector.go @@ -128,7 +128,7 @@ func selectSearch(block Block, titles []string, any bool) ([]int, Block) { return []int{}, block } - if bl, ok := block.(*SectionBlock); ok { + if bl, ok := block.(*SectionBlock); ok && bl.Title() != "" { if len(titles) == 1 && bl.Title() == titles[0] { return []int{}, bl } @@ -162,7 +162,7 @@ func selectSectionIndex(block Block, secs []int) ([]int, Block) { if secs[0] < 1 { return nil, nil } - if cb, ok2 := c.(*SectionBlock); ok2 { + if cb, ok2 := c.(*SectionBlock); ok2 && cb.Title() != "" { secs[0]-- // starts with 1 for first section if secs[0] == 0 { if len(secs) == 1 { @@ -246,8 +246,8 @@ func duplicateBlock(block Block, deep, sections bool) Block { if cb != nil && deep { for _, ch := range block.(ContainerBlock).Children() { - _, isSection := ch.(*SectionBlock) - cb.AppendChild(duplicateBlock(ch, sections || !isSection, sections)) + sb, isSection := ch.(*SectionBlock) + cb.AppendChild(duplicateBlock(ch, sections || (!isSection || sb.Title() == ""), sections)) } } diff --git a/selector_test.go b/selector_test.go index 0c714dc..908d48b 100644 --- a/selector_test.go +++ b/selector_test.go @@ -38,7 +38,16 @@ var ( }, }, }} - sec322 = &SectionBlock{ContentBlock{ + sec32 = &SectionBlock{ContentBlock{ + name: "section", + children: []Block{ + &TextBlock{ + Format: "pre", + Contents: TextPreContents{"zxc"}, + }, + }, + }} + sec332 = &SectionBlock{ContentBlock{ name: "section", args: []string{"baz"}, children: []Block{ @@ -48,7 +57,7 @@ var ( }, }, }} - sec32 = &SectionBlock{ContentBlock{ + sec33 = &SectionBlock{ContentBlock{ name: "section", args: []string{"quux"}, children: []Block{ @@ -56,7 +65,7 @@ var ( Format: "pre", Contents: TextPreContents{"123"}, }, - sec322, + sec332, }, }} sec4 = &SectionBlock{ContentBlock{ @@ -84,6 +93,7 @@ var ( children: []Block{ sec31, sec32, + sec33, &TextBlock{ Format: "pre", Contents: TextPreContents{"rty"}, @@ -112,11 +122,12 @@ var ( name: "section", args: sec31.Args(), }}, + sec32, &SectionBlock{ContentBlock{ name: "section", - args: sec32.Args(), + args: sec33.Args(), }}, - doc1.Content.Children()[2].(ContainerBlock).Children()[2], + doc1.Content.Children()[2].(ContainerBlock).Children()[3], }, }}, &SectionBlock{ContentBlock{ @@ -220,8 +231,8 @@ var selectBlockTests = []struct { {"/foo%2fbar", doc1, sec4, []int{3}}, {"/bar/foo%2fbar", doc1, sec11, []int{0, 0}}, {"/baz", doc1, sec31, []int{2, 0}}, - {"/quux", doc1, sec32, []int{2, 1}}, - {"/quux/baz", doc1, sec322, []int{2, 1, 1}}, + {"/quux", doc1, sec33, []int{2, 2}}, + {"/quux/baz", doc1, sec332, []int{2, 2, 1}}, {"/bar/baz", doc1, nil, nil}, {"$", doc1, doc1.Content, []int{}}, @@ -229,11 +240,11 @@ var selectBlockTests = []struct { {"$1", doc1, sec1, []int{0}}, {"$1.1", doc1, sec11, []int{0, 0}}, {"$2", doc1, sec31, []int{2, 0}}, - {"$3", doc1, sec32, []int{2, 1}}, + {"$3", doc1, sec33, []int{2, 2}}, {"$4", doc1, sec4, []int{3}}, {"$5", doc1, nil, nil}, {"$2.1", doc1, nil, nil}, - {"$3.1", doc1, sec322, []int{2, 1, 1}}, + {"$3.1", doc1, sec332, []int{2, 2, 1}}, {"$3.0", doc1, nil, nil}, {"$3.2", doc1, nil, nil}, @@ -242,7 +253,7 @@ var selectBlockTests = []struct { {"#bar", doc1, sec1, []int{0}}, {"#foo%2fbar", doc1, sec11, []int{0, 0}}, {"#baz", doc1, sec31, []int{2, 0}}, - {"#quux", doc1, sec32, []int{2, 1}}, + {"#quux", doc1, sec33, []int{2, 2}}, } func TestSelectBlock(t *testing.T) { |