Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fred-wang committed Nov 2, 2016
0 parents commit a2fb733
Show file tree
Hide file tree
Showing 6 changed files with 380 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mfenced-min.js
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2016 Igalia S.L.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
27 changes: 27 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright (c) 2016 Igalia S.L.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

CLOSURE_COMPILER=java -jar ~/node_modules/google-closure-compiler/compiler.jar

mfenced-min.js: mfenced.js
$(CLOSURE_COMPILER) --compilation_level ADVANCED_OPTIMIZATIONS < $< > $@

clean:
rm -f mfenced-min.js
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# mfenced polyfill

This repository contains a small polyfill for the MathML
[mfenced element](https://www.w3.org/TR/MathML/chapter3.html#presm.fenced).
In order to use it, just load the mfenced-min.js script:

<html>
<head>
...
<script src="mfenced-min.js"></script>
...
</head>
...
</html>

At page load, all the mfenced elements will be converted into their equivalent
expanded form. A function window.expandMathMLFencedElements is also defined and
can be used to execute this conversion again later.
197 changes: 197 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
<!DOCTYPE html>
<html>
<head>
<title>The mfenced element</title>
<meta charset="utf-8"/>
<script type="text/javascript">
window.addEventListener("load", function() {
var mfenced_js = document.createElement("script");
mfenced_js.setAttribute("type","text/javascript")
mfenced_js.setAttribute("src", "mfenced-min.js");
document.head.appendChild(mfenced_js);
document.getElementById("button").addEventListener("click", function() { window.expandMathMLFencedElements(); });
});
</script>
</head>
<body>
<h1>The mfenced element</h1>

<p>This page contains some tests for the <a href="https://www.w3.org/TR/MathML/chapter3.html#presm.fenced">mfenced element</a>. Click the following button to load a <a href="mfenced.js">polyfill</a> and convert them into an equivalent expanded form.
</p>
<p><input id="button" type="button" value="Load mfenced.js!"/></p>

<ol>
<li>empty:
<math>
<mfenced>
</mfenced>
</math>
</li>

<li>1 argument:
<math>
<mfenced>
<mn>0</mn>
</mfenced>
</math>
</li>

<li>2 arguments:
<math>
<mfenced>
<mn>0</mn>
<mn>1</mn>
</mfenced>
</math>
</li>

<li>3 arguments:
<math>
<mfenced>
<mn>0</mn>
<mn>1</mn>
<mn>2</mn>
</mfenced>
</math>
</li>

<li>open:
<math>
<mfenced open="[">
<mn>0</mn>
<mn>1</mn>
</mfenced>
</math>
</li>

<li>close:
<math>
<mfenced close="]">
<mn>0</mn>
<mn>1</mn>
</mfenced>
</math>
</li>

<li>separators:
<math>
<mfenced separators=";">
<mn>0</mn>
<mn>1</mn>
</mfenced>
</math>
</li>

<li>open, close, one separator:
<math>
<mfenced open="[" separators=";" close="]">
<mn>0</mn>
<mn>1</mn>
</mfenced>
</math>
</li>

<li>several separators:
<math>
<mfenced separators=",;|">
<mn>0</mn>
<mn>1</mn>
<mn>2</mn>
<mn>3</mn>
</mfenced>
</math>
</li>

<li>multiple characters in open/close attributes:
<math>
<mfenced open="ABC" close="EDF">
<mn>0</mn>
<mn>1</mn>
<mn>2</mn>
</mfenced>
</math>
</li>

<li>too few separators (last one is repeated):
<math>
<mfenced separators=",;|">
<mn>0</mn>
<mn>1</mn>
<mn>2</mn>
<mn>3</mn>
<mn>4</mn>
<mn>5</mn>
</mfenced>
</math>
</li>

<li>too many separators (excess is ignored):
<math>
<mfenced separators=",;|/">
<mn>0</mn>
<mn>1</mn>
<mn>2</mn>
</mfenced>
</math>
</li>

<li>no separators (no &lt;mo&gt; separators should be output):
<math>
<mfenced separators="">
<mn>0</mn>
<mn>1</mn>
<mn>2</mn>
</mfenced>
</math>
</li>

<li>whitespace (they should be ignored):
<math>
<mfenced open="&#x20;&#x9;&#x20;&#x9;&#xA;&#xD;&#xA;&#xD;&#x20;&#x9;&#xA;&#xD;[&#x20;&#x9;&#x20;&#x9;&#xA;&#xD;&#xA;&#xD;&#x20;&#x9;&#xA;&#xD;" close="&#x20;&#x9;&#x20;&#x9;&#xA;&#xD;&#xA;&#xD;&#x20;&#x9;&#xA;&#xD;]&#x20;&#x9;&#x20;&#x9;&#xA;&#xD;&#xA;&#xD;&#x20;&#x9;&#xA;&#xD;" separators="&#x20;&#x9;&#x20;&#x9;&#xA;&#xD;&#xA;&#xD;&#x20;&#x9;&#xA;&#xD;,&#x20;&#x9;&#x20;&#x9;&#xA;&#xD;&#xA;&#xD;&#x20;&#x9;&#xA;&#xD;;&#x20;&#x9;&#x20;&#x9;&#xA;&#xD;&#xA;&#xD;&#x20;&#x9;&#xA;&#xD;|&#x20;&#x9;&#x20;&#x9;&#xA;&#xD;&#xA;&#xD;&#x20;&#x9;&#xA;&#xD;">
<mn>0</mn>
<mn>1</mn>
<mn>2</mn>
<mn>3</mn>
</mfenced>
</math>
</li>

<li>surrogate pairs (each pair should be treated as one character):
<math>
<mfenced open="𝐀" separators="𝐁𝐂𝐃" close="𝐄">
<mn>0</mn>
<mn>1</mn>
<mn>2</mn>
<mn>3</mn>
</mfenced>
</math>
</li>

<li>Other attributes (they should be attached to the expanded mrow):
<style scoped="scoped">.b { font-family: monospace; }</style>
<math>
<mfenced class="b"
style="font-size: 3em;"
href="http://example.com/"
mathcolor="blue"
mathbackground="red">
<mn>0</mn>
<mn>1</mn>
<mn>2</mn>
</mfenced>
</math>
</li>

<li>Invalid rtl attribute (it should not be attached to the expanded mrow):
<math>
<mfenced dir="rtl">
<mn>0</mn>
<mn>1</mn>
<mn>2</mn>
</mfenced>
</math>
</li>
</ol>

</body>
</html>
118 changes: 118 additions & 0 deletions mfenced.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode:nil; c-basic-offset: 4 -*- */
/* vim: set ts=4 et sw=4 tw=80: */
/*
Copyright (c) 2016 Igalia S.L.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

(function () {
"use strict";

const namespaceURI = "http://www.w3.org/1998/Math/MathML";

function collapseWhiteSpace(text) {
// Collapse the whitespace as specified by the MathML specification.
// See https://www.w3.org/TR/MathML/chapter2.html#fund.collapse
return text.replace(/^[\s]+|[\s]+$/g, '').replace(/[\s]+/g, ' ');
}

function newOperator(text, separator) {
// Create <mo fence="true">text</mo> or <mo separator="true">text</mo>.
let operator = document.createElementNS(namespaceURI, "mo");
operator.appendChild(document.createTextNode(text));
operator.setAttribute(separator ? "separator" : "fence", "true");
return operator;
}

function newMrow() {
// Create an empty <mrow>.
return document.createElementNS(namespaceURI, "mrow");
}

function getSeparatorList(text) {
// Split the separators attribute into a list of characters.
// We ignore whitespace and handle surrogate pairs.
if (text === null) {
return [","];
}
let separatorList = [];
for (let i = 0; i < text.length; i++) {
if (!/\s/g.test(text.charAt(i))) {
let c = text.charCodeAt(i);
if (c >= 0xD800 && c < 0xDC00 && i + 1 < text.length) {
separatorList.push(text.substr(i, 2));
i++;
} else {
separatorList.push(text.charAt(i));
}
}
}
return separatorList;
}

function shouldCopyAttribute(attribute) {
// The <mfenced> and <mrow> elements have the same attributes except
// that dir is only accepted on <mrow> and open/close/separators are
// only accepted on <mfenced>. See https://www.w3.org/Math/RelaxNG/
const excludedAttributes = ["dir", "open", "close", "separators"];
return attribute.namespaceURI || !excludedAttributes.includes(attribute.localName);
}

function expandFencedElement(mfenced) {
// Return an <mrow> element representing the expanded <mfenced>.
// See https://www.w3.org/TR/MathML/chapter3.html#presm.fenced
let outerMrow = newMrow();
outerMrow.appendChild(newOperator(collapseWhiteSpace(mfenced.getAttribute("open") || "(")));
if (mfenced.childElementCount === 1) {
outerMrow.appendChild(mfenced.firstElementChild.cloneNode(true));
} else if (mfenced.childElementCount > 1) {
let separatorList = getSeparatorList(mfenced.getAttribute("separators")),
innerMrow = newMrow(),
child = mfenced.firstElementChild;
while (child) {
innerMrow.appendChild(child.cloneNode(true));
child = child.nextElementSibling;
if (child && separatorList.length) {
innerMrow.appendChild(newOperator(separatorList.length > 1 ? separatorList.shift() : separatorList[0]));
}
}
outerMrow.appendChild(innerMrow);
}
outerMrow.appendChild(newOperator(collapseWhiteSpace(mfenced.getAttribute("close") || ")")));
for (let i = 0; i < mfenced.attributes.length; i++) {
let attribute = mfenced.attributes[i];
if (shouldCopyAttribute(attribute)) {
outerMrow.setAttributeNS(attribute.namespaceURI, attribute.localName, attribute.value);
}
}
return outerMrow;
}

window["expandMathMLFencedElements"] = function () {
// Replace all the <mfenced> elements with their expanded equivalent.
let mfencedElements = document.body.getElementsByTagNameNS(namespaceURI, "mfenced");
while (mfencedElements.length) {
let fenced = mfencedElements[0];
fenced.parentNode.replaceChild(expandFencedElement(fenced), fenced);
}
};

window.addEventListener("load", window["expandMathMLFencedElements"]);
}());

0 comments on commit a2fb733

Please sign in to comment.