html_highlighter.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import * as DomUtil from '../common/dom_util.js';
  2. import { AbstractHighlighter } from './abstract_highlighter.js';
  3. export class HtmlHighlighter extends AbstractHighlighter {
  4. constructor() {
  5. super();
  6. this.mactionName = 'maction';
  7. }
  8. highlightNode(node) {
  9. const info = {
  10. node: node,
  11. foreground: node.style.color,
  12. position: node.style.position
  13. };
  14. const color = this.color.rgb();
  15. node.style.color = color.foreground;
  16. node.style.position = 'relative';
  17. const bbox = node.bbox;
  18. if (bbox && bbox.w) {
  19. const vpad = 0.05;
  20. const hpad = 0;
  21. const span = DomUtil.createElement('span');
  22. const left = parseFloat(node.style.paddingLeft || '0');
  23. span.style.backgroundColor = color.background;
  24. span.style.opacity = color.alphaback.toString();
  25. span.style.display = 'inline-block';
  26. span.style.height = bbox.h + bbox.d + 2 * vpad + 'em';
  27. span.style.verticalAlign = -bbox.d + 'em';
  28. span.style.marginTop = span.style.marginBottom = -vpad + 'em';
  29. span.style.width = bbox.w + 2 * hpad + 'em';
  30. span.style.marginLeft = left - hpad + 'em';
  31. span.style.marginRight = -bbox.w - hpad - left + 'em';
  32. node.parentNode.insertBefore(span, node);
  33. info.box = span;
  34. }
  35. return info;
  36. }
  37. unhighlightNode(info) {
  38. const node = info.node;
  39. node.style.color = info.foreground;
  40. node.style.position = info.position;
  41. if (info.box) {
  42. info.box.parentNode.removeChild(info.box);
  43. }
  44. }
  45. }