Skip to content

Commit 7a16546

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 using the existing logic to handle table elements compilation. Closes angular#6289
1 parent 47ec6f5 commit 7a16546

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-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

+22
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

0 commit comments

Comments
 (0)