Skip to content

Commit

Permalink
🐛 amp-mathml: Add support for non-block mode; fixes #19420 (#19436)
Browse files Browse the repository at this point in the history
  • Loading branch information
KENNYSOFT authored and aghassemi committed Nov 28, 2018
1 parent b3efceb commit fa99cee
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 17 deletions.
25 changes: 15 additions & 10 deletions 3p/mathml.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ export function mathml(global, data) {

getMathmlJs(
global,
'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-MML-AM_CHTML',
'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML',
mathjax => {
// Dimensions are given by the parent frame.
delete data.width;
delete data.height;
const div = document.createElement('div');
div.setAttribute('id','mathmlformula');
div.setAttribute('id', 'mathmlformula');
div.textContent = data.formula;
setStyle(div, 'visibility', 'hidden');
global.document.body.appendChild(div);
Expand All @@ -62,15 +62,20 @@ export function mathml(global, data) {
mathjax.Hub.Queue(function() {
const rendered = document.getElementById('MathJax-Element-1-Frame');
// Remove built in mathjax margins.
const display = document.getElementsByClassName('MJXc-display');
if (display[0]) {
display[0].setAttribute('style','margin-top:0;margin-bottom:0');
context.requestResize(
rendered./*OK*/offsetWidth,
rendered./*OK*/offsetHeight
);
setStyle(div, 'visibility', 'visible');
let display = document.getElementsByClassName('MJXc-display');
if (!display[0]) {
const span = document.createElement('span');
span.setAttribute('class', 'mjx-chtml MJXc-display');
span.appendChild(rendered);
div.appendChild(span);
display = document.getElementsByClassName('MJXc-display');
}
display[0].setAttribute('style','margin-top:0;margin-bottom:0');
context.requestResize(
rendered./*OK*/offsetWidth,
rendered./*OK*/offsetHeight
);
setStyle(div, 'visibility', 'visible');
});
}
);
Expand Down
6 changes: 3 additions & 3 deletions examples/amp-mathml.amp.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ <h2>Cauchy's Integral Formula</h2>
<h2>Double angle formula for Cosines</h2>
<amp-mathml
layout="container"
data-formula="\[ \cos(θ+φ)=\cos(θ)\cos(φ)−\sin(θ)\sin(φ) \]">
data-formula="$$ \cos(θ+φ)=\cos(θ)\cos(φ)−\sin(θ)\sin(φ) $$">
</amp-mathml>
<h2>Inline formula.</h2>
This is an example of a formula placed inline in the middle of a block of text. <amp-mathml layout="container" inline data-formula="\[ \cos(θ+φ) \]"></amp-mathml> This shows how the formula will fit inside a block of text and can be styled with CSS.
<h2>Inline formula</h2>
<p>This is an example of a formula of <amp-mathml layout="container" inline data-formula="`x`"></amp-mathml>, <amp-mathml layout="container" inline data-formula="\(x = {-b \pm \sqrt{b^2-4ac} \over 2a}\)"></amp-mathml> placed inline in the middle of a block of text. <amp-mathml layout="container" inline data-formula="\( \cos(θ+φ) \)"></amp-mathml> This shows how the formula will fit inside a block of text and can be styled with CSS.</p>
</body>
</html>
1 change: 1 addition & 0 deletions extensions/amp-mathml/0.1/amp-mathml.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@

amp-mathml[inline] {
display: inline-block;
vertical-align: middle;
}
2 changes: 1 addition & 1 deletion extensions/amp-mathml/0.1/test/validator-amp-mathml.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<body>
<!-- Valid -->
<amp-mathml layout="container" data-formula="\[x = {-b \pm \sqrt{b^2-4ac} \over 2a}.\]"></amp-mathml>
<amp-mathml layout="container" inline data-formula="\[x = {-b \pm \sqrt{b^2-4ac} \over 2a}.\]"></amp-mathml>
<amp-mathml layout="container" inline data-formula="\(x = {-b \pm \sqrt{b^2-4ac} \over 2a}.\)"></amp-mathml>
<!-- Invalid: unsupported layout value -->
<amp-mathml layout="responsive" width="10px" height="10px" data-formula="\[x = {-b \pm \sqrt{b^2-4ac} \over 2a}.\]"></amp-mathml>
<!-- Invalid: missing formula -->
Expand Down
2 changes: 1 addition & 1 deletion extensions/amp-mathml/0.1/test/validator-amp-mathml.out
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ FAIL
| <body>
| <!-- Valid -->
| <amp-mathml layout="container" data-formula="\[x = {-b \pm \sqrt{b^2-4ac} \over 2a}.\]"></amp-mathml>
| <amp-mathml layout="container" inline data-formula="\[x = {-b \pm \sqrt{b^2-4ac} \over 2a}.\]"></amp-mathml>
| <amp-mathml layout="container" inline data-formula="\(x = {-b \pm \sqrt{b^2-4ac} \over 2a}.\)"></amp-mathml>
| <!-- Invalid: unsupported layout value -->
| <amp-mathml layout="responsive" width="10px" height="10px" data-formula="\[x = {-b \pm \sqrt{b^2-4ac} \over 2a}.\]"></amp-mathml>
>> ^~~~~~~~~
Expand Down
4 changes: 2 additions & 2 deletions extensions/amp-mathml/amp-mathml.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ This extension creates an iframe and renders a MathML formula.
#### Example: Double angle formula for Cosines

```html
<amp-mathml layout="container" data-formula="\[ \cos(θ+φ)=\cos(θ)\cos(φ)−\sin(θ)\sin(φ) \]">
<amp-mathml layout="container" data-formula="$$ \cos(θ+φ)=\cos(θ)\cos(φ)−\sin(θ)\sin(φ) $$">
</amp-mathml>
```
#### Example: Inline formula

This is an example of a formula placed inline in the middle of a block of text. `<amp-mathml layout="container" inline data-formula="\[ \cos(θ+φ) \]"></amp-mathml>` This shows how the formula will fit inside a block of text and can be styled with CSS.
This is an example of a formula of ``<amp-mathml layout="container" inline data-formula="`x`"></amp-mathml>``, `<amp-mathml layout="container" inline data-formula="\(x = {-b \pm \sqrt{b^2-4ac} \over 2a}\)"></amp-mathml>` placed inline in the middle of a block of text. `<amp-mathml layout="container" inline data-formula="\( \cos(θ+φ) \)"></amp-mathml>` This shows how the formula will fit inside a block of text and can be styled with CSS.

## Attributes

Expand Down

0 comments on commit fa99cee

Please sign in to comment.