Commit Graph

327 Commits

Author SHA1 Message Date
Yorick Peterse a1a2190fe2 Support for the XPath substring() function. 2014-08-25 23:13:34 +02:00
Yorick Peterse 5382891106 Support for the XPath number() function. 2014-08-25 23:13:03 +02:00
Yorick Peterse ba058627f2 Corrected docs for on_call_string(). 2014-08-25 23:12:45 +02:00
Yorick Peterse b2ca18e127 Support for the XPath substring-after() function. 2014-08-25 22:19:13 +02:00
Yorick Peterse b9cdb4a72b Support for the XPath substring-before() function. 2014-08-25 22:11:05 +02:00
Yorick Peterse 83b873e3c1 Support for the XPath contains() function. 2014-08-25 21:52:33 +02:00
Yorick Peterse 5b65d6c31a Support for the xpath starts-with() function. 2014-08-25 09:43:06 +02:00
Yorick Peterse 276a5ab83b Support for lexing empty XPath strings. 2014-08-25 09:42:51 +02:00
Yorick Peterse b688c6dc1b Support for the XPath concat() function. 2014-08-23 20:24:18 +02:00
Yorick Peterse 431a253000 Return XPath integers as actual integers.
This is to ensure that calls such as string(10) return "10" and not "10.0". It
also saves integer -> float conversions when they're not needed.
2014-08-23 20:22:58 +02:00
Yorick Peterse b316fd3e1c Support for the XPath string() function. 2014-08-22 11:06:10 +02:00
Yorick Peterse ff8a4ad3aa Ignore comment nodes in NodeSet#text. 2014-08-22 11:05:39 +02:00
Yorick Peterse 99be3182ae Support for evaluating XPath floats. 2014-08-22 10:59:03 +02:00
Yorick Peterse 6ac3408a71 Match all node types when using node()
Previously this would only match element and text nodes.
2014-08-22 10:58:36 +02:00
Yorick Peterse da09f1296c Support for using namespace-uri() with attributes. 2014-08-21 22:36:18 +02:00
Yorick Peterse 0d41693bfc Support for using name() with attributes. 2014-08-21 21:05:06 +02:00
Yorick Peterse acc056eea4 Support for using local-name() on attributes. 2014-08-21 21:03:28 +02:00
Yorick Peterse 1dd6416bea Basic support for the XPath name() function. 2014-08-21 20:47:20 +02:00
Yorick Peterse e7019ceb4c Renamed context_node to function_node. 2014-08-21 19:06:14 +02:00
Yorick Peterse 8cc0db2283 Support for the XPath namespace-uri() function. 2014-08-21 19:02:41 +02:00
Yorick Peterse 9e20b5ca3e Support for the XPath local-name() function. 2014-08-21 10:21:02 +02:00
Yorick Peterse 2deb7a6d84 Support for the XPath id() function.
This comes with the limitation that it *always* uses the "id" attribute. This is
due to Oga not supporting DTD parsing/evaluation.
2014-08-20 21:06:35 +02:00
Yorick Peterse d351bc26cc Support for the XPath count() function. 2014-08-20 10:16:06 +02:00
Yorick Peterse 709fa365e0 XPath support for last() + evaluator docs.
I really dislike using a stack as it introduces an internal state. Sadly there
doesn't seem to be an easy way around this.
2014-08-19 22:59:26 +02:00
Yorick Peterse e0895be675 Better setup for XPath predicates.
This allows filtering of nodes by indexes (e.g. using last() or a literal
number) and uses the correct index range (1 to N in XPath). The function
position() is not yet supported as this requires access to the current node,
which isn't passed down the call stack just yet.
2014-08-19 19:56:56 +02:00
Yorick Peterse 202f74a8eb on_call_last() should return an index, not a node. 2014-08-19 19:56:41 +02:00
Yorick Peterse 423af37422 Basic support for the XPath last() function. 2014-08-18 19:00:32 +02:00
Yorick Peterse 2817784e6b Support for the XPath pipe operator. 2014-08-17 22:04:08 +02:00
Yorick Peterse d1735750c1 Only store unique nodes in XML::NodeSet.
It's called a "set" after all.
2014-08-17 22:03:44 +02:00
Yorick Peterse bb503728af XPath support for processing instructions. 2014-08-16 22:37:57 +02:00
Yorick Peterse 0d7609da88 Support for parsing XML processing instructions. 2014-08-15 22:23:26 +02:00
Yorick Peterse 8f4eaf3823 Lexing of XML processing instructions. 2014-08-15 22:04:45 +02:00
Yorick Peterse ccd95d69d8 Support for the XPath comment() test. 2014-08-15 20:49:13 +02:00
Yorick Peterse 4d7f224892 Support for the XPath text() type test. 2014-08-15 10:46:00 +02:00
Yorick Peterse 14aa420091 Use a new base class for XML text nodes.
The classes Text, Cdata and Comment now extend CharacterData instead of Text.
2014-08-15 10:43:16 +02:00
Yorick Peterse 24bc84e15e Added XML::Element#text_nodes.
This method returns all the text nodes directly nested in an element.
2014-08-15 10:07:49 +02:00
Yorick Peterse d0092b434d Removed Document#available_namespaces.
Namespaces aren't scoped per document but instead per element, thus this method
doesn't make that much sense. This also fixes the remaining, failing XPath test.
2014-08-14 23:12:33 +02:00
Yorick Peterse d34e4697de Match node types in node_matches?
The method XPath::Evaluator#node_matches? now has a special case to handle
"type-test" nodes. This in turn fixes a bunch of failing tests such as those for
the XPath query "parent::node()".
2014-08-14 22:54:19 +02:00
Yorick Peterse a437d67573 Renamed node_type to type_test. 2014-08-14 22:35:41 +02:00
Yorick Peterse 05f6fc2f8d Implement node() as a type test, not a function. 2014-08-14 22:30:14 +02:00
Yorick Peterse 6ad5170476 Support for lexing/parsing XPath type tests.
Unlike what I thought before syntax such as "node()" is not a function call.
Instead this is a special node test that tests the *types* of nodes, not their
names.
2014-08-14 21:51:58 +02:00
Yorick Peterse 23441bb5a4 Basic support for the XPath node() function. 2014-08-14 18:17:08 +02:00
Yorick Peterse a133b923a2 Only emit extra T_SLASH tokens for "//". 2014-08-13 01:28:43 +02:00
Yorick Peterse 4d956c9ef0 Support for the XPath "namespace" axis. 2014-08-11 00:58:57 +02:00
Yorick Peterse 873bd82273 Stricted matching of namespaced elements. 2014-08-11 00:47:07 +02:00
Yorick Peterse 33c28f633b Proper namespace support for elements.
This is still a bit rough on the edges but already way better than the broken
setup I had before.
2014-08-11 00:41:36 +02:00
Yorick Peterse 04cbbdcf9e Proper namespace support for attributes.
This separates namespace handling into namespace names and namespace objects.
The namespace objects are retrieved from the element an attribute belongs to.
Once retrieved the namespace is cached, due to the overhead of retrieving
namespaces in large documents.
2014-08-11 00:40:17 +02:00
Yorick Peterse fe8f77cf45 Basic work for supporting namespace URIs. 2014-08-08 19:03:42 +02:00
Yorick Peterse f002061aaa Extra type validation for XML::Element options. 2014-08-07 21:10:01 +02:00
Yorick Peterse b1388ff84a Ripped out inspect fuckery.
The old code used for generating Object#inspect values has been ripped out (for
the most part). The result is a non indented but far more compact #inspect
output. The code for this is also easier and doesn't break the signature of
Object#inspect.
2014-08-07 21:09:10 +02:00
Yorick Peterse 3b2279e410 Don't create empty Namespace nodes. 2014-08-07 20:16:46 +02:00
Yorick Peterse 4e18989972 Remove the uri attribute from Namespace.
Oga won't be handling URIs any time soon. The rationale is that they server zero
purpose when it comes to just parsing XML. Another goal of Oga is to make it
easy to modify and reserialize documents back to XML. If namespaces would also
store the URIs this would make this process more difficult.
2014-08-07 20:11:17 +02:00
Yorick Peterse 97e59fe449 Use the Namespace class for namespaces vs Strings. 2014-08-07 20:03:26 +02:00
Yorick Peterse f653203220 Tests for the Namespace class. 2014-08-07 20:02:56 +02:00
Yorick Peterse 8e8ea64206 Fixed serializing of elements to XML. 2014-08-06 00:04:42 +02:00
Yorick Peterse e0bbc81351 Added a very basic Namespace class. 2014-08-06 00:00:08 +02:00
Yorick Peterse d7df908649 Trimmed XML inspect values. 2014-08-05 23:57:12 +02:00
Yorick Peterse 26d4bdc5b1 Support for the XPath "self" axis. 2014-08-05 21:10:12 +02:00
Yorick Peterse 8a9b26fa73 Basic support for the preceding-sibling xpath axis 2014-08-05 19:28:26 +02:00
Yorick Peterse fc1d9776f3 Basic support for the XPath "preceding" axis. 2014-08-05 10:16:37 +02:00
Yorick Peterse 375f3d7870 Basic support for the XPath "parent" axis.
The usage of `parent::node()` is not yet supported.
2014-08-05 09:34:57 +02:00
Yorick Peterse c0a6610d65 Use has_parent? in on_axis_following_sibling. 2014-08-04 21:57:16 +02:00
Yorick Peterse a1f80b4995 Support for the "following-sibling" axis.
This also comes with some small cleanups regarding
XPath::Evaluator#node_matches?. This change removes the need to, every time,
also use can_match_node?() to prevent NoMethodError errors from popping up.
2014-08-04 21:51:51 +02:00
Yorick Peterse 57c0f4b35e Renamed `node` to `ast_node`.
This should make it a bit easier to understand what kind of data the variable is
holding.
2014-08-04 19:01:27 +02:00
Yorick Peterse 211caf00c6 Proper support for the XPath "following" axis. 2014-08-04 18:57:21 +02:00
Yorick Peterse 57fcbbd0fc Allow Document#each_node to skip child nodes.
Child nodes can be skipped by throwing :skip_children.
2014-08-04 10:00:32 +02:00
Yorick Peterse ef1ad5406a Don't yield indexes in Document#each_node.
These indexes won't be used so there's no point in yielding them.
2014-08-04 09:08:39 +02:00
Yorick Peterse 5c23333f46 Traverse document nodes in document order.
The method Document#each_node now yields the nodes in the correct order.
2014-08-01 23:34:32 +02:00
Yorick Peterse c419d8849b Shift instead of pop nodes when yielding all nodes 2014-08-01 19:00:29 +02:00
Yorick Peterse 34e2d28bbd Document#all_nodes -> Document#each_node
This method has been renamed and now yields nodes and their indexes instead of
buffering them in a node set.
2014-07-31 18:57:05 +02:00
Yorick Peterse 4bbf0c98ae Use breadth-first-search for returning all nodes.
This still uses a stack but at least no longer relies on the call stack. I
decided not to go with the Morris in-order algorithm [1] as it modifies the tree
during a search. This would not work well if a document were to be accessed from
multiple threads at once (which should be possible for read-only operations).

I might change this method to actually perform a search (opposed to just
returning everything). This will require some closer inspection of the
available XPath axes to determine if this is needed.

Tests will also be added once I've taken care of the above.

[1]: http://en.wikipedia.org/wiki/Tree_traversal#Morris_in-order_traversal_using_threading
2014-07-30 22:27:09 +02:00
Yorick Peterse 8fe71f298b Half-assed way of retrieving all document nodes.
This currently only works for documents, is not tested and most likely will leak
memory due to being recursive.
2014-07-30 19:56:56 +02:00
Yorick Peterse 52a4375278 Prepare setup for *actual* following support.
The previous commit was nonsense as I didn't understand XPath's "following" axis
properly. This commit introduces proper tests and a note for future me so that I
can implement it properly.
2014-07-30 00:16:44 +02:00
Yorick Peterse 9a97d936e3 Support for the XPath "following" axis. 2014-07-29 23:09:16 +02:00
Yorick Peterse 55e3388e30 Unfuck XPath axes evaluation.
The evaluation of axes has been fixed by changing the initial context as well as
the behaviour of some of the handler methods.

The initial context has been changed so that it's simply a NodeSet of whatever
the root object is, either a Document or an Element instance. Previously this
would be set to the child nodes of a Document in case the root object was a
Document. This in turn would prevent "child" axes from operating correctly.
2014-07-28 00:44:05 +02:00
Yorick Peterse 23de57a3a0 Parse bare XPath node tests as child axes.
When parsing a bare node test such as "A" this is now parsed as following:

    (axis "child" (test nil "A"))

Instead of this:

    (test nil "A")

According to the XPath specification both are identical and this simplifies some
of the code in the XPath evaluator.
2014-07-28 00:34:26 +02:00
Yorick Peterse 1916799fef Basic boilerplate for descendant-or-self. 2014-07-25 21:24:39 +02:00
Yorick Peterse dd37b028a0 Support for the XPath descendant axis. 2014-07-24 09:49:05 +02:00
Yorick Peterse fd37bcff1f Corrected an XPath example. 2014-07-23 18:48:53 +02:00
Yorick Peterse 54e109bf97 Corrected various YARD tags. 2014-07-22 21:28:44 +02:00
Yorick Peterse a0ecba6321 Support for the XPath child axis. 2014-07-22 21:25:02 +02:00
Yorick Peterse ec08b41737 Specs for various XPath::Evaluator helper methods. 2014-07-22 21:04:09 +02:00
Yorick Peterse 1f9d2ede95 Skip node matching for incompatible nodes. 2014-07-22 20:45:15 +02:00
Yorick Peterse 05cf07755d Basic docs for the XPath evaluator. 2014-07-22 16:12:21 +02:00
Yorick Peterse 4142504101 Revert "Lex XPath axes without a node test."
Upon further investigation this change turned out to be useless. Nokogiri/libxml
does not allow the use of long axes without tests, instead it ends up
lexing/parsing such a value as a simple node test.

This reverts commit f699b0d097.
2014-07-22 15:53:00 +02:00
Yorick Peterse c43c38fab9 Emit node() calls for certain short axes.
An axes such as "." is the same as "self::node()". To simplify things on
parser/evaluator level we'll emit the corresponding tokens for a "node()"
function call for these axes.
2014-07-22 15:50:39 +02:00
Yorick Peterse f699b0d097 Lex XPath axes without a node test. 2014-07-20 07:57:45 +02:00
Yorick Peterse 488000438b Support for querying attributes using XPath. 2014-07-20 07:47:01 +02:00
Yorick Peterse d5569ead0b Use XML::Attribute for element attributes.
Instead of using a raw Hash Oga now uses the XML::Attribute class for storing
information about element attributes.

Attributes are stored as an Array of XML::Attribute instances. This allows the
attributes to be more easily modified. If they were stored as a Hash you'd not
only have to update the attributes themselves but also the Hash that contains
them.

While using an Array has a slight runtime cost in most cases the amount of
attributes is small enough that this doesn't really pose a problem. If webscale
performance is desired at some point in the future Oga could most likely cache
the lookup of an attribute. This however is something for the future.
2014-07-20 07:29:37 +02:00
Yorick Peterse ce86785da6 Added the XML::Attribute class.
This class will replace the use of raw Hash/String values for attributes in
upcoming commits.
2014-07-16 10:08:11 +02:00
Yorick Peterse d09ab26680 Support for the ancestor-or-self axis. 2014-07-15 09:41:12 +02:00
Yorick Peterse 0211f60826 Evaluation of XPath "ancestor" axes. 2014-07-14 09:43:30 +02:00
Yorick Peterse 03f40d4024 Ripped out internal state of XPath::Evaluator.
Instead of keeping track of an internal state in @stack and @context the various
processing methods now take the context as an extra argument and return the
nodes they produced. This makes it easier to recursively call certain methods, a
requirement for processing XPath axes (e.g. the "ancestor" axis).
2014-07-14 00:07:43 +02:00
Yorick Peterse 56982dd543 Fail early when processing (test) nodes.
If a certain segment yields no nodes the on_path() method should not process any
following segments.
2014-07-12 00:22:18 +02:00
Yorick Peterse d6aec6aa16 Compare nodes in on_path instead of indexes.
This saves some code and effectively does the same thing.
2014-07-12 00:20:19 +02:00
Yorick Peterse 5808ffa7a4 Updated XPath evaluator for the new AST.
This is still a bit of a hack. Then again, it already was a hack to begin with.
2014-07-12 00:18:32 +02:00
Yorick Peterse be4f4ad744 Trimmed the XPath AST even further.
The excessive use of (path) nodes has been dropped and all parsing conflicts
have been dealt with.
2014-07-12 00:13:25 +02:00
Yorick Peterse aeb4eba260 Trim the XPath AST.
The AST has been simplified by adjusting the way (path) nodes are nested.
Operators now also use `paths` instead of `expression` to allow for expressions
such as `/A or /B`. Sadly this introduces quite a bunch of conflicts in the
parser but we'll deal with those later if needed.
2014-07-10 20:19:23 +02:00
Yorick Peterse 8fbc582547 XPath evaluation for name/namespace wildcards. 2014-07-09 22:09:20 +02:00
Yorick Peterse ed45058983 Basic support for evaluating XPath wildcards. 2014-07-09 20:06:31 +02:00