版博士V2.0程序
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

attributes.js 1.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const {
  2. TOKEN_ATTRIBUTE_KEY,
  3. TOKEN_ATTRIBUTE_ASSIGNMENT,
  4. TOKEN_OPEN_TAG_END,
  5. TOKEN_OPEN_TAG_END_SCRIPT,
  6. TOKEN_OPEN_TAG_END_STYLE
  7. } = require('../constants/token-types')
  8. const {
  9. ATTRIBUTE_CONTEXT
  10. } = require('../constants/tree-constructor-contexts')
  11. function handlerAttributeStart (state) {
  12. if (state.currentNode.content.attributes === undefined) {
  13. state.currentNode.content.attributes = []
  14. }
  15. // new empty attribute
  16. state.currentNode.content.attributes.push({})
  17. state.currentContext = {
  18. parentRef: state.currentContext,
  19. type: ATTRIBUTE_CONTEXT
  20. }
  21. return state
  22. }
  23. function handleOpenTagEnd (state) {
  24. state.currentContext = state.currentContext.parentRef
  25. return state
  26. }
  27. module.exports = function attributes (token, state) {
  28. const ATTRIBUTE_START_TOKENS = [
  29. TOKEN_ATTRIBUTE_KEY,
  30. TOKEN_ATTRIBUTE_ASSIGNMENT
  31. ]
  32. if (ATTRIBUTE_START_TOKENS.indexOf(token.type) !== -1) {
  33. return handlerAttributeStart(state)
  34. }
  35. const ATTRIBUTES_END_TOKENS = [
  36. TOKEN_OPEN_TAG_END,
  37. TOKEN_OPEN_TAG_END_SCRIPT,
  38. TOKEN_OPEN_TAG_END_STYLE
  39. ]
  40. if (ATTRIBUTES_END_TOKENS.indexOf(token.type) !== -1) {
  41. return handleOpenTagEnd(state)
  42. }
  43. state.caretPosition++
  44. return state
  45. }