Simplify HTML checking.

This commit is contained in:
ccd0 2015-03-29 12:42:13 -07:00
parent 6ad0064f41
commit c24e42be90

View File

@ -7,30 +7,6 @@ module.exports = (grunt) ->
importHTML = (filename) -> importHTML = (filename) ->
html grunt.template.process(grunt.file.read("src/General/html/#{filename}.html").replace(/^ +/gm, '').replace(/\r?\n/g, ''), data: grunt.config('pkg')) html grunt.template.process(grunt.file.read("src/General/html/#{filename}.html").replace(/^ +/gm, '').replace(/\r?\n/g, ''), data: grunt.config('pkg'))
checkHTML = (text, context) ->
while text
switch context
when ''
if part = text.match /^[^'"<>]+/ # plain text
else if part = text.match /^<\/\w+>/ # HTML end tag
else if part = text.match /^<\w+/ # start of HTML tag
context = '<'
when '<'
if part = text.match /^ [\w-]+(?![\w-=])/ # boolean attribute
else if part = text.match /^ [\w-]+=(['"])/ # start of attribute value
context = part[1]
else if part = text.match /^>/ # end of HTML tag
context = ''
when "'", '"'
if part = text.match /^[^'"<>]+/ # attribute value
else if part = text.match /^['"]/ # end of attribute value
throw new Error 'Inconsistent quoting' if part[0] isnt context
context = '<'
unless part
throw new Error "HTML template is ill-formed (at #{text})"
text = text[part[0].length..]
context
parseTemplate = (template, context='') -> parseTemplate = (template, context='') ->
context0 = context context0 = context
parts = [] parts = []
@ -38,15 +14,15 @@ module.exports = (grunt) ->
while text while text
if part = text.match /^[^{}]+(?!{)/ if part = text.match /^[^{}]+(?!{)/
text = text[part[0].length..] text = text[part[0].length..]
try context = (context + part[0])
context = checkHTML part[0], context .replace(/(=['"])[^'"<>]*/g, '$1')
catch err .replace(/(<\w+)( [\w-]+((?=[ >])|=''|=""))*/g, '$1')
throw new Error "#{err.message}: #{template}" .replace(/^([^'"<>]+|<\/?\w+>)*/, '')
parts.push json part[0] parts.push json part[0]
else if part = text.match /^([^}]){([^}`]*)}/ else if part = text.match /^([^}]){([^}`]*)}/
text = text[part[0].length..] text = text[part[0].length..]
unless context is '' or (part[1] is '$' and context isnt '<') or part[1] is '?' unless context is '' or (part[1] is '$' and /\=['"]$/.test context) or part[1] is '?'
throw new Error "Illegal insertion into HTML template: #{template}" throw new Error "Illegal insertion into HTML template (at #{context}): #{template}"
parts.push switch part[1] parts.push switch part[1]
when '$' then "E(`#{part[2]}`)" when '$' then "E(`#{part[2]}`)"
when '&' then "`#{part[2]}`.innerHTML" when '&' then "`#{part[2]}`.innerHTML"
@ -66,7 +42,7 @@ module.exports = (grunt) ->
else else
break break
if context isnt context0 if context isnt context0
throw new Error "HTML template not properly terminated: #{template}" throw new Error "HTML template is ill-formed (at #{context}): #{template}"
output = if parts.length is 0 then '""' else parts.join ' + ' output = if parts.length is 0 then '""' else parts.join ' + '
[output, text] [output, text]