element_base.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package ace
  2. import "bytes"
  3. // elementBase holds common fields for the elements.
  4. type elementBase struct {
  5. ln *line
  6. rslt *result
  7. src *source
  8. parent element
  9. children []element
  10. opts *Options
  11. lastChild bool
  12. }
  13. // AppendChild appends the child element to the element.
  14. func (e *elementBase) AppendChild(child element) {
  15. e.children = append(e.children, child)
  16. }
  17. // ContainPlainText returns false.
  18. // This method should be overrided by a struct which contains
  19. // the element base struct.
  20. func (e *elementBase) ContainPlainText() bool {
  21. return false
  22. }
  23. // Base returns the element base.
  24. func (e *elementBase) Base() *elementBase {
  25. return e
  26. }
  27. // CanHaveChildren returns true.
  28. // This method should be overrided by a struct which contains
  29. // the element base struct.
  30. func (e *elementBase) CanHaveChildren() bool {
  31. return true
  32. }
  33. func (e *elementBase) IsBlockElement() bool {
  34. return false
  35. }
  36. func (e *elementBase) IsControlElement() bool {
  37. return false
  38. }
  39. // InsertBr returns false.
  40. // This method should be overrided by a struct which contains
  41. // the element base struct.
  42. func (e *elementBase) InsertBr() bool {
  43. return false
  44. }
  45. // SetLastChild set the value to the last child field.
  46. func (e *elementBase) SetLastChild(lastChild bool) {
  47. e.lastChild = lastChild
  48. }
  49. // writeChildren writes the children's HTML.
  50. func (e *elementBase) writeChildren(bf *bytes.Buffer) (int64, error) {
  51. l := len(e.children)
  52. for index, child := range e.children {
  53. if index == l-1 {
  54. child.SetLastChild(true)
  55. }
  56. if e.opts.formatter != nil {
  57. if i, err := e.opts.formatter.OpeningElement(bf, child); err != nil {
  58. return int64(i), err
  59. }
  60. }
  61. if i, err := child.WriteTo(bf); err != nil {
  62. return int64(i), err
  63. }
  64. }
  65. return 0, nil
  66. }
  67. // newElementBase creates and returns an element base.
  68. func newElementBase(ln *line, rslt *result, src *source, parent element, opts *Options) elementBase {
  69. return elementBase{
  70. ln: ln,
  71. rslt: rslt,
  72. src: src,
  73. parent: parent,
  74. opts: opts,
  75. }
  76. }