Class: Nokogiri::CSS::Parser
- Inherits:
-
Racc::Parser
- Object
- Racc::Parser
- Nokogiri::CSS::Parser
- 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", "combinator", "simple_selector", "element_name", "hcap_0toN", "hcap_1toN", "negation", "function", "pseudo", "attrib", "class", "namespace", "attrib_val_0or1", "expr", "an_plus_b", "attribute_id", "eql_incl_dash", "negation_arg" ]
- Racc_debug_parser =
false
Class Attribute Summary collapse
-
.cache_on ⇒ Object
(also: cache_on?)
Turn on CSS parse caching.
Class Method Summary collapse
-
.[](string) ⇒ Object
Get the css selector in
string
from the cache. -
.[]=(string, value) ⇒ Object
Set the css selector in
string
in the cache tovalue
. -
.clear_cache ⇒ Object
Clear the cache.
-
.parse(selector) ⇒ Object
Parse this CSS selector in
selector
. -
.without_cache(&block) ⇒ Object
Execute
block
without cache.
Instance Method Summary collapse
-
#_reduce_1(val, _values, result) ⇒ Object
reduce 0 omitted.
- #_reduce_10(val, _values, result) ⇒ Object
- #_reduce_11(val, _values, result) ⇒ Object
-
#_reduce_13(val, _values, result) ⇒ Object
reduce 12 omitted.
- #_reduce_14(val, _values, result) ⇒ Object
- #_reduce_15(val, _values, result) ⇒ Object
- #_reduce_16(val, _values, result) ⇒ Object
- #_reduce_17(val, _values, result) ⇒ Object
-
#_reduce_19(val, _values, result) ⇒ Object
reduce 18 omitted.
- #_reduce_2(val, _values, result) ⇒ Object
- #_reduce_20(val, _values, result) ⇒ Object
- #_reduce_21(val, _values, result) ⇒ Object
- #_reduce_22(val, _values, result) ⇒ Object
- #_reduce_23(val, _values, result) ⇒ Object
-
#_reduce_25(val, _values, result) ⇒ Object
reduce 24 omitted.
- #_reduce_26(val, _values, result) ⇒ Object
- #_reduce_27(val, _values, result) ⇒ Object
- #_reduce_28(val, _values, result) ⇒ Object
- #_reduce_29(val, _values, result) ⇒ Object
- #_reduce_3(val, _values, result) ⇒ Object
- #_reduce_30(val, _values, result) ⇒ Object
- #_reduce_31(val, _values, result) ⇒ Object
- #_reduce_32(val, _values, result) ⇒ Object
- #_reduce_33(val, _values, result) ⇒ Object
- #_reduce_34(val, _values, result) ⇒ Object
- #_reduce_35(val, _values, result) ⇒ Object
-
#_reduce_38(val, _values, result) ⇒ Object
reduce 37 omitted.
- #_reduce_39(val, _values, result) ⇒ Object
- #_reduce_4(val, _values, result) ⇒ Object
- #_reduce_40(val, _values, result) ⇒ Object
- #_reduce_41(val, _values, result) ⇒ Object
- #_reduce_42(val, _values, result) ⇒ Object
- #_reduce_43(val, _values, result) ⇒ Object
-
#_reduce_46(val, _values, result) ⇒ Object
reduce 45 omitted.
- #_reduce_47(val, _values, result) ⇒ Object
- #_reduce_48(val, _values, result) ⇒ Object
- #_reduce_49(val, _values, result) ⇒ Object
- #_reduce_5(val, _values, result) ⇒ Object
-
#_reduce_54(val, _values, result) ⇒ Object
reduce 53 omitted.
- #_reduce_55(val, _values, result) ⇒ Object
- #_reduce_56(val, _values, result) ⇒ Object
-
#_reduce_58(val, _values, result) ⇒ Object
reduce 57 omitted.
- #_reduce_59(val, _values, result) ⇒ Object
- #_reduce_6(val, _values, result) ⇒ Object
- #_reduce_60(val, _values, result) ⇒ Object
- #_reduce_61(val, _values, result) ⇒ Object
- #_reduce_62(val, _values, result) ⇒ Object
- #_reduce_63(val, _values, result) ⇒ Object
- #_reduce_64(val, _values, result) ⇒ Object
- #_reduce_65(val, _values, result) ⇒ Object
- #_reduce_7(val, _values, result) ⇒ Object
- #_reduce_8(val, _values, result) ⇒ Object
- #_reduce_9(val, _values, result) ⇒ Object
-
#_reduce_none(val, _values, result) ⇒ Object
reduce 68 omitted.
-
#initialize(namespaces = {}) ⇒ Parser
constructor
Create a new CSS parser with respect to
namespaces
. - #next_token ⇒ Object
-
#on_error(error_token_id, error_value, value_stack) ⇒ Object
On CSS parser error, raise an exception.
- #parse(string) ⇒ Object
-
#xpath_for(string, options = {}) ⇒ Object
Get the xpath for
string
usingoptions
.
Constructor Details
Class Attribute Details
.cache_on ⇒ Object Also known as: cache_on?
Turn on CSS parse caching
12 13 14 |
# File 'lib/nokogiri/css/parser_extras.rb', line 12 def cache_on @cache_on end |
Class Method Details
.[](string) ⇒ Object
Get the css selector in string
from the cache
17 18 19 20 |
# File 'lib/nokogiri/css/parser_extras.rb', line 17 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
23 24 25 26 |
# File 'lib/nokogiri/css/parser_extras.rb', line 23 def []= string, value return value unless @cache_on @mutex.synchronize { @cache[string] = value } end |
.clear_cache ⇒ Object
Clear the cache
29 30 31 |
# File 'lib/nokogiri/css/parser_extras.rb', line 29 def clear_cache @mutex.synchronize { @cache = {} } end |
.parse(selector) ⇒ Object
Parse this CSS selector in selector
. Returns an AST.
43 44 45 46 47 48 49 50 |
# File 'lib/nokogiri/css/parser_extras.rb', line 43 def parse selector @warned ||= false unless @warned $stderr.puts('Nokogiri::CSS::Parser.parse is deprecated, call Nokogiri::CSS.parse(), this will be removed August 1st or version 1.4.0 (whichever is first)') @warned = true end new.parse selector end |
.without_cache(&block) ⇒ Object
Execute block
without cache
34 35 36 37 38 39 |
# File 'lib/nokogiri/css/parser_extras.rb', line 34 def without_cache &block tmp = @cache_on @cache_on = false block.call @cache_on = tmp end |
Instance Method Details
#_reduce_1(val, _values, result) ⇒ Object
reduce 0 omitted
289 290 291 292 293 |
# File 'lib/nokogiri/css/parser.rb', line 289 def _reduce_1(val, _values, result) result = [val.first, val.last].flatten result end |
#_reduce_10(val, _values, result) ⇒ Object
340 341 342 343 344 345 346 347 348 349 |
# File 'lib/nokogiri/css/parser.rb', line 340 def _reduce_10(val, _values, result) result = Node.new(:CONDITIONAL_SELECTOR, [ val.first, Node.new(:COMBINATOR, [val[1], val.last]) ] ) result end |
#_reduce_11(val, _values, result) ⇒ Object
351 352 353 354 355 |
# File 'lib/nokogiri/css/parser.rb', line 351 def _reduce_11(val, _values, result) result = Node.new(:CONDITIONAL_SELECTOR, val) result end |
#_reduce_13(val, _values, result) ⇒ Object
reduce 12 omitted
359 360 361 362 363 |
# File 'lib/nokogiri/css/parser.rb', line 359 def _reduce_13(val, _values, result) result = Node.new(:CONDITIONAL_SELECTOR, val) result end |
#_reduce_14(val, _values, result) ⇒ Object
365 366 367 368 369 |
# File 'lib/nokogiri/css/parser.rb', line 365 def _reduce_14(val, _values, result) result = Node.new(:CONDITIONAL_SELECTOR, val) result end |
#_reduce_15(val, _values, result) ⇒ Object
371 372 373 374 375 376 377 378 379 380 |
# File 'lib/nokogiri/css/parser.rb', line 371 def _reduce_15(val, _values, result) result = Node.new(:CONDITIONAL_SELECTOR, [ Node.new(:ELEMENT_NAME, ['*']), Node.new(:COMBINATOR, val) ] ) result end |
#_reduce_16(val, _values, result) ⇒ Object
382 383 384 385 386 387 388 |
# File 'lib/nokogiri/css/parser.rb', line 382 def _reduce_16(val, _values, result) result = Node.new(:CONDITIONAL_SELECTOR, [Node.new(:ELEMENT_NAME, ['*']), val.first] ) result end |
#_reduce_17(val, _values, result) ⇒ Object
390 391 392 393 394 |
# File 'lib/nokogiri/css/parser.rb', line 390 def _reduce_17(val, _values, result) result = Node.new(val[1], [val.first, val.last]) result end |
#_reduce_19(val, _values, result) ⇒ Object
reduce 18 omitted
398 399 400 401 |
# File 'lib/nokogiri/css/parser.rb', line 398 def _reduce_19(val, _values, result) result = Node.new(:CLASS_CONDITION, [val[1]]) result end |
#_reduce_2(val, _values, result) ⇒ Object
295 296 297 298 |
# File 'lib/nokogiri/css/parser.rb', line 295 def _reduce_2(val, _values, result) result = val.flatten result end |
#_reduce_20(val, _values, result) ⇒ Object
403 404 405 406 407 408 409 |
# File 'lib/nokogiri/css/parser.rb', line 403 def _reduce_20(val, _values, result) result = Node.new(:ELEMENT_NAME, [[val.first, val.last].compact.join(':')] ) result end |
#_reduce_21(val, _values, result) ⇒ Object
411 412 413 414 415 416 |
# File 'lib/nokogiri/css/parser.rb', line 411 def _reduce_21(val, _values, result) name = @namespaces.key?('xmlns') ? "xmlns:#{val.first}" : val.first result = Node.new(:ELEMENT_NAME, [name]) result end |
#_reduce_22(val, _values, result) ⇒ Object
418 419 420 421 |
# File 'lib/nokogiri/css/parser.rb', line 418 def _reduce_22(val, _values, result) result = Node.new(:ELEMENT_NAME, val) result end |
#_reduce_23(val, _values, result) ⇒ Object
423 424 425 426 |
# File 'lib/nokogiri/css/parser.rb', line 423 def _reduce_23(val, _values, result) result = val[0] result end |
#_reduce_25(val, _values, result) ⇒ Object
reduce 24 omitted
430 431 432 433 434 435 436 |
# File 'lib/nokogiri/css/parser.rb', line 430 def _reduce_25(val, _values, result) result = Node.new(:ATTRIBUTE_CONDITION, [Node.new(:ELEMENT_NAME, [val[1]])] + (val[2] || []) ) result end |
#_reduce_26(val, _values, result) ⇒ Object
438 439 440 441 442 443 444 |
# File 'lib/nokogiri/css/parser.rb', line 438 def _reduce_26(val, _values, result) result = Node.new(:ATTRIBUTE_CONDITION, [val[1]] + (val[2] || []) ) result end |
#_reduce_27(val, _values, result) ⇒ Object
446 447 448 449 450 451 452 453 |
# File 'lib/nokogiri/css/parser.rb', line 446 def _reduce_27(val, _values, result) # Non standard, but hpricot supports it. result = Node.new(:PSEUDO_CLASS, [Node.new(:FUNCTION, ['nth-child(', val[1]])] ) result end |
#_reduce_28(val, _values, result) ⇒ Object
455 456 457 458 459 |
# File 'lib/nokogiri/css/parser.rb', line 455 def _reduce_28(val, _values, result) result = Node.new(:FUNCTION, [val.first.strip]) result end |
#_reduce_29(val, _values, result) ⇒ Object
461 462 463 464 465 |
# File 'lib/nokogiri/css/parser.rb', line 461 def _reduce_29(val, _values, result) result = Node.new(:FUNCTION, [val.first.strip, val[1]].flatten) result end |
#_reduce_3(val, _values, result) ⇒ Object
300 301 302 303 |
# File 'lib/nokogiri/css/parser.rb', line 300 def _reduce_3(val, _values, result) result = :DIRECT_ADJACENT_SELECTOR result end |
#_reduce_30(val, _values, result) ⇒ Object
467 468 469 470 471 |
# File 'lib/nokogiri/css/parser.rb', line 467 def _reduce_30(val, _values, result) result = Node.new(:FUNCTION, [val.first.strip, val[1]].flatten) result end |
#_reduce_31(val, _values, result) ⇒ Object
473 474 475 476 477 |
# File 'lib/nokogiri/css/parser.rb', line 473 def _reduce_31(val, _values, result) result = Node.new(:FUNCTION, [val.first.strip, val[1]].flatten) result end |
#_reduce_32(val, _values, result) ⇒ Object
479 480 481 482 483 |
# File 'lib/nokogiri/css/parser.rb', line 479 def _reduce_32(val, _values, result) result = Node.new(:FUNCTION, [val.first.strip, val[1]].flatten) result end |
#_reduce_33(val, _values, result) ⇒ Object
485 486 487 488 |
# File 'lib/nokogiri/css/parser.rb', line 485 def _reduce_33(val, _values, result) result = [val.first, val.last] result end |
#_reduce_34(val, _values, result) ⇒ Object
490 491 492 493 |
# File 'lib/nokogiri/css/parser.rb', line 490 def _reduce_34(val, _values, result) result = [val.first, val.last] result end |
#_reduce_35(val, _values, result) ⇒ Object
495 496 497 498 |
# File 'lib/nokogiri/css/parser.rb', line 495 def _reduce_35(val, _values, result) result = [val.first, val.last] result end |
#_reduce_38(val, _values, result) ⇒ Object
reduce 37 omitted
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
# File 'lib/nokogiri/css/parser.rb', line 504 def _reduce_38(val, _values, result) if val[0] == 'even' val = ["2","n","+","0"] result = Node.new(:AN_PLUS_B, val) elsif val[0] == 'odd' val = ["2","n","+","1"] result = Node.new(:AN_PLUS_B, val) else # This is not CSS standard. It allows us to support this: # 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_39(val, _values, result) ⇒ Object
522 523 524 525 526 527 528 529 530 |
# File 'lib/nokogiri/css/parser.rb', line 522 def _reduce_39(val, _values, result) if val[1] == 'n' result = Node.new(:AN_PLUS_B, val) else raise Racc::ParseError, "parse error on IDENT '#{val[1]}'" end result end |
#_reduce_4(val, _values, result) ⇒ Object
305 306 307 308 |
# File 'lib/nokogiri/css/parser.rb', line 305 def _reduce_4(val, _values, result) result = :CHILD_SELECTOR result end |
#_reduce_40(val, _values, result) ⇒ Object
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 |
# File 'lib/nokogiri/css/parser.rb', line 532 def _reduce_40(val, _values, result) # n+3, -n+3 if val[0] == 'n' val.unshift("1") result = Node.new(:AN_PLUS_B, val) elsif val[0] == '-n' val[0] = 'n' val.unshift("-1") result = Node.new(:AN_PLUS_B, val) else raise Racc::ParseError, "parse error on IDENT '#{val[1]}'" end result end |
#_reduce_41(val, _values, result) ⇒ Object
548 549 550 551 552 553 554 555 556 557 558 |
# File 'lib/nokogiri/css/parser.rb', line 548 def _reduce_41(val, _values, result) if val[1] == 'n' val << "+" val << "0" result = Node.new(:AN_PLUS_B, val) else raise Racc::ParseError, "parse error on IDENT '#{val[1]}'" end result end |
#_reduce_42(val, _values, result) ⇒ Object
560 561 562 563 564 |
# File 'lib/nokogiri/css/parser.rb', line 560 def _reduce_42(val, _values, result) result = Node.new(:PSEUDO_CLASS, [val[1]]) result end |
#_reduce_43(val, _values, result) ⇒ Object
566 567 568 569 |
# File 'lib/nokogiri/css/parser.rb', line 566 def _reduce_43(val, _values, result) result = Node.new(:PSEUDO_CLASS, [val[1]]) result end |
#_reduce_46(val, _values, result) ⇒ Object
reduce 45 omitted
575 576 577 578 579 |
# File 'lib/nokogiri/css/parser.rb', line 575 def _reduce_46(val, _values, result) result = Node.new(:COMBINATOR, val) result end |
#_reduce_47(val, _values, result) ⇒ Object
581 582 583 584 585 |
# File 'lib/nokogiri/css/parser.rb', line 581 def _reduce_47(val, _values, result) result = Node.new(:COMBINATOR, val) result end |
#_reduce_48(val, _values, result) ⇒ Object
587 588 589 590 591 |
# File 'lib/nokogiri/css/parser.rb', line 587 def _reduce_48(val, _values, result) result = Node.new(:COMBINATOR, val) result end |
#_reduce_49(val, _values, result) ⇒ Object
593 594 595 596 597 |
# File 'lib/nokogiri/css/parser.rb', line 593 def _reduce_49(val, _values, result) result = Node.new(:COMBINATOR, val) result end |
#_reduce_5(val, _values, result) ⇒ Object
310 311 312 313 |
# File 'lib/nokogiri/css/parser.rb', line 310 def _reduce_5(val, _values, result) result = :PRECEDING_SELECTOR result end |
#_reduce_54(val, _values, result) ⇒ Object
reduce 53 omitted
607 608 609 610 |
# File 'lib/nokogiri/css/parser.rb', line 607 def _reduce_54(val, _values, result) result = Node.new(:ID, val) result end |
#_reduce_55(val, _values, result) ⇒ Object
612 613 614 615 |
# File 'lib/nokogiri/css/parser.rb', line 612 def _reduce_55(val, _values, result) result = [val.first, val[1]] result end |
#_reduce_56(val, _values, result) ⇒ Object
617 618 619 620 |
# File 'lib/nokogiri/css/parser.rb', line 617 def _reduce_56(val, _values, result) result = [val.first, val[1]] result end |
#_reduce_58(val, _values, result) ⇒ Object
reduce 57 omitted
624 625 626 627 |
# File 'lib/nokogiri/css/parser.rb', line 624 def _reduce_58(val, _values, result) result = :equal result end |
#_reduce_59(val, _values, result) ⇒ Object
629 630 631 632 |
# File 'lib/nokogiri/css/parser.rb', line 629 def _reduce_59(val, _values, result) result = :prefix_match result end |
#_reduce_6(val, _values, result) ⇒ Object
315 316 317 318 |
# File 'lib/nokogiri/css/parser.rb', line 315 def _reduce_6(val, _values, result) result = :DESCENDANT_SELECTOR result end |
#_reduce_60(val, _values, result) ⇒ Object
634 635 636 637 |
# File 'lib/nokogiri/css/parser.rb', line 634 def _reduce_60(val, _values, result) result = :suffix_match result end |
#_reduce_61(val, _values, result) ⇒ Object
639 640 641 642 |
# File 'lib/nokogiri/css/parser.rb', line 639 def _reduce_61(val, _values, result) result = :substring_match result end |
#_reduce_62(val, _values, result) ⇒ Object
644 645 646 647 |
# File 'lib/nokogiri/css/parser.rb', line 644 def _reduce_62(val, _values, result) result = :not_equal result end |
#_reduce_63(val, _values, result) ⇒ Object
649 650 651 652 |
# File 'lib/nokogiri/css/parser.rb', line 649 def _reduce_63(val, _values, result) result = :includes result end |
#_reduce_64(val, _values, result) ⇒ Object
654 655 656 657 |
# File 'lib/nokogiri/css/parser.rb', line 654 def _reduce_64(val, _values, result) result = :dash_match result end |
#_reduce_65(val, _values, result) ⇒ Object
659 660 661 662 663 |
# File 'lib/nokogiri/css/parser.rb', line 659 def _reduce_65(val, _values, result) result = Node.new(:NOT, [val[1]]) result end |
#_reduce_7(val, _values, result) ⇒ Object
320 321 322 323 |
# File 'lib/nokogiri/css/parser.rb', line 320 def _reduce_7(val, _values, result) result = :DESCENDANT_SELECTOR result end |
#_reduce_8(val, _values, result) ⇒ Object
325 326 327 328 |
# File 'lib/nokogiri/css/parser.rb', line 325 def _reduce_8(val, _values, result) result = :CHILD_SELECTOR result end |
#_reduce_9(val, _values, result) ⇒ Object
330 331 332 333 334 335 336 337 338 |
# File 'lib/nokogiri/css/parser.rb', line 330 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 68 omitted
671 672 673 |
# File 'lib/nokogiri/css/parser.rb', line 671 def _reduce_none(val, _values, result) val[0] end |
#next_token ⇒ Object
65 66 67 |
# File 'lib/nokogiri/css/parser_extras.rb', line 65 def next_token @tokenizer.next_token end |
#on_error(error_token_id, error_value, value_stack) ⇒ Object
On CSS parser error, raise an exception
85 86 87 88 |
# File 'lib/nokogiri/css/parser_extras.rb', line 85 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
60 61 62 63 |
# File 'lib/nokogiri/css/parser_extras.rb', line 60 def parse string @tokenizer.scan_setup string do_parse end |
#xpath_for(string, options = {}) ⇒ Object
Get the xpath for string
using options
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/nokogiri/css/parser_extras.rb', line 70 def xpath_for string, ={} key = "#{string}#{[:ns]}#{[:prefix]}" v = self.class[key] return v if v args = [ [:prefix] || '//', [:visitor] || XPathVisitor.new ] self.class[key] = parse(string).map { |ast| ast.to_xpath(*args) } end |