code_terminator 0.5.2 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -62,7 +62,7 @@ class CodeTerminator::Html
62
62
  @elements << node
63
63
 
64
64
  #search elements from body section
65
- if !reader.at('body').nil?
65
+ if reader.at('body')
66
66
  node = Hash.new
67
67
  node[:parent] = "html"
68
68
  node[:tag] = "body"
@@ -72,8 +72,8 @@ class CodeTerminator::Html
72
72
  node = Hash.new
73
73
  node[:parent] = "html"
74
74
  node[:tag] = "body"
75
- node[:attribute] = element_attribute.name if !element_attribute.name.nil?
76
- node[:value] = element_attribute.value if !element_attribute.value.nil?
75
+ node[:attribute] = element_attribute.name if element_attribute.name
76
+ node[:value] = element_attribute.value if element_attribute.value
77
77
  node[:pointer] = element_attribute.pointer_id
78
78
  @elements << node
79
79
  end
@@ -81,7 +81,7 @@ class CodeTerminator::Html
81
81
  #end search
82
82
 
83
83
  #search elements from head section
84
- if !reader.at('head').nil?
84
+ if reader.at('head')
85
85
  node = Hash.new
86
86
  node[:parent] = "html"
87
87
  node[:tag] = "head"
@@ -91,8 +91,10 @@ class CodeTerminator::Html
91
91
  node = Hash.new
92
92
  node[:parent] = "head"
93
93
  node[:tag] = child.name
94
- node[:content] = child.text if !child.text.nil? or child.comment?
94
+ node[:content] = child.text if child.text or child.comment?
95
95
  node[:pointer] = child.pointer_id
96
+ node[:parent_pointer] = child.parent.pointer_id
97
+
96
98
  @elements << node
97
99
  else
98
100
  child.attribute_nodes.each do |element_attribute|
@@ -105,9 +107,10 @@ class CodeTerminator::Html
105
107
  end
106
108
  # node[:tag] = ( ? "text", child.name)
107
109
  node[:content] = child.text if !child.text.nil?
108
- node[:attribute] = element_attribute.name if !element_attribute.name.nil?
109
- node[:value] = element_attribute.value if !element_attribute.value.nil?
110
+ node[:attribute] = element_attribute.name if element_attribute.name
111
+ node[:value] = element_attribute.value if element_attribute.value
110
112
  node[:pointer] = element_attribute.pointer_id
113
+ node[:parent_pointer] = child.pointer_id
111
114
  @elements << node
112
115
  end
113
116
  end
@@ -117,7 +120,7 @@ class CodeTerminator::Html
117
120
  #end search elements
118
121
 
119
122
  #search elements inside body (children)
120
- if !reader.at('body').nil?
123
+ if reader.at('body')
121
124
  reader.at('body').children.each do |child|
122
125
  if child.attribute_nodes.empty?
123
126
  node = Hash.new
@@ -125,15 +128,17 @@ class CodeTerminator::Html
125
128
  node[:tag] = child.name
126
129
  node[:content] = child.text if child.text? or child.comment?
127
130
  node[:pointer] = child.pointer_id
131
+ node[:parent_pointer] = child.parent.pointer_id
128
132
  @elements << node
129
133
  else
130
134
  child.attribute_nodes.each do |element_attribute|
131
135
  node = Hash.new
132
136
  node[:parent] = "body"
133
137
  node[:tag] = child.name
134
- node[:attribute] = element_attribute.name if !element_attribute.name.nil?
135
- node[:value] = element_attribute.value if !element_attribute.value.nil?
138
+ node[:attribute] = element_attribute.name if element_attribute.name
139
+ node[:value] = element_attribute.value if element_attribute.value
136
140
  node[:pointer] = element_attribute.pointer_id
141
+ node[:parent_pointer] = child.pointer_id
137
142
  @elements << node
138
143
  end
139
144
  end
@@ -225,11 +230,11 @@ class CodeTerminator::Html
225
230
  def print_elements(elements)
226
231
  text = ""
227
232
  elements.each do |child|
228
- text << "parent = " + child[:parent] + "<br>" if !child[:parent].nil?
229
- text << "tag = " + child[:tag] + "<br>" if !child[:tag].nil?
230
- text << "attribute = " + child[:attribute] + "<br>" if !child[:attribute].nil?
231
- text << "value = " + child[:value] + "<br>" if !child[:value].nil?
232
- text << "content = " + child[:content] + "<br>" if !child[:content].nil?
233
+ text << "parent = " + child[:parent] + "<br>" if child[:parent]
234
+ text << "tag = " + child[:tag] + "<br>" if child[:tag]
235
+ text << "attribute = " + child[:attribute] + "<br>" if child[:attribute]
236
+ text << "value = " + child[:value] + "<br>" if child[:value]
237
+ text << "content = " + child[:content] + "<br>" if child[:content]
233
238
  text << "<hr>"
234
239
  end
235
240
  text
@@ -282,95 +287,50 @@ class CodeTerminator::Html
282
287
  # code: (String)
283
288
 
284
289
  def match(source, code)
285
- html_errors = Array.new
290
+ @html_errors = Array.new
291
+ html_errors = @html_errors
286
292
 
287
293
  code = Nokogiri::HTML(code)
288
294
 
289
295
  elements = get_elements(source)
290
296
 
291
- css_code_checked = Array.new
292
297
 
293
298
  exist_in_body = Array.new
294
299
 
300
+ error_elements = Array.new
301
+
302
+ errors_count = Array.new
303
+
295
304
  error333 = nil
296
305
 
306
+ #lista de relaciones entre tags y elementos
307
+ css_code_checked = Array.new
308
+
309
+
297
310
  elements.each do |e|
298
311
 
312
+ if errors_count.select {|element| element[:parent_pointer].to_s == e[:parent_pointer].to_s && element[:tag].to_s == e[:tag]}.count < 1
313
+ error_element = Hash.new
314
+ error_element[:tag] = e[:tag]
315
+ error_element[:parent_pointer] = e[:parent_pointer]
316
+ error_element[:count] = 0
317
+ errors_count << error_element
318
+ end
319
+
299
320
  item = e[:tag]
300
321
 
301
322
  if item == "text" or item == "comment"
302
-
303
323
  # Check the text
304
- if !e[:content].nil?
324
+ if e[:content]
305
325
  if code.css(e[:parent]).count < 2
306
326
  if code.css(e[:parent]).class == Nokogiri::XML::NodeSet
307
- text_found = false
308
- @comment_found = false if item == "comment"
309
- error330 = nil
310
- if code.css(e[:parent]).children.any?
311
-
312
- code.css(e[:parent]).children.each do |node_child|
313
-
314
- if node_child.class == Nokogiri::XML::Comment
315
- if e[:content].strip != ""
316
- if node_child.text.strip! != e[:content].strip!
317
- error330 = new_error(element: e, type: 330, description: "The text inside the comment should be #{e[:content]}")
318
- end
319
- end
320
- @comment_found = true
321
- end
322
-
323
- if node_child.class == Nokogiri::XML::Text && item != "comment"
324
- if node_child.text.strip != e[:content].strip
325
- error330 = new_error(element: e, type: 330, description: "The text inside `<#{e[:parent]}>` should be #{e[:content]}")
326
- else
327
- text_found = true
328
- end
329
- end
330
-
331
- end
332
- #end each
333
- else
334
-
335
- if code.css(e[:parent]).text.strip != e[:content].strip
336
- #validate if comment exist and has the expected content
337
- if item == "comment"
338
- error330 = new_error(element: e, type: 330, description: "The text inside the comment should be #{e[:content]}")
339
- @comment_found = true
340
- else
341
- error330 = new_error(element: e, type: 330, description: "The text inside `<#{e[:parent]}>` should be #{e[:content]}")
342
- end
343
- else
344
- text_found = true
345
- end
346
-
347
- end
348
- #end if parent has children
349
-
350
- #if comment not found, throw error
351
- if !(defined? @comment_found).nil?
352
- html_errors << new_error(element: e, type: 404, description: "Remember to add the comment tag") if !@comment_found
353
- remove_instance_variable(:@comment_found)
354
- end
355
-
356
- if !text_found && !error330.nil?
357
- html_errors << error330
358
- error330 = nil
359
- end
327
+ #look for children elements with texts or comments
328
+ look_comment_or_text(code,e)
360
329
  end
361
- #end if parent is nodeset
362
330
  else
363
- exist = false
364
- code.css(e[:parent]).each do |code_css|
365
- if code_css.text == e[:content]
366
- exist = true
367
- end
368
- end
369
- if !exist
370
- html_errors << new_error(element: e, type: 330, description: "The text inside `<#{e[:parent]}>` should be #{e[:content]}")
371
- end
331
+ #check if parent tag of the user code has text apart from the children tags
332
+ look_parent_text(code,e)
372
333
  end
373
- #end if parent < 2
374
334
  end
375
335
  #end if content is null
376
336
 
@@ -379,23 +339,58 @@ class CodeTerminator::Html
379
339
 
380
340
  if code.css(e[:tag]).length > 0
381
341
 
382
-
383
342
  code.css(e[:tag]).each do |tag|
384
343
 
385
- p "code css + " + e[:tag].to_s
386
- p "pointer element " + e[:pointer].to_s
344
+ e_check = css_code_checked.select {|element| element[:original_pointer].to_s == e[:pointer].to_s }
345
+ # p "echeck " + e_check.to_s
346
+ e_check2 = css_code_checked.select {|element| element[:pointer].to_s == tag.pointer_id.to_s }
347
+
348
+ #original_pointer es el pointer del elemento e[]
349
+ #busca si el original_pointer esta en la lista de relaciones
350
+ #busca si el pointer del tag esta en la lista de relaciones
351
+ #cuando un original pointer o un pointer esta en la lista de relaciones, ya no puede volver a ser ingresado en la lista
352
+ #si el original pointer ya esta en la lista de relaciones, ya no es necesario volver a checarlo
353
+ check_original_pointer = css_code_checked.select {|element| element[:original_pointer].to_s == e[:pointer].to_s }
354
+
355
+ check_add_pointer = css_code_checked.select {|element| element[:pointer].to_s == tag.pointer_id.to_s }
356
+
357
+ #si el target pointer- pointer no esta en la lista de check agregar,
358
+ #si es target-pointer que ya existe + otro pointer da error,
359
+ #si es target-pointer y si esta, hacer nada.
360
+ #si pointer ya esta en la lista hacer nada
361
+ #look for same tags in code
362
+
363
+ if check_original_pointer.count == 0
364
+ if check_add_pointer.count < 1
365
+ element_checked = Hash.new
366
+ element_checked[:pointer] = tag.pointer_id
367
+ element_checked[:tag] = e[:tag]
368
+ element_checked[:original_pointer] = e[:pointer]
369
+ element_checked[:original_parent_pointer] = e[:parent_pointer]
370
+ css_code_checked << element_checked
371
+
372
+
373
+ error_element = errors_count.select {|element| element[:tag].to_s == e[:tag].to_s && element[:parent_pointer].to_s == e[:parent_pointer].to_s}.first
374
+ error_element[:count] += 1
375
+
376
+ else
377
+
378
+ end
379
+ end
380
+ # end
381
+ # e_check_exist = nil
382
+ # end
387
383
 
388
- p e_check = css_code_checked.select {|element| element[:target_pointer].to_s == e[:pointer].to_s }
389
- p e_check2 = css_code_checked.select {|element| element[:pointer].to_s == tag.pointer_id.to_s }
390
384
  if e_check.count < 1 and e_check2.count < 1
391
385
 
392
- element_checked = Hash.new
393
- element_checked[:pointer] = tag.pointer_id
394
- element_checked[:tag] = e[:tag]
395
- element_checked[:target_pointer]= e[:pointer]
386
+ element_checked = Hash.new
387
+ element_checked[:pointer] = tag.pointer_id
388
+ element_checked[:tag] = e[:tag]
389
+ element_checked[:target_pointer] = e[:pointer]
390
+ element_checked[:target_parent_pointer] = e[:parent_pointer]
396
391
 
397
392
 
398
- if !e[:attribute].nil?
393
+ if e[:attribute]
399
394
  # Check the tag's attributes
400
395
  if tag.attribute(e[:attribute]).nil?
401
396
  html_errors << new_error(element: e, type: 334, description: "`<#{e[:tag]}>` should have an attribute named #{e[:attribute]}")
@@ -416,37 +411,48 @@ class CodeTerminator::Html
416
411
  end
417
412
 
418
413
  end #if element checked
414
+
419
415
  end
420
416
 
417
+
418
+ # p "respond" + tag.parent.to_s
421
419
  # Check that tags exist within parent tags
422
420
  if tag.first.respond_to? :parent
423
421
 
424
- if tag.count < 2 && !tag.first.nil?
422
+ p "check if exists in parent tags"
423
+
424
+ e_check4 = css_code_checked.select {|element| element[:pointer].to_s == e[:pointer].to_s }
425
+ e_check5 = css_code_checked.select {|element| element[:target_parent_pointer].to_s == e[:parent_pointer].to_s }
426
+
427
+ if (tag.count < 2 && tag.first) or (e_check4.count < 1 && e_check5.count < 1)
425
428
  if tag.first.parent.name != e[:parent]
426
429
  html_errors << new_error(element: e, type: 440, description: "Remember to add the `<#{e[:tag]}>` tag inside `<#{e[:parent]}>`")
427
430
  end
428
431
  else
429
- exist_in_parent = false
432
+ exist_in_parent = false
430
433
  tag.each do |code_css|
431
- if code_css.parent.name == e[:parent]
432
- exist_in_parent = true
433
- end
434
- end
435
- if !exist_in_parent
436
- html_errors << new_error(element: e, type: 440, description: "Remember to add the `<#{e[:tag]}>` tag inside `<#{e[:parent]}>`")
437
- end
438
- end
434
+ exist_in_parent = true if code_css.parent.name == e[:parent]
435
+ end
436
+ html_errors << new_error(element: e, type: 440, description: "Remember to add the `<#{e[:tag]}>` tag inside `<#{e[:parent]}>`") if !exist_in_parent
437
+ end
439
438
  end
439
+
440
+
440
441
  end
441
442
 
443
+
442
444
  else
445
+ "pasa else"
443
446
  # Check that the tag is present
444
- if code.at_css(e[:tag]).nil?
447
+ # p "check if exists in parent"
448
+ e_check4 = css_code_checked.select {|element| element[:pointer].to_s == e[:pointer].to_s }
449
+ e_check5 = css_code_checked.select {|element| element[:target_parent_pointer].to_s == e[:parent_pointer].to_s }
450
+ if code.at_css(e[:tag]).nil? or e_check4.count < 1 and e_check5.count < 1
445
451
  html_errors << new_error(element: e, type: 404, description: "Remember to add the `<#{e[:tag]}>` tag")
446
452
  end
447
453
  end
448
454
 
449
- if !exist_in_body.empty? && !exist_in_body.include?(true) && !error333.nil?
455
+ if exist_in_body && !exist_in_body.include?(true) && error333
450
456
  html_errors << error333
451
457
  end
452
458
  exist_in_body = []
@@ -454,16 +460,27 @@ class CodeTerminator::Html
454
460
  end
455
461
 
456
462
  end
457
- p css_code_checked
463
+
464
+ errors_count.each do |x|
465
+ #filtrar por parent
466
+ # tag_count = code.css(x[:tag]).length
467
+ tag_count = elements.select {|element| element[:parent_pointer].to_s == x[:parent_pointer].to_s && element[:tag].to_s == x[:tag]}.count
468
+
469
+ if tag_count > 1
470
+ if x[:count] < tag_count
471
+ html_errors << new_error(element: x[:tag], type: 404, description: "Remember to add the `<#{x[:tag]}>` tag")
472
+ end
473
+ end
474
+ end
458
475
 
459
476
  html_errors
477
+
460
478
  end
461
479
 
462
480
  private
463
481
 
464
482
  def add_children(parent)
465
483
  parent.children.each do |child|
466
-
467
484
  if child.attribute_nodes.empty?
468
485
  node = Hash.new
469
486
  node[:parent] = parent.name
@@ -474,6 +491,8 @@ class CodeTerminator::Html
474
491
  node[:tag] = child.name
475
492
  end
476
493
  node[:content] = child.text if !child.text.nil? and child.class!=Nokogiri::XML::Element
494
+ node[:pointer] = child.pointer_id
495
+ node[:parent_pointer] = child.parent.pointer_id
477
496
  @elements << node
478
497
  else
479
498
  child.attribute_nodes.each do |element_attribute|
@@ -489,6 +508,8 @@ class CodeTerminator::Html
489
508
  end
490
509
  node[:attribute] = element_attribute.name if !element_attribute.name.nil?
491
510
  node[:value] = element_attribute.value if !element_attribute.value.nil?
511
+ node[:pointer] = element_attribute.pointer_id
512
+ node[:parent_pointer] = child.pointer_id
492
513
  @elements << node
493
514
  end
494
515
  end
@@ -498,17 +519,17 @@ class CodeTerminator::Html
498
519
  end
499
520
 
500
521
  def remove_empty_text (reader)
501
- if !reader.at('head').nil?
522
+ if reader.at('head')
502
523
  reader.at('head').children.each do |child|
503
- if !child.text.nil?
524
+ if child.text
504
525
  child.remove if child.content.to_s.squish.empty? && child.class == Nokogiri::XML::Text
505
526
  end
506
527
  check_children(child) if child.children.any?
507
528
  end
508
529
  end
509
- if !reader.at('body').nil?
530
+ if reader.at('body')
510
531
  reader.at('body').children.each do |child|
511
- if !child.text.nil?
532
+ if child.text
512
533
  child.remove if child.content.to_s.squish.empty? && child.class == Nokogiri::XML::Text
513
534
  end
514
535
  check_children(child) if child.children.any?
@@ -519,8 +540,8 @@ class CodeTerminator::Html
519
540
 
520
541
  def check_children(parent)
521
542
  parent.children.each do |child|
522
- if !child.text.nil?
523
- child.remove if child.content.to_s.squish.empty?
543
+ if child.text
544
+ child.remove if child.content.to_s.squish.empty? && child.class == Nokogiri::XML::Text
524
545
  end
525
546
  check_children(child) if child.children.any?
526
547
  end
@@ -537,6 +558,83 @@ class CodeTerminator::Html
537
558
  node
538
559
  end
539
560
 
561
+
562
+ #methods of match
563
+ def look_comment_or_text(code,e)
564
+ error330 = nil
565
+ text_found = false
566
+ @comment_found = false if e[:tag] == "comment"
567
+
568
+ #look for comments or text in code
569
+ #code, e
570
+ if code.css(e[:parent]).children.any?
571
+ #look for comments and text in children of body
572
+ # code, e
573
+ #save
574
+ #return
575
+ code.css(e[:parent]).children.each do |node_child|
576
+ #if class of the node is a comment, look in the code
577
+ # @e, node_child
578
+ #save error330
579
+ #return true (flag)
580
+ if node_child.class == Nokogiri::XML::Comment
581
+ error330 = new_error(element: e, type: 330, description: "The text inside the comment should be #{e[:content]}") if e[:content].strip != "" && node_child.text.strip! != e[:content].strip!
582
+ @comment_found = true
583
+ end
584
+
585
+ #if class of node is text and element is not a comment
586
+ #@e, node_child
587
+ #save a error330, text_found
588
+ #return true (flag)
589
+ if node_child.class == Nokogiri::XML::Text && e[:content] != "comment"
590
+ node_child.text.strip != e[:content].strip ? error330 = new_error(element: e, type: 330, description: "The text inside `<#{e[:parent]}>` should be #{e[:content]}") : text_found = true
591
+ end
592
+ end #each
593
+
594
+ else
595
+ #validate if comment exist and has the expected content in body
596
+ #code, @e
597
+ #save @comment_found, text_found
598
+ if code.css(e[:parent]).text.strip != e[:content].strip
599
+ if e[:tag] == "comment"
600
+ error330 = new_error(element: e, type: 330, description: "The text inside the comment should be #{e[:content]}")
601
+ @comment_found = true
602
+ else
603
+ error330 = new_error(element: e, type: 330, description: "The text inside `<#{e[:parent]}>` should be #{e[:content]}")
604
+ end
605
+ else
606
+ text_found = true
607
+ end
608
+ end #end if parent has children
609
+
610
+ #throw errors of comment or text
611
+ #if comment not found, throw error
612
+ if (defined? @comment_found) && !@comment_found
613
+ @html_errors << new_error(element: e, type: 404, description: "Remember to add the comment tag")
614
+ remove_instance_variable(:@comment_found) if !@comment_found
615
+ end
616
+
617
+ if !text_found && error330
618
+ @html_errors << error330
619
+ error330 = nil
620
+ end
621
+ #end throw errors
622
+ end #end look_comment_or_text
623
+
624
+ def look_parent_text(code,e)
625
+ exist = false
626
+ #look for text in parent, if found check flag true
627
+ code.css(e[:parent]).each do |code_css|
628
+ if code_css.text == e[:content]
629
+ exist = true
630
+ end
631
+ end
632
+ if !exist
633
+ @html_errors << new_error(element: e, type: 330, description: "The text inside `<#{e[:parent]}>` should be #{e[:content]}")
634
+ end
635
+ end
636
+
637
+
540
638
  #end
541
639
 
542
640
  end