Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Task Description */
- /*
- * Create an object domElement, that has the following properties and methods:
- * use prototypal inheritance, without function constructors
- * method init() that gets the domElement type
- * i.e. `Object.create(domElement).init('div')`
- * property type that is the type of the domElement
- * a valid type is any non-empty string that contains only Latin letters and digits
- * property innerHTML of type string
- * gets the domElement, parsed as valid HTML
- * <type attr1="value1" attr2="value2" ...> .. content / children's.innerHTML .. </type>
- * property content of type string
- * sets the content of the element
- * works only if there are no children
- * property attributes
- * each attribute has name and value
- * a valid attribute has a non-empty string for a name that contains only Latin letters and digits or dashes (-)
- * property children
- * each child is a domElement or a string
- * property parent
- * parent is a domElement
- * method appendChild(domElement / string)
- * appends to the end of children list
- * method addAttribute(name, value)
- * throw Error if type is not valid
- * method removeAttribute(attribute)
- * throw Error if attribute does not exist in the domElement
- */
- /* Example
- var meta = Object.create(domElement)
- .init('meta')
- .addAttribute('charset', 'utf-8');
- var head = Object.create(domElement)
- .init('head')
- .appendChild(meta)
- var div = Object.create(domElement)
- .init('div')
- .addAttribute('style', 'font-size: 42px');
- div.content = 'Hello, world!';
- var body = Object.create(domElement)
- .init('body')
- .appendChild(div)
- .addAttribute('id', 'cuki')
- .addAttribute('bgcolor', '#012345');
- var root = Object.create(domElement)
- .init('html')
- .appendChild(head)
- .appendChild(body);
- console.log(root.innerHTML);
- Outputs:
- <html><head><meta charset="utf-8"></meta></head><body bgcolor="#012345" id="cuki"><div style="font-size: 42px">Hello, world!</div></body></html>
- */
- function solve() {
- var domElement = (function () {
- function validateType(type) {
- if (type === undefined) {
- return false;
- } else if (!(/^[A-Z0-9a-z]+$/.test(type))) {
- return false;
- } else if (typeof type !== 'string') {
- return false;
- }
- return true;
- }
- function validateAttributeName(atrName) {
- if (atrName === undefined) {
- return false;
- } else if (!(/^[A-Z0-9a-z-]+$/.test(atrName))) {
- return false;
- } else if (typeof atrName !== 'string') {
- return false;
- }
- return true;
- }
- var domElement = {
- init: function (type) {
- this.type = type;
- this.content = '';
- this.attributes = [];
- this.children = [];
- return this;
- },
- appendChild: function (child) {
- if (child === null || arguments.length === 0) {
- throw 'null child or wrong number of args'
- }
- else if (typeof child === 'string') {
- this._children.push(child);
- }
- else {
- child.parent = this;
- this._children.push(child);
- }
- return this;
- },
- addAttribute: function (name, value) {
- if (arguments.length != 2) {
- throw 'invalid number of args'
- }
- else if (value === null || name === null) {
- throw 'null value or name attribute'
- }
- else if (name.length === 0 || /[^a-zA-z0-9\-]/.test(name)) {
- throw 'empty string ot invalid input of name attr'
- }
- for(var i=0;i<this._attributes.length;i+=1){
- if(this._attributes[i].name===name){
- this._attributes[i].value=value;
- return this;
- }
- }
- this._attributes.push({name: name, value: value});
- return this;
- },
- removeAttribute: function (attribute) {
- var isExisting = false;
- for (var i = 0; i < this._attributes.length; i += 1) {
- if (this._attributes[i].name === attribute) {
- delete this._attributes.splice(i, 1);
- i -= 1;
- isExisting = true;
- }
- }
- if (!isExisting) {
- throw 'attr does not exist'
- }
- return this;
- }
- };
- Object.defineProperties(domElement, {
- type: {
- get: function () {
- return this._type;
- },
- set: function (value) {
- if (!validateType(value)) {
- throw 'Error - invalid type!';
- }
- this._type = value;
- }
- },
- content: {
- get: function () {
- if (this.children.length > 0) {
- return '';
- }
- return this._content;
- },
- set: function (value) {
- if (typeof value !== "string") {
- throw 'Error - invalid type of content!';
- }
- this._content = value;
- }
- },
- attributes: {
- get: function () {
- return this._attributes;
- },
- set: function (value) {
- if (value === null) {
- throw 'null attribute'
- }
- value.some(function (value) {
- if (value.name.length === null || value.value.length === null) {
- throw 'null value or name of attribute'
- }
- });
- this._attributes = value;
- }
- },
- parent: {
- get: function () {
- return this._parent;
- },
- set: function (value) {
- if (!(Object.getPrototypeOf(this) === Object.getPrototypeOf(value))) {
- throw 'invalid parent type'
- }
- this._parent = value;
- }
- },
- children: {
- get: function () {
- return this._children;
- },
- set: function (value) {
- if (value === null) {
- throw 'Error - null children!';
- }
- this._children = value;
- }
- },
- innerHTML: {
- get: function () {
- var final = '';
- final += '<' + this.type;
- var sorted = this.attributes.sort();
- for (var item in sorted) {
- final += ' ' + sorted[item].name + '="' + sorted[item].value + '"';
- }
- final += '>';
- if (this.content) {
- final += this.content;
- }
- for (var i = 0; i < this.children.length; i += 1) {
- if (typeof this.children[i] === 'string') {
- final += this.children[i];
- }
- else {
- final += this.children[i].innerHTML;
- }
- }
- final += '</' + this.type + '>';
- return final;
- }
- }
- });
- return domElement;
- }());
- return domElement;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement