This repository was archived by the owner on Dec 18, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +22
-4
lines changed
src/app/shared/doc-viewer Expand file tree Collapse file tree 2 files changed +22
-4
lines changed Original file line number Diff line number Diff line change @@ -44,6 +44,20 @@ describe('DocViewer', () => {
44
44
expect ( docViewer . componentInstance . textContent ) . toBe ( 'my docs page' ) ;
45
45
} ) ;
46
46
47
+
48
+ it ( 'should correct hash based links' , ( ) => {
49
+ let fixture = TestBed . createComponent ( DocViewerTestComponent ) ;
50
+ fixture . componentRef . instance . documentUrl = `http://material.angular.io/doc-with-links.html` ;
51
+ fixture . detectChanges ( ) ;
52
+
53
+ const url = fixture . componentInstance . documentUrl ;
54
+ http . expectOne ( url ) . flush ( FAKE_DOCS [ url ] ) ;
55
+
56
+ let docViewer = fixture . debugElement . query ( By . directive ( DocViewer ) ) ;
57
+ // Our test runner runs at the page /context.html, so it will be the prepended value.
58
+ expect ( docViewer . nativeElement . innerHTML ) . toContain ( `href="/context.html#test"` ) ;
59
+ } ) ;
60
+
47
61
it ( 'should show error message when doc not found' , ( ) => {
48
62
spyOn ( console , 'log' ) ;
49
63
@@ -84,4 +98,5 @@ const FAKE_DOCS = {
84
98
'http://material.angular.io/doc-with-example.html' : `
85
99
<div>Check out this example:</div>
86
100
<div material-docs-example="some-example"></div>` ,
101
+ 'http://material.angular.io/doc-with-links.html' : `<a href="#test">Test link</a>` ,
87
102
} ;
Original file line number Diff line number Diff line change @@ -60,11 +60,14 @@ export class DocViewer implements OnDestroy {
60
60
}
61
61
62
62
/**
63
- * Updates the displayed document
64
- * @param document The raw document content to show.
63
+ * Updates the displayed document.
64
+ * @param rawDocument The raw document content to show.
65
65
*/
66
- private updateDocument ( document : string ) {
67
- this . _elementRef . nativeElement . innerHTML = document ;
66
+ private updateDocument ( rawDocument : string ) {
67
+ // Replaces all hash base links with the current path
68
+ const correctedDocument = rawDocument . replace (
69
+ / ( < a h r e f = " # ) + / g, `<a href="${ window . location . pathname } #` ) ;
70
+ this . _elementRef . nativeElement . innerHTML = correctedDocument ;
68
71
this . textContent = this . _elementRef . nativeElement . textContent ;
69
72
this . _loadComponents ( 'material-docs-example' , ExampleViewer ) ;
70
73
this . _loadComponents ( 'header-link' , HeaderLink ) ;
You can’t perform that action at this time.
0 commit comments