From adce8103fa8b44be9d0bad6eee1bbb336b8b1176 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bartosz=20Dziewo=C5=84ski?= Date: Mon, 13 Jul 2015 20:02:49 +0200 Subject: [PATCH] Use Node.ELEMENT_NODE and Node.TEXT_NODE constants instead of magic numbers Updating all non-third-party scripts to depend on 'dom-level2-shim' and use the constants. * jquery.highlightText * jquery.tablesorter * mediawiki.api.upload * test.mediawiki.qunit.testrunner Change-Id: Ib07dded9fa18c749b3c064ab90326852146567b2 --- resources/Resources.php | 13 +++++++++++-- resources/src/jquery/jquery.highlightText.js | 7 +++---- resources/src/jquery/jquery.tablesorter.js | 3 +-- resources/src/mediawiki.api/mediawiki.api.upload.js | 2 +- tests/qunit/QUnitTestResources.php | 1 + tests/qunit/data/testrunner.js | 9 +++------ 6 files changed, 20 insertions(+), 15 deletions(-) diff --git a/resources/Resources.php b/resources/Resources.php index 3217503257..704805baa5 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -247,7 +247,10 @@ return array( ), 'jquery.highlightText' => array( 'scripts' => 'resources/src/jquery/jquery.highlightText.js', - 'dependencies' => 'mediawiki.RegExp', + 'dependencies' => array( + 'mediawiki.RegExp', + 'dom-level2-shim', + ), 'targets' => array( 'desktop', 'mobile' ), ), 'jquery.hoverIntent' => array( @@ -309,6 +312,7 @@ return array( 'styles' => 'resources/src/jquery/jquery.tablesorter.css', 'messages' => array( 'sort-descending', 'sort-ascending' ), 'dependencies' => array( + 'dom-level2-shim', 'mediawiki.RegExp', 'mediawiki.language.months', ), @@ -857,7 +861,12 @@ return array( ), 'mediawiki.api.upload' => array( 'scripts' => 'resources/src/mediawiki.api/mediawiki.api.upload.js', - 'dependencies' => array( 'mediawiki.api', 'mediawiki.api.edit', 'json' ), + 'dependencies' => array( + 'dom-level2-shim', + 'mediawiki.api', + 'mediawiki.api.edit', + 'json', + ), ), 'mediawiki.api.watch' => array( 'scripts' => 'resources/src/mediawiki.api/mediawiki.api.watch.js', diff --git a/resources/src/jquery/jquery.highlightText.js b/resources/src/jquery/jquery.highlightText.js index 8deb669151..1ec4db0505 100644 --- a/resources/src/jquery/jquery.highlightText.js +++ b/resources/src/jquery/jquery.highlightText.js @@ -23,8 +23,7 @@ // scans a node looking for the pattern and wraps a span around each match innerHighlight: function ( node, pat ) { var i, match, pos, spannode, middlebit, middleclone; - // if this is a text node - if ( node.nodeType === 3 ) { + if ( node.nodeType === Node.TEXT_NODE ) { // TODO - need to be smarter about the character matching here. // non latin characters can make regex think a new word has begun: do not use \b // http://stackoverflow.com/questions/3787072/regex-wordwrap-with-utf8-characters-in-js @@ -46,8 +45,8 @@ // replace the matched node, with our span-wrapped clone of the matched node middlebit.parentNode.replaceChild( spannode, middlebit ); } - // if this is an element with childnodes, and not a script, style or an element we created - } else if ( node.nodeType === 1 + } else if ( node.nodeType === Node.ELEMENT_NODE + // element with childnodes, and not a script, style or an element we created && node.childNodes && !/(script|style)/i.test( node.tagName ) && !( node.tagName.toLowerCase() === 'span' diff --git a/resources/src/jquery/jquery.tablesorter.js b/resources/src/jquery/jquery.tablesorter.js index c8e3cb39bf..8efbb1ce8f 100644 --- a/resources/src/jquery/jquery.tablesorter.js +++ b/resources/src/jquery/jquery.tablesorter.js @@ -95,8 +95,7 @@ return $node.attr( 'alt' ) || ''; // handle undefined alt } else { return $.map( $.makeArray( node.childNodes ), function ( elem ) { - // 1 is for document.ELEMENT_NODE (the constant is undefined on old browsers) - if ( elem.nodeType === 1 ) { + if ( elem.nodeType === Node.ELEMENT_NODE ) { return getElementSortKey( elem ); } else { return $.text( elem ); diff --git a/resources/src/mediawiki.api/mediawiki.api.upload.js b/resources/src/mediawiki.api/mediawiki.api.upload.js index c2d6989b95..64085e411e 100644 --- a/resources/src/mediawiki.api/mediawiki.api.upload.js +++ b/resources/src/mediawiki.api/mediawiki.api.upload.js @@ -122,7 +122,7 @@ return $.Deferred().reject( 'No file' ); } - iframe = file.nodeType && file.nodeType === file.ELEMENT_NODE; + iframe = file.nodeType && file.nodeType === Node.ELEMENT_NODE; formData = formDataAvailable() && file instanceof window.File; if ( !iframe && !formData ) { diff --git a/tests/qunit/QUnitTestResources.php b/tests/qunit/QUnitTestResources.php index 345b7ef38e..3608a53025 100644 --- a/tests/qunit/QUnitTestResources.php +++ b/tests/qunit/QUnitTestResources.php @@ -31,6 +31,7 @@ return array( 'mediawiki.page.ready', 'mediawiki.page.startup', 'test.sinonjs', + 'dom-level2-shim', ), 'position' => 'top', 'targets' => array( 'desktop', 'mobile' ), diff --git a/tests/qunit/data/testrunner.js b/tests/qunit/data/testrunner.js index b8283572fd..41754b45c1 100644 --- a/tests/qunit/data/testrunner.js +++ b/tests/qunit/data/testrunner.js @@ -3,10 +3,7 @@ ( function ( $, mw, QUnit ) { 'use strict'; - var mwTestIgnore, mwTester, - addons, - ELEMENT_NODE = 1, - TEXT_NODE = 3; + var mwTestIgnore, mwTester, addons; /** * Add bogus to url to prevent IE crazy caching @@ -331,12 +328,12 @@ function getDomStructure( node ) { var $node, children, processedChildren, i, len, el; $node = $( node ); - if ( node.nodeType === ELEMENT_NODE ) { + if ( node.nodeType === Node.ELEMENT_NODE ) { children = $node.contents(); processedChildren = []; for ( i = 0, len = children.length; i < len; i++ ) { el = children[i]; - if ( el.nodeType === ELEMENT_NODE || el.nodeType === TEXT_NODE ) { + if ( el.nodeType === Node.ELEMENT_NODE || el.nodeType === Node.TEXT_NODE ) { processedChildren.push( getDomStructure( el ) ); } } -- 2.20.1