|
1 | 1 | import { type ManifestResource, type ParsedManifest, parseManifest } from './manifest-parser.js'; |
| 2 | +import { |
| 3 | + dirnamePackagePath, |
| 4 | + isExternalPackageHref, |
| 5 | + joinPackagePath, |
| 6 | + resolvePackagePath, |
| 7 | +} from './package-path.js'; |
2 | 8 |
|
3 | 9 | export type PackageDiagnosticSeverity = 'info' | 'warning' | 'error'; |
4 | 10 |
|
@@ -385,7 +391,7 @@ function discoverReferences(xml: string, owner: PackageResourceNode): { |
385 | 391 | } { |
386 | 392 | const references: PackageReference[] = []; |
387 | 393 | const assets: PackageAssetRef[] = []; |
388 | | - const basePath = owner.resolvedHref ? dirname(owner.resolvedHref) : ''; |
| 394 | + const basePath = owner.resolvedHref ? dirnamePackagePath(owner.resolvedHref) : ''; |
389 | 395 |
|
390 | 396 | for (const match of findAttributeReferences(xml)) { |
391 | 397 | const resolvedPath = resolvePackagePath(basePath, match.rawHref); |
@@ -433,7 +439,7 @@ function findAttributeReferences(xml: string): Array<{ |
433 | 439 | const attrs = match.groups?.attrs ?? ''; |
434 | 440 | const attribute = element === 'object' ? 'data' : element === 'img' ? 'src' : 'href'; |
435 | 441 | const rawHref = readAttribute(attrs, attribute) ?? readAttribute(attrs, 'src') ?? readAttribute(attrs, 'href'); |
436 | | - if (!rawHref || isExternalHref(rawHref)) continue; |
| 442 | + if (!rawHref || isExternalPackageHref(rawHref)) continue; |
437 | 443 | refs.push({ |
438 | 444 | kind: kindFromElement(element), |
439 | 445 | element, |
@@ -583,35 +589,6 @@ function readAttribute(attrs: string, name: string): string | undefined { |
583 | 589 | return match?.[1]; |
584 | 590 | } |
585 | 591 |
|
586 | | -function isExternalHref(href: string): boolean { |
587 | | - return /^(?:[a-z][a-z0-9+.-]*:|\/\/|#)/i.test(href); |
588 | | -} |
589 | | - |
590 | | -function joinPackagePath(...parts: Array<string | undefined>): string { |
591 | | - return parts.filter(Boolean).join('/'); |
592 | | -} |
593 | | - |
594 | | -function resolvePackagePath(basePath: string | undefined, href: string): string { |
595 | | - const raw = href.replaceAll('\\', '/'); |
596 | | - const combined = raw.startsWith('/') ? raw.slice(1) : joinPackagePath(basePath, raw); |
597 | | - const normalized: string[] = []; |
598 | | - for (const part of combined.split('/')) { |
599 | | - if (!part || part === '.') continue; |
600 | | - if (part === '..') { |
601 | | - normalized.pop(); |
602 | | - continue; |
603 | | - } |
604 | | - normalized.push(part); |
605 | | - } |
606 | | - return normalized.join('/'); |
607 | | -} |
608 | | - |
609 | | -function dirname(path: string): string { |
610 | | - const normalized = path.replaceAll('\\', '/'); |
611 | | - const index = normalized.lastIndexOf('/'); |
612 | | - return index === -1 ? '' : normalized.slice(0, index); |
613 | | -} |
614 | | - |
615 | 592 | async function packagePathExists( |
616 | 593 | path: string, |
617 | 594 | fileAccess: PackageFileAccess, |
|
0 commit comments