summaryrefslogtreecommitdiff
path: root/src/tag.cc
blob: a563b9dc0c0876c4d57aeacf3faa3410607e665a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "common.hh"

#include "htmlutil.hh"
#include "tag.hh"

#include <map>
#include <vector>

namespace {

class TextRenderable : public virtual Renderable {
public:
  explicit TextRenderable(std::string content)
    : content_(std::move(content)) {}

  void render(std::string* out) const override {
    html::escape(content_, out);
  }

private:
  std::string content_;
};

class ScriptRenderable : public virtual Renderable {
public:
  explicit ScriptRenderable(std::string content)
    : content_(std::move(content)) {}

  void render(std::string* out) const override {
    out->append(content_);
  }

private:
  std::string content_;
};

class TagImpl : public Tag {
public:
  TagImpl(std::string name, std::string content)
    : name_(std::move(name)) {
    add(std::move(content));
    assert(html::escape(name_) == name_);
  }

  std::string_view name() const override {
    return name_;
  }

  bool empty() const override {
    return child_.empty();
  }

  bool has_attr(std::string const& name) const override {
    return attr_.count(name);
  }

  Tag* clear_content() override {
    child_.clear();
    return this;
  }

  Tag* attr(std::string name, std::string value) override {
    assert(html::escape(name) == name);
    attr_[name] = std::move(value);
    return this;
  }

  Tag* add(std::string content) override {
    if (!content.empty()) {
      if (name_ == "script") {
        child_.push_back(
            std::make_unique<ScriptRenderable>(std::move(content)));
      } else {
        child_.push_back(std::make_unique<TextRenderable>(std::move(content)));
      }
    }
    return this;
  }

  Tag* add(std::unique_ptr<Tag> tag) override {
    auto* ret = tag.get();
    child_.push_back(std::move(tag));
    return ret;
  }

  void render(std::string* out) const override {
    size_t need = 1 + name_.size();
    for (auto const& pair : attr_)
      need += 1 + pair.first.size() + 2 + pair.second.size() + 1;
    if (empty())
      ++need;
    out->reserve(need);
    out->push_back('<');
    out->append(name_);
    for (auto const& pair : attr_) {
      out->push_back(' ');
      out->append(pair.first);
      out->append("=\"");
      html::escape(pair.second, out, html::EscapeTarget::ATTRIBUTE);
      out->push_back('"');
    }
    if (empty()) {
      if (name_ == "script") {
        // Some browsers don't allow <script src=""/>, must be written as
        // <script src=""></script>
        out->append("></script>");
      } else {
        // There are tags, like <br> where the / is optional but why botter.
        out->append("/>");
      }
      return;
    }
    out->push_back('>');

    for (auto& child : child_) {
      child->render(out);
    }

    out->reserve(name_.size() + 3);
    out->append("</");
    out->append(name_);
    out->push_back('>');
  }

private:
  std::string name_;
  std::map<std::string, std::string> attr_;
  std::vector<std::unique_ptr<Renderable>> child_;
};

}  // namespace

Tag* Tag::add_tag(std::string name, std::string content) {
  return add(create(std::move(name), std::move(content)));
}

std::unique_ptr<Tag> Tag::create(std::string name, std::string content) {
  return std::make_unique<TagImpl>(std::move(name), std::move(content));
}