List.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 DOM adaptor
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {LiteNode} from './Element.js';
  23. /************************************************************/
  24. /**
  25. * Implements a lightweight DocumentFragment or NodeList replacement
  26. *
  27. * @template N The HTMLElement node class
  28. */
  29. export class LiteList<N> {
  30. /**
  31. * The nodes held in the fragment
  32. */
  33. public nodes: N[] = [];
  34. /**
  35. * @param {N[]} children The children for the fragment
  36. * @constructor
  37. */
  38. constructor(children: N[]) {
  39. this.nodes = [...children];
  40. }
  41. /**
  42. * @param {N} node A node to append to the fragment
  43. */
  44. public append(node: N) {
  45. this.nodes.push(node);
  46. }
  47. /**
  48. * Make this class iterable (so it can be used with Array.from())
  49. */
  50. public [Symbol.iterator](): Iterator<LiteNode> {
  51. let i = 0;
  52. return {
  53. /**
  54. * @return {IteratorResult<LiteNode>}
  55. */
  56. next(): IteratorResult<LiteNode> {
  57. return (i === this.nodes.length ?
  58. {value: null, done: true} :
  59. {value: this.nodes[i++], done: false});
  60. }
  61. };
  62. }
  63. }