Method: Faker::HTML.element

Defined in:
lib/faker/default/html.rb

.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:



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