MCPcopy
hub / github.com/webpack/webpack / scriptWithType

Function scriptWithType

test/HtmlParser.unittest.js:509–548  ·  view source on GitHub ↗
(/** @type {string} */ source)

Source from the content-addressed store, hash-verified

507 // Build a `<script type=… src=…>` AST element with offsets derived from the
508 // source so reconcileScriptTypeAttr sees the real attribute spans.
509 const scriptWithType = (/** @type {string} */ source) => {
510 /**
511 * @param {string} name attribute name
512 * @returns {EXPECTED_ANY} attribute
513 */
514 const attr = (name) => {
515 const nameStart = source.indexOf(`${name}=`);
516 const nameEnd = nameStart + name.length;
517 const afterEq = nameEnd + 1;
518 const quote = source[afterEq] === '"' || source[afterEq] === "'";
519 const valueStart = quote ? afterEq + 1 : afterEq;
520 const end = source.indexOf(quote ? source[afterEq] : " ", valueStart);
521 const valueEnd = end === -1 ? source.indexOf(">") : end;
522 return {
523 name,
524 value: source.slice(valueStart, valueEnd),
525 nameStart,
526 nameEnd,
527 valueStart,
528 valueEnd
529 };
530 };
531 return {
532 type: NodeType.Document,
533 children: [
534 {
535 type: NodeType.Element,
536 tagName: "script",
537 namespace: 0,
538 attributes: [attr("type"), attr("src")],
539 children: [],
540 selfClosing: false,
541 start: 0,
542 end: source.length,
543 tagEnd: source.indexOf(">") + 1,
544 nameEnd: "<script".length
545 }
546 ]
547 };
548 };
549
550 it.each([
551 ["<script type='module' src='a.js'></script>"],

Callers 1

Calls 1

attrFunction · 0.85

Tested by

no test coverage detected