CMunger.mungePriority Member

Syntax

object.mungePriority(priority, text, containerTag, data);

Arguments

ArgumentSummary
priority
text
containerTag
data

Returns

Remarks

See Also

Source Code

function mng_mungePriority(priority, text, containerTag, data)
{
var matches = new Object();
var entry;
// Find all the matches in this priority
for (entry in this.entries[priority])
{
var munger = this.entries[priority][entry];
if (!munger.enabled)
continue;
var match = null;
if (typeof munger.lambdaMatch == "function")
{
var rval = munger.lambdaMatch(text, containerTag, data, munger);
if (typeof rval == "string")
match = { start: text.indexOf(rval), text: rval };
else if (typeof rval == "object")
match = rval;
}
else
{
var ary = text.match(munger.regex);
if ((ary != null) && (ary[1]))
match = { start: text.indexOf(ary[1]), text: ary[1] };
}
if (match && (match.start >= 0))
{
match.munger = munger;
matches[entry] = match;
}
}
// Find the first matching entry...
var firstMatch = { start: text.length, munger: null };
var firstPriority = 0;
for (entry in matches)
{
// If it matches before the existing first, or at the same spot but
// with a higher start-priority, this is a better match.
if (matches[entry].start < firstMatch.start ||
((matches[entry].start == firstMatch.start) &&
(this.entries[priority][entry].startPriority > firstPriority)))
{
firstMatch = matches[entry];
firstPriority = this.entries[priority][entry].startPriority;
}
}
// Replace it.
if (firstMatch.munger)
{
var munger = firstMatch.munger;
firstMatch.end = firstMatch.start + firstMatch.text.length;
// Need to deal with the text before the match, if there is any.
var beforeText = text.substr(0, firstMatch.start);
if (firstMatch.start > 0)
this.munge(beforeText, containerTag, data);
if (typeof munger.lambdaReplace == "function")
{
// The munger rule itself should take care of munging the 'inside'
// of the match.
munger.lambdaReplace(firstMatch.text, containerTag, data, munger);
this.munge(text.substr(firstMatch.end), containerTag, data);
return containerTag;
}
else
{
var tag = document.createElementNS(XHTML_NS, munger.tagName);
tag.setAttribute("class", munger.className + calcClass(data));
// Don't let this rule match again when we recurse.
munger.enabled = false;
this.munge(firstMatch.text, tag, data);
munger.enabled = true;
containerTag.appendChild(tag);
this.munge(text.substr(firstMatch.end), containerTag, data);
return containerTag;
}
}
return null;
}