Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swap_attributes not working #846

Closed
andreadellacorte opened this issue Aug 12, 2024 · 3 comments · Fixed by #847
Closed

swap_attributes not working #846

andreadellacorte opened this issue Aug 12, 2024 · 3 comments · Fixed by #847

Comments

@andreadellacorte
Copy link

andreadellacorte commented Aug 12, 2024

Hi, I am running html proofer as follows

%w[
    ./test.html
  ].each do |file|
    # See  https://github.com/gjtorikian/html-proofer?tab=readme-ov-file#configuration
    HTMLProofer.check_file(
      file,
      {
        allow_missing_href: true,
        disable_external: true,
        checks: ["Links", "Images"],
        ignore_urls: [%r{/docs}, %r{/customer-portal}, %r{/resume-submission}],
        root_dir: "_site",
        swap_attributes: { "img": ["data-src", "src"] }
      }
    ).run
  end

(note: swap_attributes: { "img": [["data-src", "src"]] } doesn't work either))

on this file

<html>
  <body>
    <p>Blah blah blah. <img alt="An http image" data-src="gpl.png" /></p>
  </body>
</html>

and getting

For the Images check, the following failures were found:

* At ./test.html:3:

  image has no src or srcset attribute

Is my syntax wrong? I copied the command and test from the repo itself.

@riccardoporreca
Copy link
Collaborator

@andreadellacorte, according to the README, the correct syntax should be (note the double [[...]])

swap_attributes: { "img": [["data-src", "src"]] }

@andreadellacorte
Copy link
Author

thanks @riccardoporreca

apologies that was me testing with 1 [] to see if it made a difference (it doesn't)

I have just now put back the [[...]] and

%w[
    ./test.html
  ].each do |file|
    # See  https://github.com/gjtorikian/html-proofer?tab=readme-ov-file#configuration
    HTMLProofer.check_file(
      file,
      {
        allow_missing_href: true,
        disable_external: true,
        checks: ["Links", "Images"],
        ignore_urls: [%r{/docs}, %r{/customer-portal}, %r{/resume-submission}],
        root_dir: "_site",
        swap_attributes: { "img": [["data-src", "src"]] }
      }
    ).run
  end

still fails with

For the Images check, the following failures were found:

* At ./test.html:3:

  image has no src or srcset attribute


HTML-Proofer found 1 failure!

on test.html

<html>
  <body>
    <p>Blah blah blah. <img alt="An http image" data-src="gpl.png" /></p>
  </body>
</html>

@riccardoporreca
Copy link
Collaborator

I see, the example in the README is not up-to-date.
I did not realize your example is actually what is already covered and fully working as expected in the unit tests:

proofer = run_proofer(custom_data_src_check, :file, swap_attributes: { "img" => [["data-src", "src"]] })

which indicates the issue is with the wrong usage of "img": ... instead of the correct "img" => ....

The correct syntax makes sure the hash key is as string, not a symbol, which is what we want since the HTML tag name, used to lookup the swapped attributes, is also a string, like "img" in this case.

The README should be updated accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants