index.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. module.exports = (function() {
  2. var __MODS__ = {};
  3. var __DEFINE__ = function(modId, func, req) { var m = { exports: {}, _tempexports: {} }; __MODS__[modId] = { status: 0, func: func, req: req, m: m }; };
  4. var __REQUIRE__ = function(modId, source) { if(!__MODS__[modId]) return require(source); if(!__MODS__[modId].status) { var m = __MODS__[modId].m; m._exports = m._tempexports; var desp = Object.getOwnPropertyDescriptor(m, "exports"); if (desp && desp.configurable) Object.defineProperty(m, "exports", { set: function (val) { if(typeof val === "object" && val !== m._exports) { m._exports.__proto__ = val.__proto__; Object.keys(val).forEach(function (k) { m._exports[k] = val[k]; }); } m._tempexports = val }, get: function () { return m._tempexports; } }); __MODS__[modId].status = 1; __MODS__[modId].func(__MODS__[modId].req, m, m.exports); } return __MODS__[modId].m.exports; };
  5. var __REQUIRE_WILDCARD__ = function(obj) { if(obj && obj.__esModule) { return obj; } else { var newObj = {}; if(obj != null) { for(var k in obj) { if (Object.prototype.hasOwnProperty.call(obj, k)) newObj[k] = obj[k]; } } newObj.default = obj; return newObj; } };
  6. var __REQUIRE_DEFAULT__ = function(obj) { return obj && obj.__esModule ? obj.default : obj; };
  7. __DEFINE__(1633751990329, function(require, module, exports) {
  8. var DomParser = require('./lib/DomParser');
  9. module.exports = DomParser;
  10. }, function(modId) {var map = {"./lib/DomParser":1633751990330}; return __REQUIRE__(map[modId], modId); })
  11. __DEFINE__(1633751990330, function(require, module, exports) {
  12. var Dom = require('./Dom');
  13. function DomParser() {
  14. }
  15. DomParser.prototype.parseFromString = function (html) {
  16. return new Dom(html);
  17. };
  18. module.exports = DomParser;
  19. }, function(modId) { var map = {"./Dom":1633751990331}; return __REQUIRE__(map[modId], modId); })
  20. __DEFINE__(1633751990331, function(require, module, exports) {
  21. var
  22. tagRegExp = /(<\/?[a-z][a-z0-9]*(?::[a-z][a-z0-9]*)?\s*(?:\s+[a-z0-9-_]+=(?:(?:'[\s\S]*?')|(?:"[\s\S]*?")))*\s*\/?>)|([^<]|<(?![a-z\/]))*/gi,
  23. attrRegExp = /\s[a-z0-9-_]+\b(\s*=\s*('|")[\s\S]*?\2)?/gi,
  24. splitAttrRegExp = /(\s[a-z0-9-_]+\b\s*)(?:=(\s*('|")[\s\S]*?\3))?/gi,
  25. startTagExp = /^<[a-z]/,
  26. selfCloseTagExp = /\/>$/,
  27. closeTagExp = /^<\//,
  28. nodeNameExp = /<\/?([a-z][a-z0-9]*)(?::([a-z][a-z0-9]*))?/i,
  29. attributeQuotesExp = /^('|")|('|")$/g,
  30. noClosingTagsExp = /^(?:area|base|br|col|command|embed|hr|img|input|link|meta|param|source)/i;
  31. var Node = require('./Node');
  32. function findByRegExp(html, selector, onlyFirst) {
  33. var
  34. result = [],
  35. tagsCount = 0,
  36. tags = html.match(tagRegExp),
  37. composing = false,
  38. currentObject = null,
  39. matchingSelector,
  40. fullNodeName,
  41. selfCloseTag,
  42. attributes,
  43. attrBuffer,
  44. attrStr,
  45. buffer,
  46. tag;
  47. for (var i = 0, l = tags.length; i < l; i++) {
  48. tag = tags[i];
  49. fullNodeName = tag.match(nodeNameExp);
  50. matchingSelector = selector.test(tag);
  51. if (matchingSelector && !composing){
  52. composing = true;
  53. }
  54. if (composing) {
  55. if (startTagExp.test(tag)) {
  56. selfCloseTag = selfCloseTagExp.test(tag) || noClosingTagsExp.test(fullNodeName[1]);
  57. attributes = [];
  58. attrStr = tag.match(attrRegExp) || [];
  59. for (var aI = 0, aL = attrStr.length; aI < aL; aI++) {
  60. splitAttrRegExp.lastIndex = 0;
  61. attrBuffer = splitAttrRegExp.exec(attrStr[aI]);
  62. attributes.push({
  63. name: attrBuffer[1].trim(),
  64. value: (attrBuffer[2] || '').trim().replace(attributeQuotesExp, '')
  65. });
  66. }
  67. ((currentObject && currentObject.childNodes) || result).push(buffer = new Node({
  68. nodeType: 1, //element node
  69. nodeName: fullNodeName[1],
  70. namespace: fullNodeName[2],
  71. attributes: attributes,
  72. childNodes: [],
  73. parentNode: currentObject,
  74. startTag: tag,
  75. selfCloseTag: selfCloseTag
  76. }));
  77. tagsCount++;
  78. if (!onlyFirst && matchingSelector && currentObject){
  79. result.push(buffer);
  80. }
  81. if (selfCloseTag) {
  82. tagsCount--;
  83. }
  84. else {
  85. currentObject = buffer;
  86. }
  87. }
  88. else if (closeTagExp.test(tag)) {
  89. if (currentObject.nodeName == fullNodeName[1]){
  90. currentObject = currentObject.parentNode;
  91. tagsCount--;
  92. }
  93. }
  94. else {
  95. currentObject.childNodes.push(new Node({
  96. nodeType: 3,
  97. text: tag,
  98. parentNode: currentObject
  99. }));
  100. }
  101. if (tagsCount == 0) {
  102. composing = false;
  103. currentObject = null;
  104. if (onlyFirst){
  105. break;
  106. }
  107. }
  108. }
  109. }
  110. return onlyFirst ? result[0] || null : result;
  111. }
  112. function Dom(rawHTML) {
  113. this.rawHTML = rawHTML;
  114. }
  115. Dom.prototype.getElementsByClassName = function (className) {
  116. var selector = new RegExp('class=(\'|")(.*?\\s)?' + className + '(\\s.*?)?\\1');
  117. return findByRegExp(this.rawHTML, selector);
  118. };
  119. Dom.prototype.getElementsByTagName = function (tagName) {
  120. var selector = new RegExp('^<'+tagName, 'i');
  121. return findByRegExp(this.rawHTML, selector);
  122. };
  123. Dom.prototype.getElementById = function(id){
  124. var selector = new RegExp('id=(\'|")' + id + '\\1');
  125. return findByRegExp(this.rawHTML, selector, true);
  126. };
  127. Dom.prototype.getElementsByName = function(name){
  128. return this.getElementsByAttribute('name', name);
  129. };
  130. Dom.prototype.getElementsByAttribute = function(attr, value){
  131. var selector = new RegExp('\\s' + attr + '=(\'|")' + value + '\\1');
  132. return findByRegExp(this.rawHTML, selector);
  133. };
  134. module.exports = Dom;
  135. }, function(modId) { var map = {"./Node":1633751990332}; return __REQUIRE__(map[modId], modId); })
  136. __DEFINE__(1633751990332, function(require, module, exports) {
  137. //https://developer.mozilla.org/en-US/docs/Web/API/Element
  138. function Node(cfg) {
  139. this.namespace = cfg.namespace || null;
  140. this.text = cfg.text;
  141. this._selfCloseTag = cfg.selfCloseTag;
  142. Object.defineProperties(this, {
  143. nodeType: {
  144. value: cfg.nodeType
  145. },
  146. nodeName: {
  147. value: cfg.nodeType == 1 ? cfg.nodeName : '#text'
  148. },
  149. childNodes: {
  150. value: cfg.childNodes
  151. },
  152. firstChild: {
  153. get: function(){
  154. return this.childNodes[0] || null;
  155. }
  156. },
  157. lastChild: {
  158. get: function(){
  159. return this.childNodes[this.childNodes.length-1] || null;
  160. }
  161. },
  162. parentNode: {
  163. value: cfg.parentNode || null
  164. },
  165. attributes: {
  166. value: cfg.attributes || []
  167. },
  168. innerHTML: {
  169. get: function(){
  170. var
  171. result = '',
  172. cNode;
  173. for (var i = 0, l = this.childNodes.length; i < l; i++) {
  174. cNode = this.childNodes[i];
  175. result += cNode.nodeType === 3 ? cNode.text : cNode.outerHTML;
  176. }
  177. return result;
  178. }
  179. },
  180. outerHTML: {
  181. get: function(){
  182. if (this.nodeType != 3){
  183. var
  184. str,
  185. attrs = (this.attributes.map(function(elem){
  186. return elem.name + (elem.value ? '=' + '"'+ elem.value +'"' : '');
  187. }) || []).join(' '),
  188. childs = '';
  189. str = '<' + this.nodeName + (attrs ? ' ' + attrs : '') + (this._selfCloseTag ? '/' : '') + '>';
  190. if (!this._selfCloseTag){
  191. childs = (this._selfCloseTag ? '' : this.childNodes.map(function(child){
  192. return child.outerHTML;
  193. }) || []).join('');
  194. str += childs;
  195. str += '</' + this.nodeName + '>';
  196. }
  197. }
  198. else{
  199. str = this.textContent;
  200. }
  201. return str;
  202. }
  203. },
  204. textContent: {
  205. get: function(){
  206. if (this.nodeType == Node.TEXT_NODE){
  207. return this.text;
  208. }
  209. else{
  210. return this.childNodes.map(function(node){
  211. return node.textContent;
  212. }).join('').replace(/\x20+/g, ' ');
  213. }
  214. }
  215. }
  216. });
  217. }
  218. Node.prototype.getAttribute = function (attributeName) {
  219. for (var i = 0, l = this.attributes.length; i < l; i++) {
  220. if (this.attributes[i].name == attributeName) {
  221. return this.attributes[i].value;
  222. }
  223. }
  224. return null;
  225. };
  226. function searchElements(root, conditionFn, onlyFirst){
  227. var result = [];
  228. onlyFirst = !!onlyFirst;
  229. if (root.nodeType !== 3) {
  230. for (var i = 0, l = root.childNodes.length; i < l; i++) {
  231. if (root.childNodes[i].nodeType !== 3 && conditionFn(root.childNodes[i])) {
  232. result.push(root.childNodes[i]);
  233. if (onlyFirst){
  234. break;
  235. }
  236. }
  237. result = result.concat(searchElements(root.childNodes[i], conditionFn));
  238. }
  239. }
  240. return onlyFirst ? result[0] : result;
  241. }
  242. Node.prototype.getElementsByTagName = function (tagName) {
  243. return searchElements(this, function(elem){
  244. return elem.nodeName == tagName;
  245. })
  246. };
  247. Node.prototype.getElementsByClassName = function (className) {
  248. var expr = new RegExp('^(.*?\\s)?' + className + '(\\s.*?)?$');
  249. return searchElements(this, function(elem){
  250. return elem.attributes.length && expr.test(elem.getAttribute('class'));
  251. })
  252. };
  253. Node.prototype.getElementById = function (id) {
  254. return searchElements(this, function(elem){
  255. return elem.attributes.length && elem.getAttribute('id') == id;
  256. }, true)
  257. };
  258. Node.prototype.getElementsByName = function (name) {
  259. return searchElements(this, function(elem){
  260. return elem.attributes.length && elem.getAttribute('name') == name;
  261. })
  262. };
  263. Node.ELEMENT_NODE = 1;
  264. Node.TEXT_NODE = 3;
  265. module.exports = Node;
  266. }, function(modId) { var map = {}; return __REQUIRE__(map[modId], modId); })
  267. return __REQUIRE__(1633751990329);
  268. })()
  269. //miniprogram-npm-outsideDeps=[]
  270. //# sourceMappingURL=index.js.map