Class: Faker::HTML

Inherits:
Base
  • Object
show all
Defined in:
lib/faker/default/html.rb

Constant Summary

Constants inherited from Base

Base::LLetters, Base::Letters, Base::NOT_GIVEN, Base::Numbers, Base::ULetters

Class Method Summary collapse

Methods inherited from Base

bothify, disable_enforce_available_locales, fetch, fetch_all, flexible, generate, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, shuffle!, translate, unique, with_locale

Class Method Details

.codeString

Produces a random code block formatted in HTML.

Examples:

Faker::HTML.code #=> "<code>Eos quasi qui.</code>"

Returns:

  • (String)

Available since:

  • 3.2.1



99
100
101
# File 'lib/faker/default/html.rb', line 99

def code
  "<code>#{Lorem.sentence(word_count: 1)}</code>"
end

.element(tag: 'div', content: Lorem.sentence(word_count: 3), attributes: { class: Lorem.word, onclick: "#{Lorem.word}()" }) ⇒ String

Generates HTML content with customizable attributes for any HTML tag.

Examples:

Faker::HTML.element(tag: 'div', content: "This is a div with XSS attributes.", attributes: {class: 'xss', onclick: "alert('XSS')"}) #=> "<div class=\"xss\" onclick=\"alert('XSS')\">This is a div with XSS attributes.</div>"

Parameters:

  • tag (String) (defaults to: 'div')

    The HTML tag to generate.

  • content (String) (defaults to: Lorem.sentence(word_count: 3))

    The Content of the HTML tag.

  • attributes (Hash) (defaults to: { class: Lorem.word, onclick: "#{Lorem.word}()" })

    The attributes to include in the tag.

Returns:

  • (String)

Available since:

  • 3.2.1



168
169
170
171
# File 'lib/faker/default/html.rb', line 168

def element(tag: 'div', content: Lorem.sentence(word_count: 3), attributes: { class: Lorem.word, onclick: "#{Lorem.word}()" })
  attribute_string = attributes.map { |key, value| "#{key}=\"#{value}\"" }.join(' ')
  "<#{tag} #{attribute_string}>#{content}</#{tag}>"
end

.emphasisString

Produces a random emphasis formatting on a random word in two HTML paragraphs.

Examples:

Faker::HTML.emphasis #=> "<em>repellat id impedit</em>"

Returns:

  • (String)

Available since:

  • 3.2.1



46
47
48
# File 'lib/faker/default/html.rb', line 46

def emphasis
  "<em>#{Faker::Lorem.paragraph(sentence_count: 1)}</em>"
end

.headingString

Produces a random HTML header format.

Examples:

Faker::HTML.heading #=> "<h5>Autem</h5>"

Returns:

  • (String)

Available since:

  • 3.2.1



15
16
17
18
# File 'lib/faker/default/html.rb', line 15

def heading
  level = rand(1..6)
  "<h#{level}>#{Lorem.word.capitalize}</h#{level}>"
end

Generates a random <link> tag with the ‘rel` attribute set to “stylesheet” and the `href` attribute set to a random URL.

Examples:

Faker::HTML.link #=> "<link rel=\"stylesheet\" href=\"http://fay.io/darryl.barrows.css\">"

Parameters:

  • rel (String) (defaults to: 'stylesheet')

    The rel of the link tag.

Returns:

  • (String)

Available since:

  • 3.2.1



152
153
154
# File 'lib/faker/default/html.rb', line 152

def link(rel: 'stylesheet')
  "<link rel=\"#{rel}\" href=\"#{Faker::Internet.url}.css\">"
end

.ordered_listString

Produces a random ordered list in HTML format, with at least one element.

Examples:

Faker::HTML.ordered_list #=> "<ol>\n<li>Qui reiciendis non consequatur atque.</li>\n<li>Quo doloremque veritatis tempora aut.</li>\n<li>Aspernatur.</li>\n<li>Ea ab.</li>\n<li>Qui.</li>\n<li>Sit pariatur nemo eveniet.</li>\n<li>Molestiae aut.</li>\n<li>Nihil molestias iure placeat.</li>\n<li>Dolore autem quisquam.</li>\n</ol>"

Returns:

  • (String)

Available since:

  • 3.2.1



59
60
61
62
63
64
65
66
67
68
# File 'lib/faker/default/html.rb', line 59

def ordered_list
  number = rand(1..10)

  items = []
  number.times do
    items << "<li>#{Faker::Lorem.sentence(word_count: 1)}</li>"
  end

  "<ol>\n#{items.join("\n")}\n</ol>"
end

.paragraph(sentence_count: 3, supplemental: false, random_sentences_to_add: 0, exclude_words: nil) ⇒ String

Produces a random HTML paragraph format.

Examples:

Faker::HTML.paragraph #=> "<p>Incidunt atque quis</p>"

Parameters:

  • sentence_count (Integer) (defaults to: 3)

    The number of sentences in the paragraph.

  • supplemental (Boolean) (defaults to: false)

    Include supplemental text.

  • random_sentences_to_add (Integer) (defaults to: 0)

    The number of random sentences to add to the paragraph.

  • exclude_words (Array<String>) (defaults to: nil)

    Words to exclude from the generated paragraph.

Returns:

  • (String)

Available since:

  • 3.2.1



33
34
35
# File 'lib/faker/default/html.rb', line 33

def paragraph(sentence_count: 3, supplemental: false, random_sentences_to_add: 0, exclude_words: nil)
  "<p>#{Faker::Lorem.paragraph(sentence_count: sentence_count, supplemental: supplemental, random_sentences_to_add: random_sentences_to_add, exclude_words: exclude_words)}</p>"
end

.random(methods) ⇒ String

Produces a random method from the methods above, excluding the methods listed in the arguments.

Examples:

Faker::HTML.random #=> returns output from a single method outlined above
Faker::HTML.random(exclude: [:table]) #=> returns output from any single method outlined above except for "table"
Faker::HTML.random(exclude: [:ordered_list, :unordered_list]) #=> returns output from any single method outlined above except for either ordered_list and unordered_list

Parameters:

  • methods (Symbol)

    Specify which methods to exclude.

Returns:

  • (String)

Available since:

  • 3.2.1



187
188
189
190
191
# File 'lib/faker/default/html.rb', line 187

def random(exclude: [])
  method_list = available_methods
  exclude.each { |ex| method_list.delete_if { |meth| meth == ex.to_sym } }
  send(method_list[Faker::Config.random.rand(0..method_list.length - 1)])
end

.sandwich(sentences: 3, repeat: 1) ⇒ String

Generates a random HTML content sandwich, starting with a header, followed by paragraphs, and random elements.

Examples:

Faker::HTML.sandwich(sentences: 3, repeat: 2) #=> returns a sandwich of HTML content with 2 repetitions, each having a header, paragraph, and random element

Parameters:

  • sentences (Integer) (defaults to: 3)

    The number of sentences in each paragraph.

  • repeat (Integer) (defaults to: 1)

    The number of times to repeat the pattern (header, paragraph, random).

Returns:

  • (String)

Available since:

  • 3.2.1



204
205
206
207
208
209
210
211
212
# File 'lib/faker/default/html.rb', line 204

def sandwich(sentences: 3, repeat: 1)
  text_block = []
  text_block << heading
  repeat.times do
    text_block << paragraph(sentence_count: sentences)
    text_block << random(exclude: %i[script link])
  end
  text_block.join("\n")
end

.scriptString

Generates a random <script> tag with the ‘src` attribute set to a random URL.

Examples:

Faker::HTML.script #=> "<script src=\"http://gulgowski.name/jordan.weimann.js\"></script>"

Returns:

  • (String)

Available since:

  • 3.2.1



138
139
140
# File 'lib/faker/default/html.rb', line 138

def script
  "<script src=\"#{Faker::Internet.url}.js\"></script>"
end

.tableString

Produces a random HTML table.

Examples:

Faker::HTML.table #=> "<table>\n<thead>\n<th>ad</th>\n<th>similique</th>\n<th>voluptatem</th>\n</thead>\n<tbody>\n<td>corrupti</td>\n<td>est</td>\n<td>rerum</td>\n<td>molestiae</td>\n<td>quidem</td>\n<td>et</td>\n<td>in</td>\n<td>tempora</td>\n<td>at</td>\n<\tbody>\n<tfoot>\n<td>voluptatem</td>\n<td>debitis</td>\n<td>rem</td>\n</tfoot>\n</table>"

Returns:

  • (String)

Available since:

  • 3.2.1



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/faker/default/html.rb', line 112

def table
  header_row = generate_table_row('th', 3)
  footer_row = generate_table_row('td', 3)

  body_rows = []
  3.times do
    row = generate_table_row('td', 3)
    body_rows << row
  end

  thead = "<thead>\n#{header_row}</thead>"
  tbody = "<tbody>\n#{body_rows.join("\n")}</tbody>"
  tfoot = "<tfoot>\n#{footer_row}</tfoot>"

  "<table>\n#{thead}\n#{tbody}\n#{tfoot}\n</table>"
end

.unordered_listString

Produces a random unordered list of items between 1 and 10 randomly in HTML format.

Examples:

Faker::HTML.unordered_list #=> "<ul>\n<li>Voluptatum aliquid tempora molestiae facilis non sed.</li>\n<li>Nostrum omnis iste impedit voluptatum dolor.</li>\n<li>Esse quidem et facere.</li>\n</ul>"

Returns:

  • (String)

Available since:

  • 3.2.1



79
80
81
82
83
84
85
86
87
88
# File 'lib/faker/default/html.rb', line 79

def unordered_list
  number = rand(1..10)

  items = []
  number.times do
    items << "<li>#{Faker::Lorem.sentence(word_count: 1)}</li>"
  end

  "<ul>\n#{items.join("\n")}\n</ul>"
end