Class: Nokogiri::CSS::Parser

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

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",
"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



57
58
59
60
61
# File 'lib/nokogiri/css/parser_extras.rb', line 57

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

Class Method Details

.[](string) ⇒ Object

Get the css selector in string from the cache



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

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



30
31
32
33
# File 'lib/nokogiri/css/parser_extras.rb', line 30

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)


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

def cache_on?
  !Thread.current[CACHE_SWITCH_NAME]
end

.clear_cache(create_new_object = false) ⇒ Object

Clear the cache



36
37
38
39
40
41
42
43
44
# File 'lib/nokogiri/css/parser_extras.rb', line 36

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.



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

def set_cache(value)
  Thread.current[CACHE_SWITCH_NAME] = !value
end

.without_cache(&block) ⇒ Object

Execute block without cache



47
48
49
50
51
52
53
# File 'lib/nokogiri/css/parser_extras.rb', line 47

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

Instance Method Details

#_reduce_1(val, _values, result) ⇒ Object

reduce 0 omitted



335
336
337
338
339
# File 'lib/nokogiri/css/parser.rb', line 335

def _reduce_1(val, _values, result)
        result = [val.first, val.last].flatten

    result
end

#_reduce_11(val, _values, result) ⇒ Object

reduce 10 omitted



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

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

    result
end

#_reduce_12(val, _values, result) ⇒ Object



394
395
396
397
398
# File 'lib/nokogiri/css/parser.rb', line 394

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

    result
end

#_reduce_13(val, _values, result) ⇒ Object



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

def _reduce_13(val, _values, result)
        result = Node.new(:CONDITIONAL_SELECTOR,
          [Node.new(:ELEMENT_NAME, ['*']), val.first]
        )

    result
end

#_reduce_14(val, _values, result) ⇒ Object



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

def _reduce_14(val, _values, result)
        result = Node.new(val.first, [nil, val.last])

    result
end

#_reduce_15(val, _values, result) ⇒ Object



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

def _reduce_15(val, _values, result)
        result = Node.new(val[1], [val.first, val.last])

    result
end

#_reduce_16(val, _values, result) ⇒ Object



420
421
422
423
424
# File 'lib/nokogiri/css/parser.rb', line 420

def _reduce_16(val, _values, result)
        result = Node.new(:DESCENDANT_SELECTOR, [val.first, val.last])

    result
end

#_reduce_18(val, _values, result) ⇒ Object

reduce 17 omitted



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

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

#_reduce_2(val, _values, result) ⇒ Object



341
342
343
344
# File 'lib/nokogiri/css/parser.rb', line 341

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

#_reduce_20(val, _values, result) ⇒ Object

reduce 19 omitted



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

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

#_reduce_21(val, _values, result) ⇒ Object



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

def _reduce_21(val, _values, result)
        result = Node.new(:ELEMENT_NAME,
          [[val.first, val.last].compact.join(':')]
        )

    result
end

#_reduce_22(val, _values, result) ⇒ Object



448
449
450
451
452
453
# File 'lib/nokogiri/css/parser.rb', line 448

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

    result
end

#_reduce_23(val, _values, result) ⇒ Object



455
456
457
458
# File 'lib/nokogiri/css/parser.rb', line 455

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

#_reduce_25(val, _values, result) ⇒ Object

reduce 24 omitted



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

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

    result
end

#_reduce_26(val, _values, result) ⇒ Object



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

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

    result
end

#_reduce_27(val, _values, result) ⇒ Object



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

def _reduce_27(val, _values, result)
        # non-standard, from hpricot
        result = Node.new(:PSEUDO_CLASS,
          [Node.new(:FUNCTION, ['nth-child(', val[1]])]
        )

    result
end

#_reduce_28(val, _values, result) ⇒ Object



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

def _reduce_28(val, _values, result)
        result = Node.new(:ELEMENT_NAME,
          [[val.first, val.last].compact.join(':')]
        )

    result
end

#_reduce_29(val, _values, result) ⇒ Object



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

def _reduce_29(val, _values, result)
        # Default namespace is not applied to attributes.
        # So we don't add prefix "xmlns:" as in namespaced_ident.
        result = Node.new(:ELEMENT_NAME, [val.first])

    result
end

#_reduce_3(val, _values, result) ⇒ Object



346
347
348
349
# File 'lib/nokogiri/css/parser.rb', line 346

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

#_reduce_30(val, _values, result) ⇒ Object



503
504
505
506
507
# File 'lib/nokogiri/css/parser.rb', line 503

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

    result
end

#_reduce_31(val, _values, result) ⇒ Object



509
510
511
512
513
# File 'lib/nokogiri/css/parser.rb', line 509

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

    result
end

#_reduce_32(val, _values, result) ⇒ Object



515
516
517
518
519
# File 'lib/nokogiri/css/parser.rb', line 515

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

    result
end

#_reduce_33(val, _values, result) ⇒ Object



521
522
523
524
525
# File 'lib/nokogiri/css/parser.rb', line 521

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

    result
end

#_reduce_34(val, _values, result) ⇒ Object



527
528
529
530
531
# File 'lib/nokogiri/css/parser.rb', line 527

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

    result
end

#_reduce_35(val, _values, result) ⇒ Object



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

def _reduce_35(val, _values, result)
 result = [val.first, val.last]
    result
end

#_reduce_36(val, _values, result) ⇒ Object



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

def _reduce_36(val, _values, result)
 result = [val.first, val.last]
    result
end

#_reduce_37(val, _values, result) ⇒ Object



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

def _reduce_37(val, _values, result)
 result = [val.first, val.last]
    result
end

#_reduce_4(val, _values, result) ⇒ Object



351
352
353
354
# File 'lib/nokogiri/css/parser.rb', line 351

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

#_reduce_40(val, _values, result) ⇒ Object

reduce 39 omitted



552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'lib/nokogiri/css/parser.rb', line 552

def _reduce_40(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
          # non-standard to support custom functions:
          # assert_xpath("//a[foo(., @href)]", @parser.parse('a:foo(@href)'))
          # assert_xpath("//a[foo(., @a, b)]", @parser.parse('a:foo(@a, b)'))
          # assert_xpath("//a[foo(., a, 10)]", @parser.parse('a:foo(a, 10)'))
          result = val
        end

    result
end

#_reduce_41(val, _values, result) ⇒ Object



571
572
573
574
575
576
577
578
579
# File 'lib/nokogiri/css/parser.rb', line 571

def _reduce_41(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_42(val, _values, result) ⇒ Object



581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
# File 'lib/nokogiri/css/parser.rb', line 581

def _reduce_42(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_43(val, _values, result) ⇒ Object



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

def _reduce_43(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_44(val, _values, result) ⇒ Object



617
618
619
620
621
# File 'lib/nokogiri/css/parser.rb', line 617

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

    result
end

#_reduce_45(val, _values, result) ⇒ Object



623
624
625
626
# File 'lib/nokogiri/css/parser.rb', line 623

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

#_reduce_48(val, _values, result) ⇒ Object

reduce 47 omitted



632
633
634
635
636
# File 'lib/nokogiri/css/parser.rb', line 632

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

    result
end

#_reduce_49(val, _values, result) ⇒ Object



638
639
640
641
642
# File 'lib/nokogiri/css/parser.rb', line 638

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

    result
end

#_reduce_5(val, _values, result) ⇒ Object



356
357
358
359
# File 'lib/nokogiri/css/parser.rb', line 356

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

#_reduce_50(val, _values, result) ⇒ Object



644
645
646
647
648
# File 'lib/nokogiri/css/parser.rb', line 644

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

    result
end

#_reduce_51(val, _values, result) ⇒ Object



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

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

    result
end

#_reduce_52(val, _values, result) ⇒ Object



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

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

    result
end

#_reduce_58(val, _values, result) ⇒ Object

reduce 57 omitted



672
673
674
675
# File 'lib/nokogiri/css/parser.rb', line 672

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

#_reduce_59(val, _values, result) ⇒ Object



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

def _reduce_59(val, _values, result)
 result = [val.first, unescape_css_identifier(val[1])]
    result
end

#_reduce_6(val, _values, result) ⇒ Object



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

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

#_reduce_60(val, _values, result) ⇒ Object



682
683
684
685
# File 'lib/nokogiri/css/parser.rb', line 682

def _reduce_60(val, _values, result)
 result = [val.first, unescape_css_string(val[1])]
    result
end

#_reduce_61(val, _values, result) ⇒ Object



687
688
689
690
# File 'lib/nokogiri/css/parser.rb', line 687

def _reduce_61(val, _values, result)
 result = [val.first, val[1]]
    result
end

#_reduce_63(val, _values, result) ⇒ Object

reduce 62 omitted



694
695
696
697
# File 'lib/nokogiri/css/parser.rb', line 694

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

#_reduce_64(val, _values, result) ⇒ Object



699
700
701
702
# File 'lib/nokogiri/css/parser.rb', line 699

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

#_reduce_65(val, _values, result) ⇒ Object



704
705
706
707
# File 'lib/nokogiri/css/parser.rb', line 704

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

#_reduce_66(val, _values, result) ⇒ Object



709
710
711
712
# File 'lib/nokogiri/css/parser.rb', line 709

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

#_reduce_67(val, _values, result) ⇒ Object



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

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

#_reduce_68(val, _values, result) ⇒ Object



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

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

#_reduce_69(val, _values, result) ⇒ Object



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

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

#_reduce_7(val, _values, result) ⇒ Object



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

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

#_reduce_70(val, _values, result) ⇒ Object



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

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

    result
end

#_reduce_8(val, _values, result) ⇒ Object



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

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

#_reduce_9(val, _values, result) ⇒ Object



376
377
378
379
380
381
382
383
384
# File 'lib/nokogiri/css/parser.rb', line 376

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

    result
end

#_reduce_none(val, _values, result) ⇒ Object

reduce 75 omitted



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

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

#next_tokenObject



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

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:



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

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

#parse(string) ⇒ Object



63
64
65
66
# File 'lib/nokogiri/css/parser_extras.rb', line 63

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

#unescape_css_identifier(identifier) ⇒ Object



18
19
20
# File 'lib/nokogiri/css/parser.rb', line 18

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



22
23
24
25
26
27
28
29
30
# File 'lib/nokogiri/css/parser.rb', line 22

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, options = {}) ⇒ Object

Get the xpath for string using options



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/nokogiri/css/parser_extras.rb', line 73

def xpath_for(string, options = {})
  key = "#{string}#{options[:ns]}#{options[:prefix]}"
  v = self.class[key]
  return v if v

  args = [
    options[:prefix] || "//",
    options[:visitor] || XPathVisitor.new,
  ]
  self.class[key] = parse(string).map { |ast|
    ast.to_xpath(*args)
  }
end