permet l'ajout des frameworks et des routes

This commit is contained in:
22107988t
2023-09-25 09:41:55 +02:00
parent 0b9f7d4dfb
commit 361112699c
2787 changed files with 864804 additions and 0 deletions

View File

@@ -0,0 +1,79 @@
const {
TOKEN_OPEN_TAG_END,
TOKEN_OPEN_TAG_END_SCRIPT,
TOKEN_OPEN_TAG_END_STYLE,
TOKEN_ATTRIBUTE_KEY,
TOKEN_ATTRIBUTE_ASSIGNMENT,
TOKEN_ATTRIBUTE_VALUE,
TOKEN_ATTRIBUTE_VALUE_WRAPPER_START,
TOKEN_ATTRIBUTE_VALUE_WRAPPER_END
} = require('../constants/token-types')
function getLastAttribute (state) {
const attributes = state.currentNode.content.attributes
return attributes[attributes.length - 1]
}
function handleValueEnd (state) {
state.currentContext = state.currentContext.parentRef
return state
}
function handleAttributeValue (state, token) {
const attribute = getLastAttribute(state)
attribute.value = token
state.caretPosition++
return state
}
function handleAttributeValueWrapperStart (state, token) {
const attribute = getLastAttribute(state)
attribute.startWrapper = token
state.caretPosition++
return state
}
function handleAttributeValueWrapperEnd (state, token) {
const attribute = getLastAttribute(state)
attribute.endWrapper = token
state.caretPosition++
return state
}
module.exports = function attributeValue (token, state) {
const VALUE_END_TOKENS = [
TOKEN_OPEN_TAG_END,
TOKEN_OPEN_TAG_END_SCRIPT,
TOKEN_OPEN_TAG_END_STYLE,
TOKEN_ATTRIBUTE_KEY,
TOKEN_ATTRIBUTE_ASSIGNMENT
]
if (VALUE_END_TOKENS.indexOf(token.type) !== -1) {
return handleValueEnd(state)
}
if (token.type === TOKEN_ATTRIBUTE_VALUE) {
return handleAttributeValue(state, token)
}
if (token.type === TOKEN_ATTRIBUTE_VALUE_WRAPPER_START) {
return handleAttributeValueWrapperStart(state, token)
}
if (token.type === TOKEN_ATTRIBUTE_VALUE_WRAPPER_END) {
return handleAttributeValueWrapperEnd(state, token)
}
state.caretPosition++
return state
}

View File

@@ -0,0 +1,79 @@
const {
TOKEN_OPEN_TAG_END,
TOKEN_OPEN_TAG_END_SCRIPT,
TOKEN_OPEN_TAG_END_STYLE,
TOKEN_ATTRIBUTE_KEY,
TOKEN_ATTRIBUTE_ASSIGNMENT
} = require('../constants/token-types')
const {
ATTRIBUTE_VALUE_CONTEXT
} = require('../constants/tree-constructor-contexts')
function getLastAttribute (state) {
const attributes = state.currentNode.content.attributes
return attributes[attributes.length - 1]
}
function handleOpenTagEnd (state) {
state.currentContext = state.currentContext.parentRef
return state
}
function handleAttributeKey (state, token) {
const attribute = getLastAttribute(state)
if (attribute.key !== undefined || attribute.value !== undefined) {
state.currentContext = state.currentContext.parentRef
return state
}
attribute.key = token
state.caretPosition++
return state
}
function handleAttributeAssignment (state) {
const attribute = getLastAttribute(state)
if (attribute.value !== undefined) {
state.currentContext = state.currentContext.parentRef
return state
}
state.currentContext = {
parentRef: state.currentContext,
type: ATTRIBUTE_VALUE_CONTEXT
}
state.caretPosition++
return state
}
module.exports = function attribute (token, state) {
const OPEN_TAG_END_TOKENS = [
TOKEN_OPEN_TAG_END,
TOKEN_OPEN_TAG_END_SCRIPT,
TOKEN_OPEN_TAG_END_STYLE
]
if (OPEN_TAG_END_TOKENS.indexOf(token.type) !== -1) {
return handleOpenTagEnd(state)
}
if (token.type === TOKEN_ATTRIBUTE_KEY) {
return handleAttributeKey(state, token)
}
if (token.type === TOKEN_ATTRIBUTE_ASSIGNMENT) {
return handleAttributeAssignment(state)
}
state.caretPosition++
return state
}

View File

@@ -0,0 +1,57 @@
const {
TOKEN_ATTRIBUTE_KEY,
TOKEN_ATTRIBUTE_ASSIGNMENT,
TOKEN_OPEN_TAG_END,
TOKEN_OPEN_TAG_END_SCRIPT,
TOKEN_OPEN_TAG_END_STYLE
} = require('../constants/token-types')
const {
ATTRIBUTE_CONTEXT
} = require('../constants/tree-constructor-contexts')
function handlerAttributeStart (state) {
if (state.currentNode.content.attributes === undefined) {
state.currentNode.content.attributes = []
}
// new empty attribute
state.currentNode.content.attributes.push({})
state.currentContext = {
parentRef: state.currentContext,
type: ATTRIBUTE_CONTEXT
}
return state
}
function handleOpenTagEnd (state) {
state.currentContext = state.currentContext.parentRef
return state
}
module.exports = function attributes (token, state) {
const ATTRIBUTE_START_TOKENS = [
TOKEN_ATTRIBUTE_KEY,
TOKEN_ATTRIBUTE_ASSIGNMENT
]
if (ATTRIBUTE_START_TOKENS.indexOf(token.type) !== -1) {
return handlerAttributeStart(state)
}
const ATTRIBUTES_END_TOKENS = [
TOKEN_OPEN_TAG_END,
TOKEN_OPEN_TAG_END_SCRIPT,
TOKEN_OPEN_TAG_END_STYLE
]
if (ATTRIBUTES_END_TOKENS.indexOf(token.type) !== -1) {
return handleOpenTagEnd(state)
}
state.caretPosition++
return state
}

View File

@@ -0,0 +1,46 @@
const {
TOKEN_COMMENT_START,
TOKEN_COMMENT_END,
TOKEN_COMMENT_CONTENT
} = require('../constants/token-types')
function handleCommentStart (state, token) {
state.currentNode.content.start = token
state.caretPosition++
return state
}
function handleCommentContent (state, token) {
state.currentNode.content.value = token
state.caretPosition++
return state
}
function handleCommentEnd (state, token) {
state.currentNode.content.end = token
state.currentNode = state.currentNode.parentRef
state.currentContext = state.currentContext.parentRef
state.caretPosition++
return state
}
module.exports = function comment (token, state) {
if (token.type === TOKEN_COMMENT_START) {
return handleCommentStart(state, token)
}
if (token.type === TOKEN_COMMENT_CONTENT) {
return handleCommentContent(state, token)
}
if (token.type === TOKEN_COMMENT_END) {
return handleCommentEnd(state, token)
}
state.caretPosition++
return state
}

View File

@@ -0,0 +1,80 @@
const {
TOKEN_DOCTYPE_END,
TOKEN_DOCTYPE_ATTRIBUTE,
TOKEN_DOCTYPE_ATTRIBUTE_WRAPPER_START,
TOKEN_DOCTYPE_ATTRIBUTE_WRAPPER_END
} = require('../constants/token-types')
function getLastAttribute (state) {
const attributes = state.currentNode.content.attributes
return attributes[attributes.length - 1]
}
function handleDoctypeEnd (state) {
state.currentContext = state.currentContext.parentRef
return state
}
function handleAttributeValue (state, token) {
const attribute = getLastAttribute(state)
if (attribute.value !== undefined) {
state.currentContext = state.currentContext.parentRef
return state
}
attribute.value = token
state.caretPosition++
return state
}
function handleAttributeWrapperStart (state, token) {
const attribute = getLastAttribute(state)
if (attribute.start !== undefined || attribute.value !== undefined) {
state.currentContext = state.currentContext.parentRef
return state
}
attribute.startWrapper = token
state.caretPosition++
return state
}
function handleAttributeWrapperEnd (state, token) {
const attribute = getLastAttribute(state)
attribute.endWrapper = token
state.currentContext = state.currentContext.parentRef
state.caretPosition++
return state
}
module.exports = function doctypeAttribute (token, state) {
if (token.type === TOKEN_DOCTYPE_END) {
return handleDoctypeEnd(state, token)
}
if (token.type === TOKEN_DOCTYPE_ATTRIBUTE_WRAPPER_START) {
return handleAttributeWrapperStart(state, token)
}
if (token.type === TOKEN_DOCTYPE_ATTRIBUTE_WRAPPER_END) {
return handleAttributeWrapperEnd(state, token)
}
if (token.type === TOKEN_DOCTYPE_ATTRIBUTE) {
return handleAttributeValue(state, token)
}
state.caretPosition++
return state
}

View File

@@ -0,0 +1,50 @@
const {
DOCTYPE_ATTRIBUTE_CONTEXT
} = require('../constants/tree-constructor-contexts')
const {
TOKEN_DOCTYPE_END,
TOKEN_DOCTYPE_ATTRIBUTE,
TOKEN_DOCTYPE_ATTRIBUTE_WRAPPER_START
} = require('../constants/token-types')
function handleDoctypeEnd (state) {
state.currentContext = state.currentContext.parentRef
return state
}
function handleAttribute (state) {
if (state.currentNode.content.attributes === undefined) {
state.currentNode.content.attributes = []
}
// new empty attribute
state.currentNode.content.attributes.push({})
state.currentContext = {
type: DOCTYPE_ATTRIBUTE_CONTEXT,
parentRef: state.currentContext
}
return state
}
module.exports = function doctypeAttributes (token, state) {
if (token.type === TOKEN_DOCTYPE_END) {
return handleDoctypeEnd(state, token)
}
const ATTRIBUTE_START_TOKENS = [
TOKEN_DOCTYPE_ATTRIBUTE_WRAPPER_START,
TOKEN_DOCTYPE_ATTRIBUTE
]
if (ATTRIBUTE_START_TOKENS.indexOf(token.type) !== -1) {
return handleAttribute(state, token)
}
state.caretPosition++
return state
}

View File

@@ -0,0 +1,57 @@
const {
TOKEN_DOCTYPE_END,
TOKEN_DOCTYPE_ATTRIBUTE,
TOKEN_DOCTYPE_START,
TOKEN_DOCTYPE_ATTRIBUTE_WRAPPER_START
} = require('../constants/token-types')
const {
DOCTYPE_ATTRIBUTES_CONTEXT
} = require('../constants/tree-constructor-contexts')
function handleDoctypeStart (state, token) {
state.currentNode.content.start = token
state.caretPosition++
return state
}
function handleDoctypeEnd (state, token) {
state.currentNode.content.end = token
state.currentNode = state.currentNode.parentRef
state.currentContext = state.currentContext.parentRef
state.caretPosition++
return state
}
function handleDoctypeAttributes (state) {
state.currentContext = {
parentRef: state.currentContext,
type: DOCTYPE_ATTRIBUTES_CONTEXT
}
return state
}
module.exports = function doctype (token, state) {
if (token.type === TOKEN_DOCTYPE_START) {
return handleDoctypeStart(state, token)
}
if (token.type === TOKEN_DOCTYPE_END) {
return handleDoctypeEnd(state, token)
}
const ATTRIBUTES_START_TOKENS = [
TOKEN_DOCTYPE_ATTRIBUTE_WRAPPER_START,
TOKEN_DOCTYPE_ATTRIBUTE
]
if (ATTRIBUTES_START_TOKENS.indexOf(token.type) !== -1) {
return handleDoctypeAttributes(state, token)
}
state.caretPosition++
return state
}

View File

@@ -0,0 +1,80 @@
const {
TOKEN_OPEN_TAG_START_SCRIPT,
TOKEN_OPEN_TAG_END_SCRIPT,
TOKEN_CLOSE_TAG_SCRIPT,
TOKEN_ATTRIBUTE_KEY,
TOKEN_ATTRIBUTE_ASSIGNMENT,
TOKEN_SCRIPT_TAG_CONTENT
} = require('../constants/token-types')
const { ATTRIBUTES_CONTEXT } = require('../constants/tree-constructor-contexts')
function handleOpenTagStartScript (state, token) {
state.currentNode.content.openStart = token
state.caretPosition++
return state
}
function handleAttributeStartScript (state) {
state.currentContext = {
parentRef: state.currentContext,
type: ATTRIBUTES_CONTEXT
}
return state
}
function handleOpenTagEndScript (state, token) {
state.currentNode.content.openEnd = token
state.caretPosition++
return state
}
function handleScriptContent (state, token) {
state.currentNode.content.value = token
state.caretPosition++
return state
}
function handleCloseTagScript (state, token) {
state.currentNode.content.close = token
state.currentNode = state.currentNode.parentRef
state.currentContext = state.currentContext.parentRef
state.caretPosition++
return state
}
module.exports = function scriptTag (token, state) {
if (token.type === TOKEN_OPEN_TAG_START_SCRIPT) {
return handleOpenTagStartScript(state, token)
}
const ATTRIBUTE_START_TOKENS = [
TOKEN_ATTRIBUTE_KEY,
TOKEN_ATTRIBUTE_ASSIGNMENT
]
if (ATTRIBUTE_START_TOKENS.indexOf(token.type) !== -1) {
return handleAttributeStartScript(state)
}
if (token.type === TOKEN_OPEN_TAG_END_SCRIPT) {
return handleOpenTagEndScript(state, token)
}
if (token.type === TOKEN_SCRIPT_TAG_CONTENT) {
return handleScriptContent(state, token)
}
if (token.type === TOKEN_CLOSE_TAG_SCRIPT) {
return handleCloseTagScript(state, token)
}
state.caretPosition++
return state
}

View File

@@ -0,0 +1,79 @@
const {
TOKEN_OPEN_TAG_START_STYLE,
TOKEN_OPEN_TAG_END_STYLE,
TOKEN_CLOSE_TAG_STYLE,
TOKEN_ATTRIBUTE_KEY,
TOKEN_ATTRIBUTE_ASSIGNMENT,
TOKEN_STYLE_TAG_CONTENT
} = require('../constants/token-types')
const { ATTRIBUTES_CONTEXT } = require('../constants/tree-constructor-contexts')
function handleOpenTagStartStyle (state, token) {
state.currentNode.content.openStart = token
state.caretPosition++
return state
}
function handleAttributeStartStyle (state) {
state.currentContext = {
parentRef: state.currentContext,
type: ATTRIBUTES_CONTEXT
}
return state
}
function handleOpenTagEndStyle (state, token) {
state.currentNode.content.openEnd = token
state.caretPosition++
return state
}
function handleStyleContent (state, token) {
state.currentNode.content.value = token
state.caretPosition++
return state
}
function handleCloseTagStyle (state, token) {
state.currentNode.content.close = token
state.currentNode = state.currentNode.parentRef
state.currentContext = state.currentContext.parentRef
state.caretPosition++
return state
}
module.exports = function styleTag (token, state) {
if (token.type === TOKEN_OPEN_TAG_START_STYLE) {
return handleOpenTagStartStyle(state, token)
}
const ATTRIBUTE_START_TOKENS = [
TOKEN_ATTRIBUTE_KEY,
TOKEN_ATTRIBUTE_ASSIGNMENT
]
if (ATTRIBUTE_START_TOKENS.indexOf(token.type) !== -1) {
return handleAttributeStartStyle(state)
}
if (token.type === TOKEN_OPEN_TAG_END_STYLE) {
return handleOpenTagEndStyle(state, token)
}
if (token.type === TOKEN_STYLE_TAG_CONTENT) {
return handleStyleContent(state, token)
}
if (token.type === TOKEN_CLOSE_TAG_STYLE) {
return handleCloseTagStyle(state, token)
}
state.caretPosition++
return state
}

View File

@@ -0,0 +1,203 @@
const parseCloseTagName = require('../helpers').parseCloseTagName
const {
TOKEN_OPEN_TAG_START,
TOKEN_CLOSE_TAG,
TOKEN_COMMENT_START,
TOKEN_DOCTYPE_START,
TOKEN_TEXT,
TOKEN_OPEN_TAG_START_SCRIPT,
TOKEN_OPEN_TAG_START_STYLE
} = require('../constants/token-types')
const {
TAG_CONTEXT,
COMMENT_CONTEXT,
DOCTYPE_CONTEXT,
SCRIPT_TAG_CONTEXT,
STYLE_TAG_CONTEXT
} = require('../constants/tree-constructor-contexts')
const {
NODE_TAG,
NODE_TEXT,
NODE_DOCTYPE,
NODE_COMMENT,
NODE_SCRIPT,
NODE_STYLE
} = require('../constants/ast-nodes')
function handleOpenTagStart (state) {
if (state.currentNode.content.children === undefined) {
state.currentNode.content.children = []
}
const tagNode = {
nodeType: NODE_TAG,
parentRef: state.currentNode,
content: {}
}
state.currentNode.content.children.push(tagNode)
state.currentNode = tagNode
state.currentContext = {
parentRef: state.currentContext,
type: TAG_CONTEXT
}
return state
}
function handleCloseTag (state, token) {
const closeTagName = parseCloseTagName(token.content)
if (closeTagName !== state.currentNode.content.name) {
state.caretPosition++
return state
}
state.currentContext = state.currentContext.parentRef
return state
}
function handleCommentStart (state) {
if (state.currentNode.content.children === undefined) {
state.currentNode.content.children = []
}
const commentNode = {
nodeType: NODE_COMMENT,
parentRef: state.currentNode,
content: {}
}
state.currentNode.content.children.push(commentNode)
state.currentNode = commentNode
state.currentContext = {
parentRef: state.currentContext,
type: COMMENT_CONTEXT
}
return state
}
function handleDoctypeStart (state) {
if (state.currentNode.content.children === undefined) {
state.currentNode.content.children = []
}
const doctypeNode = {
nodeType: NODE_DOCTYPE,
parentRef: state.currentNode,
content: {}
}
state.currentNode.content.children.push(doctypeNode)
state.currentNode = doctypeNode
state.currentContext = {
parentRef: state.currentContext,
type: DOCTYPE_CONTEXT
}
return state
}
function handleText (state, token) {
if (state.currentNode.content.children === undefined) {
state.currentNode.content.children = []
}
const textNode = {
nodeType: NODE_TEXT,
parentRef: state.currentNode,
content: {
value: token
}
}
state.currentNode.content.children.push(textNode)
state.caretPosition++
return state
}
function handleOpenTagStartScript (state) {
if (state.currentNode.content.children === undefined) {
state.currentNode.content.children = []
}
const scriptNode = {
nodeType: NODE_SCRIPT,
parentRef: state.currentNode,
content: {}
}
state.currentNode.content.children.push(scriptNode)
state.currentNode = scriptNode
state.currentContext = {
type: SCRIPT_TAG_CONTEXT,
parentRef: state.currentContext
}
return state
}
function handleOpenTagStartStyle (state) {
if (state.currentNode.content.children === undefined) {
state.currentNode.content.children = []
}
const styleNode = {
nodeType: NODE_STYLE,
parentRef: state.currentNode,
content: {}
}
state.currentNode.content.children.push(styleNode)
state.currentNode = styleNode
state.currentContext = {
type: STYLE_TAG_CONTEXT,
parentRef: state.currentContext
}
return state
}
module.exports = function tagContent (token, state) {
if (token.type === TOKEN_OPEN_TAG_START) {
return handleOpenTagStart(state, token)
}
if (token.type === TOKEN_TEXT) {
return handleText(state, token)
}
if (token.type === TOKEN_CLOSE_TAG) {
return handleCloseTag(state, token)
}
if (token.type === TOKEN_COMMENT_START) {
return handleCommentStart(state, token)
}
if (token.type === TOKEN_DOCTYPE_START) {
return handleDoctypeStart(state, token)
}
if (token.type === TOKEN_OPEN_TAG_START_SCRIPT) {
return handleOpenTagStartScript(state, token)
}
if (token.type === TOKEN_OPEN_TAG_START_STYLE) {
return handleOpenTagStartStyle(state, token)
}
state.caretPosition++
return state
}

View File

@@ -0,0 +1,29 @@
/**
* Parser for 'tag-name' context.
* Parses tag name from 'open-tag-start' (<div)
* token and save the tag name as self content.
* Ignores tokens others than 'open-tag-start'.
*/
const parseOpenTagName = require('../helpers').parseOpenTagName
const {
TOKEN_OPEN_TAG_START
} = require('../constants/token-types')
function handleTagOpenStart (state, token) {
state.currentNode.content.name = parseOpenTagName(token.content)
state.currentContext = state.currentContext.parentRef
return state
}
module.exports = function tagName (token, state) {
if (token.type === TOKEN_OPEN_TAG_START) {
handleTagOpenStart(state, token)
}
state.caretPosition++
return state
}

View File

@@ -0,0 +1,108 @@
const {
TOKEN_OPEN_TAG_START,
TOKEN_OPEN_TAG_END,
TOKEN_CLOSE_TAG,
TOKEN_ATTRIBUTE_KEY,
TOKEN_ATTRIBUTE_ASSIGNMENT
} = require('../constants/token-types')
const {
TAG_NAME_CONTEXT,
ATTRIBUTES_CONTEXT,
TAG_CONTENT_CONTEXT
} = require('../constants/tree-constructor-contexts')
function handleOpenTagStart (state, token) {
state.currentNode.content.openStart = token
state.currentContext = {
parentRef: state.currentContext,
type: TAG_NAME_CONTEXT
}
return state
}
function handleAttributeStart (state) {
state.currentContext = {
parentRef: state.currentContext,
type: ATTRIBUTES_CONTEXT
}
return state
}
function handleOpenTagEnd (state, token) {
const SELF_CLOSING_TAGS = [
'area',
'base',
'br',
'col',
'embed',
'hr',
'img',
'input',
'keygen',
'link',
'meta',
'param',
'source',
'track',
'wbr'
]
const tagName = state.currentNode.content.name
state.currentNode.content.openEnd = token
if (SELF_CLOSING_TAGS.indexOf(tagName) !== -1) {
state.currentNode.content.selfClosing = true
state.currentNode = state.currentNode.parentRef
state.currentContext = state.currentContext.parentRef
state.caretPosition++
return state
}
state.currentNode.content.selfClosing = false
state.currentContext = {
parentRef: state.currentContext,
type: TAG_CONTENT_CONTEXT
}
state.caretPosition++
return state
}
function handleCloseTag (state, token) {
state.currentNode.content.close = token
state.currentNode = state.currentNode.parentRef
state.currentContext = state.currentContext.parentRef
state.caretPosition++
return state
}
module.exports = function tag (token, state) {
if (token.type === TOKEN_OPEN_TAG_START) {
return handleOpenTagStart(state, token)
}
const ATTRIBUTE_START_TOKENS = [
TOKEN_ATTRIBUTE_KEY,
TOKEN_ATTRIBUTE_ASSIGNMENT
]
if (ATTRIBUTE_START_TOKENS.indexOf(token.type) !== -1) {
return handleAttributeStart(state)
}
if (token.type === TOKEN_OPEN_TAG_END) {
return handleOpenTagEnd(state, token)
}
if (token.type === TOKEN_CLOSE_TAG) {
return handleCloseTag(state, token)
}
state.caretPosition++
return state
}