Skip to content

Commit

Permalink
parse attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed May 13, 2018
1 parent f3692d5 commit f2ee4dd
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 3 deletions.
81 changes: 80 additions & 1 deletion dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: []
};

Expand All @@ -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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion dist/moon.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 80 additions & 1 deletion src/compiler/parser/tag.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,89 @@
import { error, pushChild } from "./util";

const whitespaceRE = /\s/;

const parseAttributes = (index, input, length, attributes) => {
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: []
};

Expand All @@ -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;
}
}

Expand Down

0 comments on commit f2ee4dd

Please sign in to comment.