Element.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2018-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 a lightweight HTML Element replacement
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {OptionList} from '../../util/Options.js';
  23. import {Styles} from '../../util/Styles.js';
  24. import {LiteText} from './Text.js';
  25. /**
  26. * Type for attribute lists
  27. */
  28. export type LiteAttributeList = OptionList;
  29. /**
  30. * Type for generic nodes in LiteAdaptor
  31. */
  32. export type LiteNode = LiteElement | LiteText;
  33. /************************************************************/
  34. /**
  35. * Implements a lightweight HTML element replacement
  36. */
  37. export class LiteElement {
  38. /**
  39. * The type of element (tag name)
  40. */
  41. public kind: string;
  42. /**
  43. * The element's attribute list
  44. */
  45. public attributes: LiteAttributeList;
  46. /**
  47. * The element's children
  48. */
  49. public children: LiteNode[];
  50. /**
  51. * The element's parent
  52. */
  53. public parent: LiteElement;
  54. /**
  55. * The styles for the element
  56. */
  57. public styles: Styles;
  58. /**
  59. * @param {string} kind The type of node to create
  60. * @param {LiteAttributeList} attributes The list of attributes to set (if any)
  61. * @param {LiteNode[]} children The children for the node (if any)
  62. * @constructor
  63. */
  64. constructor(kind: string, attributes: LiteAttributeList = {}, children: LiteNode[] = []) {
  65. this.kind = kind;
  66. this.attributes = {...attributes};
  67. this.children = [...children];
  68. for (const child of this.children) {
  69. child.parent = this;
  70. }
  71. this.styles = null;
  72. }
  73. }