PERF: stop using jQuery to sanitize strings

This commit is contained in:
Sam 2014-10-22 15:48:18 +11:00
parent 6b7c1011fd
commit e20e6b4524
5 changed files with 23 additions and 17 deletions

View File

@ -84,7 +84,7 @@ Discourse.HTML = {
) return ""; ) return "";
var name = Em.get(category, 'name'), var name = Em.get(category, 'name'),
description = Em.get(category, 'description'), description = Em.get(category, 'description_text'),
restricted = Em.get(category, 'read_restricted'), restricted = Em.get(category, 'read_restricted'),
url = Discourse.getURL("/c/") + Discourse.Category.slugFor(category), url = Discourse.getURL("/c/") + Discourse.Category.slugFor(category),
elem = (opts.link === false ? 'span' : 'a'), elem = (opts.link === false ? 'span' : 'a'),
@ -101,7 +101,7 @@ Discourse.HTML = {
name = Handlebars.Utils.escapeExpression(name); name = Handlebars.Utils.escapeExpression(name);
// Add description if we have it, without tags. Server has sanitized the description value. // Add description if we have it, without tags. Server has sanitized the description value.
if (description) html += "title=\"" + $("<div/>").html(description).text() + "\" "; if (description) html += "title=\"" + Handlebars.Utils.escapeExpression(description) + "\" ";
if (!opts.onlyStripe) { if (!opts.onlyStripe) {
categoryStyle = Discourse.HTML.categoryStyle(category); categoryStyle = Discourse.HTML.categoryStyle(category);

View File

@ -99,12 +99,6 @@ Discourse.Category = Discourse.Model.extend({
this.get("availableGroups").addObject(permission.group_name); this.get("availableGroups").addObject(permission.group_name);
}, },
// note, this is used in a data attribute, data attributes get downcased
// to avoid confusion later on using this naming here.
description_text: function(){
return $("<div>" + this.get("description") + "</div>").text();
}.property("description"),
permissions: function(){ permissions: function(){
return Em.A([ return Em.A([
{group_name: "everyone", permission: Discourse.PermissionType.create({id: 1})}, {group_name: "everyone", permission: Discourse.PermissionType.create({id: 1})},

View File

@ -181,6 +181,16 @@ SQL
topic_only_relative_url.try(:relative_url) topic_only_relative_url.try(:relative_url)
end end
def description_text
return nil unless description
@@cache ||= LruRedux::ThreadSafeCache.new(100)
@@cache.getset(self.description) do
Nokogiri::HTML(self.description).text
end
end
def ensure_slug def ensure_slug
if name.present? if name.present?
self.name.strip! self.name.strip!

View File

@ -8,6 +8,7 @@ class BasicCategorySerializer < ApplicationSerializer
:topic_count, :topic_count,
:post_count, :post_count,
:description, :description,
:description_text,
:topic_url, :topic_url,
:read_restricted, :read_restricted,
:permission, :permission,

View File

@ -19,14 +19,6 @@ describe Category do
c.errors[:name].should be_present c.errors[:name].should be_present
end end
it { should belong_to :topic }
it { should belong_to :user }
it { should have_many :topics }
it { should have_many :category_featured_topics }
it { should have_many :featured_topics }
it { should belong_to :parent_category}
describe "last_updated_at" do describe "last_updated_at" do
it "returns a number value of when the category was last updated" do it "returns a number value of when the category was last updated" do
last = Category.last_updated_at last = Category.last_updated_at
@ -49,7 +41,7 @@ describe Category do
# NOTE we also have the uncategorized category ... hence the increased count # NOTE we also have the uncategorized category ... hence the increased count
default_category = Fabricate(:category) _default_category = Fabricate(:category)
full_category = Fabricate(:category) full_category = Fabricate(:category)
can_post_category = Fabricate(:category) can_post_category = Fabricate(:category)
can_read_category = Fabricate(:category) can_read_category = Fabricate(:category)
@ -194,6 +186,15 @@ describe Category do
end end
end end
describe 'description_text' do
it 'correctly generates text description as needed' do
c = Category.new
c.description_text.should == nil
c.description = "&lt;hello <a>test</a>."
c.description_text.should == "<hello test."
end
end
describe 'after create' do describe 'after create' do
before do before do
@category = Fabricate(:category, name: 'Amazing Category') @category = Fabricate(:category, name: 'Amazing Category')