UNPKG

react-native-svg

Version:
2 lines 16.8 kB
"use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.SvgCss=SvgCss;exports.SvgCssUri=SvgCssUri;exports.inlineStyles=exports.SvgWithCssUri=exports.SvgWithCss=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));var _createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass"));var _assertThisInitialized2=_interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));var _inherits2=_interopRequireDefault(require("@babel/runtime/helpers/inherits"));var _possibleConstructorReturn2=_interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));var _getPrototypeOf2=_interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));var _defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));var _slicedToArray2=_interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));var _react=_interopRequireWildcard(require("react"));var _xml=require("./xml");var _cssTree=_interopRequireWildcard(require("css-tree"));var _cssSelect=_interopRequireDefault(require("css-select"));var _jsxFileName="/Users/wojciechlewicki/work/deeper/react-native-svg/src/css.tsx";function _getRequireWildcardCache(nodeInterop){if(typeof WeakMap!=="function")return null;var cacheBabelInterop=new WeakMap();var cacheNodeInterop=new WeakMap();return(_getRequireWildcardCache=function _getRequireWildcardCache(nodeInterop){return nodeInterop?cacheNodeInterop:cacheBabelInterop;})(nodeInterop);}function _interopRequireWildcard(obj,nodeInterop){if(!nodeInterop&&obj&&obj.__esModule){return obj;}if(obj===null||typeof obj!=="object"&&typeof obj!=="function"){return{default:obj};}var cache=_getRequireWildcardCache(nodeInterop);if(cache&&cache.has(obj)){return cache.get(obj);}var newObj={};var hasPropertyDescriptor=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var key in obj){if(key!=="default"&&Object.prototype.hasOwnProperty.call(obj,key)){var desc=hasPropertyDescriptor?Object.getOwnPropertyDescriptor(obj,key):null;if(desc&&(desc.get||desc.set)){Object.defineProperty(newObj,key,desc);}else{newObj[key]=obj[key];}}}newObj.default=obj;if(cache){cache.set(obj,newObj);}return newObj;}function _createSuper(Derived){var hasNativeReflectConstruct=_isNativeReflectConstruct();return function _createSuperInternal(){var Super=(0,_getPrototypeOf2.default)(Derived),result;if(hasNativeReflectConstruct){var NewTarget=(0,_getPrototypeOf2.default)(this).constructor;result=Reflect.construct(Super,arguments,NewTarget);}else{result=Super.apply(this,arguments);}return(0,_possibleConstructorReturn2.default)(this,result);};}function _isNativeReflectConstruct(){if(typeof Reflect==="undefined"||!Reflect.construct)return false;if(Reflect.construct.sham)return false;if(typeof Proxy==="function")return true;try{Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}));return true;}catch(e){return false;}}function _createForOfIteratorHelperLoose(o,allowArrayLike){var it=typeof Symbol!=="undefined"&&o[Symbol.iterator]||o["@@iterator"];if(it)return(it=it.call(o)).next.bind(it);if(Array.isArray(o)||(it=_unsupportedIterableToArray(o))||allowArrayLike&&o&&typeof o.length==="number"){if(it)o=it;var i=0;return function(){if(i>=o.length)return{done:true};return{done:false,value:o[i++]};};}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");}function _unsupportedIterableToArray(o,minLen){if(!o)return;if(typeof o==="string")return _arrayLikeToArray(o,minLen);var n=Object.prototype.toString.call(o).slice(8,-1);if(n==="Object"&&o.constructor)n=o.constructor.name;if(n==="Map"||n==="Set")return Array.from(o);if(n==="Arguments"||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return _arrayLikeToArray(o,minLen);}function _arrayLikeToArray(arr,len){if(len==null||len>arr.length)len=arr.length;for(var i=0,arr2=new Array(len);i<len;i++){arr2[i]=arr[i];}return arr2;}function isTag(node){return typeof node==='object';}function getParent(node){return typeof node==='object'&&node.parent||null;}function getChildren(node){return typeof node==='object'&&node.children||[];}function getName(elem){return elem.tag;}function getText(_node){return'';}function getAttributeValue(elem,name){return elem.props[name]||null;}function removeSubsets(nodes){var idx=nodes.length,node,ancestor,replace;while(--idx>-1){node=ancestor=nodes[idx];delete nodes[idx];replace=true;while(ancestor){if(nodes.includes(ancestor)){replace=false;nodes.splice(idx,1);break;}ancestor=typeof ancestor==='object'&&ancestor.parent||null;}if(replace){nodes[idx]=node;}}return nodes;}function existsOne(predicate,elems){return elems.some(function(elem){return typeof elem==='object'&&(predicate(elem)||existsOne(predicate,elem.children));});}function getSiblings(node){var parent=typeof node==='object'&&node.parent;return parent&&parent.children||[];}function hasAttrib(elem,name){return elem.props.hasOwnProperty(name);}function findOne(predicate,elems){var elem=null;for(var i=0,l=elems.length;i<l&&!elem;i++){var node=elems[i];if(typeof node==='string'){}else if(predicate(node)){elem=node;}else{var children=node.children;if(children.length!==0){elem=findOne(predicate,children);}}}return elem;}function findAll(predicate,nodes){var result=arguments.length>2&&arguments[2]!==undefined?arguments[2]:[];for(var i=0,j=nodes.length;i<j;i++){var node=nodes[i];if(typeof node!=='object'){continue;}if(predicate(node)){result.push(node);}var children=node.children;if(children.length!==0){findAll(predicate,children,result);}}return result;}var cssSelectOpts={xmlMode:true,adapter:{removeSubsets:removeSubsets,existsOne:existsOne,getSiblings:getSiblings,hasAttrib:hasAttrib,findOne:findOne,findAll:findAll,isTag:isTag,getParent:getParent,getChildren:getChildren,getName:getName,getText:getText,getAttributeValue:getAttributeValue}};function flattenToSelectors(cssAst,selectors){_cssTree.default.walk(cssAst,{visit:'Rule',enter:function enter(rule){var _ref=rule,type=_ref.type,prelude=_ref.prelude;if(type!=='Rule'){return;}var atrule=this.atrule;prelude.children.each(function(node,item){var _ref2=node,children=_ref2.children;var pseudos=[];selectors.push({item:item,atrule:atrule,rule:rule,pseudos:pseudos});children.each(function(_ref3,pseudoItem,list){var childType=_ref3.type;if(childType==='PseudoClassSelector'||childType==='PseudoElementSelector'){pseudos.push({item:pseudoItem,list:list});}});});}});}function filterByMqs(selectors){return selectors.filter(function(_ref4){var atrule=_ref4.atrule;if(atrule===null){return true;}var name=atrule.name,prelude=atrule.prelude;var atPrelude=prelude;var first=atPrelude&&atPrelude.children.first();var mq=first&&first.type==='MediaQueryList';var query=mq?_cssTree.default.generate(atPrelude):name;return useMqs.includes(query);});}var useMqs=['','screen'];function filterByPseudos(selectors){return selectors.filter(function(_ref5){var pseudos=_ref5.pseudos;return usePseudos.includes(_cssTree.default.generate({type:'Selector',children:new _cssTree.List().fromArray(pseudos.map(function(pseudo){return pseudo.item.data;}))}));});}var usePseudos=[''];function cleanPseudos(selectors){selectors.forEach(function(_ref6){var pseudos=_ref6.pseudos;return pseudos.forEach(function(pseudo){return pseudo.list.remove(pseudo.item);});});}function specificity(selector){var A=0;var B=0;var C=0;selector.children.each(function walk(node){switch(node.type){case'SelectorList':case'Selector':node.children.each(walk);break;case'IdSelector':A++;break;case'ClassSelector':case'AttributeSelector':B++;break;case'PseudoClassSelector':switch(node.name.toLowerCase()){case'not':var children=node.children;children&&children.each(walk);break;case'before':case'after':case'first-line':case'first-letter':C++;break;default:B++;}break;case'PseudoElementSelector':C++;break;case'TypeSelector':var name=node.name;if(name.charAt(name.length-1)!=='*'){C++;}break;}});return[A,B,C];}function compareSpecificity(aSpecificity,bSpecificity){for(var i=0;i<4;i+=1){if(aSpecificity[i]<bSpecificity[i]){return-1;}else if(aSpecificity[i]>bSpecificity[i]){return 1;}}return 0;}function selectorWithSpecificity(selector){return{selector:selector,specificity:specificity(selector.item.data)};}function bySelectorSpecificity(a,b){return compareSpecificity(a.specificity,b.specificity);}function pass(arr,len,chk,result){var dbl=chk*2;var l,r,e;var li,ri;var i=0;for(l=0;l<len;l+=dbl){r=l+chk;e=r+chk;if(r>len){r=len;}if(e>len){e=len;}li=l;ri=r;while(true){if(li<r&&ri<e){if(bySelectorSpecificity(arr[li],arr[ri])<=0){result[i++]=arr[li++];}else{result[i++]=arr[ri++];}}else if(li<r){result[i++]=arr[li++];}else if(ri<e){result[i++]=arr[ri++];}else{break;}}}}function exec(arr,len){var buffer=new Array(len);for(var chk=1;chk<len;chk*=2){pass(arr,len,chk,buffer);var tmp=arr;arr=buffer;buffer=tmp;}return arr;}function sortSelectors(selectors){var len=selectors.length;if(len<=1){return selectors;}var specs=selectors.map(selectorWithSpecificity);return exec(specs,len).map(function(s){return s.selector;});}var declarationParseProps={context:'declarationList',parseValue:false};function CSSStyleDeclaration(ast){var props=ast.props,styles=ast.styles;if(!props.style){props.style={};}var style=props.style;var priority=new Map();ast.style=style;ast.priority=priority;if(!styles||styles.length===0){return;}try{var declarations=_cssTree.default.parse(styles,declarationParseProps);declarations.children.each(function(node){try{var _ref7=node,property=_ref7.property,value=_ref7.value,important=_ref7.important;var name=property.trim();priority.set(name,important);style[(0,_xml.camelCase)(name)]=_cssTree.default.generate(value).trim();}catch(styleError){if(styleError instanceof Error&&styleError.message!=='Unknown node type: undefined'){console.warn("Warning: Parse error when parsing inline styles, style properties of this element cannot be used. The raw styles can still be get/set using .attr('style').value. Error details: "+styleError);}}});}catch(parseError){console.warn("Warning: Parse error when parsing inline styles, style properties of this element cannot be used. The raw styles can still be get/set using .attr('style').value. Error details: "+parseError);}}function initStyle(selectedEl){if(!selectedEl.style){CSSStyleDeclaration(selectedEl);}return selectedEl;}function closestElem(node,elemName){var elem=node;while((elem=elem.parent)&&elem.tag!==elemName){}return elem;}var parseProps={parseValue:false,parseCustomProperty:false};var inlineStyles=function inlineStyles(document){var styleElements=(0,_cssSelect.default)('style',document,cssSelectOpts);if(styleElements.length===0){return document;}var selectors=[];for(var _iterator=_createForOfIteratorHelperLoose(styleElements),_step;!(_step=_iterator()).done;){var element=_step.value;var children=element.children;if(!children.length||closestElem(element,'foreignObject')){continue;}try{var styleString=children.join('');flattenToSelectors(_cssTree.default.parse(styleString,parseProps),selectors);}catch(parseError){console.warn('Warning: Parse error of styles of <style/> element, skipped. Error details: '+parseError);}}var selectorsMq=filterByMqs(selectors);var selectorsPseudo=filterByPseudos(selectorsMq);cleanPseudos(selectorsPseudo);var sortedSelectors=sortSelectors(selectorsPseudo).reverse();for(var _iterator2=_createForOfIteratorHelperLoose(sortedSelectors),_step2;!(_step2=_iterator2()).done;){var _ref8=_step2.value;var rule=_ref8.rule;var item=_ref8.item;if(rule===null){continue;}var selectorStr=_cssTree.default.generate(item.data);try{var _ret=function(){var matched=(0,_cssSelect.default)(selectorStr,document,cssSelectOpts).map(initStyle);if(matched.length===0){return"continue";}_cssTree.default.walk(rule,{visit:'Declaration',enter:function enter(node){var _ref9=node,property=_ref9.property,value=_ref9.value,important=_ref9.important;var name=property.trim();var camel=(0,_xml.camelCase)(name);var val=_cssTree.default.generate(value).trim();for(var _iterator3=_createForOfIteratorHelperLoose(matched),_step3;!(_step3=_iterator3()).done;){var _element=_step3.value;var style=_element.style,priority=_element.priority;var current=priority.get(name);if(current===undefined||current<important){priority.set(name,important);style[camel]=val;}}}});}();if(_ret==="continue")continue;}catch(selectError){if(selectError instanceof SyntaxError){console.warn('Warning: Syntax error when trying to select \n\n'+selectorStr+'\n\n, skipped. Error details: '+selectError);continue;}throw selectError;}}return document;};exports.inlineStyles=inlineStyles;function SvgCss(props){var xml=props.xml,override=props.override;var ast=(0,_react.useMemo)(function(){return xml!==null?(0,_xml.parse)(xml,inlineStyles):null;},[xml]);return _react.default.createElement(_xml.SvgAst,{ast:ast,override:override||props,__self:this,__source:{fileName:_jsxFileName,lineNumber:694,columnNumber:10}});}function SvgCssUri(props){var uri=props.uri,_props$onError=props.onError,onError=_props$onError===void 0?_xml.err:_props$onError;var _useState=(0,_react.useState)(null),_useState2=(0,_slicedToArray2.default)(_useState,2),xml=_useState2[0],setXml=_useState2[1];(0,_react.useEffect)(function(){uri?(0,_xml.fetchText)(uri).then(setXml).catch(onError):setXml(null);},[onError,uri]);return _react.default.createElement(SvgCss,{xml:xml,override:props,__self:this,__source:{fileName:_jsxFileName,lineNumber:703,columnNumber:10}});}var SvgWithCss=function(_Component){(0,_inherits2.default)(SvgWithCss,_Component);var _super=_createSuper(SvgWithCss);function SvgWithCss(){var _this;(0,_classCallCheck2.default)(this,SvgWithCss);for(var _len=arguments.length,args=new Array(_len),_key=0;_key<_len;_key++){args[_key]=arguments[_key];}_this=_super.call.apply(_super,[this].concat(args));(0,_defineProperty2.default)((0,_assertThisInitialized2.default)(_this),"state",{ast:null});return _this;}(0,_createClass2.default)(SvgWithCss,[{key:"componentDidMount",value:function componentDidMount(){this.parse(this.props.xml);}},{key:"componentDidUpdate",value:function componentDidUpdate(prevProps){var xml=this.props.xml;if(xml!==prevProps.xml){this.parse(xml);}}},{key:"parse",value:function parse(xml){try{this.setState({ast:xml?(0,_xml.parse)(xml,inlineStyles):null});}catch(e){this.props.onError?this.props.onError(e):console.error(e);}}},{key:"render",value:function render(){var props=this.props,ast=this.state.ast;return _react.default.createElement(_xml.SvgAst,{ast:ast,override:props.override||props,__self:this,__source:{fileName:_jsxFileName,lineNumber:731,columnNumber:12}});}}]);return SvgWithCss;}(_react.Component);exports.SvgWithCss=SvgWithCss;var SvgWithCssUri=function(_Component2){(0,_inherits2.default)(SvgWithCssUri,_Component2);var _super2=_createSuper(SvgWithCssUri);function SvgWithCssUri(){var _this2;(0,_classCallCheck2.default)(this,SvgWithCssUri);for(var _len2=arguments.length,args=new Array(_len2),_key2=0;_key2<_len2;_key2++){args[_key2]=arguments[_key2];}_this2=_super2.call.apply(_super2,[this].concat(args));(0,_defineProperty2.default)((0,_assertThisInitialized2.default)(_this2),"state",{xml:null});return _this2;}(0,_createClass2.default)(SvgWithCssUri,[{key:"componentDidMount",value:function componentDidMount(){this.fetch(this.props.uri);}},{key:"componentDidUpdate",value:function componentDidUpdate(prevProps){var uri=this.props.uri;if(uri!==prevProps.uri){this.fetch(uri);}}},{key:"fetch",value:function fetch(uri){return _regenerator.default.async(function fetch$(_context){while(1){switch(_context.prev=_context.next){case 0:_context.prev=0;_context.t0=this;if(!uri){_context.next=8;break;}_context.next=5;return _regenerator.default.awrap((0,_xml.fetchText)(uri));case 5:_context.t1=_context.sent;_context.next=9;break;case 8:_context.t1=null;case 9:_context.t2=_context.t1;_context.t3={xml:_context.t2};_context.t0.setState.call(_context.t0,_context.t3);_context.next=17;break;case 14:_context.prev=14;_context.t4=_context["catch"](0);this.props.onError?this.props.onError(_context.t4):console.error(_context.t4);case 17:case"end":return _context.stop();}}},null,this,[[0,14]],Promise);}},{key:"render",value:function render(){var props=this.props,xml=this.state.xml;return _react.default.createElement(SvgWithCss,{xml:xml,override:props,__self:this,__source:{fileName:_jsxFileName,lineNumber:758,columnNumber:12}});}}]);return SvgWithCssUri;}(_react.Component);exports.SvgWithCssUri=SvgWithCssUri; //# sourceMappingURL=css.js.map