版博士V2.0程序
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

attribute-value.js 1.7 KiB

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. TOKEN_ATTRIBUTE_VALUE,
  8. TOKEN_ATTRIBUTE_VALUE_WRAPPER_START,
  9. TOKEN_ATTRIBUTE_VALUE_WRAPPER_END
  10. } = require('../constants/token-types')
  11. function getLastAttribute (state) {
  12. const attributes = state.currentNode.content.attributes
  13. return attributes[attributes.length - 1]
  14. }
  15. function handleValueEnd (state) {
  16. state.currentContext = state.currentContext.parentRef
  17. return state
  18. }
  19. function handleAttributeValue (state, token) {
  20. const attribute = getLastAttribute(state)
  21. attribute.value = token
  22. state.caretPosition++
  23. return state
  24. }
  25. function handleAttributeValueWrapperStart (state, token) {
  26. const attribute = getLastAttribute(state)
  27. attribute.startWrapper = token
  28. state.caretPosition++
  29. return state
  30. }
  31. function handleAttributeValueWrapperEnd (state, token) {
  32. const attribute = getLastAttribute(state)
  33. attribute.endWrapper = token
  34. state.caretPosition++
  35. return state
  36. }
  37. module.exports = function attributeValue (token, state) {
  38. const VALUE_END_TOKENS = [
  39. TOKEN_OPEN_TAG_END,
  40. TOKEN_OPEN_TAG_END_SCRIPT,
  41. TOKEN_OPEN_TAG_END_STYLE,
  42. TOKEN_ATTRIBUTE_KEY,
  43. TOKEN_ATTRIBUTE_ASSIGNMENT
  44. ]
  45. if (VALUE_END_TOKENS.indexOf(token.type) !== -1) {
  46. return handleValueEnd(state)
  47. }
  48. if (token.type === TOKEN_ATTRIBUTE_VALUE) {
  49. return handleAttributeValue(state, token)
  50. }
  51. if (token.type === TOKEN_ATTRIBUTE_VALUE_WRAPPER_START) {
  52. return handleAttributeValueWrapperStart(state, token)
  53. }
  54. if (token.type === TOKEN_ATTRIBUTE_VALUE_WRAPPER_END) {
  55. return handleAttributeValueWrapperEnd(state, token)
  56. }
  57. state.caretPosition++
  58. return state
  59. }