|
| 1 | +/************************************************************* |
| 2 | + * |
| 3 | + * Copyright (c) 2026 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 | +/** |
| 19 | + * @file An extension to disable dark mode for sites that don't support that |
| 20 | + * |
| 21 | + * @author dpvc@mathjax.org (Davide Cervone) |
| 22 | + */ |
| 23 | + |
| 24 | +import { MathJax } from '../../components/global.js'; |
| 25 | +import { MathJaxObject } from '../../components/startup.js'; |
| 26 | +import { AbstractHandler } from '../../core/Handler.js'; |
| 27 | + |
| 28 | +[ |
| 29 | + [ |
| 30 | + MathJax._.ui?.dialog, |
| 31 | + 'core', |
| 32 | + () => { |
| 33 | + const { DraggableDialog } = MathJax._.ui.dialog.DraggableDialog; |
| 34 | + delete DraggableDialog.styles['@media (prefers-color-scheme: dark)']; |
| 35 | + }, |
| 36 | + ], |
| 37 | + |
| 38 | + [ |
| 39 | + MathJax._.a11y?.explorer, |
| 40 | + 'a11y/explorer', |
| 41 | + () => { |
| 42 | + const Region = MathJax._.a11y.explorer.Region; |
| 43 | + for (const region of ['LiveRegion', 'HoverRegion', 'ToolTip']) { |
| 44 | + Region[region].style.styles['@media (prefers-color-scheme: dark)'] = {}; |
| 45 | + } |
| 46 | + Region.LiveRegion.style.styles['@media (prefers-color-scheme: dark)'][ |
| 47 | + 'mjx-ignore' |
| 48 | + ] = { ignore: 1 }; |
| 49 | + (MathJax as MathJaxObject).startup.extendHandler( |
| 50 | + (handler: AbstractHandler<any, any, any>) => { |
| 51 | + delete (handler.documentClass as any).speechStyles[ |
| 52 | + '@media (prefers-color-scheme: dark) /* explorer */' |
| 53 | + ]; |
| 54 | + return handler; |
| 55 | + } |
| 56 | + ); |
| 57 | + }, |
| 58 | + ], |
| 59 | + |
| 60 | + [ |
| 61 | + MathJax._.output?.chtml, |
| 62 | + 'output/chtml', |
| 63 | + () => { |
| 64 | + const { CHTML } = MathJax._.output.chtml_ts; |
| 65 | + delete CHTML.commonStyles['@media (prefers-color-scheme: dark)']; |
| 66 | + const { ChtmlMaction } = MathJax._.output.chtml.Wrappers.maction; |
| 67 | + delete ChtmlMaction.styles[ |
| 68 | + '@media (prefers-color-scheme: dark) /* chtml maction */' |
| 69 | + ]; |
| 70 | + }, |
| 71 | + ], |
| 72 | + |
| 73 | + [ |
| 74 | + MathJax._.output?.svg, |
| 75 | + 'output/svg', |
| 76 | + () => { |
| 77 | + const { SVG } = MathJax._.output.svg_ts; |
| 78 | + delete SVG.commonStyles['@media (prefers-color-scheme: dark)']; |
| 79 | + const { SvgMaction } = MathJax._.output.svg.Wrappers.maction; |
| 80 | + delete SvgMaction.styles[ |
| 81 | + '@media (prefers-color-scheme: dark) /* svg maction */' |
| 82 | + ]; |
| 83 | + }, |
| 84 | + ], |
| 85 | +].forEach(([immediate, extension, ready]) => { |
| 86 | + if (immediate) { |
| 87 | + ready(); |
| 88 | + } else { |
| 89 | + const config = MathJax.config.loader; |
| 90 | + config[extension] ??= {}; |
| 91 | + const check = config[extension].checkReady; |
| 92 | + config[extension].checkReady = async () => { |
| 93 | + if (check) await check(); |
| 94 | + return ready(); |
| 95 | + }; |
| 96 | + } |
| 97 | +}); |
0 commit comments