class DataStyleSanitizer::Processor
def inject_style_block
def inject_style_block return if @styles.empty? # Create the <style> tag style_tag = Nokogiri::XML::Node.new("style", @doc) style_tag["nonce"] = @nonce # Generate CSS rules css_rules = @styles.map do |class_name, rule| rule_lines = rule.split(";").map(&:strip).reject(&:empty?) rule_lines.map! { |line| "#{line.strip} !important;" } # Ensure override ".#{class_name} { #{rule_lines.join(" ")} }" end.join("\n") style_tag.content = css_rules # Add the <style> tag to the <head> if it exists, otherwise to the <body> or fragment head = @doc.at_css("head") if head head.add_child(style_tag) else body = @doc.at_css("body") if body body.add_child(style_tag) else @doc.add_child(style_tag) # Append directly to the fragment end end end