版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1 год назад
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. const {
  2. TOKEN_OPEN_TAG_END,
  3. TOKEN_OPEN_TAG_END_SCRIPT,
  4. TOKEN_OPEN_TAG_END_STYLE,
  5. TOKEN_ATTRIBUTE_KEY,
  6. TOKEN_ATTRIBUTE_ASSIGNMENT
  7. } = require('../constants/token-types')
  8. const {
  9. ATTRIBUTE_VALUE_CONTEXT
  10. } = require('../constants/tree-constructor-contexts')
  11. function getLastAttribute (state) {
  12. const attributes = state.currentNode.content.attributes
  13. return attributes[attributes.length - 1]
  14. }
  15. function handleOpenTagEnd (state) {
  16. state.currentContext = state.currentContext.parentRef
  17. return state
  18. }
  19. function handleAttributeKey (state, token) {
  20. const attribute = getLastAttribute(state)
  21. if (attribute.key !== undefined || attribute.value !== undefined) {
  22. state.currentContext = state.currentContext.parentRef
  23. return state
  24. }
  25. attribute.key = token
  26. state.caretPosition++
  27. return state
  28. }
  29. function handleAttributeAssignment (state) {
  30. const attribute = getLastAttribute(state)
  31. if (attribute.value !== undefined) {
  32. state.currentContext = state.currentContext.parentRef
  33. return state
  34. }
  35. state.currentContext = {
  36. parentRef: state.currentContext,
  37. type: ATTRIBUTE_VALUE_CONTEXT
  38. }
  39. state.caretPosition++
  40. return state
  41. }
  42. module.exports = function attribute (token, state) {
  43. const OPEN_TAG_END_TOKENS = [
  44. TOKEN_OPEN_TAG_END,
  45. TOKEN_OPEN_TAG_END_SCRIPT,
  46. TOKEN_OPEN_TAG_END_STYLE
  47. ]
  48. if (OPEN_TAG_END_TOKENS.indexOf(token.type) !== -1) {
  49. return handleOpenTagEnd(state)
  50. }
  51. if (token.type === TOKEN_ATTRIBUTE_KEY) {
  52. return handleAttributeKey(state, token)
  53. }
  54. if (token.type === TOKEN_ATTRIBUTE_ASSIGNMENT) {
  55. return handleAttributeAssignment(state)
  56. }
  57. state.caretPosition++
  58. return state
  59. }