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

69 строки
1.9 KiB

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