Attributes.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2017-2022 The MathJax Consortium
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * @fileoverview Implements Attribute class for MmlNodes
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {PropertyList, Property} from '../Tree/Node.js';
  23. /**
  24. * A constant for when a property should be inherited from the global defaults lists
  25. */
  26. export const INHERIT = '_inherit_';
  27. /******************************************************************/
  28. /**
  29. * Implements the Attributes class for MmlNodes
  30. * (These can be set explicitly, inherited from parent nodes,
  31. * taken from a default list of values, or taken from global
  32. * defaults.)
  33. */
  34. export class Attributes {
  35. /**
  36. * The attributes explicitly set on a node
  37. */
  38. protected attributes: PropertyList;
  39. /**
  40. * The attributes inherited from parent nodes
  41. */
  42. protected inherited: PropertyList;
  43. /**
  44. * The default attributes for the node type
  45. */
  46. protected defaults: PropertyList;
  47. /**
  48. * Global attributes from the math node itself
  49. */
  50. protected global: PropertyList;
  51. /**
  52. * @param {PropertyList} defaults The defaults for this node type
  53. * @param {PropertyList} global The global properties (from the math node)
  54. *
  55. * @constructor
  56. */
  57. constructor(defaults: PropertyList, global: PropertyList) {
  58. this.global = global;
  59. this.defaults = Object.create(global);
  60. this.inherited = Object.create(this.defaults);
  61. this.attributes = Object.create(this.inherited);
  62. Object.assign(this.defaults, defaults);
  63. }
  64. /**
  65. * @param {string} name The name of the attribute to set
  66. * @param {Property} value The value to give the named attribute
  67. */
  68. public set(name: string, value: Property) {
  69. this.attributes[name] = value;
  70. }
  71. /**
  72. * @param {PropertyList} list An object containing the properties to set
  73. */
  74. public setList(list: PropertyList) {
  75. Object.assign(this.attributes, list);
  76. }
  77. /**
  78. * @param {string} name The name of the attribute whose value is to be returned
  79. * @return {Property} The value of the named attribute (including inheritance and defaults)
  80. */
  81. public get(name: string): Property {
  82. let value = this.attributes[name];
  83. if (value === INHERIT) {
  84. value = this.global[name];
  85. }
  86. return value;
  87. }
  88. /**
  89. * @param {string} name The value of the attribute whose value is to be returned
  90. * @return {Property} The attribute whose name was given if it is explicit on the
  91. * node (not inherited or defaulted), null otherwise
  92. */
  93. public getExplicit(name: string): Property {
  94. if (!this.attributes.hasOwnProperty(name)) {
  95. return undefined;
  96. }
  97. return this.attributes[name];
  98. }
  99. /**
  100. * @param {string[]} names The names of attributes whose values are to be returned
  101. * @return {PropertyList} An object containing the attributes and their values
  102. */
  103. public getList(...names: string[]): PropertyList {
  104. let values: PropertyList = {};
  105. for (const name of names) {
  106. values[name] = this.get(name);
  107. }
  108. return values;
  109. }
  110. /**
  111. * @param {string} name The name of an inherited attribute to be set
  112. * @param {Property} value The value to assign to the named attribute
  113. */
  114. public setInherited(name: string, value: Property) {
  115. this.inherited[name] = value;
  116. }
  117. /**
  118. * @param {string} name The name of an inherited attribute whose value is to be returned
  119. * @return {Property} The value of the named attribute if it is inherited, null otherwise
  120. */
  121. public getInherited(name: string): Property {
  122. return this.inherited[name];
  123. }
  124. /**
  125. * @param {string} name The name of a default attribute whose value is to be returned
  126. * @return {Property} The value of the named attribute if a default exists for it, null otherwise
  127. */
  128. public getDefault(name: string): Property {
  129. return this.defaults[name];
  130. }
  131. /**
  132. * @param {string} name The name of a attribute to check
  133. * @return {boolean} True if attribute is set explicitly or inherited
  134. * from an explicit mstyle or math attribute
  135. */
  136. public isSet(name: string): boolean {
  137. return this.attributes.hasOwnProperty(name) || this.inherited.hasOwnProperty(name);
  138. }
  139. /**
  140. * @param {string} name The name of an attribute to test for the existence of a default
  141. * @return {boolean} True of there is a default for the named attribute, false otherwise
  142. */
  143. public hasDefault(name: string): boolean {
  144. return (name in this.defaults);
  145. }
  146. /**
  147. * @return {string[]} The names of all the attributes explicitly set on the node
  148. */
  149. public getExplicitNames(): string[] {
  150. return Object.keys(this.attributes);
  151. }
  152. /**
  153. * @return {string[]} The names of all the inherited attributes for the node
  154. */
  155. public getInheritedNames(): string[] {
  156. return Object.keys(this.inherited);
  157. }
  158. /**
  159. * @return {string[]} The names of all the default attributes for the node
  160. */
  161. public getDefaultNames(): string[] {
  162. return Object.keys(this.defaults);
  163. }
  164. /**
  165. * @return {string[]} The names of all the global attributes
  166. */
  167. public getGlobalNames(): string[] {
  168. return Object.keys(this.global);
  169. }
  170. /**
  171. * @return {PropertyList} The attribute object
  172. */
  173. public getAllAttributes(): PropertyList {
  174. return this.attributes;
  175. }
  176. /**
  177. * @return {PropertyList} The inherited object
  178. */
  179. public getAllInherited(): PropertyList {
  180. return this.inherited;
  181. }
  182. /**
  183. * @return {PropertyList} The defaults object
  184. */
  185. public getAllDefaults(): PropertyList {
  186. return this.defaults;
  187. }
  188. /**
  189. * @return {PropertyList} The global object
  190. */
  191. public getAllGlobals(): PropertyList {
  192. return this.global;
  193. }
  194. }