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

Spaced string in imgTag #1277

Closed
ianrose opened this issue May 16, 2015 · 8 comments · Fixed by hexojs/site#1256
Closed

Spaced string in imgTag #1277

ianrose opened this issue May 16, 2015 · 8 comments · Fixed by hexojs/site#1256
Labels
bug Something isn't working need-verify

Comments

@ianrose
Copy link

ianrose commented May 16, 2015

Hexo v3.0

Using the img tag plugin the Alt attribute gets rendered as part of the the title attribute.

Example code used to test:

{% img class-name /path/to/image 100 100 "Title" "Alt" %}

HTML Output

<img src="/path/to/image" class="class-name" width="100" height="100" title="Title Alt">
@chenneal
Copy link

chenneal commented Dec 4, 2015

I have the question the same as you don‘t sloved, i don't know the reason.

@PhiLhoSoft
Copy link

I looked into the source code.
plugins/tag/img.js -> function imgTag(args, content)
content is not used, args is populated with a naive split on space of the content of the tag (minus the name of the tag itself, of course).
"Naive" because if you type two spaces in the tag, that will make an empty arg and will shift the next ones, so everything is off.
Moreover, the split removed all quotes!
So, if I have the tag:
{% img /assets/images/Work-in-Progress.svg 240 75 "Work in Progress" "Work in Progress" %}
I get the array:

[ '/assets/images/Work-in-Progress.svg',
  '240', // Here I got another '', because I added a space between the two numbers...
  '75',
  'Work',
  'in',
  'Progress',
  'Work',
  'in',
  'Progress' ]

Once the dimensions are processed, the code does: meta = args.join(' '); to get back the title and alt attributes together, then use a regular expression (expecting quoted strings) to split them again.
Problem: there is no longer any quotes, so all we get is the title itself. 😢

To fix that, I have to look how the content of a tag is split...

PS.: The problem dates back to May 16! Nobody had the time to fix that? (I know, that's something that can be done by contributors...)

@PhiLhoSoft
Copy link

OK, I found a workaround...
Just use both kinds of quotes around your title and alt strings!
{% img /assets/images/Work-in-Progress.svg 240 75 '"Work in Progress"' '"Work in Progress"' %}
or
{% img /assets/images/Work-in-Progress.svg 240 75 "'Work in Progress'" "'Work in Progress'" %}

Apparently, it might be Swig's fault for bad parsing of the parameters...

@chenneal
Copy link

chenneal commented Jan 1, 2016

@PhiLhoSoft thanks very much, it's looks works through your workaround.

@Xuanwo Xuanwo closed this as completed Jan 3, 2016
@PhiLhoSoft
Copy link

@Xuanwo Why did you close this issue? It is not solved at all! I just gave a workaround.
At best, you find a way to make it work as expected, ie. with regular (not doubled) quotes. Might need something on Hexo side, or something on Swig side, from monkey patching the faulty function, to opening an issue at this project.
At worst, this needs at least a documentation update to tell users to use the workaround...

@Xuanwo Xuanwo reopened this Jan 3, 2016
@Xuanwo
Copy link
Contributor

Xuanwo commented Jan 3, 2016

Sorry for miss leading for closing the issue when I hadn't read it.

I close the issues through the mail #1277 (comment) which I thought sloved.

@leesei leesei added the bug Something isn't working label Feb 23, 2016
@leesei leesei changed the title img tag plugin does not render alt attribute Spaced string in imgTag Feb 23, 2016
@NoahDragon NoahDragon added this to the 4.0 milestone Apr 7, 2017
@NoahDragon NoahDragon mentioned this issue Apr 7, 2017
53 tasks
@tomap
Copy link
Contributor

tomap commented Jan 9, 2018

Maybe this PR will solve your issue ? #2949

@curbengh
Copy link
Contributor

curbengh commented Dec 16, 2019

imgTag first parse title and alt from the 6th argument, then split them.

* {% img [class names] /path/to/image [width] [height] [title text [alt text]] %}

{% img class-name /path/to/image 100 100 '"Title" "Alt"' %}

Note the usage is different from the previous comment, as there is only one pair of single quote.

The doc also shows title and alt are parsed from the 6th argument.

{% img [class names] /path/to/image [width] [height] "title text 'alt text'" %}

This means imgTag doesn't support parsing alt from the 7th argument.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working need-verify
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants