UNPKG

iab-vast-parser

Version:

Parses IAB VAST tags into iab-vast-model objects.

151 lines (135 loc) 5.22 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _dom = require('./dom'); var _convertors = require('./convertors'); var _convertors2 = _interopRequireDefault(_convertors); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var toProperty = function () { var re = /^[A-Z]+/; var insert = function insert(m) { return m.length === 1 ? m.toLowerCase() : m.substr(0, m.length - 1).toLowerCase() + m.charAt(m.length - 1); }; return function (str) { return str.replace(re, insert); }; }(); var hasPair = function hasPair(map, parentName, childName) { return map[parentName] != null && ~map[parentName].indexOf(childName); }; var Unmarshaler = function () { function Unmarshaler(_ref) { var collections = _ref.collections, freeforms = _ref.freeforms, types = _ref.types; _classCallCheck(this, Unmarshaler); this._collections = collections; this._freeforms = freeforms; this._types = types; } _createClass(Unmarshaler, [{ key: 'unmarshal', value: function unmarshal(xml) { return this._createNode(xml); } }, { key: '_createNode', value: function _createNode(xml) { var node = Object.create(null); node._value = this._convertPropertyValue((0, _dom.getText)(xml), xml.nodeName, '_value'); this._copyAttributes(node, xml); this._createCollections(node, xml); this._createChildren(node, xml); return node; } }, { key: '_createFreeformNode', value: function _createFreeformNode(xml) { var node = Object.create(null); this._copyAttributes(node, xml); node._value = xml; return node; } }, { key: '_copyAttributes', value: function _copyAttributes(node, xml) { var parentName = xml.nodeName; for (var i = 0; i < xml.attributes.length; ++i) { var attrNode = xml.attributes[i]; var name = attrNode.nodeName; var rawValue = attrNode.nodeValue; node[name] = this._convertPropertyValue(rawValue, parentName, name); } } }, { key: '_createCollections', value: function _createCollections(node, xml) { if (this._collections[xml.nodeName] != null) { this._collections[xml.nodeName].forEach(function (childName) { node[toProperty(childName)] = []; }); } } }, { key: '_createChildren', value: function _createChildren(node, xml) { var _this = this; var parentName = xml.nodeName; (0, _dom.getChildren)(xml, _dom.isElement).forEach(function (child) { var childName = child.nodeName; var prop = toProperty(childName); if (_this._isFreeformParent(parentName)) { if (_this._isFreeformChild(parentName, childName)) { _this._addFreeformChild(child, prop, node); } } else { _this._addNodeChild(child, prop, node, parentName, childName); } }); } }, { key: '_addNodeChild', value: function _addNodeChild(child, prop, parentNode, parentName, childName) { var childNode = this._createNode(child); if (parentNode[prop] == null) { parentNode[prop] = childNode; } else if (this._isCollection(parentName, childName)) { parentNode[prop].push(childNode); } else { throw new Error('Multiple values for ' + parentName + '.' + childName); } } }, { key: '_addFreeformChild', value: function _addFreeformChild(child, prop, parentNode) { parentNode[prop] = parentNode[prop] || []; parentNode[prop].push(this._createFreeformNode(child)); } }, { key: '_convertPropertyValue', value: function _convertPropertyValue(value, parentName, property) { var typeId = this._types[parentName] != null ? this._types[parentName][property] : null; return typeId != null ? _convertors2.default[typeId](value) : value; } }, { key: '_isCollection', value: function _isCollection(parentName, childName) { return hasPair(this._collections, parentName, childName); } }, { key: '_isFreeformParent', value: function _isFreeformParent(name) { return this._freeforms[name] != null; } }, { key: '_isFreeformChild', value: function _isFreeformChild(parentName, childName) { return hasPair(this._freeforms, parentName, childName); } }]); return Unmarshaler; }(); exports.default = Unmarshaler;