KHtml

parsedstatement.cpp
1 /*
2  * parsedstatement.cc - Copyright 2005 Frerich Raabe <[email protected]>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #include "parsedstatement.h"
26 #include "path.h"
27 
28 #include "xml/dom_nodeimpl.h"
29 #include "xml/dom_nodelistimpl.h"
30 #include "xml/dom3_xpathimpl.h"
31 
32 using namespace DOM;
33 
34 namespace khtml
35 {
36 namespace XPath
37 {
38 
39 Expression *khtmlParseXPathStatement(const DOMString &statement, int &ec);
40 
41 ParsedStatement::ParsedStatement(const DOMString &statement, khtml::XPathNSResolverImpl *res)
42  : m_res(res), m_expr(nullptr), m_ec(0)
43 {
44  parse(statement);
45 }
46 
47 ParsedStatement::~ParsedStatement()
48 {
49  delete m_expr;
50 }
51 
52 void ParsedStatement::parse(const DOMString &statement)
53 {
54  // qCDebug(KHTML_LOG) << "parsing:" << statement.string();
55  m_ec = 0;
56  delete m_expr;
57  Expression::evaluationContext().reset(nullptr, m_res.get());
58 
59  m_expr = khtmlParseXPathStatement(statement, m_ec);
60 
61  // qCDebug(KHTML_LOG) << "AST:" << (m_expr ? m_expr->dump() : QString::fromLatin1("*** parse error ***"));
62 }
63 
64 void ParsedStatement::optimize()
65 {
66  if (!m_expr) {
67  return;
68  }
69  m_expr->optimize();
70 }
71 
72 Value ParsedStatement::evaluate(NodeImpl *context, int &ec) const
73 {
74  Expression::evaluationContext().reset(context, m_res.get());
75  Value res = m_expr->evaluate();
76  ec = Expression::evaluationContext().exceptionCode;
77 
78  // If the result is a nodeset, we need to put it in document order
79  // and remove duplicates.
80  if (res.isNodeset()) {
81  res.toNodeset()->normalizeUpto(StaticNodeListImpl::DocumentOrder);
82  }
83  return res;
84 }
85 
86 QString ParsedStatement::dump() const
87 {
88  return m_expr->dump();
89 }
90 
91 } // namespace XPath
92 } // namespace khtml
93 
This file is part of the HTML rendering engine for KDE.
KHEALTHCERTIFICATE_EXPORT QVariant parse(const QByteArray &data)
This class implements the basic string we use in the DOM.
Definition: dom_string.h:44
This library provides a full-featured HTML parser and widget.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 16 2021 22:47:58 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.