MaxSem has submitted this change and it was merged.
Change subject: Cherry pick "Fix transforming sections" to REL1_24
......................................................................
Cherry pick "Fix transforming sections" to REL1_24
Cherry pick I47e43578afcc2260ed7d20f6ad49cb1ffa69ddad
Bug: T91186
Change-Id: I8b41e5a946b07201f486c1711d460caf80345606
---
M includes/Resources.php
M javascripts/PageApi.js
A templates/sectionHeading.hogan
M tests/qunit/test_PageApi.js
4 files changed, 50 insertions(+), 9 deletions(-)
Approvals:
MaxSem: Verified; Looks good to me, approved
diff --git a/includes/Resources.php b/includes/Resources.php
index 0fae181..2c7b064 100644
--- a/includes/Resources.php
+++ b/includes/Resources.php
@@ -156,6 +156,7 @@
'templates' => array(
'page.hogan',
'section.hogan',
+ 'sectionHeading.hogan',
),
'messages' => array(
'mobile-frontend-language-article-heading',
diff --git a/javascripts/PageApi.js b/javascripts/PageApi.js
index 1149f83..65557ac 100644
--- a/javascripts/PageApi.js
+++ b/javascripts/PageApi.js
@@ -1,6 +1,14 @@
( function( M, $ ) {
- var Api = M.require( 'api' ).Api, PageApi;
+ var Api = M.require( 'api' ).Api, PageApi,
+ sectionTemplate = M.template.get( 'sectionHeading.hogan' );
+ /**
+ * Add child to listOfSections if the level of child is the same as the
last
+ * child of listOfSections, otherwise add it to the children of the last
+ * section of listOfSections. If listOfSections is empty, just add
child to it.
+ * @param {Array} listOfSections
+ * @param {Object} child - Section to be added to listOfSections
+ */
function assignToParent( listOfSections, child ) {
var section;
if ( listOfSections.length === 0 ) {
@@ -18,28 +26,47 @@
}
}
+ /**
+ * Order sections hierarchically
+ * @param {Array} sections
+ * @returns {Array}
+ */
function transformSections( sections ) {
var
collapseLevel = Math.min.apply( this, $.map( sections,
function( s ) { return s.level; } ) ) + '',
lastSection,
- result = [], $tmpContainer = $( '<div>' );
+ result = [];
+ // if the first section level is not equal to collapseLevel,
this first
+ // section will not have a parent and will be appended to the
result.
$.each( sections, function( i, section ) {
if ( section.line !== undefined ) {
section.line = section.line.replace(
/<\/?a\b[^>]*>/g, '' );
}
section.children = [];
- if ( !section.level || section.level === collapseLevel
) {
+
+ if (
+ !lastSection ||
+ (
+ !section.level ||
+ section.level === collapseLevel
+ ) ||
+ // make sure lastSections first child's level
is bigger than section.level
+ (
+ lastSection.children.length &&
+ lastSection.children[0].level >
section.level
+ ) ||
+ // also make sure section.level is not bigger
than the lastSection.level
+ (
+ lastSection.level &&
+ lastSection.level >= section.level
+ )
+ ) {
result.push( section );
lastSection = section;
} else {
- // FIXME: ugly, maintain structure returned by
API and use templates instead
- $tmpContainer.html( section.text );
- $tmpContainer.prepend(
- $( '<h' + section.level + '>' ).attr(
'id', section.anchor ).html( section.line )
- );
assignToParent( lastSection.children, section );
- lastSection.text += $tmpContainer.html();
+ lastSection.text += sectionTemplate.render(
section );
}
} );
diff --git a/templates/sectionHeading.hogan b/templates/sectionHeading.hogan
new file mode 100644
index 0000000..e9c0ea0
--- /dev/null
+++ b/templates/sectionHeading.hogan
@@ -0,0 +1 @@
+<h{{level}} id="{{anchor}}">{{{line}}}</h{{level}}>{{{text}}}
\ No newline at end of file
diff --git a/tests/qunit/test_PageApi.js b/tests/qunit/test_PageApi.js
index efb66e3..f525d93 100644
--- a/tests/qunit/test_PageApi.js
+++ b/tests/qunit/test_PageApi.js
@@ -316,6 +316,18 @@
] );
} );
+ QUnit.test( '#getSectionsFromHTML malformed (h2 before h1)', 1,
function( assert ) {
+ var resp = pageApi.getSectionsFromHTML(
+ $( '<div><h2><span
id="1.0">A1</span></h2><h3><span>A2.1</span></h3><h2><span>A2.2</span></h2><h1><span>A2</span></h1><h2><span>A2.1</span></h2></div>'
) );
+ assert.deepEqual( resp, [
+ { line: 'A1', level: '2', anchor: '1.0', text: '<h3
id="">A2.1</h3>', children: [{
+ line: 'A2.1', level: '3', anchor: '', text: '',
children: [] }] },
+ { line: 'A2.2', level: '2', anchor: '', text: '',
children: [] },
+ { line: 'A2', level: '1', anchor: '', text: '<h2
id="">A2.1</h2>', children: [{
+ line: 'A2.1', level: '2', anchor: '', text: '',
children: [] }] }
+ ] );
+ } );
+
QUnit.test( '#getPage (move protected page)', 1, function( assert ) {
var expected = {
edit: [ '*' ],
--
To view, visit https://gerrit.wikimedia.org/r/193891
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I8b41e5a946b07201f486c1711d460caf80345606
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: REL1_24
Gerrit-Owner: Florianschmidtwelzow <[email protected]>
Gerrit-Reviewer: Florianschmidtwelzow <[email protected]>
Gerrit-Reviewer: Jdlrobson <[email protected]>
Gerrit-Reviewer: Kaldari <[email protected]>
Gerrit-Reviewer: Kaldari2 <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: MaxSem <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits