1 /**********************************************************************************
2 **
3 ** SigilQuery for Gumbo
4 **
5 ** A C++ library that provides jQuery-like selectors for Google's Gumbo-Parser.
6 ** Selector engine is an implementation based on cascadia.
7 **
8 ** Based on: "gumbo-query" https://github.com/lazytiger/gumbo-query
9 ** With bug fixes, extensions and improvements
10 **
11 ** The MIT License (MIT)
12 ** Copyright (c) 2021 Kevin B. Hendricks, Stratford, Ontario Canada
13 ** Copyright (c) 2015 baimashi.com.
14 ** Copyright (c) 2011 Andy Balholm. All rights reserved.
15 **
16 **
17 ** Permission is hereby granted, free of charge, to any person obtaining a copy
18 ** of this software and associated documentation files (the "Software"), to deal
19 ** in the Software without restriction, including without limitation the rights
20 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21 ** copies of the Software, and to permit persons to whom the Software is
22 ** furnished to do so, subject to the following conditions:
23 **
24 ** The above copyright notice and this permission notice shall be included in
25 ** all copies or substantial portions of the Software.
26 **
27 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30 ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32 ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33 ** THE SOFTWARE.
34 **
35 **********************************************************************************/
36
37 #include <iostream>
38 #include <exception>
39 #include <stdexcept>
40
41 #include "Query/CParser.h"
42 #include "Query/CQueryUtil.h"
43 #include "Query/CNode.h"
44 #include "Query/CSelection.h"
45
CSelection(GumboNode * apNode,bool error)46 CSelection::CSelection(GumboNode* apNode, bool error)
47 {
48 if(apNode) mNodes.push_back(apNode);
49 mparseError = error;
50 }
51
CSelection(std::vector<GumboNode * > aNodes,bool error)52 CSelection::CSelection(std::vector<GumboNode*> aNodes, bool error)
53 {
54 mNodes = aNodes;
55 mparseError = error;
56 }
57
~CSelection()58 CSelection::~CSelection()
59 {
60 }
61
find(std::string aSelector)62 CSelection CSelection::find(std::string aSelector)
63 {
64 // parsing the any selector can throw exceptions
65 // try to fail gracefully
66 try {
67 CSelector* sel = CParser::create(aSelector);
68 std::vector<GumboNode*> ret;
69 for (std::vector<GumboNode*>::iterator it = mNodes.begin(); it != mNodes.end(); it++)
70 {
71 GumboNode* pNode = *it;
72 std::vector<GumboNode*> matched = sel->matchAll(pNode);
73 ret = CQueryUtil::unionNodes(ret, matched);
74 }
75 sel->release();
76 return CSelection(ret);
77 } catch(const std::runtime_error &e) {
78 std::cout << "***Query Parser Error***: " << e.what() << std::endl;
79 return CSelection(NULL, true);
80 }
81 }
82
nodeAt(size_t i)83 CNode CSelection::nodeAt(size_t i)
84 {
85 if (i >= mNodes.size())
86 {
87 return CNode();
88 }
89
90 return CNode(mNodes[i]);
91 }
92
nodeNum()93 size_t CSelection::nodeNum()
94 {
95 return mNodes.size();
96 }
97