FIX: details tags broke excerpts

This commit is contained in:
Robin Ward 2017-12-19 17:28:55 -05:00
parent c1716d41c7
commit b3fda0ea86
5 changed files with 31 additions and 1 deletions

View File

@ -17,6 +17,7 @@ export default Ember.Component.extend(LoadMore, {
$(window).on('resize.discourse-on-scroll', () => this.scrolled());
this.$().on('click.details-disabled', 'details.disabled', () => false)
this.$().on('mouseup.discourse-redirect', '.excerpt a', function(e) {
// bypass if we are selecting stuff
const selection = window.getSelection && window.getSelection();
@ -38,6 +39,7 @@ export default Ember.Component.extend(LoadMore, {
_destroyed: function() {
this.unbindScrolling('user-stream-view');
$(window).unbind('resize.discourse-on-scroll');
this.$().off('click.details-disabled', 'details.disabled')
// Unbind link tracking
this.$().off('mouseup.discourse-redirect', '.excerpt a');

View File

@ -42,7 +42,7 @@
<ul class="usercard-controls">
{{#if user.can_send_private_message_to_user}}
<li>
<li class='compose-pm'>
{{d-button
class="btn-primary"
action=(action "composePrivateMessage" user post)

View File

@ -110,6 +110,10 @@
font-size: 0.929em;
word-wrap: break-word;
color: $primary;
details.disabled {
color: $primary-medium;
}
}
}

View File

@ -18,6 +18,7 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
@keep_onebox_source = options[:keep_onebox_source] == true
@remap_emoji = options[:remap_emoji] == true
@start_excerpt = false
@summary_contents = ""
end
def self.get_excerpt(html, length, options)
@ -108,6 +109,10 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
include_tag("span", attributes)
@in_spoiler = true
end
when "details"
@in_details = true
when "summary"
@in_summary = true
end
end
@ -126,6 +131,14 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
end
when "aside"
@in_quote = false
when "details"
@in_details = false
when "summary"
@in_summary = false
if @summary_contents.present?
@excerpt << "<details class='disabled'><summary>#{@summary_contents[0..@length]}</summary></details>"
end
@summary_contents = ""
when "div", "span"
throw :done if @start_excerpt
characters("</span>", false, false, false) if @in_spoiler
@ -135,8 +148,15 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
def characters(string, truncate = true, count_it = true, encode = true)
return if @in_quote
# we call length on this so might as well ensure we have a string
string = string.to_s
if @in_details
if @in_summary
@summary_contents << string
end
return
end
encode = encode ? lambda { |s| ERB::Util.html_escape(s) } : lambda { |s| s }
if count_it && @current_length + string.length > @length

View File

@ -385,6 +385,10 @@ describe PrettyText do
expect(PrettyText.excerpt("<span class='spoiler'>spoiler</div>", 100)).to match_html "<span class='spoiler'>spoiler</span>"
end
it "should keep details" do
expect(PrettyText.excerpt("<details><summary>expand</summary><p>hello</p></details>", 30)).to match_html "<details><summary>expand</summary></details>"
end
it "should remove meta informations" do
expect(PrettyText.excerpt(wrapped_image, 100)).to match_html "<a href='//localhost:3000/uploads/default/4399/33691397e78b4d75.png' class='lightbox' title='Screen Shot 2014-04-14 at 9.47.10 PM.png'>[image]</a>"
end