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>"


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