libwordring
simple_traits.hpp
1 #pragma once
2 
3 #include <wordring/html/simple_node.hpp>
4 
5 #include <wordring/encoding/encoding.hpp>
6 
7 #include <wordring/compatibility.hpp>
8 #include <wordring/tree/tree.hpp>
9 
10 #include <iterator>
11 #include <string>
12 #include <variant>
13 
14 namespace wordring::html
15 {
22  template <typename NodeIterator>
24  {
25  using node_pointer = NodeIterator;
26  using node_type = typename node_pointer::value_type;
27 
28  using element_type = typename node_type::element_type; // 1.
29  using text_type = typename node_type::text_type; // 3.
30  using processing_instruction_type = typename node_type::processing_instruction_type; // 7.
31  using comment_type = typename node_type::comment_type; // 8.
32  using document_type = typename node_type::document_type; // 9.
33  using document_type_type = typename node_type::document_type_type; // 10.
34  using document_fragment_type = typename node_type::document_fragment_type; // 11.
35 
36  using attribute_type = typename element_type::attribute_type;
37  using attribute_pointer = typename element_type::const_iterator;
38 
39  using string_type = typename element_type::string_type;
40 
41  // ----------------------------------------------------------------------------------------
42  // Node
43  // ----------------------------------------------------------------------------------------
44 
47  static node_pointer pointer() { return node_pointer(); }
48 
54  static node_pointer parent(node_pointer it) { return it.parent(); }
55 
58  static node_pointer begin(node_pointer it) { return it.begin(); }
59 
62  static node_pointer end(node_pointer it) { return it.end(); }
63 
66  static node_pointer prev(node_pointer it) { return --it; }
67 
70  static node_pointer next(node_pointer it) { return ++it; }
71 
72  static auto& data(node_pointer it) { return it->data(); }
73 
74  static auto target(node_pointer it) { return it->target(); }
75 
76  static auto name(node_pointer it) { return it->name(); }
77 
78  // ----------------------------------------------------------------------------------------
79  // Element
80  // ----------------------------------------------------------------------------------------
81 
82  static bool is_element(node_pointer it) { return it->is_element(); }
83 
88  static void set_document(node_pointer it, node_pointer doc) {}
89 
92  static ns_name get_namespace_name(node_pointer it)
93  {
94  return it->namespace_uri_name();
95  }
96 
97  static string_type get_namespace(node_pointer it)
98  {
99  return it->namespace_uri();
100  }
101 
104  static tag_name get_local_name_name(node_pointer it)
105  {
106  return it->local_name_name();
107  }
108 
109  static string_type get_local_name(node_pointer it)
110  {
111  return it->local_name();
112  }
113 
114  static string_type get_qualified_name(node_pointer it)
115  {
116  return it->qualified_name();
117  }
118 
119  static bool is_html_element_of(node_pointer it, tag_name tag)
120  {
121  return get_namespace_name(it) == ns_name::HTML && get_local_name_name(it) == tag;
122  }
123 
128  static bool equals(node_pointer lhs, node_pointer rhs)
129  {
130  return *lhs == *rhs;
131  }
132 
135  static auto abegin(node_pointer it) { return it->begin(); }
136 
137  static auto aend(node_pointer it) { return it->end(); }
138 
141  /*
142  static auto find(node_pointer it, attribute_name name)
143  {
144  return wordring::html::find(*it, name);
145  }
146  */
147 
152  static void set_non_blocking_flag(node_pointer it, bool b) {}
153 
158  static void set_already_started_flag(node_pointer it, bool b) {}
159 
160  // ----------------------------------------------------------------------------------------
161  // Attr
162  // ----------------------------------------------------------------------------------------
163 
164  static ns_name get_namespace_name(attribute_type const& attr)
165  {
166  return attr.namespace_uri_name();
167  }
168 
169  static string_type get_namespace(attribute_type const& attr)
170  {
171  return attr.namespace_uri();
172  }
173 
174  static attribute_name get_local_name_name(attribute_type const& attr)
175  {
176  return attr.local_name_name();
177  }
178 
179  static string_type get_local_name(attribute_type const& attr)
180  {
181  return attr.local_name();
182  }
183 
184  static string_type get_qualified_name(attribute_type const& attr)
185  {
186  return attr.qualified_name();
187  }
188 
189  static string_type const& value(attribute_type const& attr) { return attr.value(); }
190 
191  // ----------------------------------------------------------------------------------------
192  // Text
193  // ----------------------------------------------------------------------------------------
194 
195  static bool is_text(node_pointer it) { return it->is_text(); }
196 
197  static text_type create_text(char32_t cp)
198  {
199  string_type s;
200  wordring::to_string(cp, std::back_inserter(s));
201  return text_type(s);
202  }
203 
204  static void append_text(node_pointer it, char32_t cp)
205  {
206  wordring::to_string(cp, std::back_inserter(it->data()));
207  }
208 
209  // ----------------------------------------------------------------------------------------
210  // ProcessingInstruction
211  // ----------------------------------------------------------------------------------------
212 
213  static bool is_processing_instruction(node_pointer it) { return it->is_processing_instruction(); }
214 
215  // ----------------------------------------------------------------------------------------
216  // Comment
217  // ----------------------------------------------------------------------------------------
218 
219  static bool is_comment(node_pointer it) { return it->is_comment(); }
220 
221  static comment_type create_comment(string_type const& data)
222  {
223  return comment_type(data);
224  }
225 
226  // ----------------------------------------------------------------------------------------
227  // Document
228  // ----------------------------------------------------------------------------------------
229 
230  static bool is_document(node_pointer it) { return it->is_document(); }
231 
234  static document_type_name get_document_type(node_pointer it)
235  {
236  return it->document_type_name();
237  }
238 
244  static void set_document_type(node_pointer it, document_type_name type)
245  {
246  it->document_type_name(type);
247  }
248 
251  static document_mode_name get_document_mode(node_pointer it)
252  {
253  return it->document_mode_name();
254  }
255 
261  static void set_document_mode(node_pointer it, document_mode_name mode)
262  {
263  it->document_mode_name(mode);
264  }
265 
273  static bool is_iframe_srcdoc_document(node_pointer it) { return false; }
274 
280  static void set_document_ready_state(string_type const& readiness) {}
281 
282  // ----------------------------------------------------------------------------------------
283  // DocumentType
284  // ----------------------------------------------------------------------------------------
285 
286  static bool is_document_type(node_pointer it) { return it->is_document_type(); }
287 
288  static document_type_type create_document_type(
289  string_type const& name, string_type const& public_id, string_type const& system_id)
290  {
291  return document_type_type(name, public_id, system_id);
292  }
293 
294  // ----------------------------------------------------------------------------------------
295  // CSS Support
296  // ----------------------------------------------------------------------------------------
297 
298  static bool is_root(node_pointer it)
299  {
300  return is_element(it) && is_html_element_of(it, tag_name::Html);
301  }
302 
303  // ----------------------------------------------------------------------------------------
304  // tag_tree Support
305  // ----------------------------------------------------------------------------------------
306 
307  static bool is_single(node_type const& node)
308  {
309  switch (node.type())
310  {
311  case node_type::type_name::Text:
312  case node_type::type_name::Comment:
313  case node_type::type_name::DocumentType:
314  return true;
315  default:
316  break;
317  }
318  return false;
319  }
320  };
321 }
wordring::html::simple_node_traits::set_document
static void set_document(node_pointer it, node_pointer doc)
要素へオーナー文書を設定する
Definition: simple_traits.hpp:88
wordring::html::simple_node_traits::is_iframe_srcdoc_document
static bool is_iframe_srcdoc_document(node_pointer it)
文書が IFRAME ソース文書か調べる
Definition: simple_traits.hpp:273
wordring::html::simple_node_traits::end
static node_pointer end(node_pointer it)
最後の子の次を返す
Definition: simple_traits.hpp:62
wordring::html::simple_node_traits::get_document_mode
static document_mode_name get_document_mode(node_pointer it)
文書ノードから文書形式を取得する
Definition: simple_traits.hpp:251
wordring::html::simple_node_traits::get_document_type
static document_type_name get_document_type(node_pointer it)
文書ノードから文書形式を取得する
Definition: simple_traits.hpp:234
wordring::html::simple_node_traits::set_document_type
static void set_document_type(node_pointer it, document_type_name type)
文書ノードに文書形式を設定する
Definition: simple_traits.hpp:244
wordring::html::simple_node_traits::set_document_mode
static void set_document_mode(node_pointer it, document_mode_name mode)
文書ノードに文書モードを設定する
Definition: simple_traits.hpp:261
wordring::html::simple_node_traits::get_namespace_name
static ns_name get_namespace_name(node_pointer it)
要素の名前空間を返す
Definition: simple_traits.hpp:92
wordring::html::simple_node_traits::equals
static bool equals(node_pointer lhs, node_pointer rhs)
二つの要素が同じシグネチャを持つか調べる
Definition: simple_traits.hpp:128
wordring::html::simple_node_traits::set_already_started_flag
static void set_already_started_flag(node_pointer it, bool b)
script 要素の "already started" flag を設定する
Definition: simple_traits.hpp:158
wordring::html::simple_node_traits::pointer
static node_pointer pointer()
NULLで初期化された空のポインタを作成する
Definition: simple_traits.hpp:47
wordring::html::simple_node_traits::set_non_blocking_flag
static void set_non_blocking_flag(node_pointer it, bool b)
属性を検索する
Definition: simple_traits.hpp:152
wordring::html::simple_node_traits::parent
static node_pointer parent(node_pointer it)
親要素を返す
Definition: simple_traits.hpp:54
wordring::html::simple_node_traits::next
static node_pointer next(node_pointer it)
次の兄弟を返す
Definition: simple_traits.hpp:70
wordring::html::simple_node_traits
node_traits のテンプレート特殊化
Definition: simple_traits.hpp:23
wordring::html::simple_node_traits::prev
static node_pointer prev(node_pointer it)
前の兄弟を返す
Definition: simple_traits.hpp:66
wordring::html
wordring::whatwg::html::document_mode_name
document_mode_name
Definition: whatwg/html/html_defs.hpp:28
wordring::html::simple_node_traits::get_local_name_name
static tag_name get_local_name_name(node_pointer it)
要素のローカル名を返す
Definition: simple_traits.hpp:104
wordring::html::simple_node_traits::set_document_ready_state
static void set_document_ready_state(string_type const &readiness)
Definition: simple_traits.hpp:280
wordring::html::simple_node_traits::abegin
static auto abegin(node_pointer it)
要素ノードから最初の属性を指すイテレータを取得する
Definition: simple_traits.hpp:135
wordring::html::simple_node_traits::begin
static node_pointer begin(node_pointer it)
最初の子を返す
Definition: simple_traits.hpp:58