Merge branch 'autocomplete-performance' into 'master'
Improving autocomplete performance part 2 Related issue: gitlab-org/gitlab-ce#3507 See merge request !2253
This commit is contained in:
commit
cbf34cdbcb
|
@ -47,7 +47,17 @@ module Banzai
|
|||
{ object_sym => LazyReference.new(object_class, node.attr(data_reference)) }
|
||||
end
|
||||
|
||||
delegate :object_class, :object_sym, :references_in, to: :class
|
||||
def object_class
|
||||
self.class.object_class
|
||||
end
|
||||
|
||||
def object_sym
|
||||
self.class.object_sym
|
||||
end
|
||||
|
||||
def references_in(*args, &block)
|
||||
self.class.references_in(*args, &block)
|
||||
end
|
||||
|
||||
def find_object(project, id)
|
||||
# Implement in child class
|
||||
|
|
|
@ -10,7 +10,7 @@ module Banzai
|
|||
#
|
||||
class RedactorFilter < HTML::Pipeline::Filter
|
||||
def call
|
||||
doc.css('a.gfm').each do |node|
|
||||
Querying.css(doc, 'a.gfm').each do |node|
|
||||
unless user_can_see_reference?(node)
|
||||
# The reference should be replaced by the original text,
|
||||
# which is not always the same as the rendered text.
|
||||
|
|
|
@ -124,7 +124,7 @@ module Banzai
|
|||
def replace_link_nodes_with_text(pattern)
|
||||
return doc if project.nil?
|
||||
|
||||
doc.search('a').each do |node|
|
||||
doc.xpath('descendant-or-self::a').each do |node|
|
||||
klass = node.attr('class')
|
||||
next if klass && klass.include?('gfm')
|
||||
|
||||
|
@ -162,7 +162,7 @@ module Banzai
|
|||
def replace_link_nodes_with_href(pattern)
|
||||
return doc if project.nil?
|
||||
|
||||
doc.search('a').each do |node|
|
||||
doc.xpath('descendant-or-self::a').each do |node|
|
||||
klass = node.attr('class')
|
||||
next if klass && klass.include?('gfm')
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@ module Banzai
|
|||
end
|
||||
|
||||
def call
|
||||
doc.css('a.gfm').each do |node|
|
||||
Querying.css(doc, 'a.gfm').each do |node|
|
||||
gather_references(node)
|
||||
end
|
||||
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
module Banzai
|
||||
module Querying
|
||||
# Searches a Nokogiri document using a CSS query, optionally optimizing it
|
||||
# whenever possible.
|
||||
#
|
||||
# document - A document/element to search.
|
||||
# query - The CSS query to use.
|
||||
#
|
||||
# Returns a Nokogiri::XML::NodeSet.
|
||||
def self.css(document, query)
|
||||
# When using "a.foo" Nokogiri compiles this to "//a[...]" but
|
||||
# "descendant::a[...]" is quite a bit faster and achieves the same result.
|
||||
xpath = Nokogiri::CSS.xpath_for(query)[0].gsub(%r{^//}, 'descendant::')
|
||||
|
||||
document.xpath(xpath)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,13 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Banzai::Querying do
|
||||
describe '.css' do
|
||||
it 'optimizes queries for elements with classes' do
|
||||
document = double(:document)
|
||||
|
||||
expect(document).to receive(:xpath).with(/^descendant::a/)
|
||||
|
||||
described_class.css(document, 'a.gfm')
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue