html2incdom-bruno
Version:
Converts html strings into incremental dom calls
99 lines (79 loc) • 2.99 kB
JavaScript
;
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 _unescape = require('./unescape');
var _unescape2 = _interopRequireDefault(_unescape);
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 parser_;
var HTML2IncDom = function () {
function HTML2IncDom() {
_classCallCheck(this, HTML2IncDom);
}
_createClass(HTML2IncDom, null, [{
key: 'buildFn',
/**
* Should convert the given html string to a function with calls to
* incremental dom methods.
* @param {string} html
* @return {!function()} Function with incremental dom calls for building
* the given html string.
*/
value: function buildFn(html) {
return function () {
return HTML2IncDom.run(html);
};
}
/**
* Gets the html parser being currently used.
* @return {!function()}
*/
}, {
key: 'getParser',
value: function getParser() {
return parser_ || window.HTMLParser;
}
/**
* Should convert the given html string to calls to incremental dom methods.
* @param {string} html
*/
}, {
key: 'run',
value: function run(html) {
HTML2IncDom.getParser()(html, {
start: function start(tag, attrs, unary) {
var fn = unary ? IncrementalDOM.elementVoid : IncrementalDOM.elementOpen;
var args = [tag, null, []];
for (var i = 0; i < attrs.length; i++) {
args.push(attrs[i].name, attrs[i].value);
}
fn.apply(null, args);
},
end: function end(tag) {
IncrementalDOM.elementClose(tag);
},
chars: function chars(text) {
IncrementalDOM.text(text, _unescape2.default);
}
});
}
/**
* Changes the function that will be used to parse html strings. By default
* this will use the `HTMLParser` function from
* https://github.com/blowsie/Pure-JavaScript-HTML5-Parser. This will accept
* any function that follows that same api, basically accepting the html
* string and an object with `start`, `end` and `chars` functions to be called
* during the parsing.
* @param {!function(string, !Object} newParser
*/
}, {
key: 'setParser',
value: function setParser(newParser) {
parser_ = newParser;
}
}]);
return HTML2IncDom;
}();
exports.default = HTML2IncDom;