Skip to content

Commit 259815d

Browse files
author
scottjehl
committed
Make data-role=page elements optional. This change makes the framework more lenient with markup, and will ease integration with existing sites, as well as mashups with external content. Unit tests included. Fixes jquery-archive#2096
1 parent 53a8023 commit 259815d

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

js/jquery.mobile.init.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@
7373
initializePage: function(){
7474
//find present pages
7575
var $pages = $( ":jqmData(role='page')" );
76+
77+
//if no pages are found, create one with body's inner html
78+
if( !$pages.length ){
79+
$pages = $( "body" ).wrapInner( "<div data-" + $.mobile.ns + "role='page'></div>" ).children( 0 );
80+
console.log("page wasn't needed")
81+
}
7682

7783
//add dialogs, set data-url attrs
7884
$pages.add( ":jqmData(role='dialog')" ).each(function(){

js/jquery.mobile.navigation.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,9 @@
648648
&& RegExp.$1 ) {
649649
url = fileUrl = path.getFilePath( RegExp.$1 );
650650
}
651+
else{
652+
653+
}
651654

652655
if ( base ) {
653656
base.set( fileUrl );
@@ -656,6 +659,11 @@
656659
//workaround to allow scripts to execute when included in page divs
657660
all.get( 0 ).innerHTML = html;
658661
page = all.find( ":jqmData(role='page'), :jqmData(role='dialog')" ).first();
662+
663+
//if page elem couldn't be found, create one and insert the body element's contents
664+
if( !page.length ){
665+
page = $( "<div data-" + $.mobile.ns + "role='page'>" + html.split( /<\/?body[^>]*>/gmi )[1] + "</div>" );
666+
}
659667

660668
if ( newPageTitle && !page.jqmData( "title" ) ) {
661669
page.jqmData( "title", newPageTitle );

tests/unit/init/init_core.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,5 +212,27 @@
212212
}, 500);
213213
});
214214

215+
asyncTest( "page element is generated when not present in initial markup", function(){
216+
expect( 1 );
217+
218+
$("<iframe src='nopage.html'>").appendTo("body").load(function(){
219+
ok( $(this).contents().find( ".ui-page" ).length, 1 );
220+
$(this).remove();
221+
start();
222+
});
223+
});
224+
225+
asyncTest( "page element is generated when not present in ajax'd markup", function(){
226+
expect( 1 );
227+
$.mobile.changePage( "nopage.html" );
228+
229+
$( ":jqmData(url$='nopage.html')" ).live( "pagecreate", function(){
230+
ok(true, "page was created from dynamically loaded HTML that contained no page div" );
231+
start();
232+
} );
233+
});
234+
235+
236+
215237
});
216238
})(jQuery);

tests/unit/init/nopage.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>jQuery Mobile Init Test Suite</title>
6+
<script src="../../../js/jquery.js"></script>
7+
<script src="../jquery.setNameSpace.js"></script>
8+
<script src="../../../js/"></script>
9+
10+
</head>
11+
12+
<p>testing</p>
13+
14+
</body>
15+
</html>

0 commit comments

Comments
 (0)