Blame view

lib/jsdom/living/nodes/HTMLLinkElement-impl.js 2.85 KB
858f2bdf5   Boyan Georgiev   fixes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  "use strict";
  const { reflectURLAttribute } = require("../../utils");
  const DOMTokenList = require("../generated/DOMTokenList");
  const HTMLElementImpl = require("./HTMLElement-impl").implementation;
  const idlUtils = require("../generated/utils");
  const { fetchStylesheet } = require("../helpers/stylesheets");
  const { parseURLToResultingURLRecord } = require("../helpers/document-base-url");
  const whatwgURL = require("whatwg-url");
  
  // Important reading: "appropriate times to obtain the resource" in
  // https://html.spec.whatwg.org/multipage/semantics.html#link-type-stylesheet
  
  class HTMLLinkElementImpl extends HTMLElementImpl {
    constructor(globalObject, args, privateData) {
      super(globalObject, args, privateData);
  
      this.sheet = null;
    }
  
    get relList() {
      if (this._relList === undefined) {
        this._relList = DOMTokenList.createImpl(this._globalObject, [], {
          element: this,
          attributeLocalName: "rel",
          supportedTokens: new Set(["stylesheet"])
        });
      }
      return this._relList;
    }
  
    _attach() {
      super._attach();
      maybeFetchAndProcess(this);
    }
  
    _attrModified(name, value, oldValue) {
      super._attrModified(name, value, oldValue);
  
      if (name === "href") { // TODO crossorigin="" or type=""
        maybeFetchAndProcess(this);
      }
  
      if (name === "rel" && this._relList !== undefined) {
        this._relList.attrModified();
      }
    }
  
    get _accept() {
      return "text/css,*/*;q=0.1";
    }
  
    get href() {
      return reflectURLAttribute(this, "href");
    }
  
    set href(value) {
      this.setAttributeNS(null, "href", value);
    }
  }
  
  module.exports = {
    implementation: HTMLLinkElementImpl
  };
  
  // https://html.spec.whatwg.org/multipage/links.html#link-type-stylesheet
  function maybeFetchAndProcess(el) {
    if (!isExternalResourceLink(el)) {
      return;
    }
  
    // Browsing-context connected
    if (!el.isConnected || !el._ownerDocument._defaultView) {
      return;
    }
  
    fetchAndProcess(el);
  }
  
  // https://html.spec.whatwg.org/multipage/semantics.html#default-fetch-and-process-the-linked-resource
  // TODO: refactor into general link-fetching like the spec.
  function fetchAndProcess(el) {
    const href = el.getAttributeNS(null, "href");
  
    if (href === null || href === "") {
      return;
    }
  
    const url = parseURLToResultingURLRecord(href, el._ownerDocument);
    if (url === null) {
      return;
    }
  
    // TODO handle crossorigin="", nonce, integrity="", referrerpolicy=""
  
    const serialized = whatwgURL.serializeURL(url);
  
    fetchStylesheet(el, serialized);
  }
  
  function isExternalResourceLink(el) {
    // for our purposes, only stylesheets can be external resource links
    const wrapper = idlUtils.wrapperForImpl(el);
    if (!/(?:[ \t
  \r\f]|^)stylesheet(?:[ \t
  \r\f]|$)/i.test(wrapper.rel)) {
      // rel is a space-separated list of tokens, and the original rel types
      // are case-insensitive.
      return false;
    }
  
    return Boolean(el.href);
  }