Commit 4fc7fbd2f809cc4ddbbcaae4f55ce2e90b6c3f5c

Authored by Victor Costa
2 parents 1fecb0c4 3f86c4bf

Merge branch 'AI3205-comment-paragraph' into stable

plugins/comment_paragraph/lib/ext/article.rb
@@ -22,22 +22,19 @@ class Article @@ -22,22 +22,19 @@ class Article
22 comment_paragraph_plugin_set_initial_value unless persisted? 22 comment_paragraph_plugin_set_initial_value unless persisted?
23 return unless comment_paragraph_plugin_activated? 23 return unless comment_paragraph_plugin_activated?
24 if body && (body_changed? || setting_changed?(:comment_paragraph_plugin_activate)) 24 if body && (body_changed? || setting_changed?(:comment_paragraph_plugin_activate))
25 - parsed_paragraphs = []  
26 updated = body_changed? ? body_change[1] : body 25 updated = body_changed? ? body_change[1] : body
27 - doc = Nokogiri::HTML(updated).css('body')  
28 -  
29 - doc.children.each do |paragraph|  
30 - if paragraph.to_html =~ /^<div(.*)paragraph_comment(.*)$/ || paragraph.to_html =~ /^<p>\W<\/p>$/  
31 - parsed_paragraphs << paragraph.to_html  
32 - else  
33 - if paragraph.to_html =~ /^(<div|<table|<p|<ul).*/  
34 - parsed_paragraphs << comment_paragraph_plugin_parse_paragraph(paragraph.to_html, SecureRandom.uuid)  
35 - else  
36 - parsed_paragraphs << paragraph.to_html  
37 - end  
38 - end 26 + doc = Nokogiri::HTML(updated)
  27 + doc.css('body > div, body > span, body > p, li').each do |paragraph|
  28 + next if paragraph.css('[data-macro="comment_paragraph_plugin/allow_comment"]').present? || paragraph.content.blank?
  29 +
  30 + commentable = Nokogiri::XML::Node.new("span", doc)
  31 + commentable['class'] = "macro article_comments paragraph_comment #{paragraph['class']}"
  32 + commentable['data-macro'] = 'comment_paragraph_plugin/allow_comment'
  33 + commentable['data-macro-paragraph_uuid'] = SecureRandom.uuid
  34 + commentable.inner_html = paragraph.content
  35 + paragraph.inner_html = commentable
39 end 36 end
40 - self.body = parsed_paragraphs.join() 37 + self.body = doc.at('body').inner_html
41 end 38 end
42 end 39 end
43 40
@@ -50,10 +47,4 @@ class Article @@ -50,10 +47,4 @@ class Article
50 @comment_paragraph_plugin_settings ||= Noosfero::Plugin::Settings.new(environment, CommentParagraphPlugin) 47 @comment_paragraph_plugin_settings ||= Noosfero::Plugin::Settings.new(environment, CommentParagraphPlugin)
51 end 48 end
52 49
53 - def comment_paragraph_plugin_parse_paragraph(paragraph_content, paragraph_uuid)  
54 - "<div class='macro article_comments paragraph_comment' " +  
55 - "data-macro='comment_paragraph_plugin/allow_comment' " +  
56 - "data-macro-paragraph_uuid='#{paragraph_uuid}'>#{paragraph_content}</div>\r\n"  
57 - end  
58 -  
59 end 50 end
plugins/comment_paragraph/lib/ext/tinymce_helper.rb 0 → 100644
@@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
  1 +require_dependency 'tinymce_helper'
  2 +
  3 +module TinymceHelper
  4 +
  5 + def tinymce_init_js_with_comment_paragraph(options = {})
  6 + options = options.merge(:keep_styles => false) if environment.plugin_enabled?(CommentParagraphPlugin)
  7 + tinymce_init_js_without_comment_paragraph(options)
  8 + end
  9 +
  10 + alias_method_chain :tinymce_init_js, :comment_paragraph
  11 +end
plugins/comment_paragraph/public/style.css
@@ -364,3 +364,9 @@ div.article-comments-list-more{ @@ -364,3 +364,9 @@ div.article-comments-list-more{
364 #content .comment-paragraph-plugin .button-bar { 364 #content .comment-paragraph-plugin .button-bar {
365 margin: 5px 0; 365 margin: 5px 0;
366 } 366 }
  367 +
  368 +li span[data-macro="comment_paragraph_plugin/allow_comment"] {
  369 + display: inline-block;
  370 + vertical-align: top;
  371 + min-height: 30px;
  372 +}
plugins/comment_paragraph/test/test_helper.rb
1 require_relative '../../../test/test_helper' 1 require_relative '../../../test/test_helper'
  2 +
  3 +def assert_mark_paragraph(html, tag, content)
  4 + assert_tag_in_string html, :tag => tag, :child => {:tag => 'span', :attributes => {'data-macro'=>"comment_paragraph_plugin/allow_comment"}, :content => content}
  5 +end
plugins/comment_paragraph/test/unit/article_test.rb
@@ -33,20 +33,30 @@ class ArticleTest &lt; ActiveSupport::TestCase @@ -33,20 +33,30 @@ class ArticleTest &lt; ActiveSupport::TestCase
33 end 33 end
34 34
35 should 'parse html if the plugin is not enabled' do 35 should 'parse html if the plugin is not enabled' do
36 - article.body = "<p>paragraph 1</p><p>paragraph 2</p>" 36 + article.body = "<p>paragraph 1</p><div>div 1</div><span>span 1</span>"
  37 + article.comment_paragraph_plugin_activate = true
  38 + article.save!
  39 + assert_mark_paragraph article.body, 'p', 'paragraph 1'
  40 + assert_mark_paragraph article.body, 'div', 'div 1'
  41 + assert_mark_paragraph article.body, 'span', 'span 1'
  42 + end
  43 +
  44 + should 'parse html li when activate comment paragraph' do
  45 + article.body = '<ul><li class="custom_class">item1</li><li>item2</li></ul>'
37 article.comment_paragraph_plugin_activate = true 46 article.comment_paragraph_plugin_activate = true
38 article.save! 47 article.save!
39 - assert_match /data-macro="comment_paragraph_plugin\/allow_comment"/, article.body 48 + assert_mark_paragraph article.body, 'li', 'item1'
  49 + assert_mark_paragraph article.body, 'li', 'item2'
40 end 50 end
41 51
42 should 'do not remove macro div when disable comment paragraph' do 52 should 'do not remove macro div when disable comment paragraph' do
43 - article.body = "<p>paragraph 1</p><p>paragraph 2</p>" 53 + article.body = "<p>paragraph 1</p>"
44 article.comment_paragraph_plugin_activate = true 54 article.comment_paragraph_plugin_activate = true
45 article.save! 55 article.save!
46 - assert_match /data-macro="comment_paragraph_plugin\/allow_comment"/, article.body 56 + assert_mark_paragraph article.body, 'p', 'paragraph 1'
47 article.comment_paragraph_plugin_activate = false 57 article.comment_paragraph_plugin_activate = false
48 article.save! 58 article.save!
49 - assert_match /data-macro="comment_paragraph_plugin\/allow_comment"/, article.body 59 + assert_mark_paragraph article.body, 'p', 'paragraph 1'
50 end 60 end
51 61
52 should 'parse html when activate comment paragraph' do 62 should 'parse html when activate comment paragraph' do
@@ -56,7 +66,42 @@ class ArticleTest &lt; ActiveSupport::TestCase @@ -56,7 +66,42 @@ class ArticleTest &lt; ActiveSupport::TestCase
56 assert_equal "<p>paragraph 1</p><p>paragraph 2</p>", article.body 66 assert_equal "<p>paragraph 1</p><p>paragraph 2</p>", article.body
57 article.comment_paragraph_plugin_activate = true 67 article.comment_paragraph_plugin_activate = true
58 article.save! 68 article.save!
59 - assert_match /data-macro="comment_paragraph_plugin\/allow_comment"/, article.body 69 +
  70 + assert_mark_paragraph article.body, 'p', 'paragraph 1'
  71 + assert_mark_paragraph article.body, 'p', 'paragraph 2'
  72 + end
  73 +
  74 + should 'parse html when add new paragraph' do
  75 + article.body = "<p>paragraph 1</p>"
  76 + article.comment_paragraph_plugin_activate = true
  77 + article.save!
  78 + assert_mark_paragraph article.body, 'p', 'paragraph 1'
  79 +
  80 + article.body += "<p>paragraph 2</p>"
  81 + article.save!
  82 + assert_mark_paragraph article.body, 'p', 'paragraph 1'
  83 + assert_mark_paragraph article.body, 'p', 'paragraph 2'
  84 + end
  85 +
  86 + should 'keep already marked paragraph attributes when add new paragraph' do
  87 + article.body = "<p>paragraph 1</p>"
  88 + article.comment_paragraph_plugin_activate = true
  89 + article.save!
  90 + assert_mark_paragraph article.body, 'p', 'paragraph 1'
  91 + uuid = Nokogiri::HTML(article.body).at('p span.paragraph_comment')['data-macro-paragraph_uuid']
  92 +
  93 + article.body += "<p>paragraph 2</p>"
  94 + article.save!
  95 + assert_mark_paragraph article.body, 'p', 'paragraph 1'
  96 + new_uuid = Nokogiri::HTML(article.body).at('p span.paragraph_comment')['data-macro-paragraph_uuid']
  97 + assert_equal uuid, new_uuid
  98 + end
  99 +
  100 + should 'not parse empty element' do
  101 + article.body = '<div></div>'
  102 + article.comment_paragraph_plugin_activate = true
  103 + article.save!
  104 + assert_equal '<div></div>', article.body
60 end 105 end
61 106
62 should 'be enabled if plugin is enabled and article is a kind of TextArticle' do 107 should 'be enabled if plugin is enabled and article is a kind of TextArticle' do
plugins/comment_paragraph/test/unit/tinymce_helper_test.rb 0 → 100644
@@ -0,0 +1,27 @@ @@ -0,0 +1,27 @@
  1 +require_relative '../test_helper'
  2 +
  3 +class TinymceHelperTest < ActiveSupport::TestCase
  4 +
  5 + include TinymceHelper
  6 +
  7 + def setup
  8 + expects(:top_url).returns('/')
  9 + expects(:tinymce_language).returns('en')
  10 + @plugins = mock
  11 + @plugins.expects(:dispatch).returns([]).at_least_once
  12 + @environment = Environment.default
  13 + end
  14 +
  15 + attr_accessor :top_url, :environment
  16 +
  17 + should 'set keep_styles to false in tinymce options' do
  18 + environment.enable_plugin(CommentParagraphPlugin)
  19 + assert_match /"keep_styles":false/, tinymce_init_js
  20 + end
  21 +
  22 + should 'do not set keep_styles to false when plugin is not enabled' do
  23 + environment.disable_plugin(CommentParagraphPlugin)
  24 + assert_no_match /"keep_styles":false/, tinymce_init_js
  25 + end
  26 +
  27 +end