Class: JsDependency::Extractor::ExtractScriptTag

Inherits:
Object
  • Object
show all
Defined in:
lib/js_dependency/extractor/extract_script_tag.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ ExtractScriptTag

Returns a new instance of ExtractScriptTag.

Parameters:

  • str (String)


7
8
9
# File 'lib/js_dependency/extractor/extract_script_tag.rb', line 7

def initialize(str)
  @str = str
end

Class Method Details

.call(str) ⇒ String

Parameters:

  • str (String)

Returns:

  • (String)


35
36
37
# File 'lib/js_dependency/extractor/extract_script_tag.rb', line 35

def self.call(str)
  new(str).call
end

Instance Method Details

#callArray<String>

Returns:

  • (Array<String>)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/js_dependency/extractor/extract_script_tag.rb', line 12

def call
  str = @str
  scripts = str.gsub(%r{<script>(.+)</script>}m).with_object([]) do |_, list|
    list << Regexp.last_match(1)
  end

  scripts += str.gsub(%r{<script.+src=["'].+["']>(.+)</script>}m).with_object([]) do |_, list|
    list << Regexp.last_match(1)
  end

  scripts += str.gsub(%r{<script.+setup>(.+)</script>}m).with_object([]) do |_, list|
    list << Regexp.last_match(1)
  end

  scripts += str.gsub(%r{<script.+lang=["'][tj]s["']>(.+)</script>}m).with_object([]) do |_, list|
    list << Regexp.last_match(1)
  end

  scripts.uniq.sort.join("\n")
end