Module: Cataract::Flatten Private

Defined in:
lib/cataract/pure/flatten.rb

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

Constant Summary collapse

PROP_MARGIN =

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.

Property name constants (US-ASCII for merge output)

'margin'.encode(Encoding::US_ASCII).freeze
PROP_MARGIN_TOP =

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.

'margin-top'.encode(Encoding::US_ASCII).freeze
PROP_MARGIN_RIGHT =

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.

'margin-right'.encode(Encoding::US_ASCII).freeze
PROP_MARGIN_BOTTOM =

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.

'margin-bottom'.encode(Encoding::US_ASCII).freeze
PROP_MARGIN_LEFT =

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.

'margin-left'.encode(Encoding::US_ASCII).freeze
PROP_PADDING =

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.

'padding'.encode(Encoding::US_ASCII).freeze
PROP_PADDING_TOP =

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.

'padding-top'.encode(Encoding::US_ASCII).freeze
PROP_PADDING_RIGHT =

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.

'padding-right'.encode(Encoding::US_ASCII).freeze
PROP_PADDING_BOTTOM =

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.

'padding-bottom'.encode(Encoding::US_ASCII).freeze
PROP_PADDING_LEFT =

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.

'padding-left'.encode(Encoding::US_ASCII).freeze
PROP_BORDER =

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.

'border'.encode(Encoding::US_ASCII).freeze
PROP_BORDER_WIDTH =

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.

'border-width'.encode(Encoding::US_ASCII).freeze
PROP_BORDER_STYLE =

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.

'border-style'.encode(Encoding::US_ASCII).freeze
PROP_BORDER_COLOR =

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.

'border-color'.encode(Encoding::US_ASCII).freeze
PROP_BORDER_TOP =

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.

'border-top'.encode(Encoding::US_ASCII).freeze
PROP_BORDER_RIGHT =

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.

'border-right'.encode(Encoding::US_ASCII).freeze
PROP_BORDER_BOTTOM =

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.

'border-bottom'.encode(Encoding::US_ASCII).freeze
PROP_BORDER_LEFT =

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.

'border-left'.encode(Encoding::US_ASCII).freeze
PROP_BORDER_TOP_WIDTH =

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.

'border-top-width'.encode(Encoding::US_ASCII).freeze
PROP_BORDER_RIGHT_WIDTH =

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.

'border-right-width'.encode(Encoding::US_ASCII).freeze
PROP_BORDER_BOTTOM_WIDTH =

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.

'border-bottom-width'.encode(Encoding::US_ASCII).freeze
PROP_BORDER_LEFT_WIDTH =

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.

'border-left-width'.encode(Encoding::US_ASCII).freeze
PROP_BORDER_TOP_STYLE =

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.

'border-top-style'.encode(Encoding::US_ASCII).freeze
PROP_BORDER_RIGHT_STYLE =

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.

'border-right-style'.encode(Encoding::US_ASCII).freeze
PROP_BORDER_BOTTOM_STYLE =

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.

'border-bottom-style'.encode(Encoding::US_ASCII).freeze
PROP_BORDER_LEFT_STYLE =

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.

'border-left-style'.encode(Encoding::US_ASCII).freeze
PROP_BORDER_TOP_COLOR =

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.

'border-top-color'.encode(Encoding::US_ASCII).freeze
PROP_BORDER_RIGHT_COLOR =

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.

'border-right-color'.encode(Encoding::US_ASCII).freeze
PROP_BORDER_BOTTOM_COLOR =

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.

'border-bottom-color'.encode(Encoding::US_ASCII).freeze
PROP_BORDER_LEFT_COLOR =

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.

'border-left-color'.encode(Encoding::US_ASCII).freeze
PROP_FONT =

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.

'font'.encode(Encoding::US_ASCII).freeze
PROP_FONT_STYLE =

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.

'font-style'.encode(Encoding::US_ASCII).freeze
PROP_FONT_VARIANT =

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.

'font-variant'.encode(Encoding::US_ASCII).freeze
PROP_FONT_WEIGHT =

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.

'font-weight'.encode(Encoding::US_ASCII).freeze
PROP_FONT_SIZE =

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.

'font-size'.encode(Encoding::US_ASCII).freeze
PROP_LINE_HEIGHT =

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.

'line-height'.encode(Encoding::US_ASCII).freeze
PROP_FONT_FAMILY =

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.

'font-family'.encode(Encoding::US_ASCII).freeze
PROP_BACKGROUND =

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.

'background'.encode(Encoding::US_ASCII).freeze
PROP_BACKGROUND_COLOR =

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.

'background-color'.encode(Encoding::US_ASCII).freeze
PROP_BACKGROUND_IMAGE =

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.

'background-image'.encode(Encoding::US_ASCII).freeze
PROP_BACKGROUND_REPEAT =

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.

'background-repeat'.encode(Encoding::US_ASCII).freeze
PROP_BACKGROUND_ATTACHMENT =

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.

'background-attachment'.encode(Encoding::US_ASCII).freeze
PROP_BACKGROUND_POSITION =

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.

'background-position'.encode(Encoding::US_ASCII).freeze
PROP_LIST_STYLE =

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.

'list-style'.encode(Encoding::US_ASCII).freeze
PROP_LIST_STYLE_TYPE =

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.

'list-style-type'.encode(Encoding::US_ASCII).freeze
PROP_LIST_STYLE_POSITION =

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.

'list-style-position'.encode(Encoding::US_ASCII).freeze
PROP_LIST_STYLE_IMAGE =

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.

'list-style-image'.encode(Encoding::US_ASCII).freeze
MARGIN_SIDES =

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.

Shorthand property families

[PROP_MARGIN_TOP, PROP_MARGIN_RIGHT, PROP_MARGIN_BOTTOM, PROP_MARGIN_LEFT].freeze
PADDING_SIDES =

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.

[PROP_PADDING_TOP, PROP_PADDING_RIGHT, PROP_PADDING_BOTTOM, PROP_PADDING_LEFT].freeze
BORDER_WIDTHS =

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.

[
  PROP_BORDER_TOP_WIDTH,
  PROP_BORDER_RIGHT_WIDTH,
  PROP_BORDER_BOTTOM_WIDTH,
  PROP_BORDER_LEFT_WIDTH
].freeze
BORDER_STYLES =

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.

[
  PROP_BORDER_TOP_STYLE,
  PROP_BORDER_RIGHT_STYLE,
  PROP_BORDER_BOTTOM_STYLE,
  PROP_BORDER_LEFT_STYLE
].freeze
BORDER_COLORS =

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.

[
  PROP_BORDER_TOP_COLOR,
  PROP_BORDER_RIGHT_COLOR,
  PROP_BORDER_BOTTOM_COLOR,
  PROP_BORDER_LEFT_COLOR
].freeze
SIDE_TOP =

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.

Side name constants (for string operations, not CSS properties)

'top'
SIDE_RIGHT =

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.

'right'
SIDE_BOTTOM =

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.

'bottom'
SIDE_LEFT =

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.

'left'
BORDER_SIDE_PROPS =

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.

Maps a border side name to its [width, style, color] property constants

{
  SIDE_TOP => [PROP_BORDER_TOP_WIDTH, PROP_BORDER_TOP_STYLE, PROP_BORDER_TOP_COLOR],
  SIDE_RIGHT => [PROP_BORDER_RIGHT_WIDTH, PROP_BORDER_RIGHT_STYLE, PROP_BORDER_RIGHT_COLOR],
  SIDE_BOTTOM => [PROP_BORDER_BOTTOM_WIDTH, PROP_BORDER_BOTTOM_STYLE, PROP_BORDER_BOTTOM_COLOR],
  SIDE_LEFT => [PROP_BORDER_LEFT_WIDTH, PROP_BORDER_LEFT_STYLE, PROP_BORDER_LEFT_COLOR]
}.freeze
FONT_PROPERTIES =

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.

[
  PROP_FONT_STYLE,
  PROP_FONT_VARIANT,
  PROP_FONT_WEIGHT,
  PROP_FONT_SIZE,
  PROP_LINE_HEIGHT,
  PROP_FONT_FAMILY
].freeze
BACKGROUND_PROPERTIES =

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.

[
  PROP_BACKGROUND_COLOR,
  PROP_BACKGROUND_IMAGE,
  PROP_BACKGROUND_REPEAT,
  PROP_BACKGROUND_POSITION,
  PROP_BACKGROUND_ATTACHMENT
].freeze
LIST_STYLE_PROPERTIES =

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.

[PROP_LIST_STYLE_TYPE, PROP_LIST_STYLE_POSITION, PROP_LIST_STYLE_IMAGE].freeze
BORDER_ALL =

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.

(BORDER_WIDTHS + BORDER_STYLES + BORDER_COLORS).freeze
SHORTHAND_PROPERTIES =

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.

Shorthand property lookup (Hash is faster than Array#include? or Set) Used for fast-path check to avoid calling expand_shorthand for non-shorthands

{
  'margin' => true,
  'padding' => true,
  'border' => true,
  'border-top' => true,
  'border-right' => true,
  'border-bottom' => true,
  'border-left' => true,
  'border-width' => true,
  'border-style' => true,
  'border-color' => true,
  'font' => true,
  'background' => true,
  'list-style' => true
}.freeze
LIST_STYLE_POSITION_KEYWORDS =

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.

List style keywords

%w[inside outside].freeze
BORDER_WIDTH_KEYWORDS =

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.

Border property keywords

%w[thin medium thick].freeze
BORDER_STYLE_KEYWORDS =

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.

%w[none hidden dotted dashed solid double groove ridge inset outset].freeze
FONT_SIZE_KEYWORDS =

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.

Font property keywords

%w[xx-small x-small small medium large x-large xx-large smaller larger].freeze
FONT_STYLE_KEYWORDS =

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.

%w[normal italic oblique].freeze
FONT_VARIANT_KEYWORDS =

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.

%w[normal small-caps].freeze
FONT_WEIGHT_KEYWORDS =

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.

%w[normal bold bolder lighter 100 200 300 400 500 600 700 800 900].freeze
BACKGROUND_REPEAT_KEYWORDS =

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.

Background property keywords

%w[repeat repeat-x repeat-y no-repeat space round].freeze
BACKGROUND_ATTACHMENT_KEYWORDS =

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.

%w[scroll fixed local].freeze
BACKGROUND_POSITION_KEYWORDS =

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.

%w[left right center top bottom].freeze

Class Method Summary collapse

Class Method Details

.expand_shorthand(decl) ⇒ Array<Declaration>

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.

Expand a single shorthand declaration into longhand declarations. Returns an array of longhand declarations. If the declaration is not a shorthand, returns an array with just that declaration.

Parameters:

Returns:

  • (Array<Declaration>)

    Array of expanded longhand declarations



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/cataract/pure/flatten.rb', line 461

def self.expand_shorthand(decl)
  case decl.property
  when 'margin'
    expand_margin(decl)
  when 'padding'
    expand_padding(decl)
  when 'border'
    expand_border(decl)
  when 'border-top', 'border-right', 'border-bottom', 'border-left'
    expand_border_side(decl)
  when 'border-width'
    expand_border_width(decl)
  when 'border-style'
    expand_border_style(decl)
  when 'border-color'
    expand_border_color(decl)
  when 'font'
    expand_font(decl)
  when 'background'
    expand_background(decl)
  when 'list-style'
    expand_list_style(decl)
  else
    # Not a shorthand, return as-is in an array
    [decl]
  end
end

.flatten(stylesheet, mutate: false) ⇒ Stylesheet

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.

Merge stylesheet according to CSS cascade rules

Parameters:

  • stylesheet (Stylesheet)

    Stylesheet to merge

  • mutate (Boolean) (defaults to: false)

    If true, mutate the stylesheet; otherwise create new one

Returns:



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/cataract/pure/flatten.rb', line 172

def self.flatten(stylesheet, mutate: false)
  at_rules, regular_rules = partition_at_rules(stylesheet.rules)

  expand_all_shorthands!(regular_rules)
  merged_rules = merge_rules_by_selector_and_media(regular_rules)
  merged_rules.each { |rule| recreate_shorthands!(rule) }

  # Assign new IDs before checking divergence (so we can build correct selector_lists hash)
  merged_rules.each_with_index { |rule, i| rule.id = i }

  selector_lists = build_selector_lists(stylesheet, merged_rules)

  # Add passthrough AtRules to output
  merged_rules.concat(at_rules)

  media_queries = stylesheet.instance_variable_get(:@media_queries)
  media_query_lists = stylesheet.instance_variable_get(:@_media_query_lists)
  new_media_index = rebuild_media_index(merged_rules, media_queries, media_query_lists)

  build_result(stylesheet, merged_rules, new_media_index, media_queries, media_query_lists, selector_lists,
               mutate)
end