RulingSystem.js 6.4 KB

1
  1. var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;var _toConsumableArray2=_interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));var _defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));var _classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));var _createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass"));var _possibleConstructorReturn2=_interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));var _getPrototypeOf2=_interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));var _inherits2=_interopRequireDefault(require("@babel/runtime/helpers/inherits"));var _react=_interopRequireDefault(require("react"));var _graph=require("../helpers/graph");var _tree=require("../helpers/tree");var _ViewSystem=_interopRequireDefault(require("./ViewSystem"));var _System=_interopRequireDefault(require("./System"));var _css=_interopRequireDefault(require("css"));function ownKeys(object,enumerableOnly){var keys=Object.keys(object);if(Object.getOwnPropertySymbols){var symbols=Object.getOwnPropertySymbols(object);if(enumerableOnly)symbols=symbols.filter(function(sym){return Object.getOwnPropertyDescriptor(object,sym).enumerable;});keys.push.apply(keys,symbols);}return keys;}function _objectSpread(target){for(var i=1;i<arguments.length;i++){var source=arguments[i]!=null?arguments[i]:{};if(i%2){ownKeys(source,true).forEach(function(key){(0,_defineProperty2.default)(target,key,source[key]);});}else if(Object.getOwnPropertyDescriptors){Object.defineProperties(target,Object.getOwnPropertyDescriptors(source));}else{ownKeys(source).forEach(function(key){Object.defineProperty(target,key,Object.getOwnPropertyDescriptor(source,key));});}}return target;}function log(){var _console;(_console=console).log.apply(_console,arguments);}var MATCH_NODE=/ *([a-zA-Z-_*]+) */gm;var MATCH_CHILD=/ *> */gm;var MATCH_SPACES=/ +/gm;var MATCH_PLUS=/ *\+ */gm;var MATCH_MINUS=/ *\- */gm;var RulingSystem=function(_BaseSystem){(0,_inherits2.default)(RulingSystem,_BaseSystem);function RulingSystem(){var _this;(0,_classCallCheck2.default)(this,RulingSystem);_this=(0,_possibleConstructorReturn2.default)(this,(0,_getPrototypeOf2.default)(RulingSystem).call(this));_this.rules=[];_this.index={};return _this;}(0,_createClass2.default)(RulingSystem,[{key:"getDeclarations",value:function getDeclarations(rule){var res=rule.declarations.reduce(function(acc,curr){return _objectSpread({},acc,(0,_defineProperty2.default)({},curr.property,curr.value));},{});return res;}},{key:"getCSS",value:function getCSS(Node,View){var _this2=this;var _this$match=this.match(Node,View),rules=_this$match.rules,contentrules=_this$match.contentrules;var css=rules.map(function(r){return _this2.getDeclarations(r);});var main=css.reduce(function(acc,curr){return _objectSpread({},acc,{},curr);},{});var contentcss=contentrules.map(function(r){return _this2.getDeclarations(r);});var content=contentcss.reduce(function(acc,curr){return _objectSpread({},acc,{},curr);},{});return{rules:main,contentrules:content};}},{key:"match",value:function match(Node,View){var rules=[];if(!Node.content)return[];var array=[].concat((0,_toConsumableArray2.default)(this.index['*']||[]),(0,_toConsumableArray2.default)(this.index[Node.content.value]||[]));for(var i in array){var rule=array[i];if(this.matchRule(rule,Node,View)){rules.push(rule);}}var contentrules=[];var contentarray=[].concat((0,_toConsumableArray2.default)(this.index['~*']||[]),(0,_toConsumableArray2.default)(this.index['~'+Node.content.value]||[]));for(var i in contentarray){var _rule=contentarray[i];if(this.matchRule(_rule,Node,View,"~")){contentrules.push(_rule);}}return{rules:rules,contentrules:contentrules};}},{key:"matchRule",value:function matchRule(rule,Node,View,prefix){var selectors=rule.selectors,declarations=rule.declarations;for(var i in selectors){var selector=selectors[i];if(this.matchSelector(selector,Node,View,prefix)){return true;}}return false;}},{key:"matchSelector",value:function matchSelector(selector,Node,View){var prefix=arguments.length>3&&arguments[3]!==undefined?arguments[3]:"";if(Node.depth+1<selector.length)return false;for(var i=selector.length-1;i>=0;i--){var ViewNode=Node.content;var _selector$i=selector[i],nodes=_selector$i.nodes,prenodes=_selector$i.prenodes,nextnodes=_selector$i.nextnodes;if(View.isRoot(Node)&&nodes.includes('ROOT'))return true;if(!Node.content)return false;if(!nodes.includes(prefix+ViewNode.value)&&!nodes.includes(prefix+'*')){return false;}prefix="";if(prenodes.length>0){var preNode=View.getBefore(Node);var preNodeValue=preNode&&preNode.content&&preNode.content.value;if(preNodeValue&&!prenodes.includes(preNodeValue)){return false;}}if(nextnodes.length>0){var afterNode=View.getAfter(Node);var afterNodeValue=afterNode&&afterNode.content&&afterNode.content.value;if(afterNodeValue&&!nextnodes.includes(afterNodeValue)){return false;}}Node=View.getParent(Node);}return true;}},{key:"parseSelector",value:function parseSelector(selector){var string=selector;var tree=string.split(MATCH_CHILD);for(var i in tree){var minus=tree[i].split(MATCH_MINUS);if(minus.length===1){minus[1]=minus[0];minus[0]="";}var plus=minus[1].split(MATCH_PLUS);if(plus.length===1){plus[1]="";}var nodes=plus[0].split(MATCH_SPACES).filter(Boolean);var prenodes=minus[0].split(MATCH_SPACES).filter(Boolean);var nextnodes=plus[1].split(MATCH_SPACES).filter(Boolean);tree[i]={nodes:nodes,prenodes:prenodes,nextnodes:nextnodes,initial:tree[i]};}return tree;}},{key:"addRule",value:function addRule(rule){var _this3=this;var selectors=rule.selectors,declarations=rule.declarations;var parsed=selectors.map(function(s){return _this3.parseSelector(s);});var res={selectors:parsed,declarations:declarations};this.rules.push(res);parsed.forEach(function(selectorGroup){selectorGroup.forEach(function(selector){selector.nodes.forEach(function(node){if(!_this3.index[node])_this3.index[node]=[];_this3.index[node].push(res);});});});}},{key:"addRuleFile",value:function addRuleFile(file){var ruleCSS=_css.default.parse(file);console.log("RULECSS",ruleCSS);for(var i in ruleCSS.stylesheet.rules){this.addRule(ruleCSS.stylesheet.rules[i]);}this.forceUpdate();}},{key:"export",value:function _export(){return{};}},{key:"import",value:function _import(data){return this;}}]);return RulingSystem;}(_System.default);exports.default=RulingSystem;