Commit 4fc7fbd2f809cc4ddbbcaae4f55ce2e90b6c3f5c
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
Merge branch 'AI3205-comment-paragraph' into stable
Showing
6 changed files
with
110 additions
and
26 deletions
Show diff stats
plugins/comment_paragraph/lib/ext/article.rb
... | ... | @@ -22,22 +22,19 @@ class Article |
22 | 22 | comment_paragraph_plugin_set_initial_value unless persisted? |
23 | 23 | return unless comment_paragraph_plugin_activated? |
24 | 24 | if body && (body_changed? || setting_changed?(:comment_paragraph_plugin_activate)) |
25 | - parsed_paragraphs = [] | |
26 | 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 | 36 | end |
40 | - self.body = parsed_paragraphs.join() | |
37 | + self.body = doc.at('body').inner_html | |
41 | 38 | end |
42 | 39 | end |
43 | 40 | |
... | ... | @@ -50,10 +47,4 @@ class Article |
50 | 47 | @comment_paragraph_plugin_settings ||= Noosfero::Plugin::Settings.new(environment, CommentParagraphPlugin) |
51 | 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 | 50 | end | ... | ... |
... | ... | @@ -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 | 364 | #content .comment-paragraph-plugin .button-bar { |
365 | 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
plugins/comment_paragraph/test/unit/article_test.rb
... | ... | @@ -33,20 +33,30 @@ class ArticleTest < ActiveSupport::TestCase |
33 | 33 | end |
34 | 34 | |
35 | 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 | 46 | article.comment_paragraph_plugin_activate = true |
38 | 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 | 50 | end |
41 | 51 | |
42 | 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 | 54 | article.comment_paragraph_plugin_activate = true |
45 | 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 | 57 | article.comment_paragraph_plugin_activate = false |
48 | 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 | 60 | end |
51 | 61 | |
52 | 62 | should 'parse html when activate comment paragraph' do |
... | ... | @@ -56,7 +66,42 @@ class ArticleTest < ActiveSupport::TestCase |
56 | 66 | assert_equal "<p>paragraph 1</p><p>paragraph 2</p>", article.body |
57 | 67 | article.comment_paragraph_plugin_activate = true |
58 | 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 | 105 | end |
61 | 106 | |
62 | 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 @@ |
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 | ... | ... |