Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix($compile): support templates with thead and tfoot root elements #6290

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
Suffix = 'Directive',
COMMENT_DIRECTIVE_REGEXP = /^\s*directive\:\s*([\d\w\-_]+)\s+(.*)$/,
CLASS_DIRECTIVE_REGEXP = /(([\d\w\-_]+)(?:\:([^;]+))?;?)/,
TABLE_CONTENT_REGEXP = /^<\s*(tr|th|td|tbody)(\s+[^>]*)?>/i;
TABLE_CONTENT_REGEXP = /^<\s*(tr|th|td|thead|tbody|tfoot)(\s+[^>]*)?>/i;

// Ref: http://developers.whatwg.org/webappapis.html#event-handler-idl-attributes
// The assumption is that future DOM event attribute names will begin with
Expand Down Expand Up @@ -1649,16 +1649,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
template = trim(template);
if ((type = TABLE_CONTENT_REGEXP.exec(template))) {
type = type[1].toLowerCase();
var table = jqLite('<table>' + template + '</table>'),
tbody = table.children('tbody'),
leaf = /(td|th)/.test(type) && table.find('tr');
if (tbody.length && type !== 'tbody') {
table = tbody;
}
if (leaf && leaf.length) {
table = leaf;
}
return table.contents();
var table = jqLite('<table>' + template + '</table>');
if (/(thead|tbody|tfoot)/.test(type)) return table.children(type);
table = table.children('tbody');
if (type === 'tr') return table.children('tr');
return table.children('tr').contents();
}
return jqLite('<div>' +
template +
Expand Down
48 changes: 48 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,10 +529,18 @@ describe('$compile', function() {
replace: true,
template: '<th>TH</th>'
}));
directive('replaceWithThead', valueFn({
replace: true,
template: '<thead><tr><td>TD</td></tr></thead>'
}));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's another section which runs essentially the same tests with asynchronous (templateUrl) directives, just for completeness you could add those too

directive('replaceWithTbody', valueFn({
replace: true,
template: '<tbody><tr><td>TD</td></tr></tbody>'
}));
directive('replaceWithTfoot', valueFn({
replace: true,
template: '<tfoot><tr><td>TD</td></tr></tfoot>'
}));
}));


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

it('should support templates with root <thead> tags', inject(function($compile, $rootScope) {
expect(function() {
element = $compile('<div replace-with-thead></div>')($rootScope);
}).not.toThrow();
expect(nodeName_(element)).toMatch(/thead/i);
}));

it('should support templates with root <tbody> tags', inject(function($compile, $rootScope) {
expect(function() {
element = $compile('<div replace-with-tbody></div>')($rootScope);
}).not.toThrow();
expect(nodeName_(element)).toMatch(/tbody/i);
}));

it('should support templates with root <tfoot> tags', inject(function($compile, $rootScope) {
expect(function() {
element = $compile('<div replace-with-tfoot></div>')($rootScope);
}).not.toThrow();
expect(nodeName_(element)).toMatch(/tfoot/i);
}));
});


Expand Down Expand Up @@ -833,10 +855,18 @@ describe('$compile', function() {
replace: true,
templateUrl: 'th.html'
}));
directive('replaceWithThead', valueFn({
replace: true,
templateUrl: 'thead.html'
}));
directive('replaceWithTbody', valueFn({
replace: true,
templateUrl: 'tbody.html'
}));
directive('replaceWithTfoot', valueFn({
replace: true,
templateUrl: 'tfoot.html'
}));
}
));

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

it('should support templates with root <thead> tags', inject(function($compile, $rootScope, $templateCache) {
$templateCache.put('thead.html', '<thead><tr><td>TD</td></tr></thead>');
expect(function() {
element = $compile('<div replace-with-thead></div>')($rootScope);
}).not.toThrow();
$rootScope.$digest();
expect(nodeName_(element)).toMatch(/thead/i);
}));

it('should support templates with root <tbody> tags', inject(function($compile, $rootScope, $templateCache) {
$templateCache.put('tbody.html', '<tbody><tr><td>TD</td></tr></tbody>');
expect(function() {
Expand All @@ -1508,6 +1547,15 @@ describe('$compile', function() {
$rootScope.$digest();
expect(nodeName_(element)).toMatch(/tbody/i);
}));

it('should support templates with root <tfoot> tags', inject(function($compile, $rootScope, $templateCache) {
$templateCache.put('tfoot.html', '<tfoot><tr><td>TD</td></tr></tfoot>');
expect(function() {
element = $compile('<div replace-with-tfoot></div>')($rootScope);
}).not.toThrow();
$rootScope.$digest();
expect(nodeName_(element)).toMatch(/tfoot/i);
}));
});


Expand Down