Commit 940421c9 authored by Robert Knight's avatar Robert Knight

Always get metadata from injected DOM Document

The `Document` class for reading DOM document metadata takes an optional
DOM `Document` constructor argument as a test seam.

However the actual code only used the injected document in some places,
in others it used jQuery which in turn reads from the global
`window.document`.

Refactor this code to use the injected DOM Document everywhere and
remove the jQuery dependency.
parent b11801e4
$ = require('jquery')
baseURI = require('document-base-uri')
Plugin = require('../plugin')
......@@ -85,9 +84,9 @@ module.exports = class Document extends Plugin
_getMetaTags: (prefix, attribute, delimiter) =>
tags = {}
for meta in $("meta")
name = $(meta).attr(attribute)
content = $(meta).prop("content")
for meta in @document.querySelectorAll('meta')
name = meta.getAttribute(attribute)
content = meta.content
if name
match = name.match(RegExp("^#{prefix}#{delimiter}(.+)$", "i"))
if match
......@@ -112,19 +111,18 @@ module.exports = class Document extends Plugin
else if @metadata.dc.title
@metadata.title = @metadata.dc.title[0]
else
@metadata.title = $("head title").text()
@metadata.title = @document.title
_getLinks: =>
# we know our current location is a link for the document
@metadata.link = [href: this._getDocumentHref()]
# look for some relevant link relations
for link in $("link")
l = $(link)
href = this._absoluteUrl(l.prop('href')) # get absolute url
rel = l.prop('rel')
type = l.prop('type')
lang = l.prop('hreflang')
for link in @document.querySelectorAll('link')
href = this._absoluteUrl(link.href) # get absolute url
rel = link.rel
type = link.type
lang = link.hreflang
if rel not in ["alternate", "canonical", "bookmark", "shortlink"] then continue
......@@ -177,8 +175,8 @@ module.exports = class Document extends Plugin
@metadata.documentFingerprint = dcUrn
_getFavicon: =>
for link in $("link")
if $(link).prop("rel") in ["shortcut icon", "icon"]
for link in @document.querySelectorAll('link')
if link.rel in ["shortcut icon", "icon"]
@metadata["favicon"] = this._absoluteUrl(link.href)
# Hack to get a absolute url from a possibly relative one
......
......@@ -176,6 +176,7 @@ describe 'Document', ->
createDoc = (href, baseURI) ->
fakeDocument =
createElement: document.createElement.bind(document),
querySelectorAll: document.querySelectorAll.bind(document),
location:
href: href
doc = new Document($('<div></div>')[0], {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment