11var DOM = require ( '../src/dom.js' ) . DOM ;
2+ var normalizeHeaderToId = require ( '../src/dom.js' ) . normalizeHeaderToId ;
23
34describe ( 'dom' , function ( ) {
45 var dom ;
@@ -7,6 +8,31 @@ describe('dom', function() {
78 dom = new DOM ( ) ;
89 } ) ;
910
11+ describe ( 'html' , function ( ) {
12+ it ( 'should add ids to all h tags' , function ( ) {
13+ dom . html ( '<h1>Some Header</h1>' ) ;
14+ expect ( dom . toString ( ) ) . toContain ( '<h1 id="some-header">Some Header</h1>' ) ;
15+ } ) ;
16+
17+ it ( 'should collect <a name> anchors too' , function ( ) {
18+ dom . html ( '<h2>Xxx <a name="foo"></a> and bar <a name="bar"></a>' ) ;
19+ expect ( dom . anchors ) . toContain ( 'foo' ) ;
20+ expect ( dom . anchors ) . toContain ( 'bar' ) ;
21+ } )
22+ } ) ;
23+
24+ it ( 'should collect h tag ids' , function ( ) {
25+ dom . h ( 'Page Title' , function ( ) {
26+ dom . html ( '<h1>Second</h1>xxx <h2>Third</h2>' ) ;
27+ dom . h ( 'Another Header' , function ( ) { } ) ;
28+ } ) ;
29+
30+ expect ( dom . anchors ) . toContain ( 'page-title' ) ;
31+ expect ( dom . anchors ) . toContain ( 'second' ) ;
32+ expect ( dom . anchors ) . toContain ( 'second_third' ) ;
33+ expect ( dom . anchors ) . toContain ( 'another-header' ) ;
34+ } ) ;
35+
1036 describe ( 'h' , function ( ) {
1137
1238 it ( 'should render using function' , function ( ) {
@@ -25,7 +51,7 @@ describe('dom', function() {
2551 this . html ( '<h1>sub-heading</h1>' ) ;
2652 } ) ;
2753 expect ( dom . toString ( ) ) . toContain ( '<h1 id="heading">heading</h1>' ) ;
28- expect ( dom . toString ( ) ) . toContain ( '<h2>sub-heading</h2>' ) ;
54+ expect ( dom . toString ( ) ) . toContain ( '<h2 id="sub-heading" >sub-heading</h2>' ) ;
2955 } ) ;
3056
3157 it ( 'should properly number nested headings' , function ( ) {
@@ -40,12 +66,45 @@ describe('dom', function() {
4066
4167 expect ( dom . toString ( ) ) . toContain ( '<h1 id="heading">heading</h1>' ) ;
4268 expect ( dom . toString ( ) ) . toContain ( '<h2 id="heading2">heading2</h2>' ) ;
43- expect ( dom . toString ( ) ) . toContain ( '<h3>heading3</h3>' ) ;
69+ expect ( dom . toString ( ) ) . toContain ( '<h3 id="heading2_heading3" >heading3</h3>' ) ;
4470
4571 expect ( dom . toString ( ) ) . toContain ( '<h1 id="other1">other1</h1>' ) ;
46- expect ( dom . toString ( ) ) . toContain ( '<h2>other2</h2>' ) ;
72+ expect ( dom . toString ( ) ) . toContain ( '<h2 id="other2">other2</h2>' ) ;
73+ } ) ;
74+
75+
76+ it ( 'should add nested ids to all h tags' , function ( ) {
77+ dom . h ( 'Page Title' , function ( ) {
78+ dom . h ( 'Second' , function ( ) {
79+ dom . html ( 'some <h1>Third</h1>' ) ;
80+ } ) ;
81+ } ) ;
82+
83+ var resultingHtml = dom . toString ( ) ;
84+ expect ( resultingHtml ) . toContain ( '<h1 id="page-title">Page Title</h1>' ) ;
85+ expect ( resultingHtml ) . toContain ( '<h2 id="second">Second</h2>' ) ;
86+ expect ( resultingHtml ) . toContain ( '<h3 id="second_third">Third</h3>' ) ;
87+ } ) ;
88+
89+ } ) ;
90+
91+
92+ describe ( 'normalizeHeaderToId' , function ( ) {
93+ it ( 'should ignore content in the parenthesis' , function ( ) {
94+ expect ( normalizeHeaderToId ( 'One (more)' ) ) . toBe ( 'one' ) ;
95+ } ) ;
96+
97+ it ( 'should ignore html content' , function ( ) {
98+ expect ( normalizeHeaderToId ( 'Section <a name="section"></a>' ) ) . toBe ( 'section' ) ;
4799 } ) ;
48100
101+ it ( 'should ignore special characters' , function ( ) {
102+ expect ( normalizeHeaderToId ( 'Section \'!?' ) ) . toBe ( 'section' ) ;
103+ } ) ;
104+
105+ it ( 'should ignore html entities' , function ( ) {
106+ expect ( normalizeHeaderToId ( 'angular's-jqlite' ) ) . toBe ( 'angulars-jqlite' ) ;
107+ } ) ;
49108 } ) ;
50109
51110} ) ;
0 commit comments