执行过npm install命令的vue-element-admin源码
康凯
2022-05-20 aa4c235a8ca67ea8b731f90c951a465e92c0a865
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"use strict";
const vm = require("vm");
const whatwgEncoding = require("whatwg-encoding");
 
const HTMLElementImpl = require("./HTMLElement-impl").implementation;
const { reflectURLAttribute } = require("../../utils");
const resourceLoader = require("../../browser/resource-loader");
const reportException = require("../helpers/runtime-script-errors");
const { domSymbolTree } = require("../helpers/internal-constants");
const { asciiLowercase } = require("../helpers/strings");
const { childTextContent } = require("../helpers/text");
const nodeTypes = require("../node-type");
 
const jsMIMETypes = new Set([
  "application/ecmascript",
  "application/javascript",
  "application/x-ecmascript",
  "application/x-javascript",
  "text/ecmascript",
  "text/javascript",
  "text/javascript1.0",
  "text/javascript1.1",
  "text/javascript1.2",
  "text/javascript1.3",
  "text/javascript1.4",
  "text/javascript1.5",
  "text/jscript",
  "text/livescript",
  "text/x-ecmascript",
  "text/x-javascript"
]);
 
class HTMLScriptElementImpl extends HTMLElementImpl {
  constructor(args, privateData) {
    super(args, privateData);
    this._alreadyStarted = false;
    this._parserInserted = false; // set by the parser
  }
 
  _attach() {
    super._attach();
 
 
    // In our current terribly-hacky document.write() implementation, we parse in a div them move elements into the main
    // document. Thus _eval() will bail early when it gets in _poppedOffStackOfOpenElements(), since we're not attached
    // then. Instead, we'll let it eval here.
    if (!this._parserInserted || this._isMovingDueToDocumentWrite) {
      this._eval();
    }
  }
 
  _attrModified(name, value, oldValue) {
    super._attrModified(name, value, oldValue);
 
    if (this._attached && !this._startedEval && name === "src" && oldValue === null && value !== null) {
      resourceLoader.load(
        this,
        this.src,
        { defaultEncoding: whatwgEncoding.labelToName(this.getAttribute("charset")) || this._ownerDocument._encoding },
        this._innerEval
      );
    }
  }
 
  _poppedOffStackOfOpenElements() {
    // This seems to roughly correspond to
    // https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-incdata:prepare-a-script, although we certainly
    // don't implement the full semantics.
    this._eval();
  }
 
  // Vaguely similar to https://html.spec.whatwg.org/multipage/scripting.html#prepare-a-script, but we have a long way
  // to go before it's aligned.
  _eval() {
    if (this._alreadyStarted) {
      return;
    }
 
    // TODO: this text check doesn't seem completely the same as the spec, which e.g. will try to execute scripts with
    // child element nodes. Spec bug? https://github.com/whatwg/html/issues/3419
    if (!this.hasAttribute("src") && this.text.length === 0) {
      return;
    }
 
    if (!this._attached) {
      return;
    }
 
    const scriptBlocksTypeString = this._getTypeString();
    const type = getType(scriptBlocksTypeString);
 
    if (type !== "classic") {
      // TODO: implement modules, and then change the check to `type === null`.
      return;
    }
 
    this._alreadyStarted = true;
 
    // Equivalent to the spec's "scripting is disabled" check.
    if (!this._ownerDocument._defaultView || this._ownerDocument._defaultView._runScripts !== "dangerously") {
      return;
    }
 
    // TODO: implement nomodule here, **but only after we support modules**.
 
    // At this point we completely depart from the spec.
 
    if (this.hasAttribute("src")) {
      resourceLoader.load(
        this,
        this.src,
        { defaultEncoding: whatwgEncoding.labelToName(this.getAttribute("charset")) || this._ownerDocument._encoding },
        this._innerEval
      );
    } else {
      resourceLoader.enqueue(this, this._ownerDocument.URL, this._innerEval)(null, this.text);
    }
  }
 
  _innerEval(text, filename) {
    this._ownerDocument._writeAfterElement = this;
    processJavaScript(this, text, filename);
    delete this._ownerDocument._writeAfterElement;
  }
 
  _getTypeString() {
    const typeAttr = this.getAttribute("type");
    const langAttr = this.getAttribute("language");
 
    if (typeAttr === "") {
      return "text/javascript";
    }
 
    if (typeAttr === null && langAttr === "") {
      return "text/javascript";
    }
 
    if (typeAttr === null && langAttr === null) {
      return "text/javascript";
    }
 
    if (typeAttr !== null) {
      return typeAttr.trim();
    }
 
    if (langAttr !== null) {
      return "text/" + langAttr;
    }
 
    return null;
  }
 
  get text() {
    return childTextContent(this);
  }
 
  set text(text) {
    this.textContent = text;
  }
 
  get src() {
    return reflectURLAttribute(this, "src");
  }
 
  set src(V) {
    this.setAttribute("src", V);
  }
}
 
function processJavaScript(element, code, filename) {
  const document = element.ownerDocument;
  const window = document && document._global;
 
  if (window) {
    document._currentScript = element;
 
    let lineOffset = 0;
    if (!element.src) {
      for (const child of domSymbolTree.childrenIterator(element)) {
        if (child.nodeType === nodeTypes.TEXT_NODE) {
          if (child.__location) {
            lineOffset = child.__location.line - 1;
          }
          break;
        }
      }
    }
 
    try {
      vm.runInContext(code, window, { filename, lineOffset, displayErrors: false });
    } catch (e) {
      reportException(window, e, filename);
    } finally {
      document._currentScript = null;
    }
  }
}
 
function getType(typeString) {
  const lowercased = asciiLowercase(typeString);
  // Cannot use whatwg-mimetype parsing because that strips whitespace. The spec demands a strict string comparison.
  // That is, the type="" attribute is not really related to MIME types at all.
  if (jsMIMETypes.has(lowercased)) {
    return "classic";
  }
  if (lowercased === "module") {
    return "module";
  }
  return null;
}
 
module.exports = {
  implementation: HTMLScriptElementImpl
};