Skip to content

Commit

Permalink
feat: Document parser has been brought back (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegn authored Feb 28, 2018
1 parent 07d6001 commit aa0c6fe
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
35 changes: 25 additions & 10 deletions v8env/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
getOuterHTML,
getText,
getInnerHTML,
replaceElement
replaceElement,
getAttributeValue
} from 'domutils'

import { logger } from './logger'
Expand All @@ -13,15 +14,11 @@ const WritableParser = htmlparser.WritableStream

import * as css from 'css-select'

export default class Document {
export class Node {
constructor(dom) {
this._dom = dom
}

getElementById(id) {
return this.querySelector(`#${id}`)
}

querySelector(selector) {
let found = css.selectOne(selector, this._dom)
if (!found)
Expand All @@ -46,16 +43,25 @@ export default class Document {
get children() {
return this._dom
}
}

export class Document extends Node {
constructor(dom) {
super(dom)
}

getElementById(id) {
return this.querySelector(`#${id}`)
}

get documentElement() {
return new Element(this._dom)
}
}

class Element {
export class Element extends Node {
constructor(dom) {
this._dom = dom
logger.debug("made element", this._dom.attribs)
super(dom)
}

get id() {
Expand All @@ -82,6 +88,14 @@ class Element {
}
replaceElement(this._dom, parseDOMSync(html)[0])
}

getAttribute(name) {
return getAttributeValue(this._dom, name)
}

setAttribute(name, value) {
this._dom.attribs[name] = value
}
}

Document.parse = function documentParse(html) {
Expand Down Expand Up @@ -109,6 +123,8 @@ class DocumentParser {

if (stream instanceof ReadableStream)
stream = stream.getReader()
else
return this.parseSync(stream)

while (!fullyRead) {
const { done, value } = await stream.read()
Expand All @@ -119,7 +135,6 @@ class DocumentParser {
this.parser.write(value)
}
this.parser.end()
// logger.debug("done parsing!", dom.attribs)
}
}

Expand Down
6 changes: 4 additions & 2 deletions v8env/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import requestInit from './request'
import cache from './cache'
import timersInit from './timers'

import { Document, Element } from './document'

// Sets up `Error.prepareStacktrace`
import './utils/error'

Expand Down Expand Up @@ -84,8 +86,8 @@ global.bootstrap = function bootstrap() {
global.FetchEvent = FetchEvent

// DOM
// TODO: Reenable.
// global.Document = require('./document')
global.Document = Document
global.Element = Element

// Fly-specific
global.FlyBackend = FlyBackend
Expand Down
32 changes: 32 additions & 0 deletions v8env_test/document.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { expect } from 'chai'

const html = `
<html>
<p something="yo">hello</p>
</html>
`

describe("Document", () => {
it('exists', () => {
expect(typeof Document).to.equal('function')
})

it("parses html into a queryable dom tree", function () {
const dom = Document.parse(html)
expect(dom).to.be.instanceof(Document)
const p = dom.querySelector("p")
expect(p).to.be.instanceof(Element)
expect(p.getAttribute("something")).to.equal("yo")
})

it('can stringify a DOM', () => {
const doc = Document.parse(html)
expect(doc.documentElement.outerHTML).to.equal(html)
})

it('can replace elements', () => {
const doc = Document.parse(html)
doc.querySelector("p").replaceWith("<div>booya</div>")
expect(doc.querySelector("div").textContent).to.equal("booya")
})
})

0 comments on commit aa0c6fe

Please sign in to comment.