mirror of
https://github.com/Smaug123/KaTeX
synced 2025-10-10 05:28:41 +00:00
Upgrade the source to use ES6 syntax including classes, import and static properties (#679)
* Add babel transform-class-properties to have static class properties * Upgrade Lexer and Parser files to use ES6 classes * Update eslint max line length to 90 character (more indent because of using ES6 classes) * Upgrade eslint and jasmin to support ES stage-2 features * Use static properties to place constants near their functions * Migrate all remaining sources to ES6 syntax * Increase eslint max line length to 84 * Remove non-babelified endpoint in dev server.js * Clean up server.js functions after removing browserified * Make screenshotter not to use babel endpoint as we babelify everything now
This commit is contained in:
committed by
Kevin Barabash
parent
0edd3d1bbb
commit
a019f36f8a
@@ -8,94 +8,98 @@
|
||||
* domTree.js, creating namespaced DOM nodes and HTML text markup respectively.
|
||||
*/
|
||||
|
||||
const utils = require("./utils");
|
||||
import utils from "./utils";
|
||||
|
||||
/**
|
||||
* This node represents a general purpose MathML node of any type. The
|
||||
* constructor requires the type of node to create (for example, `"mo"` or
|
||||
* `"mspace"`, corresponding to `<mo>` and `<mspace>` tags).
|
||||
*/
|
||||
function MathNode(type, children) {
|
||||
this.type = type;
|
||||
this.attributes = {};
|
||||
this.children = children || [];
|
||||
class MathNode {
|
||||
constructor(type, children) {
|
||||
this.type = type;
|
||||
this.attributes = {};
|
||||
this.children = children || [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an attribute on a MathML node. MathML depends on attributes to convey a
|
||||
* semantic content, so this is used heavily.
|
||||
*/
|
||||
setAttribute(name, value) {
|
||||
this.attributes[name] = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the math node into a MathML-namespaced DOM element.
|
||||
*/
|
||||
toNode() {
|
||||
const node = document.createElementNS(
|
||||
"http://www.w3.org/1998/Math/MathML", this.type);
|
||||
|
||||
for (const attr in this.attributes) {
|
||||
if (Object.prototype.hasOwnProperty.call(this.attributes, attr)) {
|
||||
node.setAttribute(attr, this.attributes[attr]);
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
node.appendChild(this.children[i].toNode());
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the math node into an HTML markup string.
|
||||
*/
|
||||
toMarkup() {
|
||||
let markup = "<" + this.type;
|
||||
|
||||
// Add the attributes
|
||||
for (const attr in this.attributes) {
|
||||
if (Object.prototype.hasOwnProperty.call(this.attributes, attr)) {
|
||||
markup += " " + attr + "=\"";
|
||||
markup += utils.escape(this.attributes[attr]);
|
||||
markup += "\"";
|
||||
}
|
||||
}
|
||||
|
||||
markup += ">";
|
||||
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
markup += this.children[i].toMarkup();
|
||||
}
|
||||
|
||||
markup += "</" + this.type + ">";
|
||||
|
||||
return markup;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an attribute on a MathML node. MathML depends on attributes to convey a
|
||||
* semantic content, so this is used heavily.
|
||||
*/
|
||||
MathNode.prototype.setAttribute = function(name, value) {
|
||||
this.attributes[name] = value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Converts the math node into a MathML-namespaced DOM element.
|
||||
*/
|
||||
MathNode.prototype.toNode = function() {
|
||||
const node = document.createElementNS(
|
||||
"http://www.w3.org/1998/Math/MathML", this.type);
|
||||
|
||||
for (const attr in this.attributes) {
|
||||
if (Object.prototype.hasOwnProperty.call(this.attributes, attr)) {
|
||||
node.setAttribute(attr, this.attributes[attr]);
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
node.appendChild(this.children[i].toNode());
|
||||
}
|
||||
|
||||
return node;
|
||||
};
|
||||
|
||||
/**
|
||||
* Converts the math node into an HTML markup string.
|
||||
*/
|
||||
MathNode.prototype.toMarkup = function() {
|
||||
let markup = "<" + this.type;
|
||||
|
||||
// Add the attributes
|
||||
for (const attr in this.attributes) {
|
||||
if (Object.prototype.hasOwnProperty.call(this.attributes, attr)) {
|
||||
markup += " " + attr + "=\"";
|
||||
markup += utils.escape(this.attributes[attr]);
|
||||
markup += "\"";
|
||||
}
|
||||
}
|
||||
|
||||
markup += ">";
|
||||
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
markup += this.children[i].toMarkup();
|
||||
}
|
||||
|
||||
markup += "</" + this.type + ">";
|
||||
|
||||
return markup;
|
||||
};
|
||||
|
||||
/**
|
||||
* This node represents a piece of text.
|
||||
*/
|
||||
function TextNode(text) {
|
||||
this.text = text;
|
||||
class TextNode {
|
||||
constructor(text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the text node into a DOM text node.
|
||||
*/
|
||||
toNode() {
|
||||
return document.createTextNode(this.text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the text node into HTML markup (which is just the text itself).
|
||||
*/
|
||||
toMarkup() {
|
||||
return utils.escape(this.text);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the text node into a DOM text node.
|
||||
*/
|
||||
TextNode.prototype.toNode = function() {
|
||||
return document.createTextNode(this.text);
|
||||
};
|
||||
|
||||
/**
|
||||
* Converts the text node into HTML markup (which is just the text itself).
|
||||
*/
|
||||
TextNode.prototype.toMarkup = function() {
|
||||
return utils.escape(this.text);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
MathNode: MathNode,
|
||||
TextNode: TextNode,
|
||||
|
Reference in New Issue
Block a user