|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <rapidjson/document.h> |
| 4 | +#include <rapidjson/prettywriter.h> |
| 5 | +#include <rapidjson/stringbuffer.h> |
| 6 | +#include <string> |
| 7 | + |
| 8 | +using WDocument = rapidjson::GenericDocument<rapidjson::UTF16<>>; |
| 9 | +using WValue = rapidjson::GenericValue<rapidjson::UTF16<>>; |
| 10 | + |
| 11 | +enum NodeType |
| 12 | +{ |
| 13 | + ELEMENT_NODE = 1, |
| 14 | + ATTRIBUTE_NODE = 2, |
| 15 | + TEXT_NODE = 3, |
| 16 | + CDATA_SECTION_NODE = 4, |
| 17 | + PROCESSING_INSTRUCTION_NODE = 7, |
| 18 | + COMMENT_NODE = 8, |
| 19 | + DOCUMENT_NODE = 9, |
| 20 | + DOCUMENT_TYPE_NODE = 10, |
| 21 | + DOCUMENT_FRAGMENT_NODE = 11, |
| 22 | +}; |
| 23 | + |
| 24 | +namespace domutils |
| 25 | +{ |
| 26 | + bool containsClassName(const WValue& value, const wchar_t* name) |
| 27 | + { |
| 28 | + if (value[L"nodeType"].GetInt() != NodeType::ELEMENT_NODE) |
| 29 | + return false; |
| 30 | + if (!value.HasMember(L"attributes")) |
| 31 | + return false; |
| 32 | + const auto& ary = value[L"attributes"].GetArray(); |
| 33 | + for (unsigned int i = 0; i + 1 < ary.Size(); i += 2) |
| 34 | + { |
| 35 | + if (wcscmp(ary[i].GetString(), L"class") == 0 && |
| 36 | + wcsstr(ary[i + 1].GetString(), name) != nullptr) |
| 37 | + return true; |
| 38 | + } |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | + const wchar_t* getAttribute(const WValue& node, const wchar_t* name) |
| 43 | + { |
| 44 | + if (!node.HasMember(L"attributes")) |
| 45 | + return nullptr; |
| 46 | + const auto& ary = node[L"attributes"].GetArray(); |
| 47 | + for (unsigned i = 0; i < ary.Size(); i += 2) |
| 48 | + { |
| 49 | + if (wcscmp(ary[i].GetString(), name) == 0 && i + 1 < ary.Size()) |
| 50 | + return ary[i + 1].GetString(); |
| 51 | + } |
| 52 | + return nullptr; |
| 53 | + } |
| 54 | + |
| 55 | + void setAttribute(WValue& node, const wchar_t* name, const std::wstring& value, WDocument::AllocatorType& allocator) |
| 56 | + { |
| 57 | + if (!node.HasMember(L"attributes")) |
| 58 | + return; |
| 59 | + const auto& ary = node[L"attributes"].GetArray(); |
| 60 | + for (unsigned i = 0; i < ary.Size(); i += 2) |
| 61 | + { |
| 62 | + if (wcscmp(ary[i].GetString(), name) == 0 && i + 1 < ary.Size()) |
| 63 | + { |
| 64 | + ary[i + 1].SetString(value.c_str(), static_cast<unsigned>(value.length()), allocator); |
| 65 | + break; |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + void makeTextNode(WValue& textNode, const std::wstring& text, WDocument::AllocatorType& allocator) |
| 71 | + { |
| 72 | + WValue children; |
| 73 | + WValue textValue(text.c_str(), static_cast<unsigned>(text.size()), allocator); |
| 74 | + children.SetArray(); |
| 75 | + textNode.SetObject(); |
| 76 | + textNode.AddMember(L"nodeId", -1, allocator); |
| 77 | + textNode.AddMember(L"nodeType", 3, allocator); |
| 78 | + textNode.AddMember(L"nodeValue", textValue, allocator); |
| 79 | + textNode.AddMember(L"children", children, allocator); |
| 80 | + } |
| 81 | + |
| 82 | + void getFrameIdList(WValue& tree, std::vector<std::wstring>& frameIdList) |
| 83 | + { |
| 84 | + frameIdList.push_back(tree[L"frame"][L"id"].GetString()); |
| 85 | + if (tree.HasMember(L"childFrames")) |
| 86 | + { |
| 87 | + for (auto& frame : tree[L"childFrames"].GetArray()) |
| 88 | + getFrameIdList(frame, frameIdList); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + std::pair<WValue*, WValue*> findNodeId(WValue& nodeTree, int nodeId) |
| 93 | + { |
| 94 | + if (nodeTree[L"nodeId"].GetInt() == nodeId) |
| 95 | + { |
| 96 | + return { &nodeTree, nullptr }; |
| 97 | + } |
| 98 | + if (nodeTree.HasMember(L"children") && nodeTree[L"children"].IsArray()) |
| 99 | + { |
| 100 | + for (auto& child : nodeTree[L"children"].GetArray()) |
| 101 | + { |
| 102 | + auto [pvalue, pparent] = findNodeId(child, nodeId); |
| 103 | + if (pvalue) |
| 104 | + return { pvalue, pparent ? pparent : &nodeTree }; |
| 105 | + } |
| 106 | + } |
| 107 | + if (nodeTree.HasMember(L"contentDocument")) |
| 108 | + { |
| 109 | + auto [pvalue, pparent] = findNodeId(nodeTree[L"contentDocument"], nodeId); |
| 110 | + return { pvalue, pparent ? pparent : &nodeTree[L"contentDocument"] }; |
| 111 | + } |
| 112 | + return { nullptr, nullptr }; |
| 113 | + } |
| 114 | +} |
0 commit comments