Skip to content

Commit

Permalink
parse without substitution
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 20, 2015
1 parent 02c6af0 commit 26947c4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
14 changes: 7 additions & 7 deletions lib/Mojo/DOM/CSS.pm
Expand Up @@ -72,36 +72,36 @@ sub _compile {
my $css = "$_[0]";

my $pattern = [[]];
while ($css) {
while (1) {

# Separator
my $part = $pattern->[-1];
push @$part, [] unless @$part && ref $part->[-1];
my $selector = $part->[-1];
if ($css =~ s/^\s*,\s*//) { push @$pattern, [] }
if ($css =~ /\G\s*,\s*/gc) { push @$pattern, [] }

# Combinator
elsif ($css =~ s/^\s*([ >+~])\s*//) { push @$part, $1 }
elsif ($css =~ /\G\s*([ >+~])\s*/gc) { push @$part, $1 }

# Class or ID
elsif ($css =~ s/^([.#])((?:$ESCAPE_RE\s|\\.|[^,.#:[ >~+])+)//o) {
elsif ($css =~ /\G([.#])((?:$ESCAPE_RE\s|\\.|[^,.#:[ >~+])+)/gco) {
my ($name, $op) = $1 eq '.' ? ('class', '~') : ('id', '');
push @$selector, ['attr', _name($name), _value($op, $2)];
}

# Attributes
elsif ($css =~ s/^$ATTR_RE//o) {
elsif ($css =~ /\G$ATTR_RE/gco) {
push @$selector, ['attr', _name($1), _value($2 // '', $3 // $4, $5)];
}

# Pseudo-class (":not" contains more selectors)
elsif ($css =~ s/^:([\w\-]+)(?:\(((?:\([^)]+\)|[^)])+)\))?//) {
elsif ($css =~ /\G:([\w\-]+)(?:\(((?:\([^)]+\)|[^)])+)\))?/gcs) {
push @$selector,
['pc', lc $1, $1 eq 'not' ? _compile($2) : _equation($2)];
}

# Tag
elsif ($css =~ s/^((?:$ESCAPE_RE\s|\\.|[^,.#:[ >~+])+)//o) {
elsif ($css =~ /\G((?:$ESCAPE_RE\s|\\.|[^,.#:[ >~+])+)/gco) {
push @$selector, ['tag', _name($1)] unless $1 eq '*';
}

Expand Down
24 changes: 12 additions & 12 deletions lib/Mojo/Util.pm
Expand Up @@ -391,28 +391,28 @@ sub _global_destruction {
sub _header {
my ($str, $cookie) = @_;

my (@tree, @token);
while ($str =~ s/^[,;\s]*([^=;, ]+)\s*//) {
push @token, $1, undef;
my $expires = $cookie && @token > 2 && lc $1 eq 'expires';
my (@tree, @part);
while ($str =~ /\G[,;\s]*([^=;, ]+)\s*/gc) {
push @part, $1, undef;
my $expires = $cookie && @part > 2 && lc $1 eq 'expires';

# Special "expires" value
if ($expires && $str =~ s/^=\s*$EXPIRES_RE//o) { $token[-1] = $1 }
if ($expires && $str =~ /\G=\s*$EXPIRES_RE/gco) { $part[-1] = $1 }

# Quoted value
elsif ($str =~ s/^=\s*("(?:\\\\|\\"|[^"])*")//) { $token[-1] = unquote $1 }
elsif ($str =~ /\G=\s*("(?:\\\\|\\"|[^"])*")/gc) { $part[-1] = unquote $1 }

# Unquoted value
elsif ($str =~ s/^=\s*([^;, ]*)//) { $token[-1] = $1 }
elsif ($str =~ /\G=\s*([^;, ]*)/gc) { $part[-1] = $1 }

# Separator
next unless $str =~ s/^[;\s]*,\s*//;
push @tree, [@token];
@token = ();
next unless $str =~ /\G[;\s]*,\s*/gc;
push @tree, [@part];
@part = ();
}

# Take care of final token
return [@token ? (@tree, \@token) : @tree];
# Take care of final part
return [@part ? (@tree, \@part) : @tree];
}

sub _options {
Expand Down
3 changes: 0 additions & 3 deletions t/mojo/dom.t
Expand Up @@ -10,9 +10,6 @@ is(Mojo::DOM->new->parse(''), '', 'right result');
is(Mojo::DOM->new->at('p'), undef, 'no result');
is(Mojo::DOM->new->append_content(''), '', 'right result');

# Invalid selector
is(Mojo::DOM->new->at('...'), undef, 'no result');

# Simple (basics)
my $dom = Mojo::DOM->new(
'<div><div FOO="0" id="a">A</div><div id="b">B</div></div>');
Expand Down

0 comments on commit 26947c4

Please sign in to comment.