Skip to content

Instantly share code, notes, and snippets.

@silentmatt
Created November 26, 2010 17:32
Show Gist options
  • Select an option

  • Save silentmatt/716987 to your computer and use it in GitHub Desktop.

Select an option

Save silentmatt/716987 to your computer and use it in GitHub Desktop.

Revisions

  1. silentmatt revised this gist Nov 27, 2010. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions make.js
    Original file line number Diff line number Diff line change
    @@ -51,15 +51,15 @@ var make = (function() {
    return el;
    }

    function mergeAttributes(attr1, attr2) {
    for (var key in attr2) {
    if (!attr1.hasOwnProperty(key)) {
    attr1[key] = attr2[key];
    } else if (key === "class") {
    attr1[key] += " " + attr2[key];
    }
    }
    }
    function mergeAttributes(attr1, attr2) {
    for (var key in attr2) {
    if (!attr1.hasOwnProperty(key)) {
    attr1[key] = attr2[key];
    } else if (key === "class") {
    attr1[key] += " " + attr2[key];
    }
    }
    }

    function splitTag(tag) {
    var attr = {};
  2. silentmatt revised this gist Nov 27, 2010. 1 changed file with 66 additions and 30 deletions.
    96 changes: 66 additions & 30 deletions make.js
    Original file line number Diff line number Diff line change
    @@ -1,38 +1,74 @@
    function make(desc) {
    if (!isArray(desc)) {
    return make.call(this, Array.prototype.slice.call(arguments));
    }
    var make = (function() {
    if (typeof Array.isArray !== 'function') {
    Array.isArray = function(a) {
    return Object.prototype.toString.call(a) === "[object Array]";
    };
    }

    var name = desc[0];
    var attributes = desc[1];
    function make(desc) {
    if (!Array.isArray(desc)) {
    return make.call(this, Array.prototype.slice.call(arguments));
    }

    var el = document.createElement(name);
    var name = desc[0];
    name = splitTag(name);
    var attrs = name[1];
    name = name[0];

    var start = 1;
    if (typeof attributes === "object" && attributes !== null && !isArray(attributes)) {
    for (var attr in attributes) {
    if (typeof attributes[attr] === "function") {
    el[attr] = attributes[attr];
    }
    else {
    el.setAttribute(attr, attributes[attr]);
    }
    }
    start = 2;
    }
    var attributes = desc[1];

    for (var i = start; i < desc.length; i++) {
    if (isArray(desc[i])) {
    el.appendChild(make(desc[i]));
    }
    else {
    el.appendChild(document.createTextNode(desc[i]));
    var el = document.createElement(name);

    var start = 1;
    if (typeof attributes === "object" && attributes !== null && !Array.isArray(attributes)) {
    mergeAttributes(attrs, attributes);
    start = 2;
    }
    attributes = attrs;

    for (var attr in attributes) {
    if (typeof attributes[attr] === 'string') {
    console.log(attr, attributes[attr]);
    el.setAttribute(attr, attributes[attr]);
    }
    else {
    el[attr] = attributes[attr];
    }
    }

    for (var i = start; i < desc.length; i++) {
    if (Array.isArray(desc[i])) {
    el.appendChild(make(desc[i]));
    }
    else if (desc[i] instanceof Element) {
    el.appendChild(desc[i]);
    }
    else {
    el.appendChild(document.createTextNode(desc[i]));
    }
    }

    return el;
    }

    function mergeAttributes(attr1, attr2) {
    for (var key in attr2) {
    if (!attr1.hasOwnProperty(key)) {
    attr1[key] = attr2[key];
    } else if (key === "class") {
    attr1[key] += " " + attr2[key];
    }
    }
    }

    return el;
    }
    function splitTag(tag) {
    var attr = {};
    var c = tag.split(".");
    var t = c[0].split("#");
    if (t[1]) attr.id = t[1];
    if (c.length > 1) attr["class"] = c.slice(1).join(" ");
    return [t[0] || "div", attr];
    }

    function isArray(a) {
    return Object.prototype.toString.call(a) === "[object Array]";
    }
    return make;
    })();
  3. silentmatt created this gist Nov 26, 2010.
    38 changes: 38 additions & 0 deletions make.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    function make(desc) {
    if (!isArray(desc)) {
    return make.call(this, Array.prototype.slice.call(arguments));
    }

    var name = desc[0];
    var attributes = desc[1];

    var el = document.createElement(name);

    var start = 1;
    if (typeof attributes === "object" && attributes !== null && !isArray(attributes)) {
    for (var attr in attributes) {
    if (typeof attributes[attr] === "function") {
    el[attr] = attributes[attr];
    }
    else {
    el.setAttribute(attr, attributes[attr]);
    }
    }
    start = 2;
    }

    for (var i = start; i < desc.length; i++) {
    if (isArray(desc[i])) {
    el.appendChild(make(desc[i]));
    }
    else {
    el.appendChild(document.createTextNode(desc[i]));
    }
    }

    return el;
    }

    function isArray(a) {
    return Object.prototype.toString.call(a) === "[object Array]";
    }