amp-img

10 views
Skip to first unread message

asorianom...@gmail.com

unread,
Jul 3, 2017, 5:55:18 PM7/3/17
to nokogiri-talk
Is it possible to use nonstandard tags like amp-img with Nokogiri? I wrote a helper to replace all img tags with amp-img in order to comply with the Google Accelerated Mobile Pages validation. However, when I try to replace the tag it just completely removes the tag.

module AmpImageHelper
  def amp_image_helper(paragraph_item)
    fragment = Nokogiri::HTML.fragment(paragraph_item)
    fragment.css('img').each do |el|
      el.name = 'amp-img'
      el.set_attribute('height', '224')
      el.set_attribute('width', '360')
      el.set_attribute('layout', 'responsive')
    end
    fragment.to_html
  end
end

Aaron Patterson

unread,
Jul 4, 2017, 2:39:01 PM7/4/17
to nokogi...@googlegroups.com
On Mon, Jul 3, 2017 at 2:55 PM <asorianom...@gmail.com> wrote:
Is it possible to use nonstandard tags like amp-img with Nokogiri?

Yes, but you have to treat the document as XML.  Parsing as HTML will apply HTML rules (like eliminating non-standard tags).

Hope that helps! 

asorianom...@gmail.com

unread,
Jul 5, 2017, 3:30:59 PM7/5/17
to nokogiri-talk
Ahhh... Of course. Thanks, that does help.
Reply all
Reply to author
Forward
0 new messages