Commit Graph

450 Commits

Author SHA1 Message Date
Yorick Peterse 448ff56e38 Fixed CSS eval specs for nth-(first|last)-of-type. 2014-11-15 18:27:26 +01:00
Yorick Peterse b464815577 Fixed AST generation for nth-(first|last)-of-type. 2014-11-15 18:27:15 +01:00
Yorick Peterse 9eead81a7c Fixed AST for :only-of-type 2014-11-15 18:08:26 +01:00
Yorick Peterse 1c301d40e2 Properly fixed AST for first-of-type/last-of-type
This requires keeping track of the current element being processed. This in turn
allows the usage of count() + preceding-sibling/following-sibling.
2014-11-15 17:58:56 +01:00
Yorick Peterse e559b4b89b Corrected :first-of-type eval spec. 2014-11-15 17:20:17 +01:00
Yorick Peterse f1d574f342 Evaluate XPath predicates for every context node.
Instead of evaluating a predicate once for all context nodes, they should
instead be evaluated separately per context node.
2014-11-15 00:31:44 +01:00
Yorick Peterse 6daa3e7a00 Reverted AST changes for first-of-type
Functions can't be used in combination with axes, so I'll just need to fix the
position() function to work properly.
2014-11-14 23:51:46 +01:00
Yorick Peterse 2d6a2be2e8 Revert "Fixed XPath AST for :last-of-type"
Axes can't be used in combination with functions.

This reverts commit b0b572a584.
2014-11-14 23:49:49 +01:00
Yorick Peterse 8f3553f8f1 Fixed eval specs of :first-of-type & :last-of-type 2014-11-14 23:27:52 +01:00
Yorick Peterse b0b572a584 Fixed XPath AST for :last-of-type
This should count following nodes, not merely the position.
2014-11-14 23:27:15 +01:00
Yorick Peterse 0128dc50ae Fixed CSS evaluation of :first-of-type
The old XPath "position() = 1" would work in Nokogiri due to the way they
retrieve descendants. In Oga however this would simply always return the first
node.

To fix this Oga now counts the amount of preceding siblings that match the same
full name.
2014-11-14 01:25:03 +01:00
Yorick Peterse e3a26c5d15 Allow querying of nodes using CSS. 2014-11-14 01:05:29 +01:00
Yorick Peterse d47ca19ffa Remaining CSS evaluation specs. 2014-11-14 00:24:54 +01:00
Yorick Peterse 518bedc3a1 CSS evaluator specs for :nth-of-type 2014-11-14 00:16:35 +01:00
Yorick Peterse abfe6e3d61 CSS evaluator specs for nth-last-of-type 2014-11-14 00:13:11 +01:00
Yorick Peterse c874ceabb9 CSS evaluator specs for :nth-last-child 2014-11-13 23:54:47 +01:00
Yorick Peterse 5964a2cda4 CSS evaluator specs for :nth-child(n) 2014-11-13 22:50:04 +01:00
Yorick Peterse 3237617bf5 CSS eval specs for various pseudo classes.
This includes the following pseudos:

* :empty
* :first-child
* :first-of-type
* :last-child
* :last-of-type
2014-11-13 10:10:31 +01:00
Yorick Peterse 27ffa4d3d5 Added various failing following/preceding specs. 2014-11-13 01:11:13 +01:00
Yorick Peterse 97a9a11db1 Failing CSS evaluation specs for the axes.
These currently fail due to the ~ and + axes not being evaluated properly.
2014-11-12 23:38:17 +01:00
Yorick Peterse 817a5e075b Wrap predicate AST nodes _around_ other nodes.
This means that "foo[1]" uses this AST:

    (predicate (test nil "foo") (int 1))

Instead of this AST:

    (test nil "foo" (int 1))

This makes it easier for the XPath evaluator to process predicates correctly.
2014-11-12 22:59:38 +01:00
Yorick Peterse 24350fa457 Added various predicate specs for XPath axes. 2014-11-12 09:40:22 +01:00
Yorick Peterse c15604a86f CSS evaluator specs for IDs. 2014-11-11 00:21:28 +01:00
Yorick Peterse b9e1b51270 CSS evaluator specs for classes. 2014-11-11 00:18:44 +01:00
Yorick Peterse 43200238c5 CSS evaluator specs for predicates and operators. 2014-11-10 23:21:00 +01:00
Yorick Peterse d5002010fe Removed RSpec shared examples. 2014-11-10 00:06:26 +01:00
Yorick Peterse 1f855dc982 Rewrote CSS evaluator paths spec. 2014-11-09 23:51:23 +01:00
Yorick Peterse fb1927a7c7 Removed use of shared examples in Element specs. 2014-11-09 23:44:14 +01:00
Yorick Peterse ccbb19a42d Rewrote all XPath evaluator type specs. 2014-11-09 23:43:01 +01:00
Yorick Peterse c5c3c5dbc3 Rewrote all XPath evaluator type test specs. 2014-11-09 23:39:47 +01:00
Yorick Peterse ca5da3f9c9 Rewrote all XPath evaluator operator tests. 2014-11-09 23:31:05 +01:00
Yorick Peterse 28a1f1b8a9 Rewrote all XPath evaluator call specs. 2014-11-09 23:23:52 +01:00
Yorick Peterse 58209cbad0 Rewrote all XPath axis specs. 2014-11-09 22:53:47 +01:00
Yorick Peterse 5f13cc9d73 Rewrote XPath evaluator wildcards spec. 2014-11-09 18:56:49 +01:00
Yorick Peterse ceed3a6046 Rewrote XPath evaluator predicates spec. 2014-11-09 18:52:04 +01:00
Yorick Peterse 3893e56ca8 Rewrote XPath evaluator paths spec.
This is the first spec of many that will be re-written. Eventually this will
remove the need of the shared examples as well as removing lots of code
duplication and odd context blocks.
2014-11-09 18:47:20 +01:00
Yorick Peterse 857ac517d5 Added XML::NodeSet#==
This method can be used to compare two NodeSet instances. By using
XML::NodeSet#equal_nodes?() the need for exposing the "nodes" instance variable
is also removed.
2014-11-09 18:33:16 +01:00
Yorick Peterse 7e38e20586 Use "include" vs "return" in CSS path spec. 2014-11-07 09:28:18 +01:00
Yorick Peterse a586a512b8 Removed support for CSS such as "|X"
This expression could be used to get all elements that _don't_ have any
namespace. The problem is that this can't be expressed as just a node test,
instead the resulting XPath would have to look something like the following:

    X[local-name() = name()]

However, since the XPath predicates are already created for pseudo classes and
such, also injecting the above into it would be a real big pain. As such I've
decided not to support it.
2014-11-05 00:45:40 +01:00
Yorick Peterse 661741268a CSS parser tests for paths with namespaces. 2014-11-05 00:44:23 +01:00
Yorick Peterse 61801fe562 Allow CSS identifiers to start with an underscore. 2014-11-05 00:37:40 +01:00
Yorick Peterse f1316c50fb Allow XPath idents to start with an underscore.
This is valid in XML so XPath should support it as well.
2014-11-05 00:35:47 +01:00
Yorick Peterse eb3a3e7630 Use the descendant axis for CSS selectors.
Instead of using "descendant-or-self" Oga will use "descendant". This ensures
that expressions such as "foo *" don't return a set also including the "foo"
element.

Nokogiri solves this problem in a somewhat different way by using //foo//* for
the CSS expression "foo *". While this works in Nokogiri the expression
"descendant-or-self::node()" is slow as a snail in Oga (due to its nature of
retrieving _all_ nodes first). By using "descendant" we can work around this
problem.
2014-11-05 00:22:09 +01:00
Yorick Peterse 602f2fe8bb Apply node() type tests to the document too.
When running XPath queries such as "self::node()" the result should be a set
containing the document itself. This in turn fixes expressions such as
descendant-or-self::node()/a.
2014-11-04 23:41:45 +01:00
Yorick Peterse 1f3b4cb2fb Added initial CSS evaluation tests. 2014-11-04 23:34:01 +01:00
Yorick Peterse f6319ed0c7 Added failing descendant-or-self XPath specs.
These currently fail due to the child:: selector not working entirely as it
should be. Consider the following XML:

    <a><b><b><c class="x"></c></b></b></a>

And the following XPath:

    descendant-or-self::node()/a

In Nokogiri/libxml this will return a node set containing the <a> node. In Oga
however this will return an empty node set. This will require some further
investigation to see what exactly is going on, and in particular what is the
correct behaviour.
2014-11-04 23:34:01 +01:00
Yorick Peterse de04c61df9 Parsing support for the :empty pseudo. 2014-11-04 00:00:23 +01:00
Yorick Peterse 1b870406de Parsing support for the :only-of-type pseudo. 2014-11-03 23:47:25 +01:00
Yorick Peterse 2cf058457d Parsing support for the :only-child pseudo. 2014-11-03 23:27:56 +01:00
Yorick Peterse 759bb71d17 Parsing support for the :last-of-type pseudo. 2014-11-03 23:01:44 +01:00
Yorick Peterse 10632eafd4 Parsing support for the :first-of-type pseudo. 2014-11-03 23:00:15 +01:00
Yorick Peterse 7a606a11d3 Parsing support for the :last-child pseudo class. 2014-11-03 22:58:08 +01:00
Yorick Peterse b4fec3cc8c Parsing support for the :first-child pseudo class. 2014-11-03 22:45:00 +01:00
Yorick Peterse 020d979fba Parsing support for :nth-last-of-type(). 2014-11-03 22:15:21 +01:00
Yorick Peterse ab8b451dc3 Parsing support for :nth-of-type() 2014-11-02 19:29:39 +01:00
Yorick Peterse 0faceffacb Parsing support for nth-child(n+X) 2014-11-02 19:29:09 +01:00
Yorick Peterse 8d8d74ec41 Drop nth-child support of all negative sequences
This removes parsing support for selectors such as :nth-child(-n-6). According
to the CSS spec this isn't valid anyway (confirmed by testing it in Chromium).
As a result there's no point in supporting it in any way.
2014-11-02 19:19:05 +01:00
Yorick Peterse b31288b7d2 Use correct modulo for nth-child and negatives. 2014-11-02 18:53:23 +01:00
Yorick Peterse 9cce93fc4a Parsing support for :nth-last-child. 2014-11-01 20:58:28 +01:00
Yorick Peterse 64f9c570fa Use separate spec files for each pseudo class. 2014-11-01 20:20:33 +01:00
Yorick Peterse 03f897c2b7 Support for all possible nth-child arguments.
That is, as far as I can tell based on Nokogiri's behaviour (which Oga now
matches).
2014-10-30 23:03:46 +01:00
Yorick Peterse 0e6aefb727 Fixed parsing of nth-child(n) and nth-child(-n) 2014-10-30 00:24:31 +01:00
Yorick Peterse 87f6b9c723 Basic support for :nth-child()
This already includes support for formulas such as 2n, odd, even and 2n+1.
Negative formulas, just "n" and others are not yet supported.
2014-10-28 00:21:11 +01:00
Yorick Peterse 46646e2ace Support for custom grouping of XPath expressions.
This allows the use of expressions such as "(A or B) and C".

This fixes #59.
2014-10-26 22:38:05 +01:00
Yorick Peterse 39c0f7147c Support for parsing the :root pseudo class. 2014-10-26 22:20:23 +01:00
Yorick Peterse 32764c9a14 Proper parsing support for all CSS operators. 2014-10-26 12:45:43 +01:00
Yorick Peterse 24ae791f00 Better support for lexing multi-line strings.
When lexing multi-line strings everything used to work fine as long as the input
were to be read as a whole. However, when using an IO instance all hell would
break loose. Due to the lexer reading IO instances on a per line basis,
sometimes Ragel would end up setting "ts" to NULL. For example, the following
input would break the lexer:

    <foo class="\nbar" />

Due to the input being read per line, the following data would be sent to the
lexer:

    <foo class="\n
    bar" />

This would result in different (or NULL) pointers being used for building a
string, in turn resulting in memory allocation errors.

To work around this the string lexing setup has been broken into separate
machines for single and double quoted strings. The tokens used have also been
changed so that instead of just "T_STRING" there are now the following tokens:

* T_STRING_SQUOTE
* T_STRING_DQUOTE
* T_STRING_BODY

A string can have multiple T_STRING_BODY tokens (= multi-line strings, only the
case for IO inputs). These strings are stitched back together by the parser.

This fixes #58.
2014-10-26 11:39:56 +01:00
Yorick Peterse b304b8b077 Fixed descendant-or-self with a predicate.
Processing of this axis along with a predicate wouldn't quite work out. Even if
the predicate returned false the node would still be matched (which should not
be the case).
2014-10-23 01:12:10 +02:00
Yorick Peterse 7ee7f25239 Support for parsing CSS axes. 2014-10-23 00:42:45 +02:00
Yorick Peterse 9955f61bcb Renamed CSS axis tokens.
These have been renamed as following:

T_CHILD => T_GREATER
T_FOLLOWING => T_TILDE
T_FOLLOWING_DIRECT => T_PLUS
2014-10-21 23:25:11 +02:00
Yorick Peterse e3de65a258 Lex whitespace preceding CSS axes separately.
Previously input such as "x > y" would result in the following token sequences:

    T_IDENT, T_CHILD, T_IDENT

This commit changes this to the following:

    T_IDENT, T_SPACE, T_CHILD, T_IDENT

This allows the parser to use T_SPACE as a terminal token, this in turn prevents
around 16 shift/reduce conflicts from arising.

This does mean that input such as " > y" or " x > y" is now invalid. This
however can be solved by simply _not_ adding leading/trailing whitespace to CSS
queries.
2014-10-21 23:18:46 +02:00
Yorick Peterse e2b4f51e64 Updated part of the CSS axis specs. 2014-10-20 19:07:06 +02:00
Yorick Peterse 21c27bf48e Surround class values with spaces.
When using a CSS class selector the resulting XPath string passed to contains()
should be surrounded by spaces.
2014-10-20 09:29:42 +02:00
Yorick Peterse 15ebdb7de4 Fixed parsing of CSS class selectors.
When a class selector is used it should be checked as one of the possible
values, not as _the_ only value (unlike ID selectors).
2014-10-20 00:45:41 +02:00
Yorick Peterse 174d33c597 Re-enabled parsing of CSS predicates. 2014-10-20 00:39:12 +02:00
Yorick Peterse d4150fd0f5 First step at rewriting the CSS parser.
The new setup will not involve a separate transformation stage, instead the CSS
parser will directly emit an XPath AST. This reduces the overhead needed for
parsing/evaluating CSS selectors while also simplifying the code. The downside
is that I basically have to re-write 80% of the parser.
2014-10-20 00:30:16 +02:00
Yorick Peterse ea2baa2020 Swap child node order for CSS pseudo classes. 2014-10-16 23:18:14 +02:00
Yorick Peterse 63d27fa709 Swap child order of CSS class and id nodes.
This makes it easier to transform the AST at a later stage.
2014-10-16 23:13:54 +02:00
Yorick Peterse 7ccd685acb Use a helper method for transforming CSS ASTs. 2014-10-16 23:01:56 +02:00
Yorick Peterse a85cd7cbd1 Trimmed CSS class transformer specs a bit. 2014-10-16 22:51:55 +02:00
Yorick Peterse 5fde2f9092 Basic tests for the CSS transformer. 2014-10-16 10:25:30 +02:00
Yorick Peterse 48eb4f83df Lexing/parsing of CSS pseudos with ident arguments
This allows the lexing/parsing of expressions such as "html:lang(en)".
2014-10-15 09:42:26 +02:00
Yorick Peterse d9a4221a0a Remove :axis CSS node types.
The various axes are now simply their own node types.
2014-10-12 18:08:35 +02:00
Yorick Peterse ed0cd7826e Fixed precedence of ID/class CSS selectors 2014-10-07 23:05:34 +02:00
Yorick Peterse 91f9cc984b Parsing of pseudo classes without node tests. 2014-10-07 23:01:58 +02:00
Yorick Peterse a6b0bd96c8 Support for parsing CSS class/ID selectors. 2014-10-07 22:57:23 +02:00
Yorick Peterse 6792127600 Reworked CSS parser rules.
This includes better rules for parsing separate path members, pseudo class
arguments and some changes to remove all remaining parsing conflicts.
2014-10-07 22:47:47 +02:00
Yorick Peterse b40c0243ce Tighten up lexing of CSS predicates.
Operators can now only occur inside predicates and any whitespcae in these
predicates is ignored.
2014-10-07 22:17:04 +02:00
Yorick Peterse 625b9eeffd Lexing of CSS axes with surrounding whitespace. 2014-10-07 22:06:45 +02:00
Yorick Peterse 619c0bbc14 Emit tokens for whitespace in the CSS lexer. 2014-10-07 21:55:41 +02:00
Yorick Peterse 6e18287a1d Initial specs for parsing CSS IDs/classes. 2014-10-07 19:01:04 +02:00
Yorick Peterse 09315ea478 Test for operators inside CSS predicates. 2014-10-07 09:32:34 +02:00
Yorick Peterse 16d66a7eb6 Better parsing for the nth-child pseudo class.
This uses stricter (and more correct) rules in both the lexer and the parser.
The resulting AST has also received a small rework to make it more compact and
less confusing.
2014-10-06 23:52:46 +02:00
Yorick Peterse 60da2bdd3a Use RSpec.shared_example vs just shared_example. 2014-10-05 23:52:12 +02:00
Yorick Peterse d0a8a3b18c Basic support for parsing CSS pseudo classes.
This currently does not yet allow chained pseudo classes, nor does it allow for
pseudos such as nth-child(2n).
2014-10-05 23:46:41 +02:00
Yorick Peterse 50ee66419e Rename CSS "node operators" to "axes". 2014-10-05 23:33:46 +02:00
Yorick Peterse e1832adc97 Specs for parsing CSS node test wildcards. 2014-10-05 10:09:21 +02:00
Yorick Peterse 8fef62fca0 Support for parsing CSS operators. 2014-10-05 10:06:58 +02:00
Yorick Peterse 2dd148539d Parsing of CSS predicates.
This adds support for parsing expressions such as "foo[class]".
2014-10-05 09:32:21 +02:00
Yorick Peterse 665d5fe08c Added basic specs for the CSS parser. 2014-10-05 01:28:31 +02:00