2012-05-26 17:30:32 +00:00
|
|
|
/*\
|
2012-11-15 12:53:10 +00:00
|
|
|
title: $:/core/modules/parsers/wikitextparser/rules/html.js
|
2012-05-26 17:30:32 +00:00
|
|
|
type: application/javascript
|
2012-05-27 08:19:37 +00:00
|
|
|
module-type: wikitextrule
|
2012-05-26 17:30:32 +00:00
|
|
|
|
2012-06-05 21:54:36 +00:00
|
|
|
Wiki text block rule for block and run level HTML elements. For example:
|
|
|
|
|
|
|
|
{{{
|
|
|
|
<aside>
|
|
|
|
This is an HTML5 aside element
|
|
|
|
</aside>
|
|
|
|
}}}
|
2012-05-26 17:30:32 +00:00
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
exports.name = "html";
|
|
|
|
|
2012-05-26 23:22:58 +00:00
|
|
|
exports.blockParser = true;
|
|
|
|
exports.runParser = true;
|
|
|
|
|
2012-05-26 17:30:32 +00:00
|
|
|
exports.regExpString = "<[A-Za-z]+\\s*[^>]*>";
|
|
|
|
|
2012-05-28 14:51:52 +00:00
|
|
|
exports.parse = function(match,isBlock) {
|
2012-05-26 17:30:32 +00:00
|
|
|
var reStart = /<([A-Za-z]+)(\s*[^>]*)>/mg,
|
2012-07-15 22:06:51 +00:00
|
|
|
reLineBreak = /(\r?\n)/mg,
|
2012-05-26 17:30:32 +00:00
|
|
|
reAttr = /\s*([A-Za-z\-_]+)(?:\s*=\s*(?:("[^"]*")|('[^']*')|([^"'\s]+)))?/mg;
|
2012-07-15 22:06:51 +00:00
|
|
|
// Process the start regexp to get the attribute portion
|
2012-05-26 17:30:32 +00:00
|
|
|
reStart.lastIndex = this.pos;
|
|
|
|
var startMatch = reStart.exec(this.source);
|
|
|
|
if(startMatch && startMatch.index === this.pos) {
|
2012-07-15 22:06:51 +00:00
|
|
|
// Process the attributes
|
2012-05-26 17:30:32 +00:00
|
|
|
var attrMatch = reAttr.exec(startMatch[2]),
|
|
|
|
attributes = {};
|
|
|
|
while(attrMatch) {
|
|
|
|
var name = attrMatch[1],
|
|
|
|
value;
|
|
|
|
if(attrMatch[2]) { // Double quoted
|
|
|
|
value = attrMatch[2].substring(1,attrMatch[2].length-1);
|
|
|
|
} else if(attrMatch[3]) { // Single quoted
|
|
|
|
value = attrMatch[3].substring(1,attrMatch[3].length-1);
|
|
|
|
} else if(attrMatch[4]) { // Unquoted
|
|
|
|
value = attrMatch[4];
|
|
|
|
} else { // Valueless
|
|
|
|
value = true; // TODO: We should have a way of indicating we want an attribute without a value
|
|
|
|
}
|
|
|
|
attributes[name] = value;
|
|
|
|
attrMatch = reAttr.exec(startMatch[2]);
|
|
|
|
}
|
|
|
|
this.pos = startMatch.index + startMatch[0].length;
|
2012-07-15 22:06:51 +00:00
|
|
|
// Check for a line break immediate after the opening tag
|
|
|
|
reLineBreak.lastIndex = this.pos;
|
|
|
|
var lineBreakMatch = reLineBreak.exec(this.source);
|
|
|
|
if(lineBreakMatch && lineBreakMatch.index === this.pos) {
|
|
|
|
this.pos = lineBreakMatch.index + lineBreakMatch[0].length;
|
|
|
|
isBlock = true;
|
|
|
|
} else {
|
|
|
|
isBlock = false;
|
|
|
|
}
|
2012-06-10 16:26:54 +00:00
|
|
|
var reEndString = "(</" + startMatch[1] + ">)",
|
|
|
|
reEnd = new RegExp(reEndString,"mg"),
|
2012-06-09 14:08:30 +00:00
|
|
|
content;
|
|
|
|
if(isBlock) {
|
2012-06-10 16:26:54 +00:00
|
|
|
content = this.parseBlocks(reEndString);
|
2012-06-09 14:08:30 +00:00
|
|
|
} else {
|
|
|
|
content = this.parseRun(reEnd);
|
|
|
|
}
|
|
|
|
var element = $tw.Tree.Element(startMatch[1],attributes,content);
|
2012-05-26 17:30:32 +00:00
|
|
|
reEnd.lastIndex = this.pos;
|
|
|
|
match = reEnd.exec(this.source);
|
|
|
|
if(match && match.index === this.pos) {
|
|
|
|
this.pos = match.index + match[0].length;
|
|
|
|
}
|
|
|
|
return [element];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|