File size: 3,127 Bytes
f56a29b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { walker } from '../walker.js'

export function mmultiscripts(element, targetParent, previousSibling, nextSibling, ancestors) {
  if (element.children.length === 0) {
    // Don't use
    return
  }

  const base = element.children[0]
  const postSubs = []
  const postSupers = []
  const preSubs = []
  const preSupers = []
  const children = element.children.slice(1)
  let dividerFound = false
  children.forEach((child, index) => {
    if (child.name === 'mprescripts') {
      dividerFound = true
    } else if (child.name !== 'none') {
      if (index % 2) {
        if (dividerFound) {
          preSubs.push(child)
        } else {
          postSupers.push(child)
        }
      } else {
        if (dividerFound) {
          preSupers.push(child)
        } else {
          postSubs.push(child)
        }
      }
    }
  })
  ancestors = [...ancestors]
  ancestors.unshift(element)
  const tempTarget = {
    children: []
  }
  walker(base, tempTarget, false, false, ancestors)
  let topTarget = tempTarget.children[0]

  if (postSubs.length || postSupers.length) {
    const subscriptTarget = {
      name: 'm:sub',
      type: 'tag',
      attribs: {},
      children: []
    }
    postSubs.forEach((subscript) => walker(subscript, subscriptTarget, false, false, ancestors))

    const superscriptTarget = {
      name: 'm:sup',
      type: 'tag',
      attribs: {},
      children: []
    }

    postSupers.forEach((superscript) =>
      walker(superscript, superscriptTarget, false, false, ancestors)
    )

    const topPostTarget = {
      type: 'tag',
      attribs: {},
      children: [
        {
          type: 'tag',
          name: 'm:e',
          attribs: {},
          children: [topTarget]
        }
      ]
    }
    if (postSubs.length && postSupers.length) {
      topPostTarget.name = 'm:sSubSup'
      topPostTarget.children.push(subscriptTarget)
      topPostTarget.children.push(superscriptTarget)
    } else if (postSubs.length) {
      topPostTarget.name = 'm:sSub'
      topPostTarget.children.push(subscriptTarget)
    } else {
      topPostTarget.name = 'm:sSup'
      topPostTarget.children.push(superscriptTarget)
    }
    topTarget = topPostTarget
  }

  if (preSubs.length || preSupers.length) {
    const preSubscriptTarget = {
      name: 'm:sub',
      type: 'tag',
      attribs: {},
      children: []
    }
    preSubs.forEach((subscript) => walker(subscript, preSubscriptTarget, false, false, ancestors))

    const preSuperscriptTarget = {
      name: 'm:sup',
      type: 'tag',
      attribs: {},
      children: []
    }

    preSupers.forEach((superscript) =>
      walker(superscript, preSuperscriptTarget, false, false, ancestors)
    )
    const topPreTarget = {
      name: 'm:sPre',
      type: 'tag',
      attribs: {},
      children: [
        {
          name: 'm:e',
          type: 'tag',
          attribs: {},
          children: [topTarget]
        },
        preSubscriptTarget,
        preSuperscriptTarget
      ]
    }
    topTarget = topPreTarget
  }
  targetParent.children.push(topTarget)
  // Don't iterate over children in the usual way.
}