Class: CodeTerminator::Css

Inherits:
Object
  • Object
show all
Defined in:
lib/code_terminator/css.rb

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Css

Returns a new instance of Css.



5
6
7
8
9
10
11
12
# File 'lib/code_terminator/css.rb', line 5

def initialize(args = {})
  @code = args[:code]
  @source = args[:source]
  @tags = Array.new

  args[:source_type] ||= "file"
  @source_type = args[:source_type]
end

Instance Method Details

#get_elements(source) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/code_terminator/css.rb', line 52

def get_elements(source)
   reader = read_file(source)
   parser = Crass.parse(reader)
   errors = parser.pop
   elements = Array.new
   selector = ""

   parser.each do |node|
     if !node[:selector].nil?
       selector = node[:selector][:value]
       elements << {:selector => selector}
     end
     if !node[:children].nil?
       node[:children].each do |children|
         if children.has_value?(:property)
           elements << {:selector => selector, :property => children[:name], :value => children[:value]}
         end
       end
     end
   end
   elements
end

#get_instructions(source) ⇒ Object

Get the instructions to recreate the html code. Return an array with strings .

Example:

>> CodeTerminator::Css.get_instructions(file.get_elements("exercises/test.css"))
=> [["Create the selector body", "In the selector body add the property 'background-color' with value 'yellow' "]

Arguments:

instructions: (Array)


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/code_terminator/css.rb', line 166

def get_instructions(source)
  elements = get_elements(source)
  text = ""
  instructions = Array.new
  elements.each do |child|
    if child[:property].nil?
      text << "Create the selector " + child[:selector]
    else
      text << "In the selector " + child[:selector] + " add the property '"  + child[:property] + "'"  if !child[:property].nil?
      text << " with value '" + child[:value] + "' " if !child[:value].nil?
    end
    instructions.push(text)
    text = ""
  end
  instructions
end

#match(source, code) ⇒ Object

Example:

test.css
=> body { background-color: lightblue; }

>> CodeTerminator::Css.match("test.css","body {background-color: blue; }")
=> [{:element=>{:selector=>"body", :property=>"background-color", :value=>"yellow"}, :type=>111, :description=>"not the same property background-color: yellow in selector body"}]

Arguments:

source: (String)
code: (String)

Fix: Add <style> tag in the compare



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/code_terminator/css.rb', line 201

def match(source,code)
  #source = "exercises/test.css"
  #code = " a.hover { color: yellow; } body { background-color: lightblue; color: blue; } "
  elements = get_elements(source)
  css_errors = Array.new
  parser = CssParser::Parser.new

  parser.load_string!(code)
  elements.each do |e|
    item = e[:selector]
    if !e[:property].nil?
      property = e[:property]
      parser_array = parser.find_by_selector(item)
      if parser_array.any?
        parser_property = parser_array[0].split(";")
        parser_property.each {|a| a.strip! if a.respond_to? :strip! }

        if e[:value]==""
          property = e[:property] + ": "
          if parser_property.empty? { |s| s.include?(property) }
            css_errors << new_error(element: e, type: 111, description: "Make sure the property _#{property}_ is in the selector **#{item}**")
          end
        else
          property = e[:property] + ": " + e[:value]
          if !parser_property.include?(property)
            css_errors << new_error(element: e, type: 111, description: "Make sure the property _#{property}_ is in the selector **#{item}**")
          end
        end

      else
        node = Hash.new
        css_errors << new_error(element: e, type: 101, description: "Add _#{property}_ inside **#{item}**")
      end
    end
  end
  css_errors
end

#new_file(source, code) ⇒ Object

Create a CSS file with the code of the editor. Return a boolean that indicate if the file was created or not.

Example:

>> CodeTerminator::Css.new_file("test.css", "<style> body { background-color: lightblue; }</style>")
=> true

Arguments:

source: (String)
code: (String)


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/code_terminator/css.rb', line 24

def new_file(source,code)
  fileCss = File.new(source, "w+")
  result = true
  begin
    fileCss.puts code
  rescue
    result = false
  ensure
    fileCss.close unless fileCss.nil?
  end
  #return true if file was succesfully created
  result
end


145
146
147
148
149
150
151
152
153
154
155
# File 'lib/code_terminator/css.rb', line 145

def print_elements(source)
  elements = get_elements(source)
  text = ""
  elements.each do |child|
    text << "selector = " + child[:selector] + "<br>"
    text << "property = " + child[:property] + "<br>" if !child[:property].nil?
    text << "value = " + child[:value] + "<br>" if !child[:value].nil?
    text << "<hr>"
  end
  text
end

#read_file(source) ⇒ Object

Read a css file. Return a string with the text of the file.

Example:

>> CodeTerminator::Css.read_file("test.css")
=> "body { background-color: lightblue; }"

Arguments:

source: (String)


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/code_terminator/css.rb', line 117

def read_file(source)
  if @source_type == "url"
    fileCss = open(source).read
    text = fileCss
  else
    fileCss = File.open(source, "r")
    begin
      text = fileCss.read
    rescue
      text = false
    ensure
      #fileHtml.close unless fileHtml.nil?
    end
  end

  text
end

#validate_syntax(code) ⇒ Object

Validate if the syntax is correct. If is valid return boolean true.

Example:

>> CodeTerminator::Html.validate_syntax("body { background-color: lightblue; }")
=> true

Arguments:

code: (String)

Fixes:

IMPORTANT Method not validate <STYLE> tag from the code


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/code_terminator/css.rb', line 87

def validate_syntax(code)
  errors = Array.new
  tree = Crass.parse(code)
  last = tree.length
  if !tree[last-1].nil?
  nodes = tree[last-1][:children]
   if !nodes.nil?
     nodes.each do |children|
       if children[:node].to_s == "error"
         errors[0] = "error"
       end
     end
   #else
     #valid = false
   end
  else
   errors[0] = "error"
  end
 errors
end