Class: Oga::CSS::Parser Private

Inherits:
LL::Driver
  • Object
show all
Defined in:
lib/oga/css/parser.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

CONFIG =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

LL::DriverConfig.new
CACHE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

LRU.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Parser

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Parser.

Parameters:

  • data (String)

    The input to parse.



235
236
237
# File 'lib/oga/css/parser.rb', line 235

def initialize(data)
  @lexer = Lexer.new(data)
end

Class Method Details

.parse_with_cache(data) ⇒ AST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • data (String)

Returns:

  • (AST::Node)


228
229
230
# File 'lib/oga/css/parser.rb', line 228

def self.parse_with_cache(data)
  return CACHE.get_or_set(data) { new(data).parse }
end

Instance Method Details

#current_elementAST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the node test for the current element.

Returns:

  • (AST::Node)


273
274
275
# File 'lib/oga/css/parser.rb', line 273

def current_element
  @current_element ||= s(:test, nil, '*')
end

#each_token {|| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Yields the next token from the lexer.

Yield Parameters:

  • (Array)


260
261
262
263
264
265
266
# File 'lib/oga/css/parser.rb', line 260

def each_token
  @lexer.advance do |*args|
    yield args
  end

  yield [-1, -1]
end

#on_op_ends_with(attr, value) ⇒ AST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates the AST for the $= operator.

Parameters:

  • attr (AST::Node)
  • value (AST::Node)

Returns:

  • (AST::Node)


471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# File 'lib/oga/css/parser.rb', line 471

def on_op_ends_with(attr, value)
  s(
    :eq,
    s(
      :call,
      'substring',
      attr,
      s(
        :add,
        s(
          :sub,
          s(:call, 'string-length', attr),
          s(:call, 'string-length', value)
        ),
        s(:int, 1)
      ),
      s(:call, 'string-length', value)
    ),
    value
  )
end

#on_op_eq(attr, value) ⇒ AST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates the AST for the = operator.

Parameters:

  • attr (AST::Node)
  • value (AST::Node)

Returns:

  • (AST::Node)


433
434
435
# File 'lib/oga/css/parser.rb', line 433

def on_op_eq(attr, value)
  s(:eq, attr, value)
end

#on_op_hyphen_in(attr, value) ⇒ AST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates the AST for the |= operator.

Parameters:

  • attr (AST::Node)
  • value (AST::Node)

Returns:

  • (AST::Node)


511
512
513
514
515
516
517
518
519
520
521
522
# File 'lib/oga/css/parser.rb', line 511

def on_op_hyphen_in(attr, value)
  s(
    :or,
    s(:eq, attr, value),
    s(
      :call,
      'starts-with',
      attr,
      s(:call, 'concat', value, s(:string, '-'))
    )
  )
end

#on_op_in(attr, value) ⇒ AST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates the AST for the *= operator.

Parameters:

  • attr (AST::Node)
  • value (AST::Node)

Returns:

  • (AST::Node)


500
501
502
# File 'lib/oga/css/parser.rb', line 500

def on_op_in(attr, value)
  s(:call, 'contains', attr, value)
end

#on_op_space_in(attr, value) ⇒ AST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates the AST for the ~= operator.

Parameters:

  • attr (AST::Node)
  • value (AST::Node)

Returns:

  • (AST::Node)


444
445
446
447
448
449
450
451
# File 'lib/oga/css/parser.rb', line 444

def on_op_space_in(attr, value)
  s(
    :call,
    'contains',
    s(:call, 'concat', s(:string, ' '), attr, s(:string, ' ')),
    s(:call, 'concat', s(:string, ' '), value, s(:string, ' '))
  )
end

#on_op_starts_with(attr, value) ⇒ AST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates the AST for the ^= operator.

Parameters:

  • attr (AST::Node)
  • value (AST::Node)

Returns:

  • (AST::Node)


460
461
462
# File 'lib/oga/css/parser.rb', line 460

def on_op_starts_with(attr, value)
  s(:call, 'starts-with', attr, value)
end

#on_pseudo_class(name, arg = nil) ⇒ AST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • name (String)
  • arg (AST::Node) (defaults to: nil)

Returns:

  • (AST::Node)


308
309
310
311
312
# File 'lib/oga/css/parser.rb', line 308

def on_pseudo_class(name, arg = nil)
  handler = "on_pseudo_class_#{name.gsub('-', '_')}"

  arg ? send(handler, arg) : send(handler)
end

#on_pseudo_class_emptyAST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates the AST for the :empty selector.

Returns:

  • (AST::Node)


422
423
424
# File 'lib/oga/css/parser.rb', line 422

def on_pseudo_class_empty
  s(:call, 'not', s(:axis, 'child', s(:type_test, 'node')))
end

#on_pseudo_class_first_childAST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates the AST for the :first-child selector.

Returns:

  • (AST::Node)


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

def on_pseudo_class_first_child
  generate_no_siblings('preceding-sibling')
end

#on_pseudo_class_first_of_typeAST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates the AST for the :first-of-type selector.

Returns:

  • (AST::Node)


386
387
388
# File 'lib/oga/css/parser.rb', line 386

def on_pseudo_class_first_of_type
  generate_no_siblings('preceding-sibling', current_element)
end

#on_pseudo_class_last_childAST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates the AST for the :last-child selector.

Returns:

  • (AST::Node)


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

def on_pseudo_class_last_child
  generate_no_siblings('following-sibling')
end

#on_pseudo_class_last_of_typeAST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates the AST for the :last-of-type selector.

Returns:

  • (AST::Node)


395
396
397
# File 'lib/oga/css/parser.rb', line 395

def on_pseudo_class_last_of_type
  generate_no_siblings('following-sibling', current_element)
end

#on_pseudo_class_nth_child(arg) ⇒ AST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates the AST for the nth-child pseudo class.

Parameters:

  • arg (AST::Node)

Returns:

  • (AST::Node)


329
330
331
# File 'lib/oga/css/parser.rb', line 329

def on_pseudo_class_nth_child(arg)
  generate_nth_child('preceding-sibling', arg)
end

#on_pseudo_class_nth_last_child(arg) ⇒ AST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates the AST for the nth-last-child pseudo class.

Parameters:

  • arg (AST::Node)

Returns:

  • (AST::Node)


339
340
341
# File 'lib/oga/css/parser.rb', line 339

def on_pseudo_class_nth_last_child(arg)
  generate_nth_child('following-sibling', arg)
end

#on_pseudo_class_nth_last_of_type(arg) ⇒ AST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates the AST for the nth-last-of-type pseudo class.

Parameters:

  • arg (AST::Node)

Returns:

  • (AST::Node)


359
360
361
# File 'lib/oga/css/parser.rb', line 359

def on_pseudo_class_nth_last_of_type(arg)
  generate_nth_child('following-sibling', arg, current_element)
end

#on_pseudo_class_nth_of_type(arg) ⇒ AST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates the AST for the nth-of-type pseudo class.

Parameters:

  • arg (AST::Node)

Returns:

  • (AST::Node)


349
350
351
# File 'lib/oga/css/parser.rb', line 349

def on_pseudo_class_nth_of_type(arg)
  generate_nth_child('preceding-sibling', arg, current_element)
end

#on_pseudo_class_only_childAST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates the AST for the :only-child selector.

Returns:

  • (AST::Node)


404
405
406
# File 'lib/oga/css/parser.rb', line 404

def on_pseudo_class_only_child
  s(:and, on_pseudo_class_first_child, on_pseudo_class_last_child)
end

#on_pseudo_class_only_of_typeAST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates the AST for the :only-of-type selector.

Returns:

  • (AST::Node)


413
414
415
# File 'lib/oga/css/parser.rb', line 413

def on_pseudo_class_only_of_type
  s(:and, on_pseudo_class_first_of_type, on_pseudo_class_last_of_type)
end

#on_pseudo_class_rootAST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates the AST for the root pseudo class.

Returns:

  • (AST::Node)


319
320
321
# File 'lib/oga/css/parser.rb', line 319

def on_pseudo_class_root
  s(:call, 'not', s(:axis, 'parent', s(:test, nil, '*')))
end

#on_test(namespace, name) ⇒ AST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates the AST for a node test.

Parameters:

  • namespace (String)
  • name (String)

Returns:

  • (AST::Node)


299
300
301
# File 'lib/oga/css/parser.rb', line 299

def on_test(namespace, name)
  @current_element = s(:test, namespace, name)
end

#parseAST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parses the input and returns the corresponding AST.

Examples:

parser = Oga::CSS::Parser.new('foo.bar')
ast    = parser.parse

Returns:

  • (AST::Node)


286
287
288
289
290
# File 'lib/oga/css/parser.rb', line 286

def parse
  reset

  super
end

#resetObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resets the internal state of the parser.



242
243
244
# File 'lib/oga/css/parser.rb', line 242

def reset
  @current_element = nil
end

#s(type, *children) ⇒ AST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • type (Symbol)
  • children (Array)

Returns:

  • (AST::Node)


251
252
253
# File 'lib/oga/css/parser.rb', line 251

def s(type, *children)
  return AST::Node.new(type, children)
end