From a30fc911f6b315a70432418b7d01d52e0c8eb03f Mon Sep 17 00:00:00 2001 From: RainLoop Team Date: Tue, 30 Aug 2016 00:00:16 +0300 Subject: [PATCH] Fix html parsing --- dev/Settings/Admin/Branding.js | 2 +- gulpfile.js | 3 +- .../app/libraries/MailSo/Base/HtmlUtils.php | 50 ++++++++++++++++--- 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/dev/Settings/Admin/Branding.js b/dev/Settings/Admin/Branding.js index c69afffdd..a8d401c98 100644 --- a/dev/Settings/Admin/Branding.js +++ b/dev/Settings/Admin/Branding.js @@ -5,7 +5,7 @@ import _ from '_'; import ko from 'ko'; import {Magics} from 'Common/Enums'; -import {settingsSaveHelperSimpleFunction, trim, log} from 'Common/Utils'; +import {settingsSaveHelperSimpleFunction, trim} from 'Common/Utils'; import {i18n, trigger as translatorTrigger} from 'Common/Translator'; import Remote from 'Remote/Admin/Ajax'; diff --git a/gulpfile.js b/gulpfile.js index e2da1cffc..623a378d7 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -622,7 +622,7 @@ gulp.task('default', function(callback) { }); // watch -gulp.task('watch', ['css:watch', 'js:validate:watch'], function() { +gulp.task('watch', ['css:main', 'js:validate'], function() { cfg.watch = true; livereload.listen(); gulp.watch(cfg.paths.less.main.watch, {interval: cfg.watchInterval}, ['css:main']); @@ -631,7 +631,6 @@ gulp.task('watch', ['css:watch', 'js:validate:watch'], function() { // aliases gulp.task('build', ['rainloop']); -gulp.task('watch', ['css:watch', 'js:validate:watch']); gulp.task('d', ['default']); gulp.task('w', ['watch']); diff --git a/rainloop/v/0.0.0/app/libraries/MailSo/Base/HtmlUtils.php b/rainloop/v/0.0.0/app/libraries/MailSo/Base/HtmlUtils.php index f007b4069..3dd21ae1f 100644 --- a/rainloop/v/0.0.0/app/libraries/MailSo/Base/HtmlUtils.php +++ b/rainloop/v/0.0.0/app/libraries/MailSo/Base/HtmlUtils.php @@ -75,7 +75,7 @@ class HtmlUtils @$oDom->loadHTML('<'.'?xml version="1.0" encoding="utf-8"?'.'>'. ''. ''. - '
'.$sText.'
'); + '
'.\MailSo\Base\Utils::Utf8Clear($sText).'
'); @$oDom->normalizeDocument(); @@ -196,12 +196,7 @@ class HtmlUtils else { $sResult = self::domToString($oDiv, $oDom); - if (0 === strpos($sResult, '
') && '
' === substr($sResult, -6)) - { - $sResult = substr($sResult, 5); - $sResult = substr($sResult, 0, -6); - $sResult = \trim($sResult); - } + $sResult = \MailSo\Base\HtmlUtils::UnWrapTag($sResult); } } else @@ -217,7 +212,37 @@ class HtmlUtils /** * @param string $sHtml - * @param string $sHtmlAttrs = ' + * @param string $sTag = 'div' + * @param string $iUnwrapCount = 10 + * + * @return string + */ + public static function UnWrapTag($sHtml, $sTag = 'div', $iUnwrapCount = 5) + { + $iUnwrapCount = 0 < $iUnwrapCount ? $iUnwrapCount : 1; + $iUnwrapCount = 10 < $iUnwrapCount ? 10 : $iUnwrapCount; + + $sTag = $sTag ? $sTag : 'div'; + $iTagLen = \strlen($sTag); + + while (0 < $iUnwrapCount) + { + $sHtml = \trim($sHtml); + if (0 === \strpos($sHtml, '<'.$sTag.'>') && '' === \substr($sHtml, -3 - $iTagLen)) + { + $sHtml = \substr(\substr($sHtml, 2 + $iTagLen), 0, -3 - $iTagLen); + $sHtml = \trim($sHtml); + } + + $iUnwrapCount--; + } + + return $sHtml; + } + + /** + * @param string $sHtml + * @param string $sHtmlAttrs = '' * @param string $sBodyAttrs = '' * * @return string @@ -247,14 +272,23 @@ class HtmlUtils // $sHtml = 0 < $iPos ? \substr($sHtml, $iPos) : $sHtml; // } + $sHtml = \preg_replace('/]*)>/si', '', $sHtml); $sHtml = \preg_replace('/]*)>/si', '', $sHtml); $sHtml = \preg_replace('/<\/body>/i', '', $sHtml); $sHtml = \preg_replace('/]*)>/i', '', $sHtml); $sHtml = \preg_replace('/<\/html>/i', '', $sHtml); $sHtmlAttrs = \preg_replace('/xmlns:[a-z]="[^"]*"/i', '', $sHtmlAttrs); + $sHtmlAttrs = \preg_replace('/xmlns:[a-z]=\'[^\']*\'/i', '', $sHtmlAttrs); $sHtmlAttrs = \preg_replace('/xmlns="[^"]*"/i', '', $sHtmlAttrs); + $sHtmlAttrs = \preg_replace('/xmlns=\'[^\']*\'/i', '', $sHtmlAttrs); $sBodyAttrs = \preg_replace('/xmlns:[a-z]="[^"]*"/i', '', $sBodyAttrs); + $sBodyAttrs = \preg_replace('/xmlns:[a-z]=\'[^\']*\'/i', '', $sBodyAttrs); + + $sHtmlAttrs = trim($sHtmlAttrs); + $sBodyAttrs = trim($sBodyAttrs); + + $sHtml = \MailSo\Base\HtmlUtils::UnWrapTag($sHtml); return $sHtml; }