ingydotnet/pquery-pm — Issue #2: ^= etc
Author: st3vil
Created: 2011-10-29 11:34:39+00:00
State: open

Hello there. I would like to complain about the lack of comments and general difficulty to just hack here and there. Furthermore...

Attribute matchings are in a state of disarray. I'm sorry I can't help further, see above. Here are some slides from my vacation:

``` perl
--- a/lib/pQuery.pm
+++ b/lib/pQuery.pm
@@ -384,7 +384,7 @@ sub _grep {
my ($this, $elems, $callback, $inv) = @_;
my $ret = [];

- for (my ($i, $length) = (0, scalar(@$elems)); $i < $length; $i++) {
+ for my $i (0..@$elems) {
push @$ret, $elems->[$i]
if (not $inv and &$callback($elems->[$i], $i)) or
($inv and not &$callback($elems->[$i], $i));
```
Incredibly the readable 'for' syntax breaks things. Could you tell me why?

``` perl
@@ -485,7 +485,7 @@ my $expr = {
# The regular expressions that power the parsing engine
my $parse = [
# Match: [@value='test'], [@foo]
- qr/^(\[) *@?([\w-]+) *([!*$^~=]*) *('?"?)(.*?)\4 *\]/,
+ qr/^(\[) *@?([\w-]+) *([!*\$^~|=]*) *('?"?)(.*?)\4 *\]/,

# Match: :contains('foo')
qr/^(:)([\w-]+)\("?'?(.*?(\(.*?\))?[^(]*?)"?'?\)/,
```
The variable $^ was being interpolated. Added support for the |= type.

``` perl
@@ -711,10 +711,11 @@ sub _filter {
$type eq "" and $z or
$type eq "=" and $z eq $m->[5] or
$type eq "!=" and $z ne $m->[5] or
- $type eq "^=" and not index($z, $m->[5]) or
- $type eq '$=' and substr($z, (0-length($m->[5]))) or
- ($type eq "*=" or $type eq "~=") and
- index($z, $m->[5]) >= 0
+ $type eq "^=" and $z =~ /^\Q$m->[5]\E/ or
+ $type eq '$=' and $z =~ /\Q$m->[5]\E$/ or
+ $type eq "*=" and $z =~ /\Q$m->[5]\E/ or
+ $type eq "~=" and $z =~ /\W\Q$m->[5]\E\W/ or
+ $type eq "|=" and $z =~ /^\Q$m->[5]\E(?:-.+)$/
) ? 1 : 0) ^ ($not ? 1 : 0)
) { push @$tmp, $a }
}
```
See the jquery docs. It still doesn't work but I give up.

``` perl
@@ -65,3 +65,8 @@ is pQuery(':header')->size, 2,
'Two Headers';

is pQuery("table#table1")->size, 1, 'id after other selector';
+
+is pQuery('[href="http://en.wikipedia.org/wiki/Bookmarklet"]')->size, 1, 'attribute is';
+is pQuery('[href$="Bookmarklet"]')->size, 1, 'attribute ends with';
+is pQuery('[href^="http"]')->size, 1, 'attribute starts with';
+is pQuery('[class|="table"]')->[0]->tagName, 'tr', 'attribute starts with and might be followed by a dash and then something';
```

Bah. All I wanted to do was find all the links in a bit of html to somewhere under /Member/Listings.aspx, right.