Class: Nokogiri::CSS::Parser

Inherits:
Racc::Parser
  • Object
show all
Defined in:
lib/nokogiri/css/parser.rb,
lib/nokogiri/css/parser.rb,
lib/nokogiri/css/parser_extras.rb

Overview

:nodoc:

Constant Summary collapse

Racc_arg =
[
racc_action_table,
racc_action_check,
racc_action_default,
racc_action_pointer,
racc_goto_table,
racc_goto_check,
racc_goto_default,
racc_goto_pointer,
racc_nt_base,
racc_reduce_table,
racc_token_table,
racc_shift_n,
racc_reduce_n,
racc_use_result_var ]
Racc_token_to_s_table =
[
"$end",
"error",
"FUNCTION",
"INCLUDES",
"DASHMATCH",
"LBRACE",
"HASH",
"PLUS",
"GREATER",
"S",
"STRING",
"IDENT",
"COMMA",
"NUMBER",
"PREFIXMATCH",
"SUFFIXMATCH",
"SUBSTRINGMATCH",
"TILDE",
"NOT_EQUAL",
"SLASH",
"DOUBLESLASH",
"NOT",
"EQUAL",
"RPAREN",
"LSQUARE",
"RSQUARE",
"HAS",
"\"@\"",
"\".\"",
"\"*\"",
"\"|\"",
"\":\"",
"$start",
"selector",
"simple_selector_1toN",
"prefixless_combinator_selector",
"optional_S",
"combinator",
"xpath_attribute_name",
"xpath_attribute",
"simple_selector",
"element_name",
"hcap_0toN",
"function",
"pseudo",
"attrib",
"hcap_1toN",
"class",
"namespaced_ident",
"namespace",
"attrib_name",
"attrib_val_0or1",
"expr",
"nth",
"attribute_id",
"negation",
"eql_incl_dash",
"negation_arg" ]
Racc_debug_parser =
false
CACHE_SWITCH_NAME =
:nokogiri_css_parser_cache_is_off

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespaces = {}) ⇒ Parser

Create a new CSS parser with respect to namespaces



60
61
62
63
64
# File 'lib/nokogiri/css/parser_extras.rb', line 60

def initialize(namespaces = {})
  @tokenizer = Tokenizer.new
  @namespaces = namespaces
  super()
end

Class Method Details

.[](string) ⇒ Object

Get the css selector in string from the cache



25
26
27
28
29
# File 'lib/nokogiri/css/parser_extras.rb', line 25

def [](string)
  return unless cache_on?

  @mutex.synchronize { @cache[string] }
end

.[]=(string, value) ⇒ Object

Set the css selector in string in the cache to value



32
33
34
35
36
# File 'lib/nokogiri/css/parser_extras.rb', line 32

def []=(string, value)
  return value unless cache_on?

  @mutex.synchronize { @cache[string] = value }
end

.cache_on?Boolean

Return a thread-local boolean indicating whether the CSS-to-XPath cache is active. (Default is ‘true`.)

Returns:

  • (Boolean)


15
16
17
# File 'lib/nokogiri/css/parser_extras.rb', line 15

def cache_on?
  !Thread.current[CACHE_SWITCH_NAME]
end

.clear_cache(create_new_object = false) ⇒ Object

Clear the cache



39
40
41
42
43
44
45
46
47
# File 'lib/nokogiri/css/parser_extras.rb', line 39

def clear_cache(create_new_object = false)
  @mutex.synchronize do
    if create_new_object
      @cache = {}
    else
      @cache.clear
    end
  end
end

.set_cache(value) ⇒ Object

Set a thread-local boolean to turn cacheing on and off. Truthy values turn the cache on, falsey values turn the cache off.



20
21
22
# File 'lib/nokogiri/css/parser_extras.rb', line 20

def set_cache(value) # rubocop:disable Naming/AccessorMethodName
  Thread.current[CACHE_SWITCH_NAME] = !value
end

.without_cache(&block) ⇒ Object

Execute block without cache



50
51
52
53
54
55
56
# File 'lib/nokogiri/css/parser_extras.rb', line 50

def without_cache(&block)
  original_cache_setting = cache_on?
  set_cache(false)
  yield
ensure
  set_cache(original_cache_setting)
end

Instance Method Details

#_reduce_1(val, _values, result) ⇒ Object

reduce 0 omitted



361
362
363
364
365
# File 'lib/nokogiri/css/parser.rb', line 361

def _reduce_1(val, _values, result)
      result = [val[0], val[2]].flatten

    result
end

#_reduce_10(val, _values, result) ⇒ Object



407
408
409
410
# File 'lib/nokogiri/css/parser.rb', line 407

def _reduce_10(val, _values, result)
 result = Node.new(:ATTRIB_NAME, [val[0]])
    result
end

#_reduce_11(val, _values, result) ⇒ Object



412
413
414
415
416
417
418
419
420
# File 'lib/nokogiri/css/parser.rb', line 412

def _reduce_11(val, _values, result)
      result =  if val[1].nil?
                  val[0]
                else
                  Node.new(:CONDITIONAL_SELECTOR, [val[0], val[1]])
                end

    result
end

#_reduce_13(val, _values, result) ⇒ Object

reduce 12 omitted



424
425
426
427
# File 'lib/nokogiri/css/parser.rb', line 424

def _reduce_13(val, _values, result)
 result = Node.new(:CONDITIONAL_SELECTOR, val)
    result
end

#_reduce_14(val, _values, result) ⇒ Object



429
430
431
432
# File 'lib/nokogiri/css/parser.rb', line 429

def _reduce_14(val, _values, result)
 result = Node.new(:CONDITIONAL_SELECTOR, val)
    result
end

#_reduce_15(val, _values, result) ⇒ Object



434
435
436
437
# File 'lib/nokogiri/css/parser.rb', line 434

def _reduce_15(val, _values, result)
 result = Node.new(:CONDITIONAL_SELECTOR, [Node.new(:ELEMENT_NAME, ['*']), val[0]])
    result
end

#_reduce_17(val, _values, result) ⇒ Object

reduce 16 omitted



441
442
443
444
# File 'lib/nokogiri/css/parser.rb', line 441

def _reduce_17(val, _values, result)
 result = Node.new(val[0], [nil, val[1]])
    result
end

#_reduce_18(val, _values, result) ⇒ Object



446
447
448
449
# File 'lib/nokogiri/css/parser.rb', line 446

def _reduce_18(val, _values, result)
 result = Node.new(val[1], [val[0], val[2]])
    result
end

#_reduce_19(val, _values, result) ⇒ Object



451
452
453
454
# File 'lib/nokogiri/css/parser.rb', line 451

def _reduce_19(val, _values, result)
 result = Node.new(:DESCENDANT_SELECTOR, [val[0], val[2]])
    result
end

#_reduce_2(val, _values, result) ⇒ Object



367
368
369
370
# File 'lib/nokogiri/css/parser.rb', line 367

def _reduce_2(val, _values, result)
 result = val.flatten
    result
end

#_reduce_21(val, _values, result) ⇒ Object

reduce 20 omitted



458
459
460
461
# File 'lib/nokogiri/css/parser.rb', line 458

def _reduce_21(val, _values, result)
 result = Node.new(:CLASS_CONDITION, [unescape_css_identifier(val[1])])
    result
end

#_reduce_23(val, _values, result) ⇒ Object

reduce 22 omitted



465
466
467
468
# File 'lib/nokogiri/css/parser.rb', line 465

def _reduce_23(val, _values, result)
 result = Node.new(:ELEMENT_NAME, val)
    result
end

#_reduce_24(val, _values, result) ⇒ Object



470
471
472
473
# File 'lib/nokogiri/css/parser.rb', line 470

def _reduce_24(val, _values, result)
 result = Node.new(:ELEMENT_NAME, [[val[0], val[2]].compact.join(':')])
    result
end

#_reduce_25(val, _values, result) ⇒ Object



475
476
477
478
479
480
# File 'lib/nokogiri/css/parser.rb', line 475

def _reduce_25(val, _values, result)
      name = @namespaces.key?('xmlns') ? "xmlns:#{val[0]}" : val[0]
      result = Node.new(:ELEMENT_NAME, [name])

    result
end

#_reduce_26(val, _values, result) ⇒ Object



482
483
484
485
# File 'lib/nokogiri/css/parser.rb', line 482

def _reduce_26(val, _values, result)
 result = val[0]
    result
end

#_reduce_28(val, _values, result) ⇒ Object

reduce 27 omitted



489
490
491
492
493
# File 'lib/nokogiri/css/parser.rb', line 489

def _reduce_28(val, _values, result)
      result = Node.new(:ATTRIBUTE_CONDITION, [val[1]] + (val[2] || []))

    result
end

#_reduce_29(val, _values, result) ⇒ Object



495
496
497
498
499
# File 'lib/nokogiri/css/parser.rb', line 495

def _reduce_29(val, _values, result)
      result = Node.new(:ATTRIBUTE_CONDITION, [val[1]] + (val[2] || []))

    result
end

#_reduce_3(val, _values, result) ⇒ Object



372
373
374
375
# File 'lib/nokogiri/css/parser.rb', line 372

def _reduce_3(val, _values, result)
 result = [val[1]].flatten
    result
end

#_reduce_30(val, _values, result) ⇒ Object



501
502
503
504
505
# File 'lib/nokogiri/css/parser.rb', line 501

def _reduce_30(val, _values, result)
      result = Node.new(:PSEUDO_CLASS, [Node.new(:FUNCTION, ['nth-child(', val[1]])])

    result
end

#_reduce_31(val, _values, result) ⇒ Object



507
508
509
510
# File 'lib/nokogiri/css/parser.rb', line 507

def _reduce_31(val, _values, result)
 result = Node.new(:ATTRIB_NAME, [[val[0], val[2]].compact.join(':')])
    result
end

#_reduce_32(val, _values, result) ⇒ Object



512
513
514
515
# File 'lib/nokogiri/css/parser.rb', line 512

def _reduce_32(val, _values, result)
 result = Node.new(:ATTRIB_NAME, [val[0]])
    result
end

#_reduce_34(val, _values, result) ⇒ Object

reduce 33 omitted



519
520
521
522
523
# File 'lib/nokogiri/css/parser.rb', line 519

def _reduce_34(val, _values, result)
      result = Node.new(:FUNCTION, [val[0].strip])

    result
end

#_reduce_35(val, _values, result) ⇒ Object



525
526
527
528
529
# File 'lib/nokogiri/css/parser.rb', line 525

def _reduce_35(val, _values, result)
      result = Node.new(:FUNCTION, [val[0].strip, val[1]].flatten)

    result
end

#_reduce_36(val, _values, result) ⇒ Object



531
532
533
534
535
# File 'lib/nokogiri/css/parser.rb', line 531

def _reduce_36(val, _values, result)
      result = Node.new(:FUNCTION, [val[0].strip, val[1]].flatten)

    result
end

#_reduce_37(val, _values, result) ⇒ Object



537
538
539
540
541
# File 'lib/nokogiri/css/parser.rb', line 537

def _reduce_37(val, _values, result)
      result = Node.new(:FUNCTION, [val[0].strip, val[1]].flatten)

    result
end

#_reduce_38(val, _values, result) ⇒ Object



543
544
545
546
547
# File 'lib/nokogiri/css/parser.rb', line 543

def _reduce_38(val, _values, result)
      result = Node.new(:FUNCTION, [val[0].strip, val[1]].flatten)

    result
end

#_reduce_39(val, _values, result) ⇒ Object



549
550
551
552
# File 'lib/nokogiri/css/parser.rb', line 549

def _reduce_39(val, _values, result)
 result = [val[0], val[2]]
    result
end

#_reduce_4(val, _values, result) ⇒ Object



377
378
379
380
# File 'lib/nokogiri/css/parser.rb', line 377

def _reduce_4(val, _values, result)
 result = :DIRECT_ADJACENT_SELECTOR
    result
end

#_reduce_40(val, _values, result) ⇒ Object



554
555
556
557
# File 'lib/nokogiri/css/parser.rb', line 554

def _reduce_40(val, _values, result)
 result = [val[0], val[2]]
    result
end

#_reduce_41(val, _values, result) ⇒ Object



559
560
561
562
# File 'lib/nokogiri/css/parser.rb', line 559

def _reduce_41(val, _values, result)
 result = [val[0], val[2]]
    result
end

#_reduce_42(val, _values, result) ⇒ Object



564
565
566
567
# File 'lib/nokogiri/css/parser.rb', line 564

def _reduce_42(val, _values, result)
 result = [val[0], val[2]]
    result
end

#_reduce_45(val, _values, result) ⇒ Object

reduce 44 omitted



573
574
575
576
577
578
579
580
581
582
583
584
585
586
# File 'lib/nokogiri/css/parser.rb', line 573

def _reduce_45(val, _values, result)
      case val[0]
      when 'even'
        result = Node.new(:NTH, ['2','n','+','0'])
      when 'odd'
        result = Node.new(:NTH, ['2','n','+','1'])
      when 'n'
        result = Node.new(:NTH, ['1','n','+','0'])
      else
        result = val
      end

    result
end

#_reduce_47(val, _values, result) ⇒ Object

reduce 46 omitted



590
591
592
593
594
595
596
597
598
# File 'lib/nokogiri/css/parser.rb', line 590

def _reduce_47(val, _values, result)
      if val[1] == 'n'
        result = Node.new(:NTH, val)
      else
        raise Racc::ParseError, "parse error on IDENT '#{val[1]}'"
      end

    result
end

#_reduce_48(val, _values, result) ⇒ Object



600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
# File 'lib/nokogiri/css/parser.rb', line 600

def _reduce_48(val, _values, result)
               # n+3, -n+3
      if val[0] == 'n'
        val.unshift("1")
        result = Node.new(:NTH, val)
      elsif val[0] == '-n'
        val[0] = 'n'
        val.unshift("-1")
        result = Node.new(:NTH, val)
      else
        raise Racc::ParseError, "parse error on IDENT '#{val[1]}'"
      end

    result
end

#_reduce_49(val, _values, result) ⇒ Object



616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
# File 'lib/nokogiri/css/parser.rb', line 616

def _reduce_49(val, _values, result)
                    # 5n, -5n, 10n-1
      n = val[1]
      if n[0, 2] == 'n-'
        val[1] = 'n'
        val << "-"
        # b is contained in n as n is the string "n-b"
        val << n[2, n.size]
        result = Node.new(:NTH, val)
      elsif n == 'n'
        val << "+"
        val << "0"
        result = Node.new(:NTH, val)
      else
        raise Racc::ParseError, "parse error on IDENT '#{val[1]}'"
      end

    result
end

#_reduce_5(val, _values, result) ⇒ Object



382
383
384
385
# File 'lib/nokogiri/css/parser.rb', line 382

def _reduce_5(val, _values, result)
 result = :CHILD_SELECTOR
    result
end

#_reduce_50(val, _values, result) ⇒ Object



636
637
638
639
640
# File 'lib/nokogiri/css/parser.rb', line 636

def _reduce_50(val, _values, result)
      result = Node.new(:PSEUDO_CLASS, [val[1]])

    result
end

#_reduce_51(val, _values, result) ⇒ Object



642
643
644
645
# File 'lib/nokogiri/css/parser.rb', line 642

def _reduce_51(val, _values, result)
 result = Node.new(:PSEUDO_CLASS, [val[1]])
    result
end

#_reduce_54(val, _values, result) ⇒ Object

reduce 53 omitted



651
652
653
654
655
# File 'lib/nokogiri/css/parser.rb', line 651

def _reduce_54(val, _values, result)
      result = Node.new(:COMBINATOR, val)

    result
end

#_reduce_55(val, _values, result) ⇒ Object



657
658
659
660
661
# File 'lib/nokogiri/css/parser.rb', line 657

def _reduce_55(val, _values, result)
      result = Node.new(:COMBINATOR, val)

    result
end

#_reduce_56(val, _values, result) ⇒ Object



663
664
665
666
667
# File 'lib/nokogiri/css/parser.rb', line 663

def _reduce_56(val, _values, result)
      result = Node.new(:COMBINATOR, val)

    result
end

#_reduce_57(val, _values, result) ⇒ Object



669
670
671
672
673
# File 'lib/nokogiri/css/parser.rb', line 669

def _reduce_57(val, _values, result)
      result = Node.new(:COMBINATOR, val)

    result
end

#_reduce_58(val, _values, result) ⇒ Object



675
676
677
678
679
# File 'lib/nokogiri/css/parser.rb', line 675

def _reduce_58(val, _values, result)
      result = Node.new(:COMBINATOR, val)

    result
end

#_reduce_6(val, _values, result) ⇒ Object



387
388
389
390
# File 'lib/nokogiri/css/parser.rb', line 387

def _reduce_6(val, _values, result)
 result = :FOLLOWING_SELECTOR
    result
end

#_reduce_64(val, _values, result) ⇒ Object

reduce 63 omitted



691
692
693
694
# File 'lib/nokogiri/css/parser.rb', line 691

def _reduce_64(val, _values, result)
 result = Node.new(:ID, [unescape_css_identifier(val[0])])
    result
end

#_reduce_65(val, _values, result) ⇒ Object



696
697
698
699
# File 'lib/nokogiri/css/parser.rb', line 696

def _reduce_65(val, _values, result)
 result = [val[0], unescape_css_identifier(val[1])]
    result
end

#_reduce_66(val, _values, result) ⇒ Object



701
702
703
704
# File 'lib/nokogiri/css/parser.rb', line 701

def _reduce_66(val, _values, result)
 result = [val[0], unescape_css_string(val[1])]
    result
end

#_reduce_67(val, _values, result) ⇒ Object



706
707
708
709
# File 'lib/nokogiri/css/parser.rb', line 706

def _reduce_67(val, _values, result)
 result = [val[0], val[1]]
    result
end

#_reduce_69(val, _values, result) ⇒ Object

reduce 68 omitted



713
714
715
716
# File 'lib/nokogiri/css/parser.rb', line 713

def _reduce_69(val, _values, result)
 result = :equal
    result
end

#_reduce_7(val, _values, result) ⇒ Object



392
393
394
395
# File 'lib/nokogiri/css/parser.rb', line 392

def _reduce_7(val, _values, result)
 result = :DESCENDANT_SELECTOR
    result
end

#_reduce_70(val, _values, result) ⇒ Object



718
719
720
721
# File 'lib/nokogiri/css/parser.rb', line 718

def _reduce_70(val, _values, result)
 result = :prefix_match
    result
end

#_reduce_71(val, _values, result) ⇒ Object



723
724
725
726
# File 'lib/nokogiri/css/parser.rb', line 723

def _reduce_71(val, _values, result)
 result = :suffix_match
    result
end

#_reduce_72(val, _values, result) ⇒ Object



728
729
730
731
# File 'lib/nokogiri/css/parser.rb', line 728

def _reduce_72(val, _values, result)
 result = :substring_match
    result
end

#_reduce_73(val, _values, result) ⇒ Object



733
734
735
736
# File 'lib/nokogiri/css/parser.rb', line 733

def _reduce_73(val, _values, result)
 result = :not_equal
    result
end

#_reduce_74(val, _values, result) ⇒ Object



738
739
740
741
# File 'lib/nokogiri/css/parser.rb', line 738

def _reduce_74(val, _values, result)
 result = :includes
    result
end

#_reduce_75(val, _values, result) ⇒ Object



743
744
745
746
# File 'lib/nokogiri/css/parser.rb', line 743

def _reduce_75(val, _values, result)
 result = :dash_match
    result
end

#_reduce_76(val, _values, result) ⇒ Object



748
749
750
751
752
# File 'lib/nokogiri/css/parser.rb', line 748

def _reduce_76(val, _values, result)
      result = Node.new(:NOT, [val[1]])

    result
end

#_reduce_8(val, _values, result) ⇒ Object



397
398
399
400
# File 'lib/nokogiri/css/parser.rb', line 397

def _reduce_8(val, _values, result)
 result = :CHILD_SELECTOR
    result
end

#_reduce_9(val, _values, result) ⇒ Object



402
403
404
405
# File 'lib/nokogiri/css/parser.rb', line 402

def _reduce_9(val, _values, result)
 result = val[1]
    result
end

#_reduce_none(val, _values, result) ⇒ Object

reduce 81 omitted



764
765
766
# File 'lib/nokogiri/css/parser.rb', line 764

def _reduce_none(val, _values, result)
  val[0]
end

#cache_key(query, prefix, visitor) ⇒ Object



89
90
91
92
93
# File 'lib/nokogiri/css/parser_extras.rb', line 89

def cache_key(query, prefix, visitor)
  if self.class.cache_on?
    [query, prefix, @namespaces, visitor.config]
  end
end

#next_tokenObject



71
72
73
# File 'lib/nokogiri/css/parser_extras.rb', line 71

def next_token
  @tokenizer.next_token
end

#on_error(error_token_id, error_value, value_stack) ⇒ Object

On CSS parser error, raise an exception

Raises:



84
85
86
87
# File 'lib/nokogiri/css/parser_extras.rb', line 84

def on_error(error_token_id, error_value, value_stack)
  after = value_stack.compact.last
  raise SyntaxError, "unexpected '#{error_value}' after '#{after}'"
end

#parse(string) ⇒ Object



66
67
68
69
# File 'lib/nokogiri/css/parser_extras.rb', line 66

def parse(string)
  @tokenizer.scan_setup(string)
  do_parse
end

#unescape_css_identifier(identifier) ⇒ Object



26
27
28
# File 'lib/nokogiri/css/parser.rb', line 26

def unescape_css_identifier(identifier)
  identifier.gsub(/\\(?:([^0-9a-fA-F])|([0-9a-fA-F]{1,6})\s?)/){ |m| $1 || [$2.hex].pack('U') }
end

#unescape_css_string(str) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/nokogiri/css/parser.rb', line 30

def unescape_css_string(str)
  str.gsub(/\\(?:([^0-9a-fA-F])|([0-9a-fA-F]{1,6})\s?)/) do |m|
    if $1=="\n"
      ''
    else
      $1 || [$2.hex].pack('U')
    end
  end
end

#xpath_for(string, prefix, visitor) ⇒ Object

Get the xpath for string using options



76
77
78
79
80
81
# File 'lib/nokogiri/css/parser_extras.rb', line 76

def xpath_for(string, prefix, visitor)
  key = cache_key(string, prefix, visitor)
  self.class[key] ||= parse(string).map do |ast|
    ast.to_xpath(prefix, visitor)
  end
end