import { Token, TokenGroup } from "../../types/Token"; const reservedHtmlElementTags = [ 'p', 'div', 'button', 'input', 'form', 'table', 'tr', 'td', 'th', 'span', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'pre', 'section', 'i', 'b', 'body', 'document', 'html' ] import { findPropertyName } from './properties'; import { parseBackground, parseColor, parseMargin, parsePadding, parseFont, parseBorder } from './symbols'; import { StyleProperty } from "../../types/style/StyleProperty"; import { StyleRule } from "../../types/style/StyleRule"; import { CodeSection } from "../../types/CodeSection"; import { parseFloat } from "./symbols/float"; export function processStyle( body: any[] ): CodeSection { const head = body[0]; const propertySymbols = body[1]; const targetSymbols = head .filter( (token: Token) => [ TokenGroup.HTML, TokenGroup.Custom ].some( type => token.group === type ) ) .map( (token: Token) => { if( reservedHtmlElementTags.some(x => x === token.key ) ) { return token.key; } else { return `.${token.key}`; } }); let properties = []; for( let i=0; i < propertySymbols.length; i++ ) { const ruleSymbols = propertySymbols[i].filter( (x: Token) => x.key ); const propertyName = findPropertyName( ruleSymbols ); if( !propertyName ) continue; const rule = parseProperty( propertyName, ruleSymbols ); if( !rule ) continue; properties.push( rule ); } return { type: 'style', content: { targets: targetSymbols, properties } } } function parseProperty( propertyName: string, tokens: Token[] ): StyleProperty | null { switch(propertyName) { case 'background': return parseBackground( tokens ); case 'color': return parseColor( tokens ); case 'margin': return parseMargin( tokens ); case 'padding': return parsePadding( tokens ); case 'font': return parseFont ( tokens ); case 'border': return parseBorder( tokens ); case 'float': return parseFloat( tokens ); } return null; }