beautiful-dom
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -17,4 +17,4 @@ import HTMLElementData from './htmlelement'; | ||
querySelectorAll(query: string): HTMLElementData[]; | ||
querySelector(query: string): HTMLElementData; | ||
querySelector(query: string): (HTMLElementData | null); | ||
} | ||
export default BeautifulDom; |
@@ -571,3 +571,3 @@ "use strict"; | ||
BeautifulDom.prototype.querySelector = function (query) { | ||
return this.querySelectorAll(query).slice(0, 1)[0]; | ||
return this.querySelectorAll(query).slice(0, 1)[0] ? this.querySelectorAll(query).slice(0, 1)[0] : null; | ||
}; | ||
@@ -574,0 +574,0 @@ return BeautifulDom; |
@@ -16,5 +16,5 @@ declare class HTMLElementData { | ||
querySelectorAll(query: string): HTMLElementData[]; | ||
querySelector(query: string): HTMLElementData; | ||
querySelector(query: string): (HTMLElementData | null); | ||
getAttribute(attribute: string): (string | null); | ||
} | ||
export default HTMLElementData; |
@@ -489,3 +489,3 @@ "use strict"; | ||
var attributeRegExp = new RegExp(attribute + "=\\s*('|\")(\\s*" + attributeValue + "\\s+.*?)|(.*?\\s+" + attributeValue + "\\s+.*?)|(.*?\\s+" + attributeValue + "\\s*)(\"|')", 'mi'); | ||
if (!this.parsedData.length) { | ||
if (this.parsedData.length == 0) { | ||
this.parseAllTags(); | ||
@@ -531,3 +531,3 @@ } | ||
HTMLElementData.prototype.querySelector = function (query) { | ||
return this.querySelectorAll(query).slice(0, 1)[0]; | ||
return this.querySelectorAll(query).slice(0, 1)[0] ? this.querySelectorAll(query).slice(0, 1)[0] : null; | ||
}; | ||
@@ -534,0 +534,0 @@ HTMLElementData.prototype.getAttribute = function (attribute) { |
{ | ||
"name": "beautiful-dom", | ||
"version": "1.0.4", | ||
"description": "", | ||
"version": "1.0.5", | ||
"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", | ||
"main": "dist/index.js", | ||
@@ -6,0 +6,0 @@ "types":"dist/index.d.ts", |
@@ -11,2 +11,4 @@ <h1 align="center">Beautiful-dom</h1> | ||
- Complex queries with CSS selectors. | ||
## How to use | ||
@@ -68,15 +70,15 @@ ```bash | ||
let nodesWithSpecificClass = dom.getElementsByClassName('work'); | ||
let nodesWithSpecificClass = dom.reInit().getElementsByClassName('work'); | ||
// returns a list of node objects with class name 'work' | ||
let nodeWithSpecificId = dom.getElementById('container'); | ||
let nodeWithSpecificId = dom.reInit().getElementById('container'); | ||
// returns a node with id 'container' | ||
let complexQueryNodes = dom.querySelectorAll('p.paragraph b'); | ||
let complexQueryNodes = dom.reInit().querySelectorAll('p.paragraph b'); | ||
// returns a list of nodes that satisfy the complex query of CSS selectors | ||
let nodesWithSpecificName = dom.getElementsByName('name'); | ||
let nodesWithSpecificName = dom.reInit().getElementsByName('name'); | ||
// returns a list of nodes with the specific 'name' | ||
let linkNode = dom.querySelector('a#myWebsite'); | ||
let linkNode = dom.reInit().querySelector('a#myWebsite'); | ||
// returns a node object with with the CSS selector | ||
@@ -102,2 +104,38 @@ | ||
## Examples for a node object | ||
```js | ||
let paragraphNodes = dom.getElementsByTagName('p'); | ||
// returns a list of node objects with node name 'p' | ||
let nodesWithSpecificClass = paragraphNodes[0].getElementsByClassName('work'); | ||
// returns a list of node objects inside the first paragraph node with class name 'work' | ||
let complexQueryNodes = paragraphNodes[0].querySelectorAll('span.work'); | ||
// returns a list of nodes in the paragraph node that satisfy the complex query of CSS selectors | ||
let linkNode = dom.reInit().querySelector('a#myWebsite'); | ||
// returns a node object with with the CSS selector | ||
let linkHref = linkNode.getAttribute('href'); | ||
// returns the value of the attribute e.g 'https://www.ajah.xyz' | ||
let linkInnerHTML = linkNode.innerHTML | ||
// returns the innerHTML of a node object e.g ' My website ' | ||
let linkTextContent = linkNode.textContent | ||
// returns the textContent of a node object e.g ' My website ' | ||
let linkInnerText = linkNode.innerText | ||
// returns the innerText of a node object e.g ' My website ' | ||
let linkOuterHTML = linkNode.outerHTML | ||
// returns the outerHTML of a node object i.e. '<a class="myWebsite" href="https://www.ajah.xyz" > My website </a>' | ||
``` | ||
## Contributing | ||
@@ -113,1 +151,3 @@ In case you have any ideas, features you would like to be included or any bug fixes, you can send a PR. | ||
## 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. |
75655
150