Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Task Description */
  2. /*
  3.  * Create an object domElement, that has the following properties and methods:
  4.  * use prototypal inheritance, without function constructors
  5.  * method init() that gets the domElement type
  6.  * i.e. `Object.create(domElement).init('div')`
  7.  * property type that is the type of the domElement
  8.  * a valid type is any non-empty string that contains only Latin letters and digits
  9.  * property innerHTML of type string
  10.  * gets the domElement, parsed as valid HTML
  11.  * <type attr1="value1" attr2="value2" ...> .. content / children's.innerHTML .. </type>
  12.  * property content of type string
  13.  * sets the content of the element
  14.  * works only if there are no children
  15.  * property attributes
  16.  * each attribute has name and value
  17.  * a valid attribute has a non-empty string for a name that contains only Latin letters and digits or dashes (-)
  18.  * property children
  19.  * each child is a domElement or a string
  20.  * property parent
  21.  * parent is a domElement
  22.  * method appendChild(domElement / string)
  23.  * appends to the end of children list
  24.  * method addAttribute(name, value)
  25.  * throw Error if type is not valid
  26.  * method removeAttribute(attribute)
  27.  * throw Error if attribute does not exist in the domElement
  28.  */
  29.  
  30.  
  31. /* Example
  32.  var meta = Object.create(domElement)
  33.  .init('meta')
  34.  .addAttribute('charset', 'utf-8');
  35.  var head = Object.create(domElement)
  36.  .init('head')
  37.  .appendChild(meta)
  38.  var div = Object.create(domElement)
  39.  .init('div')
  40.  .addAttribute('style', 'font-size: 42px');
  41.  div.content = 'Hello, world!';
  42.  var body = Object.create(domElement)
  43.  .init('body')
  44.  .appendChild(div)
  45.  .addAttribute('id', 'cuki')
  46.  .addAttribute('bgcolor', '#012345');
  47.  var root = Object.create(domElement)
  48.  .init('html')
  49.  .appendChild(head)
  50.  .appendChild(body);
  51.  console.log(root.innerHTML);
  52.  Outputs:
  53.  <html><head><meta charset="utf-8"></meta></head><body bgcolor="#012345" id="cuki"><div style="font-size: 42px">Hello, world!</div></body></html>
  54.  */
  55.  
  56.  
  57. function solve() {
  58.     var domElement = (function () {
  59.         function validateType(type) {
  60.             if (type === undefined) {
  61.                 return false;
  62.             } else if (!(/^[A-Z0-9a-z]+$/.test(type))) {
  63.                 return false;
  64.             } else if (typeof type !== 'string') {
  65.                 return false;
  66.             }
  67.             return true;
  68.         }
  69.  
  70.         function validateAttributeName(atrName) {
  71.             if (atrName === undefined) {
  72.                 return false;
  73.             } else if (!(/^[A-Z0-9a-z-]+$/.test(atrName))) {
  74.                 return false;
  75.             } else if (typeof atrName !== 'string') {
  76.                 return false;
  77.             }
  78.             return true;
  79.         }
  80.  
  81.         var domElement = {
  82.             init: function (type) {
  83.                 this.type = type;
  84.                 this.content = '';
  85.                 this.attributes = [];
  86.                 this.children = [];
  87.  
  88.                 return this;
  89.             },
  90.             appendChild: function (child) {
  91.                 if (child === null || arguments.length === 0) {
  92.                     throw 'null child or wrong number of args'
  93.                 }
  94.                 else if (typeof child === 'string') {
  95.                     this._children.push(child);
  96.                 }
  97.                 else {
  98.                     child.parent = this;
  99.                     this._children.push(child);
  100.                 }
  101.                 return this;
  102.  
  103.             },
  104.             addAttribute: function (name, value) {
  105.                 if (arguments.length != 2) {
  106.                     throw 'invalid number of args'
  107.                 }
  108.                 else if (value === null || name === null) {
  109.                     throw 'null value or name attribute'
  110.                 }
  111.                 else if (name.length === 0 || /[^a-zA-z0-9\-]/.test(name)) {
  112.                     throw 'empty string ot invalid input of name attr'
  113.                 }
  114.                 for(var i=0;i<this._attributes.length;i+=1){
  115.                     if(this._attributes[i].name===name){
  116.                         this._attributes[i].value=value;
  117.                         return this;
  118.                     }
  119.                 }
  120.                 this._attributes.push({name: name, value: value});
  121.                 return this;
  122.             },
  123.             removeAttribute: function (attribute) {
  124.                 var isExisting = false;
  125.                 for (var i = 0; i < this._attributes.length; i += 1) {
  126.                     if (this._attributes[i].name === attribute) {
  127.                         delete this._attributes.splice(i, 1);
  128.                         i -= 1;
  129.                         isExisting = true;
  130.                     }
  131.                 }
  132.                 if (!isExisting) {
  133.                     throw 'attr does not exist'
  134.                 }
  135.                 return this;
  136.             }
  137.         };
  138.  
  139.         Object.defineProperties(domElement, {
  140.             type: {
  141.                 get: function () {
  142.                     return this._type;
  143.                 },
  144.                 set: function (value) {
  145.                     if (!validateType(value)) {
  146.                         throw 'Error - invalid type!';
  147.                     }
  148.                     this._type = value;
  149.                 }
  150.             },
  151.             content: {
  152.                 get: function () {
  153.                     if (this.children.length > 0) {
  154.                         return '';
  155.                     }
  156.                     return this._content;
  157.                 },
  158.                 set: function (value) {
  159.                     if (typeof value !== "string") {
  160.                         throw 'Error - invalid type of content!';
  161.                     }
  162.                     this._content = value;
  163.                 }
  164.             },
  165.             attributes: {
  166.                 get: function () {
  167.                     return this._attributes;
  168.                 },
  169.                 set: function (value) {
  170.                     if (value === null) {
  171.                         throw 'null attribute'
  172.                     }
  173.                     value.some(function (value) {
  174.                         if (value.name.length === null || value.value.length === null) {
  175.                             throw 'null value or name of attribute'
  176.                         }
  177.                     });
  178.                     this._attributes = value;
  179.                 }
  180.             },
  181.             parent: {
  182.                 get: function () {
  183.                     return this._parent;
  184.                 },
  185.                 set: function (value) {
  186.                     if (!(Object.getPrototypeOf(this) === Object.getPrototypeOf(value))) {
  187.                         throw 'invalid parent type'
  188.                     }
  189.                     this._parent = value;
  190.                 }
  191.             },
  192.             children: {
  193.                 get: function () {
  194.                     return this._children;
  195.                 },
  196.                 set: function (value) {
  197.                     if (value === null) {
  198.                         throw 'Error - null children!';
  199.                     }
  200.                     this._children = value;
  201.                 }
  202.             },
  203.             innerHTML: {
  204.                 get: function () {
  205.                     var final = '';
  206.                     final += '<' + this.type;
  207.  
  208.                     var sorted = this.attributes.sort();
  209.                     for (var item in sorted) {
  210.                         final += ' ' + sorted[item].name + '="' + sorted[item].value + '"';
  211.                     }
  212.                     final += '>';
  213.  
  214.                     if (this.content) {
  215.                         final += this.content;
  216.                     }
  217.                     for (var i = 0; i < this.children.length; i += 1) {
  218.                         if (typeof this.children[i] === 'string') {
  219.                             final += this.children[i];
  220.                         }
  221.                         else {
  222.                             final += this.children[i].innerHTML;
  223.                         }
  224.                     }
  225.                     final += '</' + this.type + '>';
  226.  
  227.                     return final;
  228.                 }
  229.             }
  230.         });
  231.         return domElement;
  232.     }());
  233.     return domElement;
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement