Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 26x 26x 26x 1556x 1x 26x 6x 6x 6x 6x 26x 11x 3x 2x 2x 3x 1x 26x 6x 6x 26x 4x 4x 4x 26x 6x 6x 6x 6x 2x 4x 4x 4x | import { HtmlCompilerContext, HtmlCompilerModule } from '../htmlCompiler'; import { AnchorNodeResolve, CompiledAnchorNode, FragmentContext } from '../../..'; import Path from 'path'; /** * Compiles anchor tags (<a>) */ export class AnchorModule implements HtmlCompilerModule { enterNode(htmlContext: HtmlCompilerContext): void { // if this is an anchor node, then compile it if (CompiledAnchorNode.isCompiledAnchorNode(htmlContext.node)) { compileAnchorNode(htmlContext.node, htmlContext); } } } /** * Compile a {@link CompiledAnchorNode}. * * @param anchorNode Node to compiled * @param htmlContext Current context */ export function compileAnchorNode(anchorNode: CompiledAnchorNode, htmlContext: HtmlCompilerContext): void { // resolve href anchorNode.href = resolveAnchorHref(anchorNode.href, anchorNode.resolve, htmlContext.pipelineContext.fragmentContext); // replace with uncompiled const uncompiledAnchor = anchorNode.toUncompiled(); anchorNode.replaceSelf([ uncompiledAnchor ]); htmlContext.setDeleted(); } /** * Compute the final value of the "href" attribute given a context and resolve mode. * @param href Original "href" value * @param resolve Resolve mode to use * @param fragmentContext Context of the current fragment * @return The final "href" value */ export function resolveAnchorHref(href: string, resolve: AnchorNodeResolve, fragmentContext: FragmentContext): string { switch (resolve) { // process ROOT resolve case AnchorNodeResolve.ROOT: return resolveAnchorHrefRoot(href, fragmentContext); // process LOCAL resolve case AnchorNodeResolve.LOCAL: return resolveAnchorHrefLocal(href, fragmentContext); case AnchorNodeResolve.BASE: return resolveAnchorHrefBase(href, fragmentContext); // no-op for NONE case AnchorNodeResolve.NONE: return href; default: throw new Error(`Invalid value for AnchorNodeResolve: "${ resolve }"`); } } /** * Resolve the value of the "href" attribute relative to the root fragment. * See {@link AnchorNodeResolve} for more details. * @param href Original href value * @param fragmentContext Context of the current fragment. * @return The final href value */ export function resolveAnchorHrefRoot(href: string, fragmentContext: FragmentContext): string { // extract directory name const rootFragmentDir = Path.dirname(fragmentContext.rootResPath); // combine with href return `${ rootFragmentDir }${ Path.sep }${ href }`; } /** * Resolve the value of the "href" attribute relative to the current fragment. * See {@link AnchorNodeResolve} for more details. * @param href Original href value * @param fragmentContext Context of the current fragment. * @return The final href value */ export function resolveAnchorHrefLocal(href: string, fragmentContext: FragmentContext): string { // get path to current fragment const fragmentPath = fragmentContext.fragmentResPath; // extract directory name const fragmentDir = Path.dirname(fragmentPath); // combine with href return `${ fragmentDir }${ Path.sep }${ href }`; } /** * Resolve the value of the "href" attribute relative to the project base path. * See {@link AnchorNodeResolve} for more details. * @param href Original href value * @param fragmentContext Context of the current fragment. * @return The final href value */ export function resolveAnchorHrefBase(href: string, fragmentContext: FragmentContext): string { // extract directory name const rootFragmentDir = Path.dirname(fragmentContext.rootResPath); // normalize and flatten directory (remove ../ and ./) const normalizedDir = Path.normalize(rootFragmentDir); const flattenedDir = normalizedDir.replace(/^\.([/\\]|$)/, ''); // if the base path is the root, then just use href as is if (flattenedDir === '') { return href; } // invert directory const invertedDir = flattenedDir.replace(/[^/\\]+/g, '..'); // trim trailing / const trimmedDir = invertedDir.replace(/[/\\]$/, ''); // combine with href return `${ trimmedDir }/${ href }`; } |