Skip to content

Commit

Permalink
Поправил работу с комментариями в HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
hcodes committed Mar 29, 2018
1 parent 467b5e6 commit bb29ede
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 92 deletions.
83 changes: 34 additions & 49 deletions src/safe-tags.js
Expand Up @@ -34,7 +34,6 @@ export default class SafeTags {
};

this._groups = ['own', 'html', 'url'];
this._reservedGroups = [].concat(this._groups).reverse();
}

/**
Expand All @@ -50,50 +49,51 @@ export default class SafeTags {
* Show safe tags.
*
* @param {Object} context
* @param {Function} callback
* @param {string} group
*/
show(context, callback) {
show(context, group) {
const label = Typograf._privateLabel;
const reReplace = new RegExp(label + 'tf\\d+' + label, 'g');
const reSearch = new RegExp(label + 'tf\\d');
const replaceLabel = function(match) {
return context.safeTags.hidden[context.safeTags.group][match] || match;
return context.safeTags.hidden[group][match] || match;
};

this._reservedGroups.forEach(function(group) {
context.safeTags.group = group;

for (let i = 0, len = this._tags[group].length; i < len; i++) {
context.text = context.text.replace(reReplace, replaceLabel);
if (context.text.search(reSearch) === -1) { break; }
}

callback(context, group);
}, this);

context.safeTags = null;
for (let i = 0, len = this._tags[group].length; i < len; i++) {
context.text = context.text.replace(reReplace, replaceLabel);
if (context.text.search(reSearch) === -1) { break; }
}
}

/**
* Hide safe tags.
*
* @param {Object} context
* @param {Function} callback
* @param {string} group
*/
hide(context, callback) {
context.safeTags = {
hidden: {},
i: 0
};
hide(context, group) {
context.safeTags = context.safeTags || { hidden: {}, i: 0 };
context.safeTags.hidden[group] = {};

this._groups.forEach(function(group) {
context.safeTags.hidden[group] = {};
const pasteLabel = this._pasteLabel.bind(this, context, group);
this._tags[group].forEach(function(tag) {
context.text = context.text.replace(this._prepareRegExp(tag), pasteLabel);
}, this);
}

this._groups.forEach(function(group) {
this._hide(context, group);
callback(context, group);
}, this);
/**
* Hide HTML tags.
*
* @param {Object} context
*/
hideHTMLTags(context) {
if (context.isHTML) {
const pasteLabel = this._pasteLabel.bind(this, context, 'html');
context.text = context.text
.replace(/<\/?[a-z][^]*?>/gi, pasteLabel) // Tags
.replace(/&lt;\/?[a-z][^]*?&gt;/gi, pasteLabel) // Escaping tags
.replace(/&[gl]t;/gi, pasteLabel);
}
}

/**
Expand Down Expand Up @@ -188,28 +188,13 @@ export default class SafeTags {
return result;
}

_hide(context, group) {
function pasteLabel(match) {
const safeTags = context.safeTags;
const key = Typograf._privateLabel + 'tf' + safeTags.i + Typograf._privateLabel;
safeTags.hidden[context.safeTags.group][key] = match;
safeTags.i++;

return key;
}

context.safeTags.group = group;

this._tags[group].forEach(function(tag) {
context.text = context.text.replace(this._prepareRegExp(tag), pasteLabel);
}, this);
_pasteLabel(context, group, match) {
const safeTags = context.safeTags;
const key = Typograf._privateLabel + 'tf' + safeTags.i + Typograf._privateLabel;
safeTags.hidden[group][key] = match;
safeTags.i++;

if (group === 'html' && context.isHTML) {
context.text = context.text
.replace(/<\/?[a-z][^]*?>/gi, pasteLabel) // Tags
.replace(/&lt;\/?[a-z][^]*?&gt;/gi, pasteLabel) // Escaping tags
.replace(/&[gl]t;/gi, pasteLabel);
}
return key;
}

_prepareRegExp(tag) {
Expand Down
85 changes: 42 additions & 43 deletions src/typograf.js
Expand Up @@ -191,9 +191,7 @@ export default class Typograf {

this._preparePrefs(context, prefs);

return !context.isHTML || context.prefs.processingSeparateParts === false ?
this._processAll(context) :
this._processSeparateParts(context);
return this._process(context);
}

_prepareContext(text) {
Expand Down Expand Up @@ -252,6 +250,10 @@ export default class Typograf {
}

_splitBySeparateParts(context) {
if (!context.isHTML || context.prefs.processingSeparateParts === false) {
return [ context.text ];
}

const
text = [],
label = Typograf._privateSeparateLabel,
Expand Down Expand Up @@ -284,62 +286,59 @@ export default class Typograf {
return text;
}

_processSeparateParts(context) {
const
isHTML = context.isHTML,
re = new RegExp(Typograf._privateSeparateLabel, 'g');

context.text = this._splitBySeparateParts(context)
.map(function(item) {
context.text = item;
context.isHTML = this._isHTML(item);

this._processStart(context);

return context.text.replace(re, '');
}, this)
.join('');
_process(context) {
context.text = this._removeCR(context.text);

context.isHTML = isHTML;
this._executeRules(context, 'start');

return this._processEnd(context);
}
this._safeTags.hide(context, 'own');
this._executeRules(context, 'hide-safe-tags-own');

_processAll(context) {
return this._processStart(context)._processEnd(context);
}
this._safeTags.hide(context, 'html');
this._executeRules(context, 'hide-safe-tags-html');

const
isHTML = context.isHTML,
re = new RegExp(Typograf._privateSeparateLabel, 'g');

_processStart(context) {
context.text = this._removeCR(context.text);
context.text = this._splitBySeparateParts(context).map(function(item) {
context.text = item;
context.isHTML = this._isHTML(item);
this._safeTags.hideHTMLTags(context);

this._executeRules(context, 'start');
this._safeTags.hide(context, 'url');
this._executeRules(context, 'hide-safe-tags-url');

this._safeTags.hide(context, (item, group) => {
this._executeRules(item, 'hide-safe-tags-' + group);
});
this._executeRules(context, 'hide-safe-tags');

this._executeRules(context, 'hide-safe-tags');
Typograf.HtmlEntities.toUtf(context);

Typograf.HtmlEntities.toUtf(context);
if (this._prefs.live) {
context.text = Typograf._replaceNbsp(context.text);
}

if (this._prefs.live) { context.text = Typograf._replaceNbsp(context.text); }
this._executeRules(context, 'utf');

this._executeRules(context, 'utf');
this._executeRules(context);

this._executeRules(context);
Typograf.HtmlEntities.restore(context);

Typograf.HtmlEntities.restore(context);
this._executeRules(context, 'html-entities');

this._executeRules(context, 'html-entities');
this._safeTags.show(context, 'url');
this._executeRules(context, 'show-safe-tags-url');

return context.text.replace(re, '');
}, this).join('');

context.isHTML = isHTML;

this._safeTags.show(context, (item, group) => {
this._executeRules(item, 'show-safe-tags-' + group);
});
this._safeTags.show(context, 'html');
this._executeRules(context, 'show-safe-tags-html');

return this;
}
this._safeTags.show(context, 'own');
this._executeRules(context, 'show-safe-tags-own');

_processEnd(context) {
this._executeRules(context, 'end');

return this._fixLineEnding(context.text, context.prefs.lineEnding);
Expand Down
4 changes: 4 additions & 0 deletions test/test.smoke.common.js
Expand Up @@ -32,6 +32,10 @@ const commonTests = [
' <!- Hello world! -> ',
'<! - Hello world! →'
],
[
'<!-- <h3><a href="index.html">Фотография</a></h3> -->',
'<!-- <h3><a href="index.html">Фотография</a></h3> -->'
],
[
' <a \n href="#hash"> Hello world! </a> ',
'<a \n href="#hash"> Hello world! </a>'
Expand Down

0 comments on commit bb29ede

Please sign in to comment.