Skip to content

Commit 6a8130a

Browse files
committed
fix($compile): support templates with thead and tfoot root elements
If the first element in a template is a <thead> or a <tfoot>, then use the existing logic to handle table elements compilation. Closes angular#6289
1 parent 47ec6f5 commit 6a8130a

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

src/ng/compile.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
503503
Suffix = 'Directive',
504504
COMMENT_DIRECTIVE_REGEXP = /^\s*directive\:\s*([\d\w\-_]+)\s+(.*)$/,
505505
CLASS_DIRECTIVE_REGEXP = /(([\d\w\-_]+)(?:\:([^;]+))?;?)/,
506-
TABLE_CONTENT_REGEXP = /^<\s*(tr|th|td|tbody)(\s+[^>]*)?>/i;
506+
TABLE_CONTENT_REGEXP = /^<\s*(tr|th|td|thead|tbody|tfoot)(\s+[^>]*)?>/i;
507507

508508
// Ref: http://developers.whatwg.org/webappapis.html#event-handler-idl-attributes
509509
// The assumption is that future DOM event attribute names will begin with
@@ -1651,8 +1651,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
16511651
type = type[1].toLowerCase();
16521652
var table = jqLite('<table>' + template + '</table>'),
16531653
tbody = table.children('tbody'),
1654-
leaf = /(td|th)/.test(type) && table.find('tr');
1655-
if (tbody.length && type !== 'tbody') {
1654+
leaf = /(td|th)$/.test(type) && table.find('tr');
1655+
if (tbody.length && !/(thead|tbody|tfoot)/.test(type)) {
16561656
table = tbody;
16571657
}
16581658
if (leaf && leaf.length) {

test/ng/compileSpec.js

+48
Original file line numberDiff line numberDiff line change
@@ -529,10 +529,18 @@ describe('$compile', function() {
529529
replace: true,
530530
template: '<th>TH</th>'
531531
}));
532+
directive('replaceWithThead', valueFn({
533+
replace: true,
534+
template: '<thead><tr><td>TD</td></tr></thead>'
535+
}));
532536
directive('replaceWithTbody', valueFn({
533537
replace: true,
534538
template: '<tbody><tr><td>TD</td></tr></tbody>'
535539
}));
540+
directive('replaceWithTfoot', valueFn({
541+
replace: true,
542+
template: '<tfoot><tr><td>TD</td></tr></tfoot>'
543+
}));
536544
}));
537545

538546

@@ -718,12 +726,26 @@ describe('$compile', function() {
718726
expect(nodeName_(element)).toMatch(/th/i);
719727
}));
720728

729+
it('should support templates with root <thead> tags', inject(function($compile, $rootScope) {
730+
expect(function() {
731+
element = $compile('<div replace-with-thead></div>')($rootScope);
732+
}).not.toThrow();
733+
expect(nodeName_(element)).toMatch(/thead/i);
734+
}));
735+
721736
it('should support templates with root <tbody> tags', inject(function($compile, $rootScope) {
722737
expect(function() {
723738
element = $compile('<div replace-with-tbody></div>')($rootScope);
724739
}).not.toThrow();
725740
expect(nodeName_(element)).toMatch(/tbody/i);
726741
}));
742+
743+
it('should support templates with root <tfoot> tags', inject(function($compile, $rootScope) {
744+
expect(function() {
745+
element = $compile('<div replace-with-tfoot></div>')($rootScope);
746+
}).not.toThrow();
747+
expect(nodeName_(element)).toMatch(/tfoot/i);
748+
}));
727749
});
728750

729751

@@ -833,10 +855,18 @@ describe('$compile', function() {
833855
replace: true,
834856
templateUrl: 'th.html'
835857
}));
858+
directive('replaceWithThead', valueFn({
859+
replace: true,
860+
templateUrl: 'thead.html'
861+
}));
836862
directive('replaceWithTbody', valueFn({
837863
replace: true,
838864
templateUrl: 'tbody.html'
839865
}));
866+
directive('replaceWithTfoot', valueFn({
867+
replace: true,
868+
templateUrl: 'tfoot.html'
869+
}));
840870
}
841871
));
842872

@@ -1500,6 +1530,15 @@ describe('$compile', function() {
15001530
expect(nodeName_(element)).toMatch(/th/i);
15011531
}));
15021532

1533+
it('should support templates with root <thead> tags', inject(function($compile, $rootScope, $templateCache) {
1534+
$templateCache.put('thead.html', '<thead><tr><td>TD</td></tr></thead>');
1535+
expect(function() {
1536+
element = $compile('<div replace-with-thead></div>')($rootScope);
1537+
}).not.toThrow();
1538+
$rootScope.$digest();
1539+
expect(nodeName_(element)).toMatch(/thead/i);
1540+
}));
1541+
15031542
it('should support templates with root <tbody> tags', inject(function($compile, $rootScope, $templateCache) {
15041543
$templateCache.put('tbody.html', '<tbody><tr><td>TD</td></tr></tbody>');
15051544
expect(function() {
@@ -1508,6 +1547,15 @@ describe('$compile', function() {
15081547
$rootScope.$digest();
15091548
expect(nodeName_(element)).toMatch(/tbody/i);
15101549
}));
1550+
1551+
it('should support templates with root <tfoot> tags', inject(function($compile, $rootScope, $templateCache) {
1552+
$templateCache.put('tfoot.html', '<tfoot><tr><td>TD</td></tr></tfoot>');
1553+
expect(function() {
1554+
element = $compile('<div replace-with-tfoot></div>')($rootScope);
1555+
}).not.toThrow();
1556+
$rootScope.$digest();
1557+
expect(nodeName_(element)).toMatch(/tfoot/i);
1558+
}));
15111559
});
15121560

15131561

0 commit comments

Comments
 (0)