/** * DomParser.js * * Copyright 2010, Moxiecode Systems AB * Released under LGPL License. * * License: http://tinymce.moxiecode.com/license * Contributing: http://tinymce.moxiecode.com/contributing */ (function(tinymce) { var Node = tinymce.html.Node; /** * This class parses HTML code into a DOM like structure of nodes it will remove redundant whitespace and make * sure that the node tree is valid according to the specified schema. So for example:
a
b
c will becomea
b
c
* * @example * var parser = new tinymce.html.DomParser({validate: true}, schema); * var rootNode = parser.parse('in
for better results
cdata: function(text) {
node.append(createNode('#cdata', 4)).value = text;
},
text: function(text, raw) {
var textNode;
// Trim all redundant whitespace on non white space elements
if (!whiteSpaceElements[node.name]) {
text = text.replace(allWhiteSpaceRegExp, ' ');
if (node.lastChild && blockElements[node.lastChild.name])
text = text.replace(startWhiteSpaceRegExp, '');
}
// Do we need to create the node
if (text.length !== 0) {
textNode = createNode('#text', 3);
textNode.raw = !!raw;
node.append(textNode).value = text;
}
},
comment: function(text) {
node.append(createNode('#comment', 8)).value = text;
},
pi: function(name, text) {
node.append(createNode(name, 7)).value = text;
removeWhitespaceBefore(node);
},
doctype: function(text) {
var newNode;
newNode = node.append(createNode('#doctype', 10));
newNode.value = text;
removeWhitespaceBefore(node);
},
start: function(name, attrs, empty) {
var newNode, attrFiltersLen, elementRule, textNode, attrName, text, sibling, parent;
elementRule = validate ? schema.getElementRule(name) : {};
if (elementRule) {
newNode = createNode(elementRule.outputName || name, 1);
newNode.attributes = attrs;
newNode.shortEnded = empty;
node.append(newNode);
// Check if node is valid child of the parent node is the child is
// unknown we don't collect it since it's probably a custom element
parent = children[node.name];
if (parent && children[newNode.name] && !parent[newNode.name])
invalidChildren.push(newNode);
attrFiltersLen = attributeFilters.length;
while (attrFiltersLen--) {
attrName = attributeFilters[attrFiltersLen].name;
if (attrName in attrs.map) {
list = matchedAttributes[attrName];
if (list)
list.push(newNode);
else
matchedAttributes[attrName] = [newNode];
}
}
// Trim whitespace before block
if (blockElements[name])
removeWhitespaceBefore(newNode);
// Change current node if the element wasn't empty i.e not a
or
if (!empty)
node = newNode;
}
},
end: function(name) {
var textNode, elementRule, text, sibling, tempNode;
elementRule = validate ? schema.getElementRule(name) : {};
if (elementRule) {
if (blockElements[name]) {
if (!whiteSpaceElements[node.name]) {
// Trim whitespace at beginning of block
for (textNode = node.firstChild; textNode && textNode.type === 3; ) {
text = textNode.value.replace(startWhiteSpaceRegExp, '');
if (text.length > 0) {
textNode.value = text;
textNode = textNode.next;
} else {
sibling = textNode.next;
textNode.remove();
textNode = sibling;
}
}
// Trim whitespace at end of block
for (textNode = node.lastChild; textNode && textNode.type === 3; ) {
text = textNode.value.replace(endWhiteSpaceRegExp, '');
if (text.length > 0) {
textNode.value = text;
textNode = textNode.prev;
} else {
sibling = textNode.prev;
textNode.remove();
textNode = sibling;
}
}
}
// Trim start white space
textNode = node.prev;
if (textNode && textNode.type === 3) {
text = textNode.value.replace(startWhiteSpaceRegExp, '');
if (text.length > 0)
textNode.value = text;
else
textNode.remove();
}
}
// Handle empty nodes
if (elementRule.removeEmpty || elementRule.paddEmpty) {
if (node.isEmpty(nonEmptyElements)) {
if (elementRule.paddEmpty)
node.empty().append(new Node('#text', '3')).value = '\u00a0';
else {
// Leave nodes that have a name like
if (!node.attributes.map.name) {
tempNode = node.parent;
node.empty().remove();
node = tempNode;
return;
}
}
}
}
node = node.parent;
}
}
}, schema);
rootNode = node = new Node(args.context || settings.root_name, 11);
parser.parse(html);
// Fix invalid children or report invalid children in a contextual parsing
if (validate && invalidChildren.length) {
if (!args.context)
fixInvalidChildren(invalidChildren);
else
args.invalid = true;
}
// Wrap nodes in the root into block elements if the root is body
if (rootBlockName && rootNode.name == 'body')
addRootBlocks();
// Run filters only when the contents is valid
if (!args.invalid) {
// Run node filters
for (name in matchedNodes) {
list = nodeFilters[name];
nodes = matchedNodes[name];
// Remove already removed children
fi = nodes.length;
while (fi--) {
if (!nodes[fi].parent)
nodes.splice(fi, 1);
}
for (i = 0, l = list.length; i < l; i++)
list[i](nodes, name, args);
}
// Run attribute filters
for (i = 0, l = attributeFilters.length; i < l; i++) {
list = attributeFilters[i];
if (list.name in matchedAttributes) {
nodes = matchedAttributes[list.name];
// Remove already removed children
fi = nodes.length;
while (fi--) {
if (!nodes[fi].parent)
nodes.splice(fi, 1);
}
for (fi = 0, fl = list.callbacks.length; fi < fl; fi++)
list.callbacks[fi](nodes, list.name, args);
}
}
}
return rootNode;
};
// Remove
at end of block elements Gecko and WebKit injects BR elements to
// make it possible to place the caret inside empty blocks. This logic tries to remove
// these elements and keep br elements that where intended to be there intact
if (settings.remove_trailing_brs) {
self.addNodeFilter('br', function(nodes, name) {
var i, l = nodes.length, node, blockElements = schema.getBlockElements(),
nonEmptyElements = schema.getNonEmptyElements(), parent, prev, prevName;
// Remove brs from body element as well
blockElements.body = 1;
// Must loop forwards since it will otherwise remove all brs in
structure then don't remove anything
if (prevName === 'br') {
node = null;
break;
}
}
prev = prev.prev;
}
if (node) {
node.remove();
// Is the parent to be considered empty after we removed the BR
if (parent.isEmpty(nonEmptyElements)) {
elementRule = schema.getElementRule(parent.name);
// Remove or padd the element depending on schema rule
if (elementRule) {
if (elementRule.removeEmpty)
parent.remove();
else if (elementRule.paddEmpty)
parent.empty().append(new tinymce.html.Node('#text', 3)).value = '\u00a0';
}
}
}
}
}
});
}
}
})(tinymce);