diff --git a/dist/moon.js b/dist/moon.js index ce371f40..379b5f31 100644 --- a/dist/moon.js +++ b/dist/moon.js @@ -46,16 +46,90 @@ stack[stack.length - 1].children.push(child); }; + var whitespaceRE = /\s/; + + var parseAttributes = function (index, input, length, attributes) { + while (index < length) { + var char = input[index]; + + if (char === "/" || char === ">") { + break; + } else if (whitespaceRE.test(char)) { + index += 1; + continue; + } else { + var key = ""; + var value = ""; + var literal = false; + + while (index < length) { + char = input[index]; + + if (char === "/" || char === ">" || whitespaceRE.test(char)) { + value = key; + break; + } else if (char === "=") { + index += 1; + break; + } else { + key += char; + index += 1; + } + } + + if (value.length === 0) { + var quote = (void 0); + char = input[index]; + + if (char === "\"" || char === "'") { + quote = char; + index += 1; + } else if (char === "{") { + quote = "}"; + literal = true; + index += 1; + } else { + quote = whitespaceRE; + } + + while (index < length) { + char = input[index]; + + if (char === "/" || char === ">") { + break; + } else if ((typeof quote === "object" && quote.test(char)) || char === quote) { + index += 1; + break; + } else { + value += char; + index += 1; + } + } + } + + attributes.push({ + key: key, + value: value, + literal: literal + }); + } + } + + return index; + }; + var parseOpeningTag = function (index, input, length, stack) { var type = ""; + var attributes; - for (; index < length; index++) { + while (index < length) { var char = input[index]; if (char === ">") { var element = { index: stack.parseIndex++, type: type, + attributes: attributes, children: [] }; @@ -68,13 +142,18 @@ pushChild({ index: stack.parseIndex++, type: type, + attributes: attributes, children: [] }, stack); index += 2; break; + } else if (whitespaceRE.test(char)) { + attributes = []; + index = parseAttributes(index + 1, input, length, attributes); } else { type += char; + index += 1; } } diff --git a/dist/moon.min.js b/dist/moon.min.js index ce872a90..ac723cf6 100644 --- a/dist/moon.min.js +++ b/dist/moon.min.js @@ -4,4 +4,4 @@ * Released under the MIT License * https://kbrsh.github.io/moon */ -!function(n,e){"undefined"==typeof module?n.Moon=e():module.exports=e()}(this,function(){"use strict";var a=function(n,e,t){for(;n"===u){n+=3;break}n+=1}}return n},n={silent:!0},d=function(n,e){e[e.length-1].children.push(n)},f=function(n,e,t,r){for(var i="";n"===u){var o={index:r.parseIndex++,type:i,children:[]};d(o,r),r.push(o),n+=1;break}if("/"===u&&">"===e[n+1]){d({index:r.parseIndex++,type:i,children:[]},r),n+=2;break}i+=u}return n},s=function(n,e,t,r){for(;n"===i){n+=1;break}i}var u=r.pop();return u.type,n},o=/(?:(?:&(?:amp|gt|lt|nbsp|quot);)|"|\\|\n)/g,c={"&":"&",">":">","<":"<"," ":" ",""":'\\"',"\\":"\\\\",'"':'\\"',"\n":"\\n"},m=function(n,e,t,r){for(var i="";n"===u){e+=3;break}e+=1}}return e},e={silent:!0},s=function(e,t){t[t.length-1].children.push(e)},f=/\s/,d=function(e,t,n,r){for(;e"===i)break;if(f.test(i))e+=1;else{for(var u="",o="",c=!1;e"===i||f.test(i)){o=u;break}if("="===i){e+=1;break}u+=i,e+=1}if(0===o.length){var a=void 0;for('"'===(i=t[e])||"'"===i?(a=i,e+=1):"{"===i?(a="}",c=!0,e+=1):a=f;e"!==i;){if("object"==typeof a&&a.test(i)||i===a){e+=1;break}o+=i,e+=1}}r.push({key:u,value:o,literal:c})}}return e},m=function(e,t,n,r){for(var i,u="";e"===o){var c={index:r.parseIndex++,type:u,attributes:i,children:[]};s(c,r),r.push(c),e+=1;break}if("/"===o&&">"===t[e+1]){s({index:r.parseIndex++,type:u,attributes:i,children:[]},r),e+=2;break}f.test(o)?e=d(e+1,t,n,i=[]):(u+=o,e+=1)}return e},p=function(e,t,n,r){for(;e"===i){e+=1;break}i}var u=r.pop();return u.type,e},o=/(?:(?:&(?:amp|gt|lt|nbsp|quot);)|"|\\|\n)/g,c={"&":"&",">":">","<":"<"," ":" ",""":'\\"',"\\":"\\\\",'"':'\\"',"\n":"\\n"},l=function(e,t,n,r){for(var i="";e { + while (index < length) { + let char = input[index]; + + if (char === "/" || char === ">") { + break; + } else if (whitespaceRE.test(char)) { + index += 1; + continue; + } else { + let key = ""; + let value = ""; + let literal = false; + + while (index < length) { + char = input[index]; + + if (char === "/" || char === ">" || whitespaceRE.test(char)) { + value = key; + break; + } else if (char === "=") { + index += 1; + break; + } else { + key += char; + index += 1; + } + } + + if (value.length === 0) { + let quote; + char = input[index]; + + if (char === "\"" || char === "'") { + quote = char; + index += 1; + } else if (char === "{") { + quote = "}"; + literal = true; + index += 1; + } else { + quote = whitespaceRE; + } + + while (index < length) { + char = input[index]; + + if (char === "/" || char === ">") { + break; + } else if ((typeof quote === "object" && quote.test(char)) || char === quote) { + index += 1; + break; + } else { + value += char; + index += 1; + } + } + } + + attributes.push({ + key: key, + value: value, + literal: literal + }); + } + } + + return index; +}; + export const parseOpeningTag = (index, input, length, stack) => { let type = ""; + let attributes; - for (; index < length; index++) { + while (index < length) { const char = input[index]; if (char === ">") { const element = { index: stack.parseIndex++, type: type, + attributes: attributes, children: [] }; @@ -22,13 +96,18 @@ export const parseOpeningTag = (index, input, length, stack) => { pushChild({ index: stack.parseIndex++, type: type, + attributes: attributes, children: [] }, stack); index += 2; break; + } else if (whitespaceRE.test(char)) { + attributes = []; + index = parseAttributes(index + 1, input, length, attributes); } else { type += char; + index += 1; } }