More math components

This commit is contained in:
Dolan
2020-10-10 13:41:26 +01:00
parent 32a84a5ad0
commit 700a74fd4c
85 changed files with 1716 additions and 123 deletions

View File

@ -0,0 +1,4 @@
export * from "./math-round-brackets";
export * from "./math-square-brackets";
export * from "./math-curly-brackets";
export * from "./math-angled-brackets";

View File

@ -0,0 +1,51 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathRun } from "../math-run";
import { MathAngledBrackets } from "./math-angled-brackets";
describe("MathAngledBrackets", () => {
describe("#constructor()", () => {
it("should create a MathAngledBrackets with correct root key", () => {
const mathAngledBrackets = new MathAngledBrackets({
child: new MathRun("60"),
});
const tree = new Formatter().format(mathAngledBrackets);
expect(tree).to.deep.equal({
"m:d": [
{
"m:dPr": [
{
"m:begChr": {
_attr: {
"m:val": "〈",
},
},
},
{
"m:endChr": {
_attr: {
"m:val": "〉",
},
},
},
],
},
{
"m:e": [
{
"m:r": [
{
"m:t": ["60"],
},
],
},
],
},
],
});
});
});
});

View File

@ -0,0 +1,20 @@
// http://www.datypic.com/sc/ooxml/e-m_d-1.html
import { XmlComponent } from "file/xml-components";
import { MathComponent } from "../math-component";
import { MathBase } from "../n-ary";
import { MathBracketProperties } from "./math-bracket-properties";
export class MathAngledBrackets extends XmlComponent {
constructor(options: { readonly child: MathComponent }) {
super("m:d");
this.root.push(
new MathBracketProperties({
beginningCharacter: "〈",
endingCharacter: "〉",
}),
);
this.root.push(new MathBase(options.child));
}
}

View File

@ -0,0 +1,22 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathBeginningCharacter } from "./math-beginning-character";
describe("MathBeginningCharacter", () => {
describe("#constructor()", () => {
it("should create a MathBeginningCharacter with correct root key", () => {
const mathBeginningCharacter = new MathBeginningCharacter("[");
const tree = new Formatter().format(mathBeginningCharacter);
expect(tree).to.deep.equal({
"m:begChr": {
_attr: {
"m:val": "[",
},
},
});
});
});
});

View File

@ -0,0 +1,14 @@
// http://www.datypic.com/sc/ooxml/e-m_begChr-1.html
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
class MathBeginningCharacterAttributes extends XmlAttributeComponent<{ readonly character: string }> {
protected readonly xmlKeys = { character: "m:val" };
}
export class MathBeginningCharacter extends XmlComponent {
constructor(character: string) {
super("m:begChr");
this.root.push(new MathBeginningCharacterAttributes({ character }));
}
}

View File

@ -0,0 +1,45 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathBracketProperties } from "./math-bracket-properties";
describe("MathBracketProperties", () => {
describe("#constructor()", () => {
it("should create a MathBracketProperties with correct root key", () => {
const mathBracketProperties = new MathBracketProperties();
const tree = new Formatter().format(mathBracketProperties);
expect(tree).to.deep.equal({
"m:dPr": {},
});
});
it("should create a MathBracketProperties with correct root key and add brackets", () => {
const mathBracketProperties = new MathBracketProperties({
beginningCharacter: "[",
endingCharacter: "]",
});
const tree = new Formatter().format(mathBracketProperties);
expect(tree).to.deep.equal({
"m:dPr": [
{
"m:begChr": {
_attr: {
"m:val": "[",
},
},
},
{
"m:endChr": {
_attr: {
"m:val": "]",
},
},
},
],
});
});
});
});

View File

@ -0,0 +1,16 @@
// http://www.datypic.com/sc/ooxml/e-m_dPr-1.html
import { XmlComponent } from "file/xml-components";
import { MathBeginningCharacter } from "./math-beginning-character";
import { MathEndingCharacter } from "./math-ending-char";
export class MathBracketProperties extends XmlComponent {
constructor(options?: { readonly beginningCharacter: string; readonly endingCharacter: string }) {
super("m:dPr");
if (!!options) {
this.root.push(new MathBeginningCharacter(options.beginningCharacter));
this.root.push(new MathEndingCharacter(options.endingCharacter));
}
}
}

View File

@ -0,0 +1,51 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathRun } from "../math-run";
import { MathCurlyBrackets } from "./math-curly-brackets";
describe("MathCurlyBrackets", () => {
describe("#constructor()", () => {
it("should create a MathCurlyBrackets with correct root key", () => {
const mathCurlyBrackets = new MathCurlyBrackets({
child: new MathRun("60"),
});
const tree = new Formatter().format(mathCurlyBrackets);
expect(tree).to.deep.equal({
"m:d": [
{
"m:dPr": [
{
"m:begChr": {
_attr: {
"m:val": "{",
},
},
},
{
"m:endChr": {
_attr: {
"m:val": "}",
},
},
},
],
},
{
"m:e": [
{
"m:r": [
{
"m:t": ["60"],
},
],
},
],
},
],
});
});
});
});

View File

@ -0,0 +1,20 @@
// http://www.datypic.com/sc/ooxml/e-m_d-1.html
import { XmlComponent } from "file/xml-components";
import { MathComponent } from "../math-component";
import { MathBase } from "../n-ary";
import { MathBracketProperties } from "./math-bracket-properties";
export class MathCurlyBrackets extends XmlComponent {
constructor(options: { readonly child: MathComponent }) {
super("m:d");
this.root.push(
new MathBracketProperties({
beginningCharacter: "{",
endingCharacter: "}",
}),
);
this.root.push(new MathBase(options.child));
}
}

View File

@ -0,0 +1,14 @@
// http://www.datypic.com/sc/ooxml/e-m_endChr-1.html
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
class MathEndingCharacterAttributes extends XmlAttributeComponent<{ readonly character: string }> {
protected readonly xmlKeys = { character: "m:val" };
}
export class MathEndingCharacter extends XmlComponent {
constructor(character: string) {
super("m:endChr");
this.root.push(new MathEndingCharacterAttributes({ character }));
}
}

View File

@ -0,0 +1,22 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathEndingCharacter } from "./math-ending-char";
describe("MathEndingCharacter", () => {
describe("#constructor()", () => {
it("should create a MathEndingCharacter with correct root key", () => {
const mathEndingCharacter = new MathEndingCharacter("]");
const tree = new Formatter().format(mathEndingCharacter);
expect(tree).to.deep.equal({
"m:endChr": {
_attr: {
"m:val": "]",
},
},
});
});
});
});

View File

@ -0,0 +1,36 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathRun } from "../math-run";
import { MathRoundBrackets } from "./math-round-brackets";
describe("MathRoundBrackets", () => {
describe("#constructor()", () => {
it("should create a MathRoundBrackets with correct root key", () => {
const mathRoundBrackets = new MathRoundBrackets({
child: new MathRun("60"),
});
const tree = new Formatter().format(mathRoundBrackets);
expect(tree).to.deep.equal({
"m:d": [
{
"m:dPr": {},
},
{
"m:e": [
{
"m:r": [
{
"m:t": ["60"],
},
],
},
],
},
],
});
});
});
});

View File

@ -0,0 +1,15 @@
// http://www.datypic.com/sc/ooxml/e-m_d-1.html
import { XmlComponent } from "file/xml-components";
import { MathComponent } from "../math-component";
import { MathBase } from "../n-ary";
import { MathBracketProperties } from "./math-bracket-properties";
export class MathRoundBrackets extends XmlComponent {
constructor(options: { readonly child: MathComponent }) {
super("m:d");
this.root.push(new MathBracketProperties());
this.root.push(new MathBase(options.child));
}
}

View File

@ -0,0 +1,51 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathRun } from "../math-run";
import { MathSquareBrackets } from "./math-square-brackets";
describe("MathSquareBrackets", () => {
describe("#constructor()", () => {
it("should create a MathSquareBrackets with correct root key", () => {
const mathSquareBrackets = new MathSquareBrackets({
child: new MathRun("60"),
});
const tree = new Formatter().format(mathSquareBrackets);
expect(tree).to.deep.equal({
"m:d": [
{
"m:dPr": [
{
"m:begChr": {
_attr: {
"m:val": "[",
},
},
},
{
"m:endChr": {
_attr: {
"m:val": "]",
},
},
},
],
},
{
"m:e": [
{
"m:r": [
{
"m:t": ["60"],
},
],
},
],
},
],
});
});
});
});

View File

@ -0,0 +1,20 @@
// http://www.datypic.com/sc/ooxml/e-m_d-1.html
import { XmlComponent } from "file/xml-components";
import { MathComponent } from "../math-component";
import { MathBase } from "../n-ary";
import { MathBracketProperties } from "./math-bracket-properties";
export class MathSquareBrackets extends XmlComponent {
constructor(options: { readonly child: MathComponent }) {
super("m:d");
this.root.push(
new MathBracketProperties({
beginningCharacter: "[",
endingCharacter: "]",
}),
);
this.root.push(new MathBase(options.child));
}
}

View File

@ -3,7 +3,7 @@ import { XmlComponent } from "file/xml-components";
import { MathRun } from "../math-run";
export class MathDenominator extends XmlComponent {
constructor(readonly text: string) {
constructor(text: string) {
super("m:den");
this.root.push(new MathRun(text));

View File

@ -9,7 +9,7 @@ export interface IMathFractionOptions {
}
export class MathFraction extends XmlComponent {
constructor(readonly options: IMathFractionOptions) {
constructor(options: IMathFractionOptions) {
super("m:f");
this.root.push(options.numerator);

View File

@ -3,7 +3,7 @@ import { XmlComponent } from "file/xml-components";
import { MathRun } from "../math-run";
export class MathNumerator extends XmlComponent {
constructor(readonly text: string) {
constructor(text: string) {
super("m:num");
this.root.push(new MathRun(text));

View File

@ -0,0 +1,3 @@
export * from "./math-function";
export * from "./math-function-name";
export * from "./math-function-properties";

View File

@ -0,0 +1,27 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathRun } from "../math-run";
import { MathFunctionName } from "./math-function-name";
describe("MathFunctionName", () => {
describe("#constructor()", () => {
it("should create a MathFunctionName with correct root key", () => {
const mathFunctionName = new MathFunctionName(new MathRun("2"));
const tree = new Formatter().format(mathFunctionName);
expect(tree).to.deep.equal({
"m:fName": [
{
"m:r": [
{
"m:t": ["2"],
},
],
},
],
});
});
});
});

View File

@ -0,0 +1,11 @@
// http://www.datypic.com/sc/ooxml/e-m_fName-1.html
import { XmlComponent } from "file/xml-components";
import { MathComponent } from "../math-component";
export class MathFunctionName extends XmlComponent {
constructor(child: MathComponent) {
super("m:fName");
this.root.push(child);
}
}

View File

@ -0,0 +1,18 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathFunctionProperties } from "./math-function-properties";
describe("MathFunctionProperties", () => {
describe("#constructor()", () => {
it("should create a MathFunctionProperties with correct root key", () => {
const mathFunctionProperties = new MathFunctionProperties();
const tree = new Formatter().format(mathFunctionProperties);
expect(tree).to.deep.equal({
"m:funcPr": {},
});
});
});
});

View File

@ -0,0 +1,8 @@
// http://www.datypic.com/sc/ooxml/e-m_radPr-1.html
import { XmlComponent } from "file/xml-components";
export class MathFunctionProperties extends XmlComponent {
constructor() {
super("m:funcPr");
}
}

View File

@ -0,0 +1,48 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathRun } from "../math-run";
import { MathFunction } from "./math-function";
describe("MathFunction", () => {
describe("#constructor()", () => {
it("should create a MathFunction with correct root key", () => {
const mathFunction = new MathFunction({
name: new MathRun("sin"),
child: new MathRun("60"),
});
const tree = new Formatter().format(mathFunction);
expect(tree).to.deep.equal({
"m:func": [
{
"m:funcPr": {},
},
{
"m:fName": [
{
"m:r": [
{
"m:t": ["sin"],
},
],
},
],
},
{
"m:e": [
{
"m:r": [
{
"m:t": ["60"],
},
],
},
],
},
],
});
});
});
});

View File

@ -0,0 +1,22 @@
// http://www.datypic.com/sc/ooxml/e-m_func-1.html
import { XmlComponent } from "file/xml-components";
import { MathComponent } from "../math-component";
import { MathBase } from "../n-ary";
import { MathFunctionName } from "./math-function-name";
import { MathFunctionProperties } from "./math-function-properties";
export interface IMathFunctionOptions {
readonly child: MathComponent;
readonly name: MathComponent;
}
export class MathFunction extends XmlComponent {
constructor(options: IMathFunctionOptions) {
super("m:func");
this.root.push(new MathFunctionProperties());
this.root.push(new MathFunctionName(options.name));
this.root.push(new MathBase(options.child));
}
}

View File

@ -2,3 +2,8 @@ export * from "./math";
export * from "./math-run";
export * from "./fraction";
export * from "./n-ary";
export * from "./script";
export * from "./math-component";
export * from "./radical";
export * from "./function";
export * from "./brackets";

View File

@ -0,0 +1,21 @@
import { MathAngledBrackets, MathCurlyBrackets, MathRoundBrackets, MathSquareBrackets } from "./brackets";
import { MathFraction } from "./fraction";
import { MathFunction } from "./function";
import { MathRun } from "./math-run";
import { MathSum } from "./n-ary";
import { MathRadical } from "./radical";
import { MathSubScript, MathSubSuperScript, MathSuperScript } from "./script";
export type MathComponent =
| MathRun
| MathFraction
| MathSum
| MathSuperScript
| MathSubScript
| MathSubSuperScript
| MathRadical
| MathFunction
| MathRoundBrackets
| MathCurlyBrackets
| MathAngledBrackets
| MathSquareBrackets;

View File

@ -4,7 +4,7 @@ import { XmlComponent } from "file/xml-components";
import { MathText } from "./math-text";
export class MathRun extends XmlComponent {
constructor(readonly text: string) {
constructor(text: string) {
super("m:r");
this.root.push(new MathText(text));

View File

@ -1,7 +1,7 @@
import { XmlComponent } from "file/xml-components";
export class MathText extends XmlComponent {
constructor(readonly text: string) {
constructor(text: string) {
super("m:t");
this.root.push(text);

View File

@ -1,16 +1,14 @@
// http://www.datypic.com/sc/ooxml/e-m_oMath-1.html
import { XmlComponent } from "file/xml-components";
import { MathFraction } from "./fraction";
import { MathRun } from "./math-run";
import { MathSum } from "./n-ary";
import { MathComponent } from "./math-component";
export interface IMathOptions {
readonly children: Array<MathRun | MathFraction | MathSum>;
readonly children: MathComponent[];
}
export class Math extends XmlComponent {
constructor(readonly options: IMathOptions) {
constructor(options: IMathOptions) {
super("m:oMath");
for (const child of options.children) {

View File

@ -6,7 +6,7 @@ class MathAccentCharacterAttributes extends XmlAttributeComponent<{ readonly acc
}
export class MathAccentCharacter extends XmlComponent {
constructor(readonly accent: string) {
constructor(accent: string) {
super("m:chr");
this.root.push(new MathAccentCharacterAttributes({ accent }));

View File

@ -1,10 +1,10 @@
// http://www.datypic.com/sc/ooxml/e-m_e-1.html
import { XmlComponent } from "file/xml-components";
import { MathRun } from "../math-run";
import { MathComponent } from "../math-component";
export class MathBase extends XmlComponent {
constructor(readonly run: MathRun) {
constructor(run: MathComponent) {
super("m:e");
this.root.push(run);

View File

@ -7,7 +7,7 @@ import { MathNArayProperties } from "./math-naray-properties";
describe("MathNArayProperties", () => {
describe("#constructor()", () => {
it("should create a MathNArayProperties with correct root key", () => {
const mathNArayProperties = new MathNArayProperties("∑");
const mathNArayProperties = new MathNArayProperties("∑", true, true);
const tree = new Formatter().format(mathNArayProperties);
expect(tree).to.deep.equal({
@ -29,5 +29,105 @@ describe("MathNArayProperties", () => {
],
});
});
it("should add super-script hide attributes", () => {
const mathNArayProperties = new MathNArayProperties("∑", false, true);
const tree = new Formatter().format(mathNArayProperties);
expect(tree).to.deep.equal({
"m:naryPr": [
{
"m:chr": {
_attr: {
"m:val": "∑",
},
},
},
{
"m:limLoc": {
_attr: {
"m:val": "undOvr",
},
},
},
{
"m:supHide": {
_attr: {
"m:val": 1,
},
},
},
],
});
});
it("should add sub-script hide attributes", () => {
const mathNArayProperties = new MathNArayProperties("∑", true, false);
const tree = new Formatter().format(mathNArayProperties);
expect(tree).to.deep.equal({
"m:naryPr": [
{
"m:chr": {
_attr: {
"m:val": "∑",
},
},
},
{
"m:limLoc": {
_attr: {
"m:val": "undOvr",
},
},
},
{
"m:subHide": {
_attr: {
"m:val": 1,
},
},
},
],
});
});
it("should add both super-script and sub-script hide attributes", () => {
const mathNArayProperties = new MathNArayProperties("∑", false, false);
const tree = new Formatter().format(mathNArayProperties);
expect(tree).to.deep.equal({
"m:naryPr": [
{
"m:chr": {
_attr: {
"m:val": "∑",
},
},
},
{
"m:limLoc": {
_attr: {
"m:val": "undOvr",
},
},
},
{
"m:supHide": {
_attr: {
"m:val": 1,
},
},
},
{
"m:subHide": {
_attr: {
"m:val": 1,
},
},
},
],
});
});
});
});

View File

@ -3,12 +3,22 @@ import { XmlComponent } from "file/xml-components";
import { MathAccentCharacter } from "./math-accent-character";
import { MathLimitLocation } from "./math-limit-location";
import { MathSubScriptHide } from "./math-sub-script-hide";
import { MathSuperScriptHide } from "./math-super-script-hide";
export class MathNArayProperties extends XmlComponent {
constructor(readonly accent: string) {
constructor(accent: string, hasSuperScript: boolean, hasSubScript: boolean) {
super("m:naryPr");
this.root.push(new MathAccentCharacter(accent));
this.root.push(new MathLimitLocation());
if (!hasSuperScript) {
this.root.push(new MathSuperScriptHide());
}
if (!hasSubScript) {
this.root.push(new MathSubScriptHide());
}
}
}

View File

@ -0,0 +1,22 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathSubScriptHide } from "./math-sub-script-hide";
describe("MathSubScriptHide", () => {
describe("#constructor()", () => {
it("should create a MathSubScriptHide with correct root key", () => {
const mathSubScriptHide = new MathSubScriptHide();
const tree = new Formatter().format(mathSubScriptHide);
expect(tree).to.deep.equal({
"m:subHide": {
_attr: {
"m:val": 1,
},
},
});
});
});
});

View File

@ -0,0 +1,14 @@
// http://www.datypic.com/sc/ooxml/e-m_subHide-1.html
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
class MathSubScriptHideAttributes extends XmlAttributeComponent<{ readonly hide: number }> {
protected readonly xmlKeys = { hide: "m:val" };
}
export class MathSubScriptHide extends XmlComponent {
constructor() {
super("m:subHide");
this.root.push(new MathSubScriptHideAttributes({ hide: 1 }));
}
}

View File

@ -3,14 +3,14 @@ import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathRun } from "../math-run";
import { MathSubScript } from "./math-sub-script";
import { MathSubScriptElement } from "./math-sub-script";
describe("MathSubScript", () => {
describe("MathSubScriptElement", () => {
describe("#constructor()", () => {
it("should create a MathSubScript with correct root key", () => {
const mathSubScript = new MathSubScript(new MathRun("2+2"));
it("should create a MathSubScriptElement with correct root key", () => {
const mathSubScriptElement = new MathSubScriptElement(new MathRun("2+2"));
const tree = new Formatter().format(mathSubScript);
const tree = new Formatter().format(mathSubScriptElement);
expect(tree).to.deep.equal({
"m:sub": [
{

View File

@ -1,12 +1,12 @@
// http://www.datypic.com/sc/ooxml/e-m_e-1.html
// http://www.datypic.com/sc/ooxml/e-m_sub-3.html
import { XmlComponent } from "file/xml-components";
import { MathRun } from "../math-run";
import { MathComponent } from "../math-component";
export class MathSubScript extends XmlComponent {
constructor(readonly run: MathRun) {
export class MathSubScriptElement extends XmlComponent {
constructor(child: MathComponent) {
super("m:sub");
this.root.push(run);
this.root.push(child);
}
}

View File

@ -40,7 +40,7 @@ describe("MathSum", () => {
{
"m:r": [
{
"m:t": ["1"],
"m:t": ["2"],
},
],
},
@ -51,7 +51,7 @@ describe("MathSum", () => {
{
"m:r": [
{
"m:t": ["1"],
"m:t": ["3"],
},
],
},

View File

@ -1,25 +1,32 @@
// http://www.datypic.com/sc/ooxml/e-m_nary-1.html
import { XmlComponent } from "file/xml-components";
import { MathRun } from "../math-run";
import { MathComponent } from "../math-component";
import { MathBase } from "./math-base";
import { MathNArayProperties } from "./math-naray-properties";
import { MathSubScript } from "./math-sub-script";
import { MathSuperScript } from "./math-super-script";
import { MathSubScriptElement } from "./math-sub-script";
import { MathSuperScriptElement } from "./math-super-script";
export interface IMathSumOptions {
readonly child: MathRun;
readonly subScript?: MathRun;
readonly superScript?: MathRun;
readonly child: MathComponent;
readonly subScript?: MathComponent;
readonly superScript?: MathComponent;
}
export class MathSum extends XmlComponent {
constructor(readonly options: IMathSumOptions) {
constructor(options: IMathSumOptions) {
super("m:nary");
this.root.push(new MathNArayProperties("∑"));
this.root.push(new MathSubScript(options.child));
this.root.push(new MathSuperScript(options.child));
this.root.push(new MathNArayProperties("∑", !!options.superScript, !!options.subScript));
if (!!options.subScript) {
this.root.push(new MathSubScriptElement(options.subScript));
}
if (!!options.superScript) {
this.root.push(new MathSuperScriptElement(options.superScript));
}
this.root.push(new MathBase(options.child));
}
}

View File

@ -0,0 +1,22 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathSuperScriptHide } from "./math-super-script-hide";
describe("MathSuperScriptHide", () => {
describe("#constructor()", () => {
it("should create a MathSuperScriptHide with correct root key", () => {
const mathSuperScriptHide = new MathSuperScriptHide();
const tree = new Formatter().format(mathSuperScriptHide);
expect(tree).to.deep.equal({
"m:supHide": {
_attr: {
"m:val": 1,
},
},
});
});
});
});

View File

@ -0,0 +1,14 @@
// http://www.datypic.com/sc/ooxml/e-m_subHide-1.html
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
class MathSuperScriptHideAttributes extends XmlAttributeComponent<{ readonly hide: number }> {
protected readonly xmlKeys = { hide: "m:val" };
}
export class MathSuperScriptHide extends XmlComponent {
constructor() {
super("m:supHide");
this.root.push(new MathSuperScriptHideAttributes({ hide: 1 }));
}
}

View File

@ -3,14 +3,14 @@ import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathRun } from "../math-run";
import { MathSuperScript } from "./math-super-script";
import { MathSuperScriptElement } from "./math-super-script";
describe("MathSuperScript", () => {
describe("MathSuperScriptElement", () => {
describe("#constructor()", () => {
it("should create a MathSuperScript with correct root key", () => {
const mathSuperScript = new MathSuperScript(new MathRun("2+2"));
it("should create a MathSuperScriptElement with correct root key", () => {
const mathSuperScriptElement = new MathSuperScriptElement(new MathRun("2+2"));
const tree = new Formatter().format(mathSuperScript);
const tree = new Formatter().format(mathSuperScriptElement);
expect(tree).to.deep.equal({
"m:sup": [
{

View File

@ -1,12 +1,12 @@
// http://www.datypic.com/sc/ooxml/e-m_e-1.html
// http://www.datypic.com/sc/ooxml/e-m_sup-3.html
import { XmlComponent } from "file/xml-components";
import { MathRun } from "../math-run";
import { MathComponent } from "../math-component";
export class MathSuperScript extends XmlComponent {
constructor(readonly run: MathRun) {
export class MathSuperScriptElement extends XmlComponent {
constructor(child: MathComponent) {
super("m:sup");
this.root.push(run);
this.root.push(child);
}
}

View File

@ -0,0 +1,3 @@
export * from "./math-degree";
export * from "./math-radical";
export * from "./math-radical-properties";

View File

@ -0,0 +1,22 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathDegreeHide } from "./math-degree-hide";
describe("MathDegreeHide", () => {
describe("#constructor()", () => {
it("should create a MathDegreeHide with correct root key", () => {
const mathDegreeHide = new MathDegreeHide();
const tree = new Formatter().format(mathDegreeHide);
expect(tree).to.deep.equal({
"m:degHide": {
_attr: {
"m:val": 1,
},
},
});
});
});
});

View File

@ -0,0 +1,14 @@
// http://www.datypic.com/sc/ooxml/e-m_degHide-1.html
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
class MathDegreeHideAttributes extends XmlAttributeComponent<{ readonly hide: number }> {
protected readonly xmlKeys = { hide: "m:val" };
}
export class MathDegreeHide extends XmlComponent {
constructor() {
super("m:degHide");
this.root.push(new MathDegreeHideAttributes({ hide: 1 }));
}
}

View File

@ -0,0 +1,36 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathRun } from "../math-run";
import { MathDegree } from "./math-degree";
describe("MathDegree", () => {
describe("#constructor()", () => {
it("should create a MathDegree with correct root key", () => {
const mathDegree = new MathDegree();
const tree = new Formatter().format(mathDegree);
expect(tree).to.deep.equal({
"m:deg": {},
});
});
it("should create a MathDegree with correct root key with child", () => {
const mathDegree = new MathDegree(new MathRun("2"));
const tree = new Formatter().format(mathDegree);
expect(tree).to.deep.equal({
"m:deg": [
{
"m:r": [
{
"m:t": ["2"],
},
],
},
],
});
});
});
});

View File

@ -0,0 +1,13 @@
// http://www.datypic.com/sc/ooxml/e-m_deg-1.html
import { XmlComponent } from "file/xml-components";
import { MathComponent } from "../math-component";
export class MathDegree extends XmlComponent {
constructor(child?: MathComponent) {
super("m:deg");
if (!!child) {
this.root.push(child);
}
}
}

View File

@ -0,0 +1,35 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathRadicalProperties } from "./math-radical-properties";
describe("MathRadicalProperties", () => {
describe("#constructor()", () => {
it("should create a MathRadicalProperties with correct root key", () => {
const mathRadicalProperties = new MathRadicalProperties(true);
const tree = new Formatter().format(mathRadicalProperties);
expect(tree).to.deep.equal({
"m:radPr": {},
});
});
it("should create a MathRadicalProperties with correct root key with degree hide", () => {
const mathRadicalProperties = new MathRadicalProperties(false);
const tree = new Formatter().format(mathRadicalProperties);
expect(tree).to.deep.equal({
"m:radPr": [
{
"m:degHide": {
_attr: {
"m:val": 1,
},
},
},
],
});
});
});
});

View File

@ -0,0 +1,13 @@
// http://www.datypic.com/sc/ooxml/e-m_radPr-1.html
import { XmlComponent } from "file/xml-components";
import { MathDegreeHide } from "./math-degree-hide";
export class MathRadicalProperties extends XmlComponent {
constructor(hasDegree: boolean) {
super("m:radPr");
if (!hasDegree) {
this.root.push(new MathDegreeHide());
}
}
}

View File

@ -0,0 +1,48 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathRun } from "../math-run";
import { MathRadical } from "./math-radical";
describe("MathRadical", () => {
describe("#constructor()", () => {
it("should create a MathRadical with correct root key", () => {
const mathRadical = new MathRadical({
child: new MathRun("e"),
degree: new MathRun("2"),
});
const tree = new Formatter().format(mathRadical);
expect(tree).to.deep.equal({
"m:rad": [
{
"m:radPr": {},
},
{
"m:deg": [
{
"m:r": [
{
"m:t": ["2"],
},
],
},
],
},
{
"m:e": [
{
"m:r": [
{
"m:t": ["e"],
},
],
},
],
},
],
});
});
});
});

View File

@ -0,0 +1,22 @@
// http://www.datypic.com/sc/ooxml/e-m_rad-1.html
import { XmlComponent } from "file/xml-components";
import { MathComponent } from "../math-component";
import { MathBase } from "../n-ary";
import { MathDegree } from "./math-degree";
import { MathRadicalProperties } from "./math-radical-properties";
export interface IMathRadicalOptions {
readonly child: MathComponent;
readonly degree?: MathComponent;
}
export class MathRadical extends XmlComponent {
constructor(options: IMathRadicalOptions) {
super("m:rad");
this.root.push(new MathRadicalProperties(!!options.degree));
this.root.push(new MathDegree(options.degree));
this.root.push(new MathBase(options.child));
}
}

View File

@ -0,0 +1,4 @@
export * from "./super-script";
export * from "./sub-script";
export * from "./sub-super-script";
export * from "./pre-sub-super-script";

View File

@ -0,0 +1,2 @@
export * from "./math-pre-sub-super-script-function";
export * from "./math-pre-sub-super-script-function-properties";

View File

@ -0,0 +1,17 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathPreSubSuperScriptProperties } from "./math-pre-sub-super-script-function-properties";
describe("MathPreSubSuperScriptProperties", () => {
describe("#constructor()", () => {
it("should create a MathPreSubSuperScriptProperties with correct root key", () => {
const mathPreSubSuperScriptProperties = new MathPreSubSuperScriptProperties();
const tree = new Formatter().format(mathPreSubSuperScriptProperties);
expect(tree).to.deep.equal({
"m:sPrePr": {},
});
});
});
});

View File

@ -0,0 +1,8 @@
// http://www.datypic.com/sc/ooxml/e-m_sPrePr-1.html
import { XmlComponent } from "file/xml-components";
export class MathPreSubSuperScriptProperties extends XmlComponent {
constructor() {
super("m:sPrePr");
}
}

View File

@ -0,0 +1,60 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathRun } from "../../math-run";
import { MathPreSubSuperScript } from "./math-pre-sub-super-script-function";
describe("MathPreSubScript", () => {
describe("#constructor()", () => {
it("should create a MathPreSubScript with correct root key", () => {
const mathPreSubScript = new MathPreSubSuperScript({
child: new MathRun("e"),
subScript: new MathRun("2"),
superScript: new MathRun("5"),
});
const tree = new Formatter().format(mathPreSubScript);
expect(tree).to.deep.equal({
"m:sPre": [
{
"m:sPrePr": {},
},
{
"m:e": [
{
"m:r": [
{
"m:t": ["e"],
},
],
},
],
},
{
"m:sub": [
{
"m:r": [
{
"m:t": ["2"],
},
],
},
],
},
{
"m:sup": [
{
"m:r": [
{
"m:t": ["5"],
},
],
},
],
},
],
});
});
});
});

View File

@ -0,0 +1,23 @@
// http://www.datypic.com/sc/ooxml/e-m_sPre-1.html
import { XmlComponent } from "file/xml-components";
import { MathComponent } from "../../math-component";
import { MathBase, MathSubScriptElement, MathSuperScriptElement } from "../../n-ary";
import { MathPreSubSuperScriptProperties } from "./math-pre-sub-super-script-function-properties";
export interface IMathPreSubSuperScriptOptions {
readonly child: MathComponent;
readonly subScript: MathComponent;
readonly superScript: MathComponent;
}
export class MathPreSubSuperScript extends XmlComponent {
constructor(options: IMathPreSubSuperScriptOptions) {
super("m:sPre");
this.root.push(new MathPreSubSuperScriptProperties());
this.root.push(new MathBase(options.child));
this.root.push(new MathSubScriptElement(options.subScript));
this.root.push(new MathSuperScriptElement(options.superScript));
}
}

View File

@ -0,0 +1,2 @@
export * from "./math-sub-script-function";
export * from "./math-sub-script-function-properties";

View File

@ -0,0 +1,17 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathSubScriptProperties } from "./math-sub-script-function-properties";
describe("MathSubScriptProperties", () => {
describe("#constructor()", () => {
it("should create a MathSubScriptProperties with correct root key", () => {
const mathSubScriptProperties = new MathSubScriptProperties();
const tree = new Formatter().format(mathSubScriptProperties);
expect(tree).to.deep.equal({
"m:sSubPr": {},
});
});
});
});

View File

@ -0,0 +1,8 @@
// http://www.datypic.com/sc/ooxml/e-m_sSubPr-1.html
import { XmlComponent } from "file/xml-components";
export class MathSubScriptProperties extends XmlComponent {
constructor() {
super("m:sSubPr");
}
}

View File

@ -0,0 +1,48 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathRun } from "../../math-run";
import { MathSubScript } from "./math-sub-script-function";
describe("MathSubScript", () => {
describe("#constructor()", () => {
it("should create a MathSubScript with correct root key", () => {
const mathSubScript = new MathSubScript({
child: new MathRun("e"),
subScript: new MathRun("2"),
});
const tree = new Formatter().format(mathSubScript);
expect(tree).to.deep.equal({
"m:sSub": [
{
"m:sSubPr": {},
},
{
"m:e": [
{
"m:r": [
{
"m:t": ["e"],
},
],
},
],
},
{
"m:sub": [
{
"m:r": [
{
"m:t": ["2"],
},
],
},
],
},
],
});
});
});
});

View File

@ -0,0 +1,21 @@
// http://www.datypic.com/sc/ooxml/e-m_sSub-1.html
import { XmlComponent } from "file/xml-components";
import { MathComponent } from "../../math-component";
import { MathBase, MathSubScriptElement } from "../../n-ary";
import { MathSubScriptProperties } from "./math-sub-script-function-properties";
export interface IMathSubScriptOptions {
readonly child: MathComponent;
readonly subScript: MathComponent;
}
export class MathSubScript extends XmlComponent {
constructor(options: IMathSubScriptOptions) {
super("m:sSub");
this.root.push(new MathSubScriptProperties());
this.root.push(new MathBase(options.child));
this.root.push(new MathSubScriptElement(options.subScript));
}
}

View File

@ -0,0 +1,2 @@
export * from "./math-sub-super-script-function";
export * from "./math-sub-super-script-function-properties";

View File

@ -0,0 +1,17 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathSubSuperScriptProperties } from "./math-sub-super-script-function-properties";
describe("MathSubSuperScriptProperties", () => {
describe("#constructor()", () => {
it("should create a MathSubSuperScriptProperties with correct root key", () => {
const mathSubSuperScriptProperties = new MathSubSuperScriptProperties();
const tree = new Formatter().format(mathSubSuperScriptProperties);
expect(tree).to.deep.equal({
"m:sSubSupPr": {},
});
});
});
});

View File

@ -0,0 +1,8 @@
// http://www.datypic.com/sc/ooxml/e-m_sSubSupPr-1.html
import { XmlComponent } from "file/xml-components";
export class MathSubSuperScriptProperties extends XmlComponent {
constructor() {
super("m:sSubSupPr");
}
}

View File

@ -0,0 +1,60 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathRun } from "../../math-run";
import { MathSubSuperScript } from "./math-sub-super-script-function";
describe("MathSubScript", () => {
describe("#constructor()", () => {
it("should create a MathSubScript with correct root key", () => {
const mathSubScript = new MathSubSuperScript({
child: new MathRun("e"),
subScript: new MathRun("2"),
superScript: new MathRun("5"),
});
const tree = new Formatter().format(mathSubScript);
expect(tree).to.deep.equal({
"m:sSubSup": [
{
"m:sSubSupPr": {},
},
{
"m:e": [
{
"m:r": [
{
"m:t": ["e"],
},
],
},
],
},
{
"m:sub": [
{
"m:r": [
{
"m:t": ["2"],
},
],
},
],
},
{
"m:sup": [
{
"m:r": [
{
"m:t": ["5"],
},
],
},
],
},
],
});
});
});
});

View File

@ -0,0 +1,23 @@
// http://www.datypic.com/sc/ooxml/e-m_sSubSup-1.html
import { XmlComponent } from "file/xml-components";
import { MathComponent } from "../../math-component";
import { MathBase, MathSubScriptElement, MathSuperScriptElement } from "../../n-ary";
import { MathSubSuperScriptProperties } from "./math-sub-super-script-function-properties";
export interface IMathSubSuperScriptOptions {
readonly child: MathComponent;
readonly subScript: MathComponent;
readonly superScript: MathComponent;
}
export class MathSubSuperScript extends XmlComponent {
constructor(options: IMathSubSuperScriptOptions) {
super("m:sSubSup");
this.root.push(new MathSubSuperScriptProperties());
this.root.push(new MathBase(options.child));
this.root.push(new MathSubScriptElement(options.subScript));
this.root.push(new MathSuperScriptElement(options.superScript));
}
}

View File

@ -0,0 +1,2 @@
export * from "./math-super-script-function";
export * from "./math-super-script-function-properties";

View File

@ -0,0 +1,17 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathSuperScriptProperties } from "./math-super-script-function-properties";
describe("MathSuperScriptProperties", () => {
describe("#constructor()", () => {
it("should create a MathSuperScriptProperties with correct root key", () => {
const mathSuperScriptProperties = new MathSuperScriptProperties();
const tree = new Formatter().format(mathSuperScriptProperties);
expect(tree).to.deep.equal({
"m:sSupPr": {},
});
});
});
});

View File

@ -0,0 +1,8 @@
// http://www.datypic.com/sc/ooxml/e-m_sSupPr-1.html
import { XmlComponent } from "file/xml-components";
export class MathSuperScriptProperties extends XmlComponent {
constructor() {
super("m:sSupPr");
}
}

View File

@ -0,0 +1,48 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathRun } from "../../math-run";
import { MathSuperScript } from "./math-super-script-function";
describe("MathSuperScript", () => {
describe("#constructor()", () => {
it("should create a MathSuperScript with correct root key", () => {
const mathSuperScript = new MathSuperScript({
child: new MathRun("e"),
superScript: new MathRun("2"),
});
const tree = new Formatter().format(mathSuperScript);
expect(tree).to.deep.equal({
"m:sSup": [
{
"m:sSupPr": {},
},
{
"m:e": [
{
"m:r": [
{
"m:t": ["e"],
},
],
},
],
},
{
"m:sup": [
{
"m:r": [
{
"m:t": ["2"],
},
],
},
],
},
],
});
});
});
});

View File

@ -0,0 +1,21 @@
// http://www.datypic.com/sc/ooxml/e-m_sSup-1.html
import { XmlComponent } from "file/xml-components";
import { MathComponent } from "../../math-component";
import { MathBase, MathSuperScriptElement } from "../../n-ary";
import { MathSuperScriptProperties } from "./math-super-script-function-properties";
export interface IMathSuperScriptOptions {
readonly child: MathComponent;
readonly superScript: MathComponent;
}
export class MathSuperScript extends XmlComponent {
constructor(options: IMathSuperScriptOptions) {
super("m:sSup");
this.root.push(new MathSuperScriptProperties());
this.root.push(new MathBase(options.child));
this.root.push(new MathSuperScriptElement(options.superScript));
}
}