Class: AntiSamy::CssFilter

Inherits:
RSAC::DocumentHandler show all
Defined in:
lib/antisamy/css/css_filter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(policy, tag) ⇒ CssFilter

Returns a new instance of CssFilter.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/antisamy/css/css_filter.rb', line 7

def initialize(policy,tag)
  @policy = policy
  @validator = CssValidator.new(@policy)
  @errors = []
  @clean = ''
  @tag = tag
  @selector_open = false
  @style_sheets = []
  @inline = !tag.nil?
  @media_open = false
  @current_media = nil
end

Instance Attribute Details

#cleanObject

Returns the value of attribute clean.



5
6
7
# File 'lib/antisamy/css/css_filter.rb', line 5

def clean
  @clean
end

#errorsObject

Returns the value of attribute errors.



5
6
7
# File 'lib/antisamy/css/css_filter.rb', line 5

def errors
  @errors
end

#style_sheetsObject

Returns the value of attribute style_sheets.



5
6
7
# File 'lib/antisamy/css/css_filter.rb', line 5

def style_sheets
  @style_sheets
end

Instance Method Details

#comment(text) ⇒ Object

Receive notification of a comment



28
29
30
# File 'lib/antisamy/css/css_filter.rb', line 28

def comment(text)
  @errors << ScanMessage.new(ScanMessage::ERROR_COMMENT_REMOVED,@tag,text)
end

#end_document(input_source) ⇒ Object

Receive notification of the end of a style sheet.



24
25
# File 'lib/antisamy/css/css_filter.rb', line 24

def end_document(input_source) #:nodoc:
end

#end_font_faceObject

Notification of the end of a font face statement.



116
117
# File 'lib/antisamy/css/css_filter.rb', line 116

def end_font_face #:nodoc:
end

#end_media(media) ⇒ Object

Notification of the end of a media statement



98
99
100
101
# File 'lib/antisamy/css/css_filter.rb', line 98

def end_media(media)
  @media_open = false
  @current_media = nil
end

#end_page(name = nil, pseudo_page = nil) ⇒ Object

Notification of the end of a page statement



108
109
# File 'lib/antisamy/css/css_filter.rb', line 108

def end_page(name = nil, pseudo_page = nil) # :nodoc:
end

#end_selector(selectors) ⇒ Object

Notification of the end of a rule statement.



158
159
160
161
162
163
# File 'lib/antisamy/css/css_filter.rb', line 158

def end_selector(selectors)
  if @selector_open
    clean << "}\n"
  end
  @selector_open = false
end

#error(t) ⇒ Object



32
33
34
# File 'lib/antisamy/css/css_filter.rb', line 32

def error(exception)
  #puts exception
end

#fatal_error(exception) ⇒ Object



36
37
38
# File 'lib/antisamy/css/css_filter.rb', line 36

def fatal_error(exception)
  #puts exception
end

#ignorable_at_rule(at_rule) ⇒ Object

Receive notification of an unknown at rule not supported by this parser.



52
53
54
55
56
57
58
# File 'lib/antisamy/css/css_filter.rb', line 52

def ignorable_at_rule(at_rule)
  if inline
    @errors << ScanMessage.new(ScanMessage::ERROR_CSS_TAG_RULE_NOTFOUND,@tag,at_rule)
  else
    @errors << ScanMessage.new(ScanMessage::ERROR_STYLESHEET_RULE_NOTFOUND,@tag,at_rule)
  end
end

#import_style(uri, media, default_namespace_uri = nil) ⇒ Object

Called on an import statement



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/antisamy/css/css_filter.rb', line 65

def import_style(uri, media, default_namespace_uri = nil)
  # check directive
  unless @policy.directive(Policy::EMBED_STYLESHEETS)
    @errors << ScanMessage.new(ScanMessage::ERROR_CSS_IMPORT_DISABLED,@tag,uri)
    return
  end
  # check for null uri
  if uri.nil?
    @errors << ScanMessage.new(ScanMessage::ERROR_CSS_IMPORT_URL_INVALID,@tag)
  end
  # check uri rules
  begin
    luri = RSAC::LexicalURI.new(uri)
    link = URI.parse(luri.string_value)
    link.normalize!
    onsite = @policy.expression("offsiteURL")
    offsite = @policy.expression("onsiteURL")
    # bad uri
    raise "Invalid URI Pattern" if link.to_s !~ onsite and link.to_s !~ offsite
    raise "Invalid URI" unless link.absolute?
    @style_sheets << link
  rescue Exception => e
    @errors << ScanMessage.new(ScanMessage::ERROR_CSS_IMPORT_URL_INVALID,@tag,uri)        
  end
end

#namespace_declaration(prefix, uri) ⇒ Object

Namespace declaration



61
62
# File 'lib/antisamy/css/css_filter.rb', line 61

def namespace_declaration(prefix, uri) #:nodoc:
end

#property(name, value, important) ⇒ Object

Notification of a declaration.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/antisamy/css/css_filter.rb', line 166

def property(name, value, important)      
  return unless @selector_open and @inline
  if @validator.valid_property?(name,value)
    clean << "\t" unless @inline
    clean << "#{name}:"
    value.each do |v|
      clean << " #{v.to_s}"
    end
    clean << ";"
    clean << "\n" unless @inline
  else
    cval = value.to_s
    if @inline
      @errors << ScanMessage.new(ScanMessage::ERROR_CSS_TAG_PROPERTY_INVALID,@tag,name,cval)
    else
      @errors << ScanMessage.new(ScanMessage::ERROR_STYLESHEET_PROPERTY_INVALID,@tag,name,cval)
    end        
  end
  
end

#start_document(input_source) ⇒ Object

Start of document



20
21
# File 'lib/antisamy/css/css_filter.rb', line 20

def start_document(input_source) #:nodoc:
end

#start_font_faceObject

Notification of the beginning of a font face statement.



112
113
# File 'lib/antisamy/css/css_filter.rb', line 112

def start_font_face #:nodoc:
end

#start_media(media) ⇒ Object

Notification of the start of a media statement



92
93
94
95
# File 'lib/antisamy/css/css_filter.rb', line 92

def start_media(media)
  @media_open = true
  @current_media = media
end

#start_page(name = nil, pseudo_page = nil) ⇒ Object

Notification of the start of a page statement



104
105
# File 'lib/antisamy/css/css_filter.rb', line 104

def start_page(name = nil, pseudo_page = nil) #:nodoc:
end

#start_selector(selectors) ⇒ Object

Notification of the beginning of a rule statement.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/antisamy/css/css_filter.rb', line 120

def start_selector(selectors)
  count = 0
  selectors.each do |s|
    name = s.to_css
    valid = false
    begin
      @validator.valid_selector?(name,s)
      valid = true
    rescue Exception => e
      if @inline
        @errors << ScanMessage.new(ScanMessage::ERROR_CSS_TAG_SELECTOR_NOTFOUND,@tag,name)
      else
        @errors << ScanMessage.new(ScanMessage::ERROR_STYLESHEET_SELECTOR_NOTFOUND,@tag,name)
      end
    
    end
    if valid
      if count > 0
        clean << ", "
      end
      clean << name
      count += 1
    else
      # not allowed selector
      if @inline
        @errors << ScanMessage.new(ScanMessage::ERROR_CSS_TAG_SELECTOR_DISALLOWED,@tag,name)
      else
        @errors << ScanMessage.new(ScanMessage::ERROR_STYLESHEET_SELECTOR_DISALLOWED,@tag,name)
      end
    end
  end
  if count > 0
    clean << " {\n"
    @selector_open = true
  end
end

#warn(t) ⇒ Object



44
45
46
# File 'lib/antisamy/css/css_filter.rb', line 44

def warn(t)
  #puts t
end

#warning_error(exception) ⇒ Object



48
49
50
# File 'lib/antisamy/css/css_filter.rb', line 48

def warning_error(exception)
  #puts exception
end