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

[svg] href creates rect in bad position #2530

Closed
pkra opened this issue Sep 9, 2020 · 3 comments
Closed

[svg] href creates rect in bad position #2530

pkra opened this issue Sep 9, 2020 · 3 comments
Labels
Accepted Issue has been reproduced by MathJax team Fixed Test Needed v3 v3.1
Milestone

Comments

@pkra
Copy link
Contributor

pkra commented Sep 9, 2020

Example input: $$\text{A display equation with a link inside } \href{#myid}{17}$$

The rect with data-hitbox=true ends up in the wrong position.

From

https://github.com/mathjax/MathJax-src/blob/32213009962a887e262d9930adcfb468da4967ce/ts/output/svg/Wrapper.ts#L146-L151

I'm guessing it should be in the g (or get an appropriate x attribute to match the translation of the g).

@pkra
Copy link
Contributor Author

pkra commented Sep 9, 2020

The following seems to fix it -- happy to make a PR.

  protected createSVGnode(parent: N): N {
    const href = this.node.attributes.get('href');
    if (href) {
      parent = this.adaptor.append(parent, this.svg('a', {href: href})) as N;
    }
    this.element = this.adaptor.append(parent, this.svg('g', {'data-mml-node': this.node.kind})) as N;
    if (href) {
      const {h, d, w} = this.getBBox();
      this.adaptor.append(this.element, this.svg('rect', {
      'data-hitbox': true, fill: 'none', stroke: 'none', 'pointer-events': 'all',
      width: this.fixed(w), height: this.fixed(h + d), y: this.fixed(-d)
    }));
    }
    return this.element;
  }

@dpvc dpvc added Accepted Issue has been reproduced by MathJax team v3 labels Sep 10, 2020
@dpvc
Copy link
Member

dpvc commented Sep 10, 2020

Thanks for spotting this issue.

There are a couple of places where the the first child of this.element is assumed to be the actual content of the element, so while moving the rect into the main g element works most of the time, it will cause problems in a few cases. That is why the rect was outside the main g element.

I think the correct fix is actually in the SVGWrapper's place() method, since that is where the element gets its x and y coordinates.

https://github.com/mathjax/MathJax-src/blob/32213009962a887e262d9930adcfb468da4967ce/ts/output/svg/Wrapper.ts#L244-L252

I think that moving the hitbox along with the group should do the trick.

  public place(x: number, y: number, element: N = null) {
    if (!(x || y)) return;
    const adaptor = this.adaptor;
    const translate = 'translate(' + this.fixed(x) + ', ' + this.fixed(y) + ')';
    if (!element) {
      element = this.element;
      if (this.node.attributes.get('href')) {
        const rect = adaptor.previous(element);
        if (rect && adaptor.kind(rect) === 'rect' && adaptor.getAttribute(rect, 'data-hitbox')) {
          adaptor.setAttribute(rect, 'transform', translate);
        }
      }
    }
    let transform = adaptor.getAttribute(element, 'transform') || '';
    adaptor.setAttribute(element, 'transform', translate + (transform ? ' ' + transform : ''));
  }

@pkra
Copy link
Contributor Author

pkra commented Sep 10, 2020

Thanks, Davide!

@dpvc dpvc added this to the 3.1.3 milestone Jan 31, 2021
@dpvc dpvc self-assigned this Jan 31, 2021
@dpvc dpvc removed their assignment Feb 9, 2021
dpvc added a commit to mathjax/MathJax-src that referenced this issue Mar 8, 2021
Make sure hitbox is translated along with the hit-able element. (mathjax/MathJax#2530)
@dpvc dpvc added Merged Merged into develop branch and removed Ready for Review labels Mar 8, 2021
dpvc added a commit to mathjax/MathJax-src that referenced this issue Mar 8, 2021
dpvc added a commit to mathjax/MathJax-src that referenced this issue Mar 8, 2021
Revert "Make sure hitbox is translated along with the hit-able element. (mathjax/MathJax#2530)"
dpvc added a commit to mathjax/MathJax-src that referenced this issue Mar 8, 2021
Make sure hitbox is translated along with the hit-able element. (mathjax/MathJax#2530)
@dpvc dpvc added Fixed v3.1 and removed Merged Merged into develop branch labels Apr 27, 2021
@dpvc dpvc closed this as completed Apr 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Accepted Issue has been reproduced by MathJax team Fixed Test Needed v3 v3.1
Projects
None yet
Development

No branches or pull requests

2 participants