jenkins-bot has submitted this change and it was merged.
Change subject: Refactored link handler for code readability
......................................................................
Refactored link handler for code readability
* Split up a couple large, hard to comprehend conditions into
their own helper methods. These functions could possibly be
simplified further later on.
* No change in parser test results.
Change-Id: I1527ba33fb7d9fea2764cad27eb7110f5ab50656
---
M lib/mediawiki.WikitextSerializer.js
1 file changed, 86 insertions(+), 76 deletions(-)
Approvals:
Marcoil: Looks good to me, approved
jenkins-bot: Verified
diff --git a/lib/mediawiki.WikitextSerializer.js
b/lib/mediawiki.WikitextSerializer.js
index 4747abd..dbd6766 100644
--- a/lib/mediawiki.WikitextSerializer.js
+++ b/lib/mediawiki.WikitextSerializer.js
@@ -1562,7 +1562,7 @@
[ { name: 'link', value: link,
cond: !(link && link.value === resource.value) },
{ name: 'alt', value: alt, cond: alt.value !== null },
- { name: 'lang', value: lang, cond: lang.value !== null },
+ { name: 'lang', value: lang, cond: lang.value !== null }
].forEach(function(o) {
if (!o.cond) { return; }
if (o.value && o.value.fromsrc) {
@@ -1816,16 +1816,91 @@
return linkTarget;
}
var linkTitle = Title.fromPrefixedText(this.env, linkTarget);
- if (linkTitle &&
- (linkTitle.ns.isCategory() || linkTitle.ns.isFile()) &&
- linkData.type === 'mw:WikiLink' &&
- !/^:/.test(linkTarget))
+ if (linkTitle
+ && (linkTitle.ns.isCategory() || linkTitle.ns.isFile())
+ && linkData.type === 'mw:WikiLink'
+ && !/^:/.test(linkTarget))
{
// Escape category and file links
return ':' + linkTarget;
} else {
return linkTarget;
}
+};
+
+// Figure out if we need a piped or simple link
+WSP._isSimpleWikiLink = function(env, dp, target, linkData) {
+
+ var contentString = linkData.content.string,
+ canUseSimple = false;
+
+ // Would need to pipe for any non-string content
+ // Preserve unmodified or non-minimal piped links
+ if ( contentString !== undefined
+ && ( target.modified
+ || linkData.contentModified
+ || ( dp.stx !== 'piped' && !dp.pipetrick ) ))
+ {
+ // Strip colon escapes from the original target as that is
+ // stripped when deriving the content string.
+ var strippedTargetValue = target.value.replace(/^:/, ''),
+ decodedTarget =
Util.decodeURI(Util.decodeEntities(strippedTargetValue));
+
+ // See if the (normalized) content matches the
+ // target, either shadowed or actual.
+ canUseSimple = (
+ contentString === decodedTarget
+ // try wrapped in forward slashes in case they were
stripped
+ || ('/' + contentString + '/') === decodedTarget
+ || contentString === linkData.href
+ // normalize without underscores for comparison
+ // with target.value and strip any colon escape
+ || env.normalizeTitle( contentString, true ) ===
Util.decodeURI( strippedTargetValue )
+ // Relative link
+ || ( env.conf.wiki.namespacesWithSubpages[ env.page.ns ] &&
+ ( /^\.\.\/.*[^\/]$/.test(strippedTargetValue) &&
+ contentString ===
env.resolveTitle(strippedTargetValue, env.page.ns) ) ||
+ ( /^\.\.\/.*?\/$/.test(strippedTargetValue) &&
+ contentString ===
strippedTargetValue.replace(/^(?:\.\.\/)+(.*?)\/$/, '$1') ))
+ // normalize with underscores for comparison with href
+ || env.normalizeTitle( contentString ) === Util.decodeURI(
linkData.href )
+ );
+ }
+
+ return canUseSimple;
+};
+
+// Figure out if we need to use the pipe trick
+WSP._usePipeTrick = function(env, dp, target, linkData) {
+
+ var contentString = linkData.content.string;
+ if (!dp.pipetrick) {
+ return false;
+ } else if (linkData.type === 'mw:PageProp/Language') {
+ return true;
+ } else if (contentString === undefined || linkData.type ===
'mw:PageProp/Category') {
+ return false;
+ }
+
+ // Strip colon escapes from the original target as that is
+ // stripped when deriving the content string.
+ var strippedTargetValue = target.value.replace(/^:/, ''),
+ identicalTarget = function (a, b) {
+ return (
+ a === Util.stripPipeTrickChars(b) ||
+ env.normalizeTitle(a) ===
env.normalizeTitle(Util.stripPipeTrickChars(Util.decodeURI(b)))
+ );
+ };
+
+ // Only preserve pipe trick instances across edits, but don't
+ // introduce new ones.
+ return identicalTarget(contentString, strippedTargetValue)
+ || identicalTarget(contentString, linkData.href)
+ // Interwiki links with pipetrick have their prefix
+ // stripped, so compare against a stripped version
+ || ( linkData.isInterwiki &&
+ env.normalizeTitle( contentString ) ===
+ target.value.replace(/^:?[a-zA-Z]+:/, '') );
};
WSP.linkHandler = function(node, state, cb) {
@@ -1952,77 +2027,10 @@
}
}
- // Strip colon escapes from the original target
as that is
- // stripped when deriving the content string.
- var strippedTargetValue = target.value.replace(/^:/,
''),
- // Just a convenient shortcut. The string value
of the
- // content, if it is plain text.
- contentString = linkData.content.string,
-
- // figure out if we need a piped or simple link
- canUseSimple =
- // Would need to pipe for any
non-string content
- contentString !== undefined &&
- // See if the (normalized) content
matches the
- // target, either shadowed or actual.
- ( contentString ===
-
Util.decodeURI(Util.decodeEntities(strippedTargetValue)) ||
- // try wrapped in forward
slashes in case they were stripped
- ('/' + contentString + '/') ===
-
Util.decodeURI(Util.decodeEntities(strippedTargetValue)) ||
- contentString === linkData.href
||
- // normalize without
underscores for
- // comparison with target.value
and strip
- // any colon escape
- env.normalizeTitle(
contentString, true ) ===
- Util.decodeURI(
strippedTargetValue ) ||
- // Relative link
- (
env.conf.wiki.namespacesWithSubpages[ env.page.ns ] &&
- (
/^\.\.\/.*[^\/]$/.test(strippedTargetValue) &&
-
env.resolveTitle(strippedTargetValue, env.page.ns) ===
- contentString ) ||
- (
/^\.\.\/.*?\/$/.test(strippedTargetValue) &&
-
strippedTargetValue.replace(/^(?:\.\.\/)+(.*?)\/$/, '$1') ===
- contentString )
- ) ||
- // normalize with underscores
for comparison with href
- env.normalizeTitle(
contentString ) ===
- Util.decodeURI( linkData.href )
- ) &&
- // but preserve non-minimal piped links
- ( target.modified ||
linkData.contentModified ||
- ( dp.stx !== 'piped' &&
!dp.pipetrick ) ),
-
- canUsePipeTrick =
- linkData.type ===
'mw:PageProp/Language' ||
- contentString !== undefined &&
- linkData.type !==
'mw:PageProp/Category' &&
- (
-
Util.stripPipeTrickChars(strippedTargetValue) ===
- contentString ||
-
Util.stripPipeTrickChars(linkData.href) ===
- contentString ||
-
env.normalizeTitle(Util.stripPipeTrickChars(
-
Util.decodeURI(strippedTargetValue))) ===
-
env.normalizeTitle(contentString) ||
- env.normalizeTitle(
-
Util.stripPipeTrickChars(Util.decodeURI(linkData.href))) ===
-
env.normalizeTitle(contentString) ||
- // Interwiki links with
pipetrick have their prefix
- // stripped, so compare against
a stripped version
- ( linkData.isInterwiki &&
- dp.pipetrick &&
- env.normalizeTitle(
contentString ) ===
-
target.value.replace(/^:?[a-zA-Z]+:/, '') )
- ),
- // Only preserve pipe trick instances across
edits, but don't
- // introduce new ones.
- willUsePipeTrick = canUsePipeTrick &&
dp.pipetrick;
- //console.log(linkData.content.string, canUsePipeTrick);
-
+ // The string value of the content, if it is plain text.
var linkTarget;
- if ( canUseSimple ) {
+ if ( this._isSimpleWikiLink(env, dp, target, linkData)
) {
// Simple case
if (!target.modified &&
!linkData.contentModified) {
linkTarget = target.value;
@@ -2036,6 +2044,8 @@
return;
} else {
+ var usePipeTrick = this._usePipeTrick(env, dp,
target, linkData);
+
// First get the content source
if ( linkData.contentNode ) {
contentSrc =
state.serializeLinkChildrenToString(
@@ -2048,7 +2058,7 @@
linkData.tail = contentParts.tail;
dp.prefix = contentParts.prefix;
linkData.prefix = contentParts.prefix;
- } else if ( !willUsePipeTrick ) {
+ } else if ( !usePipeTrick ) {
if (linkData.content.fromsrc) {
contentSrc =
linkData.content.string;
} else {
@@ -2057,7 +2067,7 @@
}
}
- if ( contentSrc === '' && ! willUsePipeTrick &&
+ if ( contentSrc === '' && !usePipeTrick &&
linkData.type !==
'mw:PageProp/Category' ) {
// Protect empty link content from PST
pipe trick
contentSrc = '<nowiki/>';
--
To view, visit https://gerrit.wikimedia.org/r/109440
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I1527ba33fb7d9fea2764cad27eb7110f5ab50656
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/services/parsoid
Gerrit-Branch: master
Gerrit-Owner: Subramanya Sastry <[email protected]>
Gerrit-Reviewer: Arlolra <[email protected]>
Gerrit-Reviewer: Cscott <[email protected]>
Gerrit-Reviewer: GWicke <[email protected]>
Gerrit-Reviewer: Marcoil <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits