span.js 887 B

123456789101112131415161718192021222324252627282930313233
  1. export class Span {
  2. constructor(speech, attributes) {
  3. this.speech = speech;
  4. this.attributes = attributes;
  5. }
  6. static empty() {
  7. return new Span('', {});
  8. }
  9. static stringEmpty(str) {
  10. return new Span(str, {});
  11. }
  12. static stringAttr(str, attr) {
  13. return new Span(str, attr);
  14. }
  15. static singleton(str, def = {}) {
  16. return [Span.stringAttr(str, def)];
  17. }
  18. static node(str, node, def = {}) {
  19. const attr = Span.getAttributes(node);
  20. Object.assign(attr, def);
  21. return new Span(str, attr);
  22. }
  23. static getAttributes(node) {
  24. const attrs = {};
  25. for (const attr of Span.attributeList) {
  26. if (node.hasAttribute(attr)) {
  27. attrs[attr] = node.getAttribute(attr);
  28. }
  29. }
  30. return attrs;
  31. }
  32. }
  33. Span.attributeList = ['id', 'extid'];