Implemented `\href' command (#923)

* Implements `\href' command.

* Added `functions/href.js`.
* Added `domTree.anchor` and `buildCommon.makeAnchor` functions.
* Make `buildHTML.getTypeOfDomTree` exported.

* Reflects the code reviews

* Create new argType "url" to treat link string in math appropriately
* Stop using too shorten variable names
* Start using template strings

* Adopts template literal

* Elaborates on glueing

* If-clause restructuring

* Solved confusing explanation

* Allow balanced braces in url

* Adds unit-test for \href

* Adds snapshot tests
This commit is contained in:
Hiromi Ishii
2017-11-24 13:23:35 +09:00
committed by Kevin Barabash
parent 75af19c5bb
commit fd82c4fad0
10 changed files with 335 additions and 2 deletions

View File

@@ -220,6 +220,25 @@ exports[`A MathML builder should render mathchoice as if there was nothing 4`] =
`;
exports[`A MathML builder should set href attribute for href appropriately 1`] = `
<math>
<semantics>
<mrow>
<mrow href="http://example.org">
<mi>
α
</mi>
</mrow>
</mrow>
<annotation encoding="application/x-tex">
\\href{http://example.org}{\\alpha}
</annotation>
</semantics>
</math>
`;
exports[`A MathML builder should use <menclose> for colorbox 1`] = `
<math>

View File

@@ -2420,6 +2420,36 @@ describe("An aligned environment", function() {
});
});
describe("An href command", function() {
it("should parse its input", function() {
expect("\\href{http://example.com/}{example here}").toParse();
});
it("should allow letters [#$%&~_^] without escaping", function() {
const url = "http://example.org/~bar/#top?foo=$foo&bar=ba^r_boo%20baz";
const hash = getParsed(`\\href{${url}}{\\alpha}`)[0];
expect(hash.value.href).toBe(url);
});
it("should allow balanced braces in url", function() {
const url = "http://example.org/{too}";
const hash = getParsed(`\\href{${url}}{\\alpha}`)[0];
expect(hash.value.href).toBe(url);
});
it("should not allow unbalanced brace(s) in url", function() {
expect("\\href{http://example.com/{a}{bar}").toNotParse();
expect("\\href{http://example.com/}a}{bar}").toNotParse();
});
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 ae = getParsed(`\\href{${input}}{\\alpha}`)[0];
expect(ae.value.href).toBe(url);
});
});
describe("A parser that does not throw on unsupported commands", function() {
// The parser breaks on unsupported commands unless it is explicitly
// told not to

View File

@@ -59,6 +59,11 @@ describe("A MathML builder", function() {
expect(getMathML("\\colorbox{red}{b}")).toMatchSnapshot();
});
it('should set href attribute for href appropriately', () => {
expect(getMathML("\\href{http://example.org}{\\alpha}")).toMatchSnapshot();
expect(getMathML("p \\Vdash \\beta \\href{http://example.org}{+ \\alpha} \\times \\gamma"));
});
it('should render mathchoice as if there was nothing', () => {
const cmd = "\\sum_{k = 0}^{\\infty} x^k";
expect(getMathML(`\\displaystyle\\mathchoice{${cmd}}{T}{S}{SS}`))