Skip to content

Commit

Permalink
Merge pull request #11 from colinc86/develop
Browse files Browse the repository at this point in the history
Fix block equation whitespace.
  • Loading branch information
colinc86 authored May 29, 2023
2 parents edc4c2d + 45ccfcf commit f56f060
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 51 deletions.
1 change: 1 addition & 0 deletions Sources/LaTeXSwiftUI/Extensions/CGRect+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
//

import Foundation
import CoreGraphics

extension CGRect: Hashable {

Expand Down
8 changes: 4 additions & 4 deletions Sources/LaTeXSwiftUI/Models/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,28 @@ internal struct Parser {

/// An TeX-style block equation component.
static let tex = EquationComponent(
regex: #/\$\$(.*?)\$\$/#,
regex: #/\$\$\s*(.*?)\s*\$\$/#,
terminatingRegex: #/\$\$/#,
equation: .texEquation,
supportsRecursion: false)

/// A block equation.
static let block = EquationComponent(
regex: #/\\\[(.*?)\\\]/#,
regex: #/\\\[\s*(.*?)\s*\\\]/#,
terminatingRegex: #/\\\]/#,
equation: .blockEquation,
supportsRecursion: false)

/// A named equation component.
static let named = EquationComponent(
regex: #/\\begin{equation}(.*?)\\end{equation}/#,
regex: #/\\begin{equation}\s*(.*?)\s*\\end{equation}/#,
terminatingRegex: #/\\end{equation}/#,
equation: .namedEquation,
supportsRecursion: true)

/// A named no number equation component.
static let namedNoNumber = EquationComponent(
regex: #/\\begin{equation\*}(.*?)\\end{equation\*}/#,
regex: #/\\begin{equation\*}\s*(.*?)\s*\\end{equation\*}/#,
terminatingRegex: #/\\end{equation\*}/#,
equation: .namedNoNumberEquation,
supportsRecursion: true)
Expand Down
43 changes: 43 additions & 0 deletions Tests/LaTeXSwiftUITests/GeometryTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import MathJaxSwift
import XCTest
@testable import LaTeXSwiftUI

final class GeometryTests: XCTestCase {

func testSVGGeometry_parseAlignment() {
let input = "\"vertical-align: -1.602ex;\""
let value = SVGGeometry.parseAlignment(from: input)
XCTAssertEqual(value, -1.602)
}

func testSVGGeometry_parseAlignmentZero() {
let input = "\"vertical-align: 0;\""
let value = SVGGeometry.parseAlignment(from: input)
XCTAssertEqual(value, 0)
}

func testSVGGeometry_parseXHeight() {
let input = "\"-1.602ex\""
let value = SVGGeometry.parseXHeight(from: input)
XCTAssertEqual(value, -1.602)
}

func testSVGGeometry_parseViewBox() {
let input = "\"0 -1342 940 2050\""
let value = SVGGeometry.parseViewBox(from: input)
XCTAssertEqual(value, CGRect(x: 0, y: -1342, width: 940, height: 2050))
}

func testSVGGeometry() throws {
let input = """
<svg style="vertical-align: -1.602ex;" xmlns="http://www.w3.org/2000/svg" width="2.127ex" height="4.638ex" role="img" focusable="false" viewBox="0 -1342 940 2050" xmlns:xlink="http://www.w3.org/1999/xlink"><defs><path id="MJX-1-MM-N-32" d="M449 174L424 174C419 144 412 100 402 85C395 77 329 77 307 77L127 77L233 180C389 318 449 372 449 472C449 586 359 666 237 666C124 666 50 574 50 485C50 429 100 429 103 429C120 429 155 441 155 482C155 508 137 534 102 534C94 534 92 534 89 533C112 598 166 635 224 635C315 635 358 554 358 472C358 392 308 313 253 251L61 37C50 26 50 24 50 0L421 0Z"></path><path id="MJX-1-MM-N-33" d="M457 171C457 253 394 331 290 352C372 379 430 449 430 528C430 610 342 666 246 666C145 666 69 606 69 530C69 497 91 478 120 478C151 478 171 500 171 529C171 579 124 579 109 579C140 628 206 641 242 641C283 641 338 619 338 529C338 517 336 459 310 415C280 367 246 364 221 363C213 362 189 360 182 360C174 359 167 358 167 348C167 337 174 337 191 337L235 337C317 337 354 269 354 171C354 35 285 6 241 6C198 6 123 23 88 82C123 77 154 99 154 137C154 173 127 193 98 193C74 193 42 179 42 135C42 44 135-22 244-22C366-22 457 69 457 171Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="mfrac"><g data-mml-node="mn" transform="translate(220,676)"><use data-c="32" xlink:href="#MJX-1-MM-N-32"></use></g><g data-mml-node="mn" transform="translate(220,-686)"><use data-c="33" xlink:href="#MJX-1-MM-N-33"></use></g><rect width="700" height="60" x="120" y="220"></rect></g></g></g></svg>
"""
let geometry = try SVGGeometry(svg: input)
XCTAssertNoThrow(geometry)
XCTAssertEqual(geometry.verticalAlignment, -1.602)
XCTAssertEqual(geometry.width, 2.127)
XCTAssertEqual(geometry.height, 4.638)
XCTAssertEqual(geometry.frame, CGRect(x: 0, y: -1342, width: 940, height: 2050))
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
//
// ParserTests.swift
//
//
// Created by Colin Campbell on 5/29/23.
//

import MathJaxSwift
import XCTest
@testable import LaTeXSwiftUI

final class LaTeXSwiftUITests: XCTestCase {
final class ParserTests: XCTestCase {

func assertComponent(_ components: [Component], _ index: Int, _ text: String, _ type: Component.ComponentType, file: StaticString = #file, line: UInt = #line) {
guard index < components.count else {
XCTFail()
Expand Down Expand Up @@ -85,6 +92,30 @@ final class LaTeXSwiftUITests: XCTestCase {
assertComponent(components, 0, input, .text)
}

func testParseDoubleDollarOnly_LeadingLineBreak() {
let equation = "\nf(x)=5x+2"
let input = "$$\(equation)$$"
let components = Parser.parse(input)
XCTAssertEqual(components.count, 1)
assertComponent(components, 0, equation, .texEquation)
}

func testParseDoubleDollarOnly_TrailingLineBreak() {
let equation = "f(x)=5x+2\n"
let input = "$$\(equation)$$"
let components = Parser.parse(input)
XCTAssertEqual(components.count, 1)
assertComponent(components, 0, equation, .texEquation)
}

func testParseDoubleDollarOnly_Whitespace() {
let equation = " \nf(x)=5x+2\n "
let input = "$$\(equation)$$"
let components = Parser.parse(input)
XCTAssertEqual(components.count, 1)
assertComponent(components, 0, equation, .texEquation)
}

func testParseBracketsOnly() {
let input = "\\[\\TeX\\]"
let components = Parser.parse(input)
Expand Down Expand Up @@ -115,11 +146,28 @@ final class LaTeXSwiftUITests: XCTestCase {
assertComponent(components, 0, input, .text)
}

func testParseBracketsOnly_LineBreak() {
let input = "Hello, \\[\\TeX\n\\\\]!"
func testParseBracketsOnly_LeadingLineBreak() {
let equation = "\n\\TeX"
let input = "Hello, \\[\(equation)\\]!"
let components = Parser.parse(input)
XCTAssertEqual(components.count, 1)
assertComponent(components, 0, input, .text)
XCTAssertEqual(components.count, 3)
assertComponent(components, 1, equation, .blockEquation)
}

func testParseBracketsOnly_TrailingLineBreak() {
let equation = "\\TeX\n"
let input = "Hello, \\[\(equation)\\]!"
let components = Parser.parse(input)
XCTAssertEqual(components.count, 3)
assertComponent(components, 1, equation, .blockEquation)
}

func testParseBracketsOnly_Whitespace() {
let equation = " \n\\TeX\n "
let input = "Hello, \\[\(equation)\\]!"
let components = Parser.parse(input)
XCTAssertEqual(components.count, 3)
assertComponent(components, 1, equation, .blockEquation)
}

func testParseBeginEndOnly() {
Expand Down Expand Up @@ -152,11 +200,28 @@ final class LaTeXSwiftUITests: XCTestCase {
assertComponent(components, 0, input, .text)
}

func testParseBeginEndOnly_LineBreak() {
let input = "Hello, \\begin{equation}\\TeX\n\\\\end{equation}!"
func testParseBeginEndOnly_LeadingLineBreak() {
let equation = "\n\\TeX"
let input = "Hello, \\begin{equation}\(equation)\\end{equation}!"
let components = Parser.parse(input)
XCTAssertEqual(components.count, 1)
assertComponent(components, 0, input, .text)
XCTAssertEqual(components.count, 3)
assertComponent(components, 1, equation, .namedEquation)
}

func testParseBeginEndOnly_TrailingLineBreak() {
let equation = "\\TeX\n"
let input = "Hello, \\begin{equation}\(equation)\\end{equation}!"
let components = Parser.parse(input)
XCTAssertEqual(components.count, 3)
assertComponent(components, 1, equation, .namedEquation)
}

func testParseBeginEndOnly_Whitespace() {
let equation = " \n\\TeX\n "
let input = "Hello, \\begin{equation}\(equation)\\end{equation}!"
let components = Parser.parse(input)
XCTAssertEqual(components.count, 3)
assertComponent(components, 1, equation, .namedEquation)
}

func testParseBeginEndStarOnly() {
Expand Down Expand Up @@ -189,47 +254,28 @@ final class LaTeXSwiftUITests: XCTestCase {
assertComponent(components, 0, input, .text)
}

func testParseBeginEndStarOnly_LineBreak() {
let input = "Hello, \\begin{equation*}\\TeX\n\\\\end{equation*}!"
func testParseBeginEndStarOnly_LeadingLineBreak() {
let equation = "\n\\TeX"
let input = "Hello, \\begin{equation*}\(equation)\\end{equation*}!"
let components = Parser.parse(input)
XCTAssertEqual(components.count, 1)
assertComponent(components, 0, input, .text)
}

func testSVGGeometry_parseAlignment() {
let input = "\"vertical-align: -1.602ex;\""
let value = SVGGeometry.parseAlignment(from: input)
XCTAssertEqual(value, -1.602)
}

func testSVGGeometry_parseAlignmentZero() {
let input = "\"vertical-align: 0;\""
let value = SVGGeometry.parseAlignment(from: input)
XCTAssertEqual(value, 0)
}

func testSVGGeometry_parseXHeight() {
let input = "\"-1.602ex\""
let value = SVGGeometry.parseXHeight(from: input)
XCTAssertEqual(value, -1.602)
XCTAssertEqual(components.count, 3)
assertComponent(components, 1, equation, .namedNoNumberEquation)
}

func testSVGGeometry_parseViewBox() {
let input = "\"0 -1342 940 2050\""
let value = SVGGeometry.parseViewBox(from: input)
XCTAssertEqual(value, CGRect(x: 0, y: -1342, width: 940, height: 2050))
func testParseBeginEndStarOnly_TrailingLineBreak() {
let equation = "\\TeX\n"
let input = "Hello, \\begin{equation*}\(equation)\\end{equation*}!"
let components = Parser.parse(input)
XCTAssertEqual(components.count, 3)
assertComponent(components, 1, equation, .namedNoNumberEquation)
}

func testSVGGeometry() throws {
let input = """
<svg style="vertical-align: -1.602ex;" xmlns="http://www.w3.org/2000/svg" width="2.127ex" height="4.638ex" role="img" focusable="false" viewBox="0 -1342 940 2050" xmlns:xlink="http://www.w3.org/1999/xlink"><defs><path id="MJX-1-MM-N-32" d="M449 174L424 174C419 144 412 100 402 85C395 77 329 77 307 77L127 77L233 180C389 318 449 372 449 472C449 586 359 666 237 666C124 666 50 574 50 485C50 429 100 429 103 429C120 429 155 441 155 482C155 508 137 534 102 534C94 534 92 534 89 533C112 598 166 635 224 635C315 635 358 554 358 472C358 392 308 313 253 251L61 37C50 26 50 24 50 0L421 0Z"></path><path id="MJX-1-MM-N-33" d="M457 171C457 253 394 331 290 352C372 379 430 449 430 528C430 610 342 666 246 666C145 666 69 606 69 530C69 497 91 478 120 478C151 478 171 500 171 529C171 579 124 579 109 579C140 628 206 641 242 641C283 641 338 619 338 529C338 517 336 459 310 415C280 367 246 364 221 363C213 362 189 360 182 360C174 359 167 358 167 348C167 337 174 337 191 337L235 337C317 337 354 269 354 171C354 35 285 6 241 6C198 6 123 23 88 82C123 77 154 99 154 137C154 173 127 193 98 193C74 193 42 179 42 135C42 44 135-22 244-22C366-22 457 69 457 171Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="mfrac"><g data-mml-node="mn" transform="translate(220,676)"><use data-c="32" xlink:href="#MJX-1-MM-N-32"></use></g><g data-mml-node="mn" transform="translate(220,-686)"><use data-c="33" xlink:href="#MJX-1-MM-N-33"></use></g><rect width="700" height="60" x="120" y="220"></rect></g></g></g></svg>
"""
let geometry = try SVGGeometry(svg: input)
XCTAssertNoThrow(geometry)
XCTAssertEqual(geometry.verticalAlignment, -1.602)
XCTAssertEqual(geometry.width, 2.127)
XCTAssertEqual(geometry.height, 4.638)
XCTAssertEqual(geometry.frame, CGRect(x: 0, y: -1342, width: 940, height: 2050))
func testParseBeginEndStarOnly_Whitespace() {
let equation = " \n\\TeX\n "
let input = "Hello, \\begin{equation*}\(equation)\\end{equation*}!"
let components = Parser.parse(input)
XCTAssertEqual(components.count, 3)
assertComponent(components, 1, equation, .namedNoNumberEquation)
}

}

0 comments on commit f56f060

Please sign in to comment.