walker_util.js 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. import * as DomUtil from '../common/dom_util.js';
  2. import { Attribute } from '../enrich_mathml/enrich_attr.js';
  3. export function splitAttribute(attr) {
  4. return !attr ? [] : attr.split(/,/);
  5. }
  6. export function getAttribute(node, attr) {
  7. return node.getAttribute(attr);
  8. }
  9. export function getSemanticRoot(node) {
  10. if (node.hasAttribute(Attribute.TYPE) &&
  11. !node.hasAttribute(Attribute.PARENT)) {
  12. return node;
  13. }
  14. const semanticNodes = DomUtil.querySelectorAllByAttr(node, Attribute.TYPE);
  15. for (let i = 0, semanticNode; (semanticNode = semanticNodes[i]); i++) {
  16. if (!semanticNode.hasAttribute(Attribute.PARENT)) {
  17. return semanticNode;
  18. }
  19. }
  20. return node;
  21. }
  22. export function getBySemanticId(root, id) {
  23. if (root.getAttribute(Attribute.ID) === id) {
  24. return root;
  25. }
  26. return DomUtil.querySelectorAllByAttrValue(root, Attribute.ID, id)[0];
  27. }
  28. export function getAllBySemanticId(root, id) {
  29. if (root.getAttribute(Attribute.ID) === id) {
  30. return [root];
  31. }
  32. return DomUtil.querySelectorAllByAttrValue(root, Attribute.ID, id);
  33. }