Re includegraphics (#2053)

* Re-enable \includegraphics now that we have trust setting

This reverts commit 5806b240b3.

* Include Khan Academy test logo in repo and use in test (fix #1892)

* Update screenshots

* Update documentation

* Add tests, cleanup existing tests

* Update snapshots

* Enable trust testing (trust=true by default)
This commit is contained in:
Erik Demaine
2019-07-21 11:15:29 -04:00
committed by ylemkimon
parent 223b9c3f24
commit 285c9fe0b6
12 changed files with 111 additions and 41 deletions

View File

@@ -12,7 +12,7 @@ import Options from "../src/Options";
import Settings from "../src/Settings";
import Style from "../src/Style";
import {
strictSettings, nonstrictSettings, r,
strictSettings, nonstrictSettings, trustSettings, r,
getBuilt, getParsed, stripPositions,
} from "./helpers";
@@ -1981,15 +1981,24 @@ describe("A MathML font tree-builder", function() {
});
});
// Disabled until https://github.com/KaTeX/KaTeX/pull/1794 is merged.
describe.skip("An includegraphics builder", function() {
describe("An includegraphics builder", function() {
const img = "\\includegraphics[height=0.9em, totalheight=0.9em, width=0.9em, alt=KA logo]{https://cdn.kastatic.org/images/apple-touch-icon-57x57-precomposed.new.png}";
it("should not fail", function() {
expect(img).toBuild();
expect(img).toBuild(trustSettings);
});
it("should produce mords", function() {
expect(getBuilt(img)[0].classes).toContain("mord");
expect(getBuilt(img, trustSettings)[0].classes).toContain("mord");
});
it("should not render without trust setting", function() {
const built = getBuilt(img);
expect(built).toMatchSnapshot();
});
it("should render with trust setting", function() {
const built = getBuilt(img, trustSettings);
expect(built).toMatchSnapshot();
});
});
@@ -2661,41 +2670,41 @@ describe("href and url commands", function() {
// We can't use raw strings for \url because \u is for Unicode escapes.
it("should parse its input", function() {
expect`\href{http://example.com/}{\sin}`.toBuild();
expect("\\url{http://example.com/}").toBuild();
expect`\href{http://example.com/}{\sin}`.toBuild(trustSettings);
expect("\\url{http://example.com/}").toBuild(trustSettings);
});
it("should allow empty URLs", function() {
expect`\href{}{example here}`.toBuild();
expect("\\url{}").toBuild();
expect`\href{}{example here}`.toBuild(trustSettings);
expect("\\url{}").toBuild(trustSettings);
});
it("should allow single-character URLs", () => {
expect`\href%end`.toParseLike("\\href{%}end");
expect("\\url%end").toParseLike("\\url{%}end");
expect("\\url%%end\n").toParseLike("\\url{%}");
expect("\\url end").toParseLike("\\url{e}nd");
expect("\\url%end").toParseLike("\\url {%}end");
expect`\href%end`.toParseLike("\\href{%}end", trustSettings);
expect("\\url%end").toParseLike("\\url{%}end", trustSettings);
expect("\\url%%end\n").toParseLike("\\url{%}", trustSettings);
expect("\\url end").toParseLike("\\url{e}nd", trustSettings);
expect("\\url%end").toParseLike("\\url {%}end", trustSettings);
});
it("should allow spaces single-character URLs", () => {
expect`\href %end`.toParseLike("\\href{%}end");
expect("\\url %end").toParseLike("\\url{%}end");
expect`\href %end`.toParseLike("\\href{%}end", trustSettings);
expect("\\url %end").toParseLike("\\url{%}end", trustSettings);
});
it("should allow letters [#$%&~_^] without escaping", function() {
const url = "http://example.org/~bar/#top?foo=$foo&bar=ba^r_boo%20baz";
const parsed1 = getParsed(`\\href{${url}}{\\alpha}`, new Settings({trust: true}))[0];
const parsed1 = getParsed(`\\href{${url}}{\\alpha}`, trustSettings)[0];
expect(parsed1.href).toBe(url);
const parsed2 = getParsed(`\\url{${url}}`, new Settings({trust: true}))[0];
const parsed2 = getParsed(`\\url{${url}}`, trustSettings)[0];
expect(parsed2.href).toBe(url);
});
it("should allow balanced braces in url", function() {
const url = "http://example.org/{{}t{oo}}";
const parsed1 = getParsed(`\\href{${url}}{\\alpha}`, new Settings({trust: true}))[0];
const parsed1 = getParsed(`\\href{${url}}{\\alpha}`, trustSettings)[0];
expect(parsed1.href).toBe(url);
const parsed2 = getParsed(`\\url{${url}}`, new Settings({trust: true}))[0];
const parsed2 = getParsed(`\\url{${url}}`, trustSettings)[0];
expect(parsed2.href).toBe(url);
});
@@ -2709,9 +2718,9 @@ describe("href and url commands", function() {
it("should allow escape for letters [#$%&~_^{}]", function() {
const url = "http://example.org/~bar/#top?foo=$}foo{&bar=bar^r_boo%20baz";
const input = url.replace(/([#$%&~_^{}])/g, '\\$1');
const parsed1 = getParsed(`\\href{${input}}{\\alpha}`, new Settings({trust: true}))[0];
const parsed1 = getParsed(`\\href{${input}}{\\alpha}`, trustSettings)[0];
expect(parsed1.href).toBe(url);
const parsed2 = getParsed(`\\url{${input}}`, new Settings({trust: true}))[0];
const parsed2 = getParsed(`\\url{${input}}`, trustSettings)[0];
expect(parsed2.href).toBe(url);
});
@@ -2725,7 +2734,7 @@ describe("href and url commands", function() {
});
it("should not affect spacing around", function() {
const built = getBuilt("a\\href{http://example.com/}{+b}", new Settings({trust: true}));
const built = getBuilt("a\\href{http://example.com/}{+b}", trustSettings);
expect(built).toMatchSnapshot();
});
@@ -2743,10 +2752,7 @@ describe("href and url commands", function() {
});
it("should allow all protocols when trust option is true", () => {
const parsed = getParsed(
"\\href{ftp://x}{foo}",
new Settings({trust: true}),
);
const parsed = getParsed("\\href{ftp://x}{foo}", trustSettings);
expect(parsed).toMatchSnapshot();
});
@@ -2765,8 +2771,7 @@ describe("A raw text parser", function() {
// Unicode combining character. So this is a test that the parser will catch a bad string.
expect("\\includegraphics[\u030aheight=0.8em, totalheight=0.9em, width=0.9em]{" + "https://cdn.kastatic.org/images/apple-touch-icon-57x57-precomposed.new.png}").not.toParse();
});
// Disabled until https://github.com/KaTeX/KaTeX/pull/1794 is merged.
it.skip("should return null for a omitted optional string", function() {
it("should return null for a omitted optional string", function() {
expect("\\includegraphics{https://cdn.kastatic.org/images/apple-touch-icon-57x57-precomposed.new.png}").toParse();
});
});