beautiful-dom
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -6,2 +6,3 @@ import HTMLElementData from './htmlelement'; | ||
private parsedData; | ||
private done; | ||
constructor(data_source: string); | ||
@@ -8,0 +9,0 @@ reInit(): this; |
@@ -14,8 +14,2 @@ "use strict"; | ||
action: function (match, tokens) { | ||
if (tokens[tokens.length - 1] == '+') { | ||
console.log('this is a difficult problem charlie!'); | ||
} | ||
else if (tokens[tokens.length - 1] == '>') { | ||
console.log('na here we dey ooo'); | ||
} | ||
tokens.push(match[0]); | ||
@@ -27,5 +21,5 @@ var holder = []; | ||
}); | ||
var returnValue = []; | ||
var returnValue_1 = []; | ||
var scrapData_1 = []; | ||
returnValue = holder.filter(function (element) { | ||
returnValue_1 = holder.filter(function (element) { | ||
if (scrapData_1.indexOf(element.outerHTML) == -1) { | ||
@@ -36,6 +30,8 @@ scrapData_1.push(element.outerHTML); | ||
}); | ||
_this.parsedData = returnValue; | ||
_this.parsedData = returnValue_1; | ||
return _this.parsedData; | ||
} | ||
return _this.parseAllTags(); | ||
var returnValue = _this.parseAllTags(); | ||
_this.done = false; | ||
return returnValue; | ||
} | ||
@@ -47,7 +43,6 @@ }, | ||
if (tokens[tokens.length - 1] == '+') { | ||
console.log('this is not a difficult problem charlie! oooo i hear you'); | ||
var htmlElements = _this.parsedData; | ||
var allElements = _this.parseAllTags(); | ||
_this.parsedData = htmlElements; | ||
var returnValue_1 = []; | ||
var returnValue_2 = []; | ||
var secondSiblingRegExp = _this.createTagRegExp(match[1]); | ||
@@ -76,3 +71,3 @@ var secondSiblingMatch = void 0; | ||
scrapData.push(allElements[x].outerHTML); | ||
returnValue_1.push(allElements[x]); | ||
returnValue_2.push(allElements[x]); | ||
breakingCondition = true; | ||
@@ -90,6 +85,5 @@ u = x; | ||
} | ||
_this.parsedData = returnValue_1; | ||
_this.parsedData = returnValue_2; | ||
} | ||
else if (tokens[tokens.length - 1] == '>') { | ||
console.log('check me out'); | ||
var matchingTags = []; | ||
@@ -109,3 +103,2 @@ var scrapData = []; | ||
if (i == _this.parsedData.length) { | ||
console.log(match[1]); | ||
// that means it is not in any of the other matchingTags | ||
@@ -121,3 +114,2 @@ if (scrapData.length > 0) { | ||
matchingTags.push(matchingElement); | ||
console.log(matchingTags.length, 'this is the length of matching tags'); | ||
} | ||
@@ -141,5 +133,5 @@ } | ||
}); | ||
var returnValue_2 = []; | ||
var returnValue_3 = []; | ||
var scrapData_2 = []; | ||
returnValue_2 = holder_1.filter(function (element) { | ||
returnValue_3 = holder_1.filter(function (element) { | ||
if (scrapData_2.indexOf(element.outerHTML) == -1) { | ||
@@ -150,3 +142,3 @@ scrapData_2.push(element.outerHTML); | ||
}); | ||
_this.parsedData = returnValue_2; | ||
_this.parsedData = returnValue_3; | ||
} | ||
@@ -171,2 +163,3 @@ else { | ||
_this.parsedData = returnValue; | ||
_this.done = false; | ||
return _this.parsedData; | ||
@@ -180,3 +173,2 @@ } | ||
tokens.push(match[0]); | ||
console.log('this is not a difficult problem charlie! oooo i hear you'); | ||
var htmlElements = _this.parsedData; | ||
@@ -222,6 +214,6 @@ var allElements = _this.parseAllTags(); | ||
_this.parsedData = returnValue; | ||
_this.done = false; | ||
return _this.parsedData; | ||
} | ||
else if (tokens[tokens.length - 1] == '>') { | ||
console.log('check me out'); | ||
var matchingTags = []; | ||
@@ -241,3 +233,2 @@ var scrapData = []; | ||
if (i == _this.parsedData.length) { | ||
console.log(match[1]); | ||
// that means it is not in any of the other matchingTags | ||
@@ -253,3 +244,2 @@ if (scrapData.length > 0) { | ||
matchingTags.push(matchingElement); | ||
console.log(matchingTags.length, 'this is the length of matching tags'); | ||
} | ||
@@ -266,2 +256,3 @@ } | ||
_this.parsedData = matchingTags; | ||
_this.done = false; | ||
return _this.parsedData; | ||
@@ -271,5 +262,3 @@ } | ||
var holder = []; | ||
console.log('parsed data length', _this.parsedData.length); | ||
if (_this.parsedData.length > 0) { | ||
console.log('we are in the depths'); | ||
_this.parsedData.forEach(function (htmlElement) { | ||
@@ -287,6 +276,9 @@ holder = holder.concat(htmlElement.getElementsByTagName(match[0])); | ||
_this.parsedData = returnValue; | ||
_this.done = false; | ||
return _this.parsedData; | ||
} | ||
else { | ||
return _this.getElementsByTagName(match[0]); | ||
var returnValue = _this.getElementsByTagName(match[0]); | ||
_this.done = false; | ||
return returnValue; | ||
} | ||
@@ -300,3 +292,2 @@ } | ||
tokens.push(match[0]); | ||
console.log('this is not a difficult problem charlie! oooo i hear you'); | ||
var htmlElements = _this.parsedData; | ||
@@ -343,6 +334,6 @@ var allElements = _this.parseAllTags(); | ||
_this.parsedData = returnValue; | ||
_this.done = false; | ||
return _this.parsedData; | ||
} | ||
else if (tokens[tokens.length - 1] == '>') { | ||
console.log('check me out'); | ||
var matchingTags = []; | ||
@@ -362,3 +353,2 @@ var scrapData = []; | ||
if (i == _this.parsedData.length) { | ||
console.log(match[1]); | ||
// that means it is not in any of the other matchingTags | ||
@@ -374,3 +364,2 @@ if (scrapData.length > 0) { | ||
matchingTags.push(matchingElement); | ||
console.log(matchingTags.length, 'this is the length of matching tags'); | ||
} | ||
@@ -387,2 +376,4 @@ } | ||
_this.parsedData = matchingTags; | ||
_this.done = false; | ||
return _this.parsedData; | ||
} | ||
@@ -403,6 +394,9 @@ var holder = []; | ||
_this.parsedData = returnValue; | ||
_this.done = false; | ||
return _this.parsedData; | ||
} | ||
else { | ||
return _this.getElementsByClassName(match[0].slice(1)); | ||
var returnValue = _this.getElementsByClassName(match[0].slice(1)); | ||
_this.done = false; | ||
return returnValue; | ||
} | ||
@@ -417,2 +411,3 @@ } | ||
returnValue.push(_this.getElementById(match[0].slice(1))); | ||
_this.done = false; | ||
return returnValue; | ||
@@ -432,9 +427,5 @@ } | ||
tokens.push(match[0]); | ||
if (tokens[tokens.length - 1] == '+') { | ||
console.log('this is a difficult problem charlie!'); | ||
} | ||
else if (tokens[tokens.length - 1] == '>') { | ||
console.log('na here we dey ooo'); | ||
} | ||
return _this.getByAttribute(match[1]); | ||
var returnValue = _this.getByAttribute(match[1]); | ||
_this.done = false; | ||
return returnValue; | ||
} | ||
@@ -444,2 +435,3 @@ }, | ||
this.parsedData = []; | ||
this.done = false; | ||
if (data_source.length == 0) { | ||
@@ -519,2 +511,3 @@ throw new Error("Please input the html document you want to parse"); | ||
} | ||
this.done = true; | ||
return this.parsedData; | ||
@@ -546,5 +539,5 @@ }; | ||
BeautifulDom.prototype.getByAttribute = function (attribute, attributeValue) { | ||
var attributeRegExp = new RegExp(attribute + "=\\s*('|\")(\\s*" + attributeValue + "\\s+.*?)|(.*?\\s+" + attributeValue + "\\s+.*?)|(.*?\\s+" + attributeValue + "\\s*)(\"|')", 'mi'); | ||
var attributeRegExp = new RegExp(attribute + "=\\s*('|\")((\\b" + attributeValue + "\\b.*?)|(.*?\\b" + attributeValue + "\\b.*?)|(.*?\\b" + attributeValue + "\\b))(\"|')", 'mi'); | ||
var matchingTags = []; | ||
if (this.parsedData.length == 0) { | ||
if (this.parsedData.length == 0 || this.done) { | ||
this.parseAllTags(); | ||
@@ -565,2 +558,3 @@ } | ||
BeautifulDom.prototype.getElementsByClassName = function (classValue) { | ||
this.done = true; | ||
return this.getByAttribute('class', classValue.trim()); | ||
@@ -571,11 +565,11 @@ }; | ||
var element = matchingElements.length > 0 ? matchingElements[0] : null; | ||
this.done = true; | ||
return element; | ||
}; | ||
BeautifulDom.prototype.getElementsByName = function (name) { | ||
this.done = true; | ||
return this.getByAttribute("name", name.trim()); | ||
}; | ||
BeautifulDom.prototype.querySelectorAll = function (query) { | ||
if (this.parsedData.length == 0) { | ||
this.parseAllTags(); | ||
} | ||
this.parsedData = this.done ? [] : this.parsedData; | ||
var tokens = []; | ||
@@ -599,6 +593,9 @@ query = query.trim(); | ||
} | ||
this.done = true; | ||
return this.parsedData; | ||
}; | ||
BeautifulDom.prototype.querySelector = function (query) { | ||
return this.querySelectorAll(query).slice(0, 1)[0] ? this.querySelectorAll(query).slice(0, 1)[0] : null; | ||
var returnValue = this.querySelectorAll(query).slice(0, 1)[0] ? this.querySelectorAll(query).slice(0, 1)[0] : null; | ||
this.done = true; | ||
return returnValue; | ||
}; | ||
@@ -605,0 +602,0 @@ return BeautifulDom; |
@@ -6,5 +6,7 @@ declare class HTMLElementData { | ||
textContent: string; | ||
private done; | ||
parsedData: HTMLElementData[]; | ||
private patterns; | ||
constructor(outerHTML: string); | ||
getStatus(): Boolean; | ||
private getAllTags; | ||
@@ -11,0 +13,0 @@ private createTagRegExp; |
@@ -6,2 +6,3 @@ "use strict"; | ||
var _this = this; | ||
this.done = true; | ||
this.parsedData = []; | ||
@@ -15,3 +16,2 @@ this.patterns = [ | ||
if (_this.parsedData.length) { | ||
console.log(_this.parsedData); | ||
_this.parsedData.forEach(function (htmlElement) { | ||
@@ -29,4 +29,6 @@ holder = holder.concat(htmlElement.parseAllTags()); | ||
_this.parsedData = returnValue; | ||
_this.done = false; | ||
return _this.parsedData; | ||
} | ||
_this.done = false; | ||
return _this.parseAllTags(); | ||
@@ -38,3 +40,2 @@ } | ||
action: function (match, tokens) { | ||
console.log('how is it'); | ||
if (tokens[tokens.length - 1] == '+') { | ||
@@ -81,6 +82,6 @@ var htmlElements = _this.parsedData; | ||
} | ||
_this.done = false; | ||
_this.parsedData = returnValue_1; | ||
} | ||
else if (tokens[tokens.length - 1] == '>') { | ||
console.log('check me out'); | ||
var matchingTags = []; | ||
@@ -100,3 +101,2 @@ var scrapData = []; | ||
if (i == _this.parsedData.length) { | ||
console.log(match[1]); | ||
// that means it is not in any of the other matchingTags | ||
@@ -112,3 +112,2 @@ if (scrapData.length > 0) { | ||
matchingTags.push(matchingElement); | ||
console.log(matchingTags.length, 'this is the length of matching tags'); | ||
} | ||
@@ -160,2 +159,3 @@ } | ||
_this.parsedData = returnValue; | ||
_this.done = false; | ||
return _this.parsedData; | ||
@@ -209,6 +209,6 @@ } | ||
_this.parsedData = returnValue; | ||
_this.done = false; | ||
return _this.parsedData; | ||
} | ||
else if (tokens[tokens.length - 1] == '>') { | ||
console.log('check me out'); | ||
var matchingTags = []; | ||
@@ -228,3 +228,2 @@ var scrapData = []; | ||
if (i == _this.parsedData.length) { | ||
console.log(match[1]); | ||
// that means it is not in any of the other matchingTags | ||
@@ -240,3 +239,2 @@ if (scrapData.length > 0) { | ||
matchingTags.push(matchingElement); | ||
console.log(matchingTags.length, 'this is the length of matching tags'); | ||
} | ||
@@ -253,2 +251,3 @@ } | ||
_this.parsedData = matchingTags; | ||
_this.done = false; | ||
return _this.parsedData; | ||
@@ -271,6 +270,9 @@ } | ||
_this.parsedData = returnValue; | ||
_this.done = false; | ||
return _this.parsedData; | ||
} | ||
else { | ||
return _this.getElementsByTagName(match[0]); | ||
var returnValue = _this.getElementsByTagName(match[0]); | ||
_this.done = false; | ||
return returnValue; | ||
} | ||
@@ -284,3 +286,2 @@ } | ||
tokens.push(match[0]); | ||
console.log('this is not a difficult problem charlie! oooo i hear you'); | ||
var htmlElements = _this.parsedData; | ||
@@ -327,2 +328,3 @@ var allElements = _this.parseAllTags(); | ||
_this.parsedData = returnValue; | ||
_this.done = false; | ||
return _this.parsedData; | ||
@@ -355,3 +357,2 @@ } | ||
matchingTags.push(matchingElement); | ||
console.log(matchingTags.length, 'this is the length of matching tags'); | ||
} | ||
@@ -368,2 +369,4 @@ } | ||
_this.parsedData = matchingTags; | ||
_this.done = false; | ||
return _this.parsedData; | ||
} | ||
@@ -384,6 +387,9 @@ var holder = []; | ||
_this.parsedData = returnValue; | ||
_this.done = false; | ||
return _this.parsedData; | ||
} | ||
else { | ||
return _this.getElementsByClassName(match[0].slice(1)); | ||
var returnValue = _this.getElementsByClassName(match[0].slice(1)); | ||
_this.done = false; | ||
return returnValue; | ||
} | ||
@@ -402,9 +408,4 @@ } | ||
action: function (match, tokens, elements) { | ||
if (tokens[tokens.length - 1] == '+') { | ||
console.log('this is a difficult problem charlie!'); | ||
} | ||
else if (tokens[tokens.length - 1] == '>') { | ||
console.log('na here we dey ooo'); | ||
} | ||
tokens.push(match[0]); | ||
_this.done = false; | ||
return _this.getByAttribute(match[1]); | ||
@@ -420,2 +421,5 @@ } | ||
} | ||
HTMLElementData.prototype.getStatus = function () { | ||
return this.done; | ||
}; | ||
HTMLElementData.prototype.getAllTags = function (data) { | ||
@@ -482,2 +486,3 @@ var tagsRegex = /<\s*(\w+)\s*.*?>/mi; | ||
} | ||
this.done = true; | ||
return this.parsedData; | ||
@@ -509,4 +514,4 @@ }; | ||
HTMLElementData.prototype.getByAttribute = function (attribute, attributeValue) { | ||
var attributeRegExp = new RegExp(attribute + "=\\s*('|\")(\\s*" + attributeValue + "\\s+.*?)|(.*?\\s+" + attributeValue + "\\s+.*?)|(.*?\\s+" + attributeValue + "\\s*)(\"|')", 'mi'); | ||
if (this.parsedData.length == 0) { | ||
var attributeRegExp = new RegExp(attribute + "=\\s*('|\")((\\b" + attributeValue + "\\b.*?)|(.*?\\b" + attributeValue + "\\b.*?)|(.*?\\b" + attributeValue + "\\b))(\"|')", 'mi'); | ||
if (this.parsedData.length == 0 || this.done) { | ||
this.parseAllTags(); | ||
@@ -528,5 +533,7 @@ } | ||
HTMLElementData.prototype.getElementsByClassName = function (classValue) { | ||
this.done = true; | ||
return this.getByAttribute('class', classValue); | ||
}; | ||
HTMLElementData.prototype.querySelectorAll = function (query) { | ||
this.parsedData = this.done ? [] : this.parsedData; | ||
var tokens = []; | ||
@@ -550,6 +557,9 @@ query = query.trim(); | ||
} | ||
this.done = true; | ||
return this.parsedData; | ||
}; | ||
HTMLElementData.prototype.querySelector = function (query) { | ||
return this.querySelectorAll(query).slice(0, 1)[0] ? this.querySelectorAll(query).slice(0, 1)[0] : null; | ||
var returnValue = this.querySelectorAll(query).slice(0, 1)[0] ? this.querySelectorAll(query).slice(0, 1)[0] : null; | ||
this.done = true; | ||
return returnValue; | ||
}; | ||
@@ -560,2 +570,3 @@ HTMLElementData.prototype.getAttribute = function (attribute) { | ||
var match = outerHTML.match(attributeRegExp); | ||
this.done = true; | ||
return match ? match[2] : null; | ||
@@ -562,0 +573,0 @@ }; |
{ | ||
"name": "beautiful-dom", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "Beautiful-dom is a lightweight library that mirrors the capabilities of the HTML DOM API needed for parsing crawled HTML/XML pages. It models the methods and properties of HTML nodes that are relevant for extracting data from HTML nodes. It is written in TypeScript and can be used as a CommonJS library", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -45,3 +45,2 @@ <h1 align="center">Beautiful-dom</h1> | ||
- document.querySelector() | ||
- document.reInit() : clears the memory of DOM object after consecutive operations and returns a reference to the object. | ||
@@ -70,15 +69,15 @@ Methods on the HTML node object | ||
let nodesWithSpecificClass = dom.reInit().getElementsByClassName('work'); | ||
let nodesWithSpecificClass = dom.getElementsByClassName('work'); | ||
// returns a list of node objects with class name 'work' | ||
let nodeWithSpecificId = dom.reInit().getElementById('container'); | ||
let nodeWithSpecificId = dom.getElementById('container'); | ||
// returns a node with id 'container' | ||
let complexQueryNodes = dom.reInit().querySelectorAll('p.paragraph b'); | ||
let complexQueryNodes = dom.querySelectorAll('p.paragraph b'); | ||
// returns a list of nodes that satisfy the complex query of CSS selectors | ||
let nodesWithSpecificName = dom.reInit().getElementsByName('name'); | ||
let nodesWithSpecificName = dom.getElementsByName('name'); | ||
// returns a list of nodes with the specific 'name' | ||
let linkNode = dom.reInit().querySelector('a#myWebsite'); | ||
let linkNode = dom.querySelector('a#myWebsite'); | ||
// returns a node object with with the CSS selector | ||
@@ -119,3 +118,3 @@ | ||
let linkNode = dom.reInit().querySelector('a#myWebsite'); | ||
let linkNode = dom.querySelector('a#myWebsite'); | ||
// returns a node object with with the CSS selector | ||
@@ -150,4 +149,1 @@ | ||
``` | ||
## Call for advice | ||
I need help with a procedure on how to remove the 'reInit' method used for clearing the memory of DOM object after each method call so as to model actual HTML DOM behaviour. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1246
74578
146