Skip to content
This repository has been archived by the owner on Apr 24, 2022. It is now read-only.

Commit

Permalink
Add HtmlParser_.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
munierujp committed Aug 24, 2019
1 parent 5da7074 commit 5964f4c
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/HtmlParser_.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// NOTE: Below class will be private in Google Apps Script.
class HtmlParser_ {
// eslint-disable-next-line camelcase
private readonly element: GoogleAppsScript.XML_Service.Element

// eslint-disable-next-line camelcase
constructor (element: GoogleAppsScript.XML_Service.Element) {
this.element = element
}

// eslint-disable-next-line camelcase
getElementById (id: string): GoogleAppsScript.XML_Service.Element {
const descendantElements = this.getDescendantElements()
return descendantElements.filter(element => {
const idAttribute = element.getAttribute('id')
if (idAttribute == null) {
return false
}
const idAttributeValue = idAttribute.getValue()
return idAttributeValue === id
})[0]
}

// eslint-disable-next-line camelcase
getElementsByClassName (className: string): GoogleAppsScript.XML_Service.Element[] {
const descendantElements = this.getDescendantElements()
descendantElements.push(this.element)
return descendantElements.filter(element => {
const classAttribute = element.getAttribute('class')
if (classAttribute == null) {
return false
}
const classAttributeValue = classAttribute.getValue()
return (classAttributeValue === className) || (classAttributeValue.split(' ').some(name => name === className))
})
}

// eslint-disable-next-line camelcase
getElementsByTagName (tagName: string): GoogleAppsScript.XML_Service.Element[] {
const descendantElements = this.getDescendantElements()
return descendantElements.filter(element => element.getName() === tagName)
}

// eslint-disable-next-line camelcase
private getDescendantElements (): GoogleAppsScript.XML_Service.Element[] {
return this.element.getDescendants()
.map(content => content.asElement())
.filter(element => element != null)
}
}

// NOTE: Below statement will be removed by clasp.
export default HtmlParser_

0 comments on commit 5964f4c

Please sign in to comment.