Skip to content

Commit e4d2e61

Browse files
committed
Use the firstElementChild property to support templates with whitespaces
1 parent d25f8de commit e4d2e61

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/services/builder.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,19 @@ angular.module('schemaForm').provider('sfBuilder', ['sfPathProvider', function(s
1111
};
1212
var formId = 0;
1313

14+
if (!("firstElementChild" in document.createDocumentFragment())) {
15+
Object.defineProperty(DocumentFragment.prototype, "firstElementChild", {
16+
get: function () {
17+
for (var nodes = this.childNodes, n, i = 0, l = nodes.length; i < l; ++i)
18+
if (n = nodes[i], 1 === n.nodeType) return n;
19+
return null;
20+
}
21+
});
22+
}
23+
1424
var builders = {
1525
sfField: function(args) {
16-
args.fieldFrag.firstChild.setAttribute('sf-field', formId);
26+
args.fieldFrag.firstElementChild.setAttribute('sf-field', formId);
1727

1828
// We use a lookup table for easy access to our form.
1929
args.lookup['f' + formId] = args.form;

test/directives/schema-form-test.js

+29
Original file line numberDiff line numberDiff line change
@@ -1902,6 +1902,35 @@ describe('directive',function(){
19021902
});
19031903
});
19041904

1905+
it('should use supplied template with leading whitespace in template field',function() {
1906+
1907+
inject(function($compile, $rootScope){
1908+
var scope = $rootScope.$new();
1909+
scope.person = {};
1910+
1911+
scope.schema = {
1912+
type: 'object',
1913+
properties: {
1914+
name: {type: 'string'}
1915+
}
1916+
};
1917+
1918+
scope.form = [
1919+
{
1920+
type: 'template',
1921+
template: ' <div>{{form.foo}}</div>',
1922+
foo: "Hello World"
1923+
}
1924+
];
1925+
1926+
var tmpl = angular.element('<form sf-schema="schema" sf-form="form" sf-model="person"></form>');
1927+
1928+
$compile(tmpl)(scope);
1929+
$rootScope.$apply();
1930+
tmpl.html().should.be.eq(' <div sf-field="0" class="ng-scope ng-binding">Hello World</div>');
1931+
});
1932+
});
1933+
19051934
it('should load template by templateUrl, with template field type',function() {
19061935

19071936
inject(function($compile, $rootScope, $httpBackend){

0 commit comments

Comments
 (0)