/proc/self/cwd/cpp/htmlparser/foreign.cc
Line | Count | Source (jump to first uncovered line) |
1 | | #include "cpp/htmlparser/foreign.h" |
2 | | |
3 | | #include <algorithm> |
4 | | |
5 | | #include "cpp/htmlparser/comparators.h" |
6 | | #include "cpp/htmlparser/strings.h" |
7 | | |
8 | | namespace htmlparser { |
9 | | |
10 | 2.59M | bool HtmlIntegrationPoint(const Node& node) { |
11 | 2.59M | if (node.Type() != NodeType::ELEMENT_NODE) { |
12 | 0 | return false; |
13 | 0 | } |
14 | | |
15 | 2.59M | if (node.NameSpace() == "math") { |
16 | 2.53M | if (node.DataAtom() == Atom::ANNOTATION_XML) { |
17 | 0 | for (const Attribute& attr : node.Attributes()) { |
18 | 0 | if (attr.key == "encoding") { |
19 | 0 | std::string value = attr.value; |
20 | 0 | Strings::ToLower(&value); |
21 | 0 | if (value == "text/html" || value == "application/xhtml+xml") { |
22 | 0 | return true; |
23 | 0 | } |
24 | 0 | } |
25 | 0 | } |
26 | 0 | } |
27 | 2.53M | } else if (node.NameSpace() == "svg") { |
28 | 64.0k | if (node.DataAtom() == Atom::DESC || |
29 | 64.0k | node.DataAtom() == Atom::FOREIGN_OBJECT || |
30 | 64.0k | node.DataAtom() == Atom::TITLE) { |
31 | 1.19k | return true; |
32 | 1.19k | } |
33 | 64.0k | } |
34 | | |
35 | 2.59M | return false; |
36 | 2.59M | } |
37 | | |
38 | 2.59M | bool MathMLTextIntegrationPoint(const Node& node) { |
39 | 2.59M | static constexpr std::array<Atom, 5> textNodes { |
40 | 2.59M | Atom::MI, Atom::MO, Atom::MN, Atom::MS, Atom::MTEXT}; |
41 | | |
42 | 2.59M | if (node.NameSpace() != "math") return false; |
43 | 12.6M | for (auto tn : textNodes) { |
44 | 12.6M | if (node.DataAtom() == tn) return true; |
45 | 12.6M | } |
46 | 2.53M | return false; |
47 | 2.53M | } |
48 | | |
49 | 34.1k | void AdjustSVGAttributeNames(std::vector<Attribute>* attrs) { |
50 | 34.1k | for (Attribute& attr : *attrs) { |
51 | 6.93k | if (auto iter = std::lower_bound(std::begin(kSvgAttributeAdjustments), |
52 | 6.93k | std::end(kSvgAttributeAdjustments), |
53 | 6.93k | attr.key, |
54 | 6.93k | PairComparator<std::string_view, |
55 | 6.93k | std::string_view>()); |
56 | 6.93k | iter != std::end(kSvgAttributeAdjustments) && |
57 | 6.93k | iter->first == attr.key) { |
58 | 258 | attr.key = iter->second.data(); |
59 | 258 | } |
60 | 6.93k | } |
61 | 34.1k | } |
62 | | |
63 | 1.80M | void AdjustMathMLAttributeNames(std::vector<Attribute>* attrs) { |
64 | 1.80M | for (Attribute& attr : *attrs) { |
65 | 1.65k | if (auto iter = std::lower_bound(std::begin(kMathMLAttributeAdjustments), |
66 | 1.65k | std::end(kMathMLAttributeAdjustments), |
67 | 1.65k | attr.key, |
68 | 1.65k | PairComparator<std::string_view, |
69 | 1.65k | std::string_view>()); |
70 | 1.65k | iter != std::end(kMathMLAttributeAdjustments) && |
71 | 1.65k | iter->first == attr.key) { |
72 | 0 | attr.key = iter->second.data(); |
73 | 0 | } |
74 | 1.65k | } |
75 | 1.80M | } |
76 | | |
77 | 1.84M | void AdjustForeignAttributes(std::vector<Attribute>* attrs) { |
78 | 1.84M | static constexpr std::array<std::string_view, 11> keys { |
79 | 1.84M | "xlink:actuate", "xlink:arcrole", "xlink:href", "xlink:role", "xlink:show", |
80 | 1.84M | "xlink:title", "xlink:type", "xml:base", "xml:lang", "xml:space", |
81 | 1.84M | "xmlns:xlink", |
82 | 1.84M | }; |
83 | | |
84 | 1.84M | for (Attribute& attr : *attrs) { |
85 | 8.59k | if (attr.key.empty() || attr.key.at(0) != 'x') continue; |
86 | 2.84k | if (std::find(keys.begin(), keys.end(), attr.key) != keys.end()) { |
87 | 0 | int j = attr.key.find(':'); |
88 | 0 | attr.name_space = attr.key.substr(0, j); |
89 | 0 | attr.key = attr.key.substr(j + 1); |
90 | 0 | } |
91 | 2.84k | } |
92 | 1.84M | } |
93 | | |
94 | | } // namespace htmlparser |