HTMLHandler.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 the HTMLHandler class
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {AbstractHandler} from '../../core/Handler.js';
  23. import {MinHTMLAdaptor} from '../../adaptors/HTMLAdaptor.js';
  24. import {HTMLDocument} from './HTMLDocument.js';
  25. import {OptionList} from '../../util/Options.js';
  26. /*****************************************************************/
  27. /**
  28. * Implements the HTMLHandler class (extends AbstractHandler)
  29. *
  30. * @template N The HTMLElement node class
  31. * @template T The Text node class
  32. * @template D The Document class
  33. */
  34. export class HTMLHandler<N, T, D> extends AbstractHandler<N, T, D> {
  35. /**
  36. * The DOMAdaptor for the document being handled
  37. */
  38. public adaptor: MinHTMLAdaptor<N, T, D>; // declare a more specific adaptor type
  39. /**
  40. * @override
  41. */
  42. public documentClass = HTMLDocument;
  43. /**
  44. * @override
  45. */
  46. public handlesDocument(document: any) {
  47. const adaptor = this.adaptor;
  48. if (typeof(document) === 'string') {
  49. try {
  50. document = adaptor.parse(document, 'text/html');
  51. } catch (err) {}
  52. }
  53. if (document instanceof adaptor.window.Document ||
  54. document instanceof adaptor.window.HTMLElement ||
  55. document instanceof adaptor.window.DocumentFragment) {
  56. return true;
  57. }
  58. return false;
  59. }
  60. /**
  61. * If the document isn't already a Document object, create one
  62. * using the given data
  63. *
  64. * @override
  65. */
  66. public create(document: any, options: OptionList) {
  67. const adaptor = this.adaptor;
  68. if (typeof(document) === 'string') {
  69. document = adaptor.parse(document, 'text/html');
  70. } else if (document instanceof adaptor.window.HTMLElement ||
  71. document instanceof adaptor.window.DocumentFragment) {
  72. let child = document as N;
  73. document = adaptor.parse('', 'text/html');
  74. adaptor.append(adaptor.body(document), child);
  75. }
  76. return super.create(document, options) as HTMLDocument<N, T, D>;
  77. }
  78. }