/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 | 424k | bool HtmlIntegrationPoint(const Node& node) { |
11 | 424k | if (node.Type() != NodeType::ELEMENT_NODE) { |
12 | 0 | return false; |
13 | 0 | } |
14 | | |
15 | 424k | if (node.NameSpace() == "math") { |
16 | 318k | 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 | 318k | } else if (node.NameSpace() == "svg") { |
28 | 105k | if (node.DataAtom() == Atom::DESC || |
29 | 105k | node.DataAtom() == Atom::FOREIGN_OBJECT || |
30 | 105k | node.DataAtom() == Atom::TITLE) { |
31 | 134 | return true; |
32 | 134 | } |
33 | 105k | } |
34 | | |
35 | 424k | return false; |
36 | 424k | } |
37 | | |
38 | 429k | bool MathMLTextIntegrationPoint(const Node& node) { |
39 | 429k | static constexpr std::array<Atom, 5> textNodes { |
40 | 429k | Atom::MI, Atom::MO, Atom::MN, Atom::MS, Atom::MTEXT}; |
41 | | |
42 | 429k | if (node.NameSpace() != "math") return false; |
43 | 1.58M | for (auto tn : textNodes) { |
44 | 1.58M | if (node.DataAtom() == tn) return true; |
45 | 1.58M | } |
46 | 306k | return false; |
47 | 324k | } |
48 | | |
49 | 56.8k | void AdjustSVGAttributeNames(std::vector<Attribute>* attrs) { |
50 | 56.8k | for (Attribute& attr : *attrs) { |
51 | 12.4k | if (auto iter = std::lower_bound(std::begin(kSvgAttributeAdjustments), |
52 | 12.4k | std::end(kSvgAttributeAdjustments), |
53 | 12.4k | attr.key, |
54 | 12.4k | PairComparator<std::string_view, |
55 | 12.4k | std::string_view>()); |
56 | 12.4k | iter != std::end(kSvgAttributeAdjustments) && |
57 | 12.4k | iter->first == attr.key) { |
58 | 257 | attr.key = iter->second.data(); |
59 | 257 | } |
60 | 12.4k | } |
61 | 56.8k | } |
62 | | |
63 | 299k | void AdjustMathMLAttributeNames(std::vector<Attribute>* attrs) { |
64 | 299k | for (Attribute& attr : *attrs) { |
65 | 2.33k | if (auto iter = std::lower_bound(std::begin(kMathMLAttributeAdjustments), |
66 | 2.33k | std::end(kMathMLAttributeAdjustments), |
67 | 2.33k | attr.key, |
68 | 2.33k | PairComparator<std::string_view, |
69 | 2.33k | std::string_view>()); |
70 | 2.33k | iter != std::end(kMathMLAttributeAdjustments) && |
71 | 2.33k | iter->first == attr.key) { |
72 | 0 | attr.key = iter->second.data(); |
73 | 0 | } |
74 | 2.33k | } |
75 | 299k | } |
76 | | |
77 | 356k | void AdjustForeignAttributes(std::vector<Attribute>* attrs) { |
78 | 356k | static constexpr std::array<std::string_view, 11> keys { |
79 | 356k | "xlink:actuate", "xlink:arcrole", "xlink:href", "xlink:role", "xlink:show", |
80 | 356k | "xlink:title", "xlink:type", "xml:base", "xml:lang", "xml:space", |
81 | 356k | "xmlns:xlink", |
82 | 356k | }; |
83 | | |
84 | 356k | for (Attribute& attr : *attrs) { |
85 | 14.7k | if (attr.key.empty() || attr.key.at(0) != 'x') continue; |
86 | 3.39k | 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 | 3.39k | } |
92 | 356k | } |
93 | | |
94 | | } // namespace htmlparser |