-
Notifications
You must be signed in to change notification settings - Fork 1.8k
#1554 Add space to empty anchor tags to fix internal links. #2949
Conversation
//when the anchor's content is empty. This adds a space with | ||
//a height of 0 when it is empty so that internal links | ||
//are linked correctly. | ||
if(elm.firstChild().isNull()){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be outside i.e.
QString h=elm.attribute("href");
if (h.isEmpty()) h=elm.attribute("ns0:href");
// Internal links ...
if (elm.firstChild().isNull()) {
Otherwise, this will get executed only when there is no href
attribute but only a ns0:href
attribute -- unless that is what is wanted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic is that if the a
tag has an empty href
attribute then it must be an anchor tag, but it only adds the space if there aren't any children. I tested the following scenarios which all worked:
<a name="abc"></a>
<a id="abc"></a>
<a name="abc" href=""></a>
<a id="abc" href=""></a>
If I move the code outside then it will execute for all a
tags and will replace the link text with a space. I tested that just to make sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, so then this approach is wrong/incomplete. The ns0:href
is generated by the TOC support, so it is quite possible that it won't work there. A better approach is to check directly for the condition that you want (<a name="not_empty"></a>
) rather than the absence of something (missing href
on <a>
) and work on that directly. Additionally, you might want to check for id
tags on headers/divs and add some content there too (they can also be used as the target of an href).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The check could be changed to also check that the a
tag has an id
or name
too. That would be a stronger check and less likely to produce side effects. However an empty href
attribute is the key condition for an a
tag.
For ns0:href
, are you saying that we also need to check that ns0:href
is empty?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the TOC is generated via XSLT which unfortunately adds tags with the ns0:
namespace, which is why you see code for that being specifically handled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You make a good point about other tags being used as targets which I didn't think about. Here are the cases I can think of:
- An
a
tag with eitherid
orname
attributes, emptyhref
orns0:href
attributes, and no children. - Any other anchor tags (e.g.
div, p, h1
) that have anid
and no children. I can look up the full list of tags than can be used as an anchor.
Am I missing any cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks OK to me. Also, just noticed -- please target master
and not the 0.12.3.x
branch.
Also, please rework the commit message (see previous commits) so that it includes the comments you have put in the PR as a part of the commit message. Additionally, I'd prefer to keep the comments out of the code ( You can use `git commit --author "Name Email@Domain" to override the author, with you as the committer. Please make sure you rebase/amend the commit so that there is only a single commit which needs to be merged. |
@rpedela: any update? |
Nope. I haven't had time to work on it. Hopefully soon. |
I have tested
How about both |
I'm planning to release |
Internal links (anchor tags) are not linked correctly when the anchor's content is empty. This adds a space with a height of 0 when it is empty so that internal links are linked correctly. Fixes issue #1554.
Internal links (anchor tags) are not linked correctly when the anchor's content is empty. This adds a space with a height of 0 when it is empty so that internal links are linked correctly.
@partychen deserves credit for this fix. I am just making the PR.